-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsqlite_fdw.c
executable file
·1503 lines (1258 loc) · 43.5 KB
/
sqlite_fdw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* sqlite Foreign Data Wrapper for PostgreSQL
*
* Copyright (c) 2013-2016 Guillaume Lelarge
*
* This software is released under the PostgreSQL Licence
*
* Author: Guillaume Lelarge <[email protected]>
*
* IDENTIFICATION
* sqlite_fdw/src/sqlite_fdw.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/reloptions.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "funcapi.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/rel.h"
#include "utils/lsyscache.h"
#include <sqlite3.h>
#include <sys/stat.h>
PG_MODULE_MAGIC;
/*
* Default values
*/
/* ----
* This value is taken from sqlite
(without stats, sqlite defaults to 1 million tuples for a table)
*/
#define DEFAULT_ESTIMATED_LINES 1000000
/*
* SQL functions
*/
extern Datum sqlite_fdw_handler(PG_FUNCTION_ARGS);
extern Datum sqlite_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(sqlite_fdw_handler);
PG_FUNCTION_INFO_V1(sqlite_fdw_validator);
/* callback functions */
#if (PG_VERSION_NUM >= 90200)
static void sqliteGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static void sqliteGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static ForeignScan *sqliteGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
#if (PG_VERSION_NUM >= 90500)
List *scan_clauses,
Plan *outer_plan);
#else
List *scan_clauses);
#endif
#else
static FdwPlan *sqlitePlanForeignScan(Oid foreigntableid, PlannerInfo *root, RelOptInfo *baserel);
#endif
static void sqliteBeginForeignScan(ForeignScanState *node,
int eflags);
static TupleTableSlot *sqliteIterateForeignScan(ForeignScanState *node);
static void sqliteReScanForeignScan(ForeignScanState *node);
static void sqliteEndForeignScan(ForeignScanState *node);
#if (PG_VERSION_NUM >= 90300)
static void sqliteAddForeignUpdateTargets(Query *parsetree,
RangeTblEntry *target_rte,
Relation target_relation);
static List *sqlitePlanForeignModify(PlannerInfo *root,
ModifyTable *plan,
Index resultRelation,
int subplan_index);
static void sqliteBeginForeignModify(ModifyTableState *mtstate,
ResultRelInfo *rinfo,
List *fdw_private,
int subplan_index,
int eflags);
static TupleTableSlot *sqliteExecForeignInsert(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static TupleTableSlot *sqliteExecForeignUpdate(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static TupleTableSlot *sqliteExecForeignDelete(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static void sqliteEndForeignModify(EState *estate,
ResultRelInfo *rinfo);
#endif
static void sqliteExplainForeignScan(ForeignScanState *node,
struct ExplainState *es);
#if (PG_VERSION_NUM >= 90300)
static void sqliteExplainForeignModify(ModifyTableState *mtstate,
ResultRelInfo *rinfo,
List *fdw_private,
int subplan_index,
struct ExplainState *es);
#endif
#if (PG_VERSION_NUM >= 90200)
static bool sqliteAnalyzeForeignTable(Relation relation,
AcquireSampleRowsFunc *func,
BlockNumber *totalpages);
#endif
#if (PG_VERSION_NUM >= 90500)
static List *sqliteImportForeignSchema(ImportForeignSchemaStmt *stmt,
Oid serverOid);
#endif
/*
* Helper functions
*/
static void sqliteOpen(char *filename, sqlite3 **db);
static void sqlitePrepare(sqlite3 *db, char *query, sqlite3_stmt **result, const char **pzTail);
static bool sqliteIsValidOption(const char *option, Oid context);
static void sqliteGetOptions(Oid foreigntableid, char **database, char **table);
static int GetEstimatedRows(Oid foreigntableid);
static bool file_exists(const char *name);
#if (PG_VERSION_NUM >= 90500)
/* only used for IMPORT FOREIGN SCHEMA */
static void sqliteTranslateType(StringInfo str, char *typname);
#endif
/*
* structures used by the FDW
*
* These next two are not actually used by sqlite, but something like this
* will be needed by anything more complicated that does actual work.
*
*/
/*
* Describes the valid options for objects that use this wrapper.
*/
struct SQLiteFdwOption
{
const char *optname;
Oid optcontext; /* Oid of catalog in which option may appear */
};
/*
* Describes the valid options for objects that use this wrapper.
*/
static struct SQLiteFdwOption valid_options[] =
{
/* Connection options */
{ "database", ForeignServerRelationId },
/* Table options */
{ "table", ForeignTableRelationId },
/* Sentinel */
{ NULL, InvalidOid }
};
/*
* This is what will be set and stashed away in fdw_private and fetched
* for subsequent routines.
*/
typedef struct
{
char *foo;
int bar;
} sqliteFdwPlanState;
/*
* FDW-specific information for ForeignScanState.fdw_state.
*/
typedef struct SQLiteFdwExecutionState
{
sqlite3 *conn;
sqlite3_stmt *result;
char *query;
} SQLiteFdwExecutionState;
Datum
sqlite_fdw_handler(PG_FUNCTION_ARGS)
{
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
elog(DEBUG1,"entering function %s",__func__);
/* assign the handlers for the FDW */
/* these are required */
#if (PG_VERSION_NUM >= 90200)
fdwroutine->GetForeignRelSize = sqliteGetForeignRelSize;
fdwroutine->GetForeignPaths = sqliteGetForeignPaths;
fdwroutine->GetForeignPlan = sqliteGetForeignPlan;
#else
fdwroutine->PlanForeignScan = sqlitePlanForeignScan;
#endif
fdwroutine->BeginForeignScan = sqliteBeginForeignScan;
fdwroutine->IterateForeignScan = sqliteIterateForeignScan;
fdwroutine->ReScanForeignScan = sqliteReScanForeignScan;
fdwroutine->EndForeignScan = sqliteEndForeignScan;
/* remainder are optional - use NULL if not required */
/* support for insert / update / delete */
#if (PG_VERSION_NUM >= 90300)
fdwroutine->AddForeignUpdateTargets = sqliteAddForeignUpdateTargets;
fdwroutine->PlanForeignModify = sqlitePlanForeignModify;
fdwroutine->BeginForeignModify = sqliteBeginForeignModify;
fdwroutine->ExecForeignInsert = sqliteExecForeignInsert;
fdwroutine->ExecForeignUpdate = sqliteExecForeignUpdate;
fdwroutine->ExecForeignDelete = sqliteExecForeignDelete;
fdwroutine->EndForeignModify = sqliteEndForeignModify;
#endif
/* support for EXPLAIN */
fdwroutine->ExplainForeignScan = sqliteExplainForeignScan;
#if (PG_VERSION_NUM >= 90300)
fdwroutine->ExplainForeignModify = sqliteExplainForeignModify;
#endif
#if (PG_VERSION_NUM >= 90200)
/* support for ANALYSE */
fdwroutine->AnalyzeForeignTable = sqliteAnalyzeForeignTable;
#endif
#if (PG_VERSION_NUM >= 90500)
/* support for IMPORT FOREIGN SCHEMA */
fdwroutine->ImportForeignSchema = sqliteImportForeignSchema;
#endif
PG_RETURN_POINTER(fdwroutine);
}
Datum
sqlite_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
char *svr_database = NULL;
char *svr_table = NULL;
ListCell *cell;
elog(DEBUG1,"entering function %s",__func__);
/*
* Check that only options supported by sqlite_fdw,
* and allowed for the current object type, are given.
*/
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
if (!sqliteIsValidOption(def->defname, catalog))
{
struct SQLiteFdwOption *opt;
StringInfoData buf;
/*
* Unknown option specified, complain about it. Provide a hint
* with list of valid options for the object.
*/
initStringInfo(&buf);
for (opt = valid_options; opt->optname; opt++)
{
if (catalog == opt->optcontext)
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
opt->optname);
}
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname),
errhint("Valid options in this context are: %s", buf.len ? buf.data : "<none>")
));
}
if (strcmp(def->defname, "database") == 0)
{
if (svr_database)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options: database (%s)", defGetString(def))
));
if (!file_exists(defGetString(def)))
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not access file \"%s\"", defGetString(def))
));
svr_database = defGetString(def);
}
else if (strcmp(def->defname, "table") == 0)
{
if (svr_table)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options: table (%s)", defGetString(def))
));
svr_table = defGetString(def);
}
}
/* Check we have the options we need to proceed */
if (catalog == ForeignServerRelationId && !svr_database)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("The database name must be specified")
));
PG_RETURN_VOID();
}
/*
* Open the given sqlite3 file, and throw an error if the file couldn't be
* opened
*/
static void
sqliteOpen(char *filename, sqlite3 **db)
{
if (sqlite3_open(filename, db) != SQLITE_OK) {
ereport(ERROR,
(errcode(ERRCODE_FDW_OUT_OF_MEMORY),
errmsg("Can't open sqlite database %s: %s", filename, sqlite3_errmsg(*db))
));
sqlite3_close(*db);
}
}
/*
* Prepare the given query. If case of error, close the db and throw an error
*/
static void
sqlitePrepare(sqlite3 *db, char *query, sqlite3_stmt **result,
const char **pzTail)
{
int rc;
/* Execute the query */
rc = sqlite3_prepare(db, query, -1, result, pzTail);
if (rc != SQLITE_OK)
{
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("SQL error during prepare: %s", sqlite3_errmsg(db))
));
sqlite3_close(db);
}
}
/*
* Check if the provided option is one of the valid options.
* context is the Oid of the catalog holding the object the option is for.
*/
static bool
sqliteIsValidOption(const char *option, Oid context)
{
struct SQLiteFdwOption *opt;
for (opt = valid_options; opt->optname; opt++)
{
if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
return true;
}
return false;
}
/*
* Fetch the options for a mysql_fdw foreign table.
*/
static void
sqliteGetOptions(Oid foreigntableid, char **database, char **table)
{
ForeignTable *f_table;
ForeignServer *f_server;
List *options;
ListCell *lc;
/*
* Extract options from FDW objects.
*/
f_table = GetForeignTable(foreigntableid);
f_server = GetForeignServer(f_table->serverid);
options = NIL;
options = list_concat(options, f_table->options);
options = list_concat(options, f_server->options);
/* Loop through the options */
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
if (strcmp(def->defname, "database") == 0)
{
*database = defGetString(def);
}
if (strcmp(def->defname, "table") == 0)
{
*table = defGetString(def);
}
}
if (!*table)
{
*table = get_rel_name(foreigntableid);
}
/* Check we have the options we need to proceed */
if (!*database || !*table)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("a database and a table must be specified")
));
}
#if (PG_VERSION_NUM >= 90200)
static void
sqliteGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
/*
* Obtain relation size estimates for a foreign table. This is called at
* the beginning of planning for a query that scans a foreign table. root
* is the planner's global information about the query; baserel is the
* planner's information about this table; and foreigntableid is the
* pg_class OID of the foreign table. (foreigntableid could be obtained
* from the planner data structures, but it's passed explicitly to save
* effort.)
*
* This function should update baserel->rows to be the expected number of
* rows returned by the table scan, after accounting for the filtering
* done by the restriction quals. The initial value of baserel->rows is
* just a constant default estimate, which should be replaced if at all
* possible. The function may also choose to update baserel->width if it
* can compute a better estimate of the average result row width.
*/
sqliteFdwPlanState *fdw_private;
elog(DEBUG1,"entering function %s",__func__);
baserel->rows = 0;
fdw_private = palloc0(sizeof(sqliteFdwPlanState));
baserel->fdw_private = (void *) fdw_private;
/* initialize required state in fdw_private */
baserel->rows = GetEstimatedRows(foreigntableid);
}
static void
sqliteGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
/*
* Create possible access paths for a scan on a foreign table. This is
* called during query planning. The parameters are the same as for
* GetForeignRelSize, which has already been called.
*
* This function must generate at least one access path (ForeignPath node)
* for a scan on the foreign table and must call add_path to add each such
* path to baserel->pathlist. It's recommended to use
* create_foreignscan_path to build the ForeignPath nodes. The function
* can generate multiple access paths, e.g., a path which has valid
* pathkeys to represent a pre-sorted result. Each access path must
* contain cost estimates, and can contain any FDW-private information
* that is needed to identify the specific scan method intended.
*/
/*
* sqliteFdwPlanState *fdw_private = baserel->fdw_private;
*/
Cost startup_cost,
total_cost;
elog(DEBUG1,"entering function %s",__func__);
startup_cost = 0;
total_cost = startup_cost + baserel->rows;
/* Create a ForeignPath node and add it as only possible path */
add_path(baserel, (Path *)
create_foreignscan_path(root, baserel,
#if (PG_VERSION_NUM >= 90600)
NULL, /* default pathtarget */
#endif
baserel->rows,
startup_cost,
total_cost,
NIL, /* no pathkeys */
NULL, /* no outer rel either */
#if (PG_VERSION_NUM >= 90500)
NULL, /* no extra plan */
#endif
NIL)); /* no fdw_private data */
}
static ForeignScan *
sqliteGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
#if (PG_VERSION_NUM >= 90500)
List *scan_clauses,
Plan *outer_plan)
#else
List *scan_clauses)
#endif
{
/*
* Create a ForeignScan plan node from the selected foreign access path.
* This is called at the end of query planning. The parameters are as for
* GetForeignRelSize, plus the selected ForeignPath (previously produced
* by GetForeignPaths), the target list to be emitted by the plan node,
* and the restriction clauses to be enforced by the plan node.
*
* This function must create and return a ForeignScan plan node; it's
* recommended to use make_foreignscan to build the ForeignScan node.
*
*/
Index scan_relid = baserel->relid;
/*
* We have no native ability to evaluate restriction clauses, so we just
* put all the scan_clauses into the plan node's qual list for the
* executor to check. So all we have to do here is strip RestrictInfo
* nodes from the clauses and ignore pseudoconstants (which will be
* handled elsewhere).
*/
elog(DEBUG1,"entering function %s",__func__);
scan_clauses = extract_actual_clauses(scan_clauses, false);
/* Create the ForeignScan node */
return make_foreignscan(tlist,
scan_clauses,
scan_relid,
NIL, /* no expressions to evaluate */
#if (PG_VERSION_NUM >= 90500)
NIL, /* no private state */
NIL, /* no custom tlist */
NIL, /* no recheck quals */
NULL); /* no extra plan */
#else
NIL); /* no private state */
#endif
}
#else
static FdwPlan *
sqlitePlanForeignScan(Oid foreigntableid, PlannerInfo *root, RelOptInfo *baserel)
{
FdwPlan *fdwplan;
/* Construct FdwPlan with cost estimates. */
fdwplan = makeNode(FdwPlan);
baserel->rows = GetEstimatedRows(foreigntableid);
/* TODO: find a way to estimate the average row size */
/*baserel->width = ?; */
baserel->tuples = baserel->rows;
fdwplan->startup_cost = 10;
fdwplan->total_cost = baserel->rows + fdwplan->startup_cost;
fdwplan->fdw_private = NIL; /* not used */
return fdwplan;
}
#endif
static void
sqliteBeginForeignScan(ForeignScanState *node,
int eflags)
{
/*
* Begin executing a foreign scan. This is called during executor startup.
* It should perform any initialization needed before the scan can start,
* but not start executing the actual scan (that should be done upon the
* first call to IterateForeignScan). The ForeignScanState node has
* already been created, but its fdw_state field is still NULL.
* Information about the table to scan is accessible through the
* ForeignScanState node (in particular, from the underlying ForeignScan
* plan node, which contains any FDW-private information provided by
* GetForeignPlan). eflags contains flag bits describing the executor's
* operating mode for this plan node.
*
* Note that when (eflags & EXEC_FLAG_EXPLAIN_ONLY) is true, this function
* should not perform any externally-visible actions; it should only do
* the minimum required to make the node state valid for
* ExplainForeignScan and EndForeignScan.
*
*/
sqlite3 *db;
SQLiteFdwExecutionState *festate;
char *svr_database = NULL;
char *svr_table = NULL;
char *query;
size_t len;
elog(DEBUG1,"entering function %s",__func__);
/* Fetch options */
sqliteGetOptions(RelationGetRelid(node->ss.ss_currentRelation), &svr_database, &svr_table);
/* Connect to the server */
sqliteOpen(svr_database, &db);
/* Build the query */
len = strlen(svr_table) + 15;
query = (char *)palloc(len);
snprintf(query, len, "SELECT * FROM %s", svr_table);
/* Stash away the state info we have already */
festate = (SQLiteFdwExecutionState *) palloc(sizeof(SQLiteFdwExecutionState));
node->fdw_state = (void *) festate;
festate->conn = db;
festate->result = NULL;
festate->query = query;
}
static TupleTableSlot *
sqliteIterateForeignScan(ForeignScanState *node)
{
/*
* Fetch one row from the foreign source, returning it in a tuple table
* slot (the node's ScanTupleSlot should be used for this purpose). Return
* NULL if no more rows are available. The tuple table slot infrastructure
* allows either a physical or virtual tuple to be returned; in most cases
* the latter choice is preferable from a performance standpoint. Note
* that this is called in a short-lived memory context that will be reset
* between invocations. Create a memory context in BeginForeignScan if you
* need longer-lived storage, or use the es_query_cxt of the node's
* EState.
*
* The rows returned must match the column signature of the foreign table
* being scanned. If you choose to optimize away fetching columns that are
* not needed, you should insert nulls in those column positions.
*
* Note that PostgreSQL's executor doesn't care whether the rows returned
* violate any NOT NULL constraints that were defined on the foreign table
* columns — but the planner does care, and may optimize queries
* incorrectly if NULL values are present in a column declared not to
* contain them. If a NULL value is encountered when the user has declared
* that none should be present, it may be appropriate to raise an error
* (just as you would need to do in the case of a data type mismatch).
*/
char **values;
HeapTuple tuple;
int x;
const char *pzTail;
SQLiteFdwExecutionState *festate = (SQLiteFdwExecutionState *) node->fdw_state;
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
elog(DEBUG1,"entering function %s",__func__);
/* Execute the query, if required */
if (!festate->result)
{
sqlitePrepare(festate->conn, festate->query, &festate->result, &pzTail);
}
ExecClearTuple(slot);
/* get the next record, if any, and fill in the slot */
if (sqlite3_step(festate->result) == SQLITE_ROW)
{
/* Build the tuple */
values = (char **) palloc(sizeof(char *) * sqlite3_column_count(festate->result));
for (x = 0; x < sqlite3_column_count(festate->result); x++)
{
values[x] = (char *) sqlite3_column_text(festate->result, x);
}
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(node->ss.ss_currentRelation->rd_att), values);
ExecStoreTuple(tuple, slot, InvalidBuffer, false);
}
/* then return the slot */
return slot;
}
static void
sqliteReScanForeignScan(ForeignScanState *node)
{
/*
* Restart the scan from the beginning. Note that any parameters the scan
* depends on may have changed value, so the new scan does not necessarily
* return exactly the same rows.
*/
elog(DEBUG1,"entering function %s",__func__);
}
static void
sqliteEndForeignScan(ForeignScanState *node)
{
/*
* End the scan and release resources. It is normally not important to
* release palloc'd memory, but for example open files and connections to
* remote servers should be cleaned up.
*/
SQLiteFdwExecutionState *festate = (SQLiteFdwExecutionState *) node->fdw_state;
elog(DEBUG1,"entering function %s",__func__);
if (festate->result)
{
sqlite3_finalize(festate->result);
festate->result = NULL;
}
if (festate->conn)
{
sqlite3_close(festate->conn);
festate->conn = NULL;
}
if (festate->query)
{
pfree(festate->query);
festate->query = 0;
}
}
#if (PG_VERSION_NUM >= 90300)
static void
sqliteAddForeignUpdateTargets(Query *parsetree,
RangeTblEntry *target_rte,
Relation target_relation)
{
/*
* UPDATE and DELETE operations are performed against rows previously
* fetched by the table-scanning functions. The FDW may need extra
* information, such as a row ID or the values of primary-key columns, to
* ensure that it can identify the exact row to update or delete. To
* support that, this function can add extra hidden, or "junk", target
* columns to the list of columns that are to be retrieved from the
* foreign table during an UPDATE or DELETE.
*
* To do that, add TargetEntry items to parsetree->targetList, containing
* expressions for the extra values to be fetched. Each such entry must be
* marked resjunk = true, and must have a distinct resname that will
* identify it at execution time. Avoid using names matching ctidN or
* wholerowN, as the core system can generate junk columns of these names.
*
* This function is called in the rewriter, not the planner, so the
* information available is a bit different from that available to the
* planning routines. parsetree is the parse tree for the UPDATE or DELETE
* command, while target_rte and target_relation describe the target
* foreign table.
*
* If the AddForeignUpdateTargets pointer is set to NULL, no extra target
* expressions are added. (This will make it impossible to implement
* DELETE operations, though UPDATE may still be feasible if the FDW
* relies on an unchanging primary key to identify rows.)
*/
elog(DEBUG1,"entering function %s",__func__);
}
static List *
sqlitePlanForeignModify(PlannerInfo *root,
ModifyTable *plan,
Index resultRelation,
int subplan_index)
{
/*
* Perform any additional planning actions needed for an insert, update,
* or delete on a foreign table. This function generates the FDW-private
* information that will be attached to the ModifyTable plan node that
* performs the update action. This private information must have the form
* of a List, and will be delivered to BeginForeignModify during the
* execution stage.
*
* root is the planner's global information about the query. plan is the
* ModifyTable plan node, which is complete except for the fdwPrivLists
* field. resultRelation identifies the target foreign table by its
* rangetable index. subplan_index identifies which target of the
* ModifyTable plan node this is, counting from zero; use this if you want
* to index into plan->plans or other substructure of the plan node.
*
* If the PlanForeignModify pointer is set to NULL, no additional
* plan-time actions are taken, and the fdw_private list delivered to
* BeginForeignModify will be NIL.
*/
elog(DEBUG1,"entering function %s",__func__);
return NULL;
}
static void
sqliteBeginForeignModify(ModifyTableState *mtstate,
ResultRelInfo *rinfo,
List *fdw_private,
int subplan_index,
int eflags)
{
/*
* Begin executing a foreign table modification operation. This routine is
* called during executor startup. It should perform any initialization
* needed prior to the actual table modifications. Subsequently,
* ExecForeignInsert, ExecForeignUpdate or ExecForeignDelete will be
* called for each tuple to be inserted, updated, or deleted.
*
* mtstate is the overall state of the ModifyTable plan node being
* executed; global data about the plan and execution state is available
* via this structure. rinfo is the ResultRelInfo struct describing the
* target foreign table. (The ri_FdwState field of ResultRelInfo is
* available for the FDW to store any private state it needs for this
* operation.) fdw_private contains the private data generated by
* PlanForeignModify, if any. subplan_index identifies which target of the
* ModifyTable plan node this is. eflags contains flag bits describing the
* executor's operating mode for this plan node.
*
* Note that when (eflags & EXEC_FLAG_EXPLAIN_ONLY) is true, this function
* should not perform any externally-visible actions; it should only do
* the minimum required to make the node state valid for
* ExplainForeignModify and EndForeignModify.
*
* If the BeginForeignModify pointer is set to NULL, no action is taken
* during executor startup.
*/
elog(DEBUG1,"entering function %s",__func__);
}
static TupleTableSlot *
sqliteExecForeignInsert(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot)
{
/*
* Insert one tuple into the foreign table. estate is global execution
* state for the query. rinfo is the ResultRelInfo struct describing the
* target foreign table. slot contains the tuple to be inserted; it will
* match the rowtype definition of the foreign table. planSlot contains
* the tuple that was generated by the ModifyTable plan node's subplan; it
* differs from slot in possibly containing additional "junk" columns.
* (The planSlot is typically of little interest for INSERT cases, but is
* provided for completeness.)
*
* The return value is either a slot containing the data that was actually
* inserted (this might differ from the data supplied, for example as a
* result of trigger actions), or NULL if no row was actually inserted
* (again, typically as a result of triggers). The passed-in slot can be
* re-used for this purpose.
*
* The data in the returned slot is used only if the INSERT query has a
* RETURNING clause. Hence, the FDW could choose to optimize away
* returning some or all columns depending on the contents of the
* RETURNING clause. However, some slot must be returned to indicate
* success, or the query's reported rowcount will be wrong.
*
* If the ExecForeignInsert pointer is set to NULL, attempts to insert
* into the foreign table will fail with an error message.
*
*/
elog(DEBUG1,"entering function %s",__func__);
return slot;
}
static TupleTableSlot *
sqliteExecForeignUpdate(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot)
{
/*
* Update one tuple in the foreign table. estate is global execution state
* for the query. rinfo is the ResultRelInfo struct describing the target
* foreign table. slot contains the new data for the tuple; it will match
* the rowtype definition of the foreign table. planSlot contains the
* tuple that was generated by the ModifyTable plan node's subplan; it
* differs from slot in possibly containing additional "junk" columns. In
* particular, any junk columns that were requested by
* AddForeignUpdateTargets will be available from this slot.
*
* The return value is either a slot containing the row as it was actually
* updated (this might differ from the data supplied, for example as a
* result of trigger actions), or NULL if no row was actually updated
* (again, typically as a result of triggers). The passed-in slot can be
* re-used for this purpose.
*
* The data in the returned slot is used only if the UPDATE query has a
* RETURNING clause. Hence, the FDW could choose to optimize away
* returning some or all columns depending on the contents of the
* RETURNING clause. However, some slot must be returned to indicate
* success, or the query's reported rowcount will be wrong.
*
* If the ExecForeignUpdate pointer is set to NULL, attempts to update the
* foreign table will fail with an error message.
*
*/
elog(DEBUG1,"entering function %s",__func__);
return slot;
}
static TupleTableSlot *
sqliteExecForeignDelete(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot)
{
/*
* Delete one tuple from the foreign table. estate is global execution
* state for the query. rinfo is the ResultRelInfo struct describing the
* target foreign table. slot contains nothing useful upon call, but can
* be used to hold the returned tuple. planSlot contains the tuple that
* was generated by the ModifyTable plan node's subplan; in particular, it
* will carry any junk columns that were requested by
* AddForeignUpdateTargets. The junk column(s) must be used to identify
* the tuple to be deleted.
*
* The return value is either a slot containing the row that was deleted,
* or NULL if no row was deleted (typically as a result of triggers). The
* passed-in slot can be used to hold the tuple to be returned.
*
* The data in the returned slot is used only if the DELETE query has a
* RETURNING clause. Hence, the FDW could choose to optimize away
* returning some or all columns depending on the contents of the
* RETURNING clause. However, some slot must be returned to indicate
* success, or the query's reported rowcount will be wrong.