-
Notifications
You must be signed in to change notification settings - Fork 3
/
dc_fdw.c
1241 lines (1077 loc) · 34.9 KB
/
dc_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
/*-------------------------------------------------------------------------
*
* dc_fdw.c
* foreign-data wrapper for server-side document collections.
*
* Copyright (c) 2012, PostgreSQL Global Development Group
*
* This software is released under the PostgreSQL Licence.
*
* Author: Zheng Yang <zhengyang4k@gmail.com>
*
* IDENTIFICATION
* contrib/dc_fdw/dc_fdw.c
*
*-------------------------------------------------------------------------
*/
/* Debug mode flag */
/*#define DEBUG*/
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "miscadmin.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "qual_pushdown.h"
#include "qual_extract.h"
PG_MODULE_MAGIC;
/*
* Describes the valid options for objects that use this wrapper.
*/
struct DcFdwOption
{
const char *optname;
Oid optcontext; /* Oid of catalog in which option may appear */
};
/*
* Valid options for dc_fdw.
*
* Note: If you are adding new option for user mapping, you need to modify
* dcGetOptions(), which currently doesn't bother to look at user mappings.
*/
static struct DcFdwOption valid_options[] = {
/* Foreign table options */
/* where the data files located */
{"data_dir", ForeignTableRelationId},
/* where the index file located */
{"index_dir", ForeignTableRelationId},
/* the indexing method used: (IM, SPIM) */
{"index_method", ForeignTableRelationId},
/* buffer size for SPIM in MB */
{"buffer_size", ForeignTableRelationId},
/* column mapping options */
{"id_col", ForeignTableRelationId},
{"text_col", ForeignTableRelationId},
/* Sentinel */
{NULL, InvalidOid}
};
/*
* FDW-specific information for RelOptInfo.fdw_private.
*/
typedef struct DcFdwPlanState
{
char *data_dir; /* documents to read */
char *index_dir; /* index to output */
List *mapping; /* column mapping function */
CollectionStats *stats; /* collection-wise stats */
BlockNumber pages; /* estimate of collection's physical size */
double ntuples; /* estimate of number of rows in collection */
List * rlist; /* reduced list of doc ids by quals pushdown */
} DcFdwPlanState;
/*
* FDW-specific information for ForeignScanState.fdw_state.
*/
typedef struct DcFdwExecutionState
{
char *data_dir; /* dc to read */
DIR *dir_state; /* for sequential scan only */
AttInMetadata *attinmeta;
CollectionStats *stats; /* collection-wise stats */
int dc_size; /* collection size in bytes */
double ntuples; /* estimate of number of rows in file */
List *rlist; /* reduced list of doc ids by quals pushdown */
int rlistptr; /* for looping through the rList */
int *mask; /* mask for column mapping */
int ncols; /* number of columns in the table */
} DcFdwExecutionState;
/*
* SQL functions
*/
extern Datum dc_fdw_handler(PG_FUNCTION_ARGS);
extern Datum dc_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(dc_fdw_handler);
PG_FUNCTION_INFO_V1(dc_fdw_validator);
/*
* FDW callback routines
*/
static void dcGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static void dcGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static ForeignScan *dcGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
List *scan_clauses);
static void dcExplainForeignScan(ForeignScanState *node, ExplainState *es);
static void dcBeginForeignScan(ForeignScanState *node, int eflags);
static TupleTableSlot *dcIterateForeignScan(ForeignScanState *node);
static void dcReScanForeignScan(ForeignScanState *node);
static void dcEndForeignScan(ForeignScanState *node);
static bool dcAnalyzeForeignTable(Relation relation,
AcquireSampleRowsFunc *func,
BlockNumber *totalpages);
/*
* Helper functions
*/
static bool is_valid_option(const char *option, Oid context);
static void dcGetOptions(Oid foreigntableid,
char **data_dir,
char **index_dir,
List **col_mapping);
static void estimate_size(PlannerInfo *root,
RelOptInfo *baserel,
DcFdwPlanState *fdw_private,
CollectionStats *stats);
static void estimate_costs(PlannerInfo *root,
RelOptInfo *baserel,
DcFdwPlanState *fdw_private,
Cost *startup_cost,
Cost *total_cost);
static int dc_acquire_sample_rows(Relation onerel,
int elevel,
HeapTuple *rows,
int targrows,
double *totalrows,
double *totaldeadrows);
int dc_col_mapping_mask(Relation rel, List *mapping_list, int **mask);
void cstring_tuple(Datum **tuple_as_array, bool **nulls, int *mask, int mask_len, List *values);
/*
* Foreign-data wrapper handler function: return a struct with pointers
* to my callback routines.
*/
Datum
dc_fdw_handler(PG_FUNCTION_ARGS)
{
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
#ifdef DEBUG
elog(NOTICE, "dc_fdw_handler");
#endif
fdwroutine->GetForeignRelSize = dcGetForeignRelSize;
fdwroutine->GetForeignPaths = dcGetForeignPaths;
fdwroutine->GetForeignPlan = dcGetForeignPlan;
fdwroutine->ExplainForeignScan = dcExplainForeignScan;
fdwroutine->BeginForeignScan = dcBeginForeignScan;
fdwroutine->IterateForeignScan = dcIterateForeignScan;
fdwroutine->ReScanForeignScan = dcReScanForeignScan;
fdwroutine->EndForeignScan = dcEndForeignScan;
fdwroutine->AnalyzeForeignTable = dcAnalyzeForeignTable;
PG_RETURN_POINTER(fdwroutine);
}
/*
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
* USER MAPPING or FOREIGN TABLE that uses dc_fdw.
*
* Raise an ERROR if the option or its value is considered invalid.
*/
Datum
dc_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
char *data_dir = NULL;
char *index_dir = NULL;
char *index_method = NULL;
char *buffer_size = NULL;
char *id_col = NULL;
char *text_col = NULL;
List *other_options = NIL;
ListCell *cell;
#ifdef DEBUG
elog(NOTICE, "dc_fdw_validator");
#endif
/*
* Only superusers are allowed to set options of a dc_fdw foreign table.
* This is because the data_dir is one of those options, and we don't want
* non-superusers to be able to determine which file gets read.
*
* Putting this sort of permissions check in a validator is a bit of a
* crock, but there doesn't seem to be any other place that can enforce
* the check more cleanly.
*
* Note that the valid_options[] array disallows setting data_dir at any
* options level other than foreign table --- otherwise there'd still be a
* security hole.
*/
if (catalog == ForeignTableRelationId && !superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("Only superuser can change options of a dc_fdw foreign table")));
/*
* Check that only options supported by dc_fdw, and allowed for the
* current object type, are given.
*/
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
if (!is_valid_option(def->defname, catalog))
{
struct DcFdwOption *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.data)));
}
if (strcmp(def->defname, "data_dir") == 0)
{
if (data_dir)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
data_dir = defGetString(def);
}
if (strcmp(def->defname, "index_dir") == 0)
{
if (index_dir)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
index_dir = defGetString(def);
}
if (strcmp(def->defname, "index_method") == 0)
{
if (index_method)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
if (strcmp(defGetString(def), "IM") != 0 && strcmp(defGetString(def), "SPIM") != 0)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid index_method options \"%s\"", defGetString(def)),
errhint("Valid options in this context are: IM, SPIM")));
index_method = defGetString(def);
}
if (strcmp(def->defname, "buffer_size") == 0)
{
if (buffer_size)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
if (atoi(defGetString(def)) == 0)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid buffer_size options \"%s\"", defGetString(def)),
errhint("buffer size needs to be a integer in MB")));
buffer_size = defGetString(def);
}
if (strcmp(def->defname, "id_col") == 0)
{
if (id_col)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
id_col = defGetString(def);
}
if (strcmp(def->defname, "text_col") == 0)
{
if (text_col)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("redundant options")));
text_col = defGetString(def);
}
else
/* essentially all column mapping options which are optional */
other_options = lappend(other_options, def);
}
/*
* options are required for dc_fdw foreign tables.
*/
if (catalog == ForeignTableRelationId && data_dir == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("data_dir is required for dc_fdw foreign tables")));
if (catalog == ForeignTableRelationId && index_dir == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("index_dir is required for dc_fdw foreign tables")));
if (catalog == ForeignTableRelationId && index_method == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("index_method is required for dc_fdw foreign tables")));
if (catalog == ForeignTableRelationId && id_col == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("id_col is required for dc_fdw foreign tables")));
if (catalog == ForeignTableRelationId && text_col == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("text_col is required for dc_fdw foreign tables")));
/*
* Start indexing procedures
* Note that validate function is called when
*
* 1. Creating ForeignServer
* 2. Creating ForeignTable
* 3. Creating UserMapping
*
* Call the index function only when ForeignTable is being created.
*/
if (catalog == ForeignTableRelationId) {
elog(NOTICE, "%s", "-Start indexing document collection, this may take a while...");
if (strcmp(index_method, "SPIM") == 0)
{
spimIndex(data_dir, index_dir, atoi(buffer_size));
}
else if (strcmp(index_method, "IM") == 0)
imIndex(data_dir, index_dir);
}
PG_RETURN_VOID();
}
/*
* 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
is_valid_option(const char *option, Oid context)
{
struct DcFdwOption *opt;
#ifdef DEBUG
elog(NOTICE, "is_valid_option");
#endif
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 dc_fdw foreign table.
*
* We have to separate out "data_dir" from the other options because
* it must not appear in the options list passed to the core COPY code.
*/
static void
dcGetOptions(Oid foreigntableid,
char **data_dir, char **index_dir, List **col_mapping)
{
ForeignTable *table;
ForeignServer *server;
ForeignDataWrapper *wrapper;
char *text_col;
char *id_col;
List *options;
ListCell *lc,
*prev;
#ifdef DEBUG
elog(NOTICE, "dcGetOptions");
#endif
/*
* Extract options from FDW objects. We ignore user mappings & server because
* dc_fdw doesn't have any options that can be specified there.
*
* (XXX Actually, given the current contents of valid_options[], there's
* no point in examining anything except the foreign table's own options.
* Simplify?)
*/
table = GetForeignTable(foreigntableid);
server = GetForeignServer(table->serverid);
wrapper = GetForeignDataWrapper(server->fdwid);
options = NIL;
options = list_concat(options, wrapper->options);
options = list_concat(options, server->options);
options = list_concat(options, table->options);
/*
* Separate out individual options.
*/
*data_dir = NULL;
*index_dir = NULL;
prev = NULL;
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
#ifdef DEBUG
elog(NOTICE, "<options> %s:%s", def->defname, defGetString(def));
#endif
if (strcmp(def->defname, "data_dir") == 0)
{
*data_dir = defGetString(def);
continue;
}
if (strcmp(def->defname, "index_dir") == 0)
{
*index_dir = defGetString(def);
continue;
}
if (strcmp(def->defname, "id_col") == 0)
{
id_col = defGetString(def);
continue;
}
if (strcmp(def->defname, "text_col") == 0)
{
text_col = defGetString(def);
continue;
}
}
/*
* The validator should have checked that a data_dir directory was included in the
* options, but check again, just in case.
*/
if (*data_dir == NULL)
elog(ERROR, "data_dir is required for dc_fdw foreign tables");
if (*data_dir == NULL)
elog(ERROR, "index_dir is required for dc_fdw foreign tables");
if (text_col == NULL)
elog(ERROR, "text_col is required for dc_fdw foreign tables");
if (id_col == NULL)
elog(ERROR, "id_col is required for dc_fdw foreign tables");
/* column mapping list, index0: id, index1: content */
*col_mapping = list_make2(id_col, text_col);
}
/*
* dcGetForeignRelSize
* Obtain relation size estimates for a foreign table
*/
static void
dcGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
DcFdwPlanState *fpstate;
/* File handles */
File statFile;
File dictFile;
File postFile;
/* stat info */
CollectionStats *stats;
/* dict settings */
HTAB *dict;
/* qual eval */
PushableQualNode *qualRoot;
List *allList;
#ifdef DEBUG
elog(NOTICE, "dcGetForeignRelSize");
#endif
/*
* init stats data
*/
stats = (CollectionStats *) palloc(sizeof(CollectionStats));
/*
* Fetch options. We only need filename at this point, but we might as
* well get everything and not need to re-fetch it later in planning.
*/
fpstate = (DcFdwPlanState *) palloc(sizeof(DcFdwPlanState));
dcGetOptions(foreigntableid,
&fpstate->data_dir,
&fpstate->index_dir,
&fpstate->mapping);
baserel->fdw_private = (void *) fpstate;
/*
* Fetch collection-wise stats
*/
statFile = openStat(fpstate->index_dir);
loadStat(&stats, statFile);
closeStat(statFile);
fpstate->stats = stats;
/*
* fill in dc size information
*/
estimate_size(root, baserel, fpstate, stats);
/*
* Load Dictionary. Dict is stored in memory for fast access and
* postings lists are in hard disk as it may be too large to fit
* into main memory.
*/
dictFile = openDict(fpstate->index_dir);
loadDict(&dict, dictFile);
closeDict(dictFile);
/*
* Extract Quals. We only extract quals that we can push down and
* convert them into a tree structure for evaluation.
*
* Evaluate QualTree. Filtered doc_id list.
*/
postFile = openPost(fpstate->index_dir);
allList = searchTerm(ALL, dict, postFile, TRUE, FALSE);
/* no quals to push down */
if (extractQuals(&qualRoot, root, baserel, fpstate->mapping) == 0)
{
#ifdef DEBUG
elog(NOTICE, "No quals to pushdown, sequential scan");
#endif
fpstate->rlist = allList;
}
/* there are quals available to pushdown */
else
{
fpstate->rlist = evalQualTree(qualRoot, dict, postFile, allList);
#ifdef DEBUG
printQualTree(qualRoot, 1);
#endif
freeQualTree(qualRoot);
}
#ifdef DEBUG
elog(NOTICE, "rlist length:%d", list_length(fpstate->rlist));
#endif
closePost(postFile);
}
/*
* dcGetForeignPaths
* Create possible access paths for a scan on the foreign table
*
* Currently we don't support any push-down feature, so there is only one
* possible access path, which simply returns all records in the order in
* the data file.
*/
static void
dcGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
DcFdwPlanState *fpstate = (DcFdwPlanState *) baserel->fdw_private;
ForeignPath *path;
Cost startup_cost;
Cost total_cost;
List *fdw_private;
#ifdef DEBUG
elog(NOTICE, "dcGetForeignPaths");
#endif
/* Estimate costs */
estimate_costs(root, baserel, fpstate,
&startup_cost, &total_cost);
/* result list after pushing down. */
fdw_private = lappend(NIL, fpstate->rlist);
fdw_private = lappend(fdw_private, fpstate->stats);
/* Create a ForeignPath node and add it as only possible path */
path = create_foreignscan_path(root, baserel,
baserel->rows,
startup_cost,
total_cost,
NIL, /* no pathkeys */
NULL, /* no outer rel either */
fdw_private);
add_path(baserel, (Path *) path);
}
/*
* dcGetForeignPlan
* Create a ForeignScan plan node for scanning the foreign table
*/
static ForeignScan *
dcGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
List *scan_clauses)
{
DcFdwPlanState *fpstate = (DcFdwPlanState *) baserel->fdw_private;
Index scan_relid = baserel->relid;
List *fdw_private;
#ifdef DEBUG
elog(NOTICE, "dcGetForeignPlan");
#endif
/* result list after pushing down. */
fdw_private = lappend(NIL, fpstate->rlist);
fdw_private = lappend(fdw_private, fpstate->stats);
/*
* 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).
*/
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 */
fdw_private);
}
/*
* dcExplainForeignScan
* Produce extra output for EXPLAIN
*/
static void
dcExplainForeignScan(ForeignScanState *node, ExplainState *es)
{
char *data_dir;
char *index_dir;
List *col_mapping;
CollectionStats *stats;
#ifdef DEBUG
elog(NOTICE, "dcExplainForeignScan");
#endif
/* retrieve stats list */
stats = (CollectionStats *) list_nth( (List *) ((ForeignScan *) node->ss.ps.plan)->fdw_private, 1);
/* Fetch options --- we only need data_dir at this point */
dcGetOptions(RelationGetRelid(node->ss.ss_currentRelation),
&data_dir, &index_dir, &col_mapping);
ExplainPropertyText("Foreign Document Collection", data_dir, es);
ExplainPropertyLong("Foreign Document Collection Size", (long) stats->numOfBytes, es);
ExplainPropertyLong("Number of Documents", (long) stats->numOfDocs, es);
ExplainPropertyText("Index Location", index_dir, es);
}
/*
* dcBeginForeignScan
* Initiate access to the dc by creating CopyState
*/
static void
dcBeginForeignScan(ForeignScanState *node, int eflags)
{
char *data_dir;
char *index_dir;
DcFdwExecutionState *festate;
int *mask;
List *mappingList;
int numOfColumns;
Relation rel;
#ifdef DEBUG
elog(NOTICE, "dcBeginForeignScan");
#endif
/*
* Do nothing in EXPLAIN (no ANALYZE) case. node->fdw_state stays NULL.
*/
if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
return;
/* Fetch options of foreign table */
dcGetOptions(RelationGetRelid(node->ss.ss_currentRelation),
&data_dir, &index_dir, &mappingList);
rel = heap_open(RelationGetRelid(node->ss.ss_currentRelation), AccessShareLock);
numOfColumns = dc_col_mapping_mask(rel, mappingList, &mask);
heap_close(rel, NoLock);
/*
* Save state in node->fdw_state. We must save enough information to call
* BeginCopyFrom() again.
*/
festate = (DcFdwExecutionState *) palloc(sizeof(DcFdwExecutionState));
festate->rlist = (List *) list_nth( (List *) ((ForeignScan *) node->ss.ps.plan)->fdw_private, 0);
festate->stats = (CollectionStats *) list_nth( (List *) ((ForeignScan *) node->ss.ps.plan)->fdw_private, 1);
festate->rlistptr = 0;
festate->data_dir = data_dir;
festate->dir_state = AllocateDir(data_dir);
festate->mask = mask;
festate->ncols = numOfColumns;
/* Store the additional state info */
festate->attinmeta = TupleDescGetAttInMetadata(node->ss.ss_currentRelation->rd_att);
node->fdw_state = (void *) festate;
}
/*
* dcIterateForeignScan
* Read next record from the data dc and store it into the
* ScanTupleSlot as a virtual tuple
*/
static TupleTableSlot *
dcIterateForeignScan(ForeignScanState *node)
{
DcFdwExecutionState *festate = (DcFdwExecutionState *) node->fdw_state;
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
List *tupleItemList;
bool end_of_list = FALSE;
Datum *values;
bool *nulls;
#ifdef DEBUG
elog(NOTICE, "dcIterateForeignScan");
#endif
if (list_length(festate->rlist) > festate->rlistptr)
{
StringInfoData sidDocPath;
StringInfoData sidFName;
File currFile;
char *buf;
int doc_id = list_nth_int(festate->rlist, festate->rlistptr);
initStringInfo(&sidFName);
appendStringInfo(&sidFName, "%d", doc_id);
/* get full path/name of the file */
initStringInfo(&sidDocPath);
appendStringInfo(&sidDocPath, "%s/%s", festate->data_dir, sidFName.data);
/*
* load file content into buffer
*/
currFile = openDoc(sidDocPath.data);
loadDoc(&buf, currFile);
closeDoc(currFile);
tupleItemList = list_make2(sidFName.data, buf);
festate->rlistptr += 1;
}
else
end_of_list = TRUE;
/*
* The protocol for loading a virtual tuple into a slot is first
* ExecClearTuple, then fill the values/isnull arrays, then
* ExecStoreVirtualTuple. If we don't find another row in the dc, we
* just skip the last step, leaving the slot empty as required.
*
* We can pass ExprContext = NULL because we read all columns from the
* dc, so no need to evaluate default expressions.
*
* We can also pass tupleOid = NULL because we don't allow oids for
* foreign tables.
*/
ExecClearTuple(slot);
if (!end_of_list)
{
HeapTuple tuple;
values = (Datum *) palloc(festate->ncols * sizeof(Datum));
nulls = (bool *) palloc(festate->ncols * sizeof(bool));
cstring_tuple(&values, &nulls, festate->mask, festate->ncols, tupleItemList);
tuple = BuildTupleFromCStrings(festate->attinmeta, (char **) values);
ExecStoreTuple(tuple, slot, InvalidBuffer, FALSE);
}
return slot;
}
/*
* dcEndForeignScan
* Finish scanning foreign table and dispose objects used for this scan
*/
static void
dcEndForeignScan(ForeignScanState *node)
{
DcFdwExecutionState *festate = (DcFdwExecutionState *) node->fdw_state;
#ifdef DEBUG
elog(NOTICE, "dcEndForeignScan");
#endif
/* if festate is NULL, we are in EXPLAIN; nothing to do */
if (festate == NULL)
return;
}
/*
* dcReScanForeignScan
* Rescan table, possibly with new parameters
*/
static void
dcReScanForeignScan(ForeignScanState *node)
{
DcFdwExecutionState *festate = (DcFdwExecutionState *) node->fdw_state;
#ifdef DEBUG
elog(NOTICE, "dcReScanForeignScan");
#endif
/* If we haven't have valid result yet, nothing to do. */
if (festate->rlist == NIL)
return;
}
/*
* dcAnalyzeForeignTable
* Test whether analyzing this foreign table is supported
*/
static bool
dcAnalyzeForeignTable(Relation relation,
AcquireSampleRowsFunc *func,
BlockNumber *totalpages)
{
char *data_dir;
char *index_dir;
List *mappings;
File statFile;
CollectionStats *stats;
#ifdef DEBUG
elog(NOTICE, "dcAnalyzeForeignTable");
#endif
stats = (CollectionStats *) palloc(sizeof(CollectionStats));
/* Fetch options of foreign table */
dcGetOptions(RelationGetRelid(relation),
&data_dir, &index_dir, &mappings);
/*
* Get size of the collection. (XXX if we fail here, would it be better to just
* return false to skip analyzing the table?)
*/
statFile = openStat(index_dir);
loadStat(&stats, statFile);
closeStat(statFile);
/*
* Convert size to pages. Must return at least 1 so that we can tell
* later on that pg_class.relpages is not default.
*/
*totalpages = (stats->numOfBytes + (BLCKSZ - 1)) / BLCKSZ;
if (*totalpages < 1)
*totalpages = 1;
*func = dc_acquire_sample_rows;
return true;
}
/*
* Estimate size of a foreign table.
*
* The main result is returned in baserel->rows. We also set
* fdw_private->pages and fdw_private->ntuples for later use in the cost
* calculation.
*/
static void
estimate_size(PlannerInfo *root, RelOptInfo *baserel,
DcFdwPlanState *fpstate, CollectionStats *stats)
{
BlockNumber pages;
double nrows;
int nbytes = stats->numOfBytes;
/*
* Convert size to pages for use in I/O cost estimate later.
*/
pages = (nbytes + (BLCKSZ - 1)) / BLCKSZ;
if (pages < 1)
pages = 1;
fpstate->pages = pages;
/*
* Estimate the number of tuples in the collection.
*/
fpstate->ntuples = (double) stats->numOfDocs;
/*
* Now estimate the number of rows returned by the scan after applying the
* baserestrictinfo quals.
*/
nrows = fpstate->ntuples *
clauselist_selectivity(root,
baserel->baserestrictinfo,
0,
JOIN_INNER,
NULL);
nrows = clamp_row_est(nrows);
/* Save the output-rows estimate for the planner */
baserel->rows = nrows;
}
/*
* Estimate costs of scanning a foreign table.
*
* Results are returned in *startup_cost and *total_cost.
*/
static void
estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
DcFdwPlanState *fpstate,
Cost *startup_cost, Cost *total_cost)
{
BlockNumber pages = fpstate->pages;
double ntuples = fpstate->ntuples;
Cost run_cost = 0;
Cost cpu_per_tuple;
/*
* We estimate costs almost the same way as cost_seqscan(), thus assuming
* that I/O costs are equivalent to a regular table file of the same size.
* However, we take per-tuple CPU costs as 10x of a seqscan, to account
* for the cost of parsing records.
*/
run_cost += seq_page_cost * pages;
*startup_cost = baserel->baserestrictcost.startup;
cpu_per_tuple = cpu_tuple_cost * 10 + baserel->baserestrictcost.per_tuple;
run_cost += cpu_per_tuple * ntuples;