PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
file_fdw.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * file_fdw.c
4  * foreign-data wrapper for server-side flat files (or programs).
5  *
6  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * contrib/file_fdw/file_fdw.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include <sys/stat.h>
16 #include <unistd.h>
17 
18 #include "access/htup_details.h"
19 #include "access/reloptions.h"
20 #include "access/sysattr.h"
22 #include "commands/copy.h"
23 #include "commands/defrem.h"
24 #include "commands/explain.h"
25 #include "commands/vacuum.h"
26 #include "foreign/fdwapi.h"
27 #include "foreign/foreign.h"
28 #include "miscadmin.h"
29 #include "nodes/makefuncs.h"
30 #include "optimizer/cost.h"
31 #include "optimizer/pathnode.h"
32 #include "optimizer/planmain.h"
33 #include "optimizer/restrictinfo.h"
34 #include "optimizer/var.h"
35 #include "utils/memutils.h"
36 #include "utils/rel.h"
37 #include "utils/sampling.h"
38 
40 
41 /*
42  * Describes the valid options for objects that use this wrapper.
43  */
45 {
46  const char *optname;
47  Oid optcontext; /* Oid of catalog in which option may appear */
48 };
49 
50 /*
51  * Valid options for file_fdw.
52  * These options are based on the options for the COPY FROM command.
53  * But note that force_not_null and force_null are handled as boolean options
54  * attached to a column, not as table options.
55  *
56  * Note: If you are adding new option for user mapping, you need to modify
57  * fileGetOptions(), which currently doesn't bother to look at user mappings.
58  */
59 static const struct FileFdwOption valid_options[] = {
60  /* Data source options */
61  {"filename", ForeignTableRelationId},
62  {"program", ForeignTableRelationId},
63 
64  /* Format options */
65  /* oids option is not supported */
66  {"format", ForeignTableRelationId},
67  {"header", ForeignTableRelationId},
68  {"delimiter", ForeignTableRelationId},
69  {"quote", ForeignTableRelationId},
70  {"escape", ForeignTableRelationId},
71  {"null", ForeignTableRelationId},
72  {"encoding", ForeignTableRelationId},
73  {"force_not_null", AttributeRelationId},
74  {"force_null", AttributeRelationId},
75 
76  /*
77  * force_quote is not supported by file_fdw because it's for COPY TO.
78  */
79 
80  /* Sentinel */
81  {NULL, InvalidOid}
82 };
83 
84 /*
85  * FDW-specific information for RelOptInfo.fdw_private.
86  */
87 typedef struct FileFdwPlanState
88 {
89  char *filename; /* file or program to read from */
90  bool is_program; /* true if filename represents an OS command */
91  List *options; /* merged COPY options, excluding filename and
92  * is_program */
93  BlockNumber pages; /* estimate of file's physical size */
94  double ntuples; /* estimate of number of data rows */
96 
97 /*
98  * FDW-specific information for ForeignScanState.fdw_state.
99  */
100 typedef struct FileFdwExecutionState
101 {
102  char *filename; /* file or program to read from */
103  bool is_program; /* true if filename represents an OS command */
104  List *options; /* merged COPY options, excluding filename and
105  * is_program */
106  CopyState cstate; /* COPY execution state */
108 
109 /*
110  * SQL functions
111  */
114 
115 /*
116  * FDW callback routines
117  */
118 static void fileGetForeignRelSize(PlannerInfo *root,
119  RelOptInfo *baserel,
120  Oid foreigntableid);
121 static void fileGetForeignPaths(PlannerInfo *root,
122  RelOptInfo *baserel,
123  Oid foreigntableid);
125  RelOptInfo *baserel,
126  Oid foreigntableid,
127  ForeignPath *best_path,
128  List *tlist,
129  List *scan_clauses,
130  Plan *outer_plan);
132 static void fileBeginForeignScan(ForeignScanState *node, int eflags);
134 static void fileReScanForeignScan(ForeignScanState *node);
135 static void fileEndForeignScan(ForeignScanState *node);
136 static bool fileAnalyzeForeignTable(Relation relation,
137  AcquireSampleRowsFunc *func,
138  BlockNumber *totalpages);
140  RangeTblEntry *rte);
141 
142 /*
143  * Helper functions
144  */
145 static bool is_valid_option(const char *option, Oid context);
146 static void fileGetOptions(Oid foreigntableid,
147  char **filename,
148  bool *is_program,
149  List **other_options);
151 static bool check_selective_binary_conversion(RelOptInfo *baserel,
152  Oid foreigntableid,
153  List **columns);
154 static void estimate_size(PlannerInfo *root, RelOptInfo *baserel,
155  FileFdwPlanState *fdw_private);
156 static void estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
157  FileFdwPlanState *fdw_private,
158  Cost *startup_cost, Cost *total_cost);
159 static int file_acquire_sample_rows(Relation onerel, int elevel,
160  HeapTuple *rows, int targrows,
161  double *totalrows, double *totaldeadrows);
162 
163 
164 /*
165  * Foreign-data wrapper handler function: return a struct with pointers
166  * to my callback routines.
167  */
168 Datum
170 {
171  FdwRoutine *fdwroutine = makeNode(FdwRoutine);
172 
174  fdwroutine->GetForeignPaths = fileGetForeignPaths;
175  fdwroutine->GetForeignPlan = fileGetForeignPlan;
180  fdwroutine->EndForeignScan = fileEndForeignScan;
183 
184  PG_RETURN_POINTER(fdwroutine);
185 }
186 
187 /*
188  * Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
189  * USER MAPPING or FOREIGN TABLE that uses file_fdw.
190  *
191  * Raise an ERROR if the option or its value is considered invalid.
192  */
193 Datum
195 {
196  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
197  Oid catalog = PG_GETARG_OID(1);
198  char *filename = NULL;
199  DefElem *force_not_null = NULL;
200  DefElem *force_null = NULL;
201  List *other_options = NIL;
202  ListCell *cell;
203 
204  /*
205  * Only superusers are allowed to set options of a file_fdw foreign table.
206  * This is because we don't want non-superusers to be able to control
207  * which file gets read or which program gets executed.
208  *
209  * Putting this sort of permissions check in a validator is a bit of a
210  * crock, but there doesn't seem to be any other place that can enforce
211  * the check more cleanly.
212  *
213  * Note that the valid_options[] array disallows setting filename and
214  * program at any options level other than foreign table --- otherwise
215  * there'd still be a security hole.
216  */
217  if (catalog == ForeignTableRelationId && !superuser())
218  ereport(ERROR,
219  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
220  errmsg("only superuser can change options of a file_fdw foreign table")));
221 
222  /*
223  * Check that only options supported by file_fdw, and allowed for the
224  * current object type, are given.
225  */
226  foreach(cell, options_list)
227  {
228  DefElem *def = (DefElem *) lfirst(cell);
229 
230  if (!is_valid_option(def->defname, catalog))
231  {
232  const struct FileFdwOption *opt;
234 
235  /*
236  * Unknown option specified, complain about it. Provide a hint
237  * with list of valid options for the object.
238  */
239  initStringInfo(&buf);
240  for (opt = valid_options; opt->optname; opt++)
241  {
242  if (catalog == opt->optcontext)
243  appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
244  opt->optname);
245  }
246 
247  ereport(ERROR,
248  (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
249  errmsg("invalid option \"%s\"", def->defname),
250  buf.len > 0
251  ? errhint("Valid options in this context are: %s",
252  buf.data)
253  : errhint("There are no valid options in this context.")));
254  }
255 
256  /*
257  * Separate out filename, program, and column-specific options, since
258  * ProcessCopyOptions won't accept them.
259  */
260  if (strcmp(def->defname, "filename") == 0 ||
261  strcmp(def->defname, "program") == 0)
262  {
263  if (filename)
264  ereport(ERROR,
265  (errcode(ERRCODE_SYNTAX_ERROR),
266  errmsg("conflicting or redundant options")));
267  filename = defGetString(def);
268  }
269 
270  /*
271  * force_not_null is a boolean option; after validation we can discard
272  * it - it will be retrieved later in get_file_fdw_attribute_options()
273  */
274  else if (strcmp(def->defname, "force_not_null") == 0)
275  {
276  if (force_not_null)
277  ereport(ERROR,
278  (errcode(ERRCODE_SYNTAX_ERROR),
279  errmsg("conflicting or redundant options"),
280  errhint("option \"force_not_null\" supplied more than once for a column")));
281  force_not_null = def;
282  /* Don't care what the value is, as long as it's a legal boolean */
283  (void) defGetBoolean(def);
284  }
285  /* See comments for force_not_null above */
286  else if (strcmp(def->defname, "force_null") == 0)
287  {
288  if (force_null)
289  ereport(ERROR,
290  (errcode(ERRCODE_SYNTAX_ERROR),
291  errmsg("conflicting or redundant options"),
292  errhint("option \"force_null\" supplied more than once for a column")));
293  force_null = def;
294  (void) defGetBoolean(def);
295  }
296  else
297  other_options = lappend(other_options, def);
298  }
299 
300  /*
301  * Now apply the core COPY code's validation logic for more checks.
302  */
303  ProcessCopyOptions(NULL, NULL, true, other_options);
304 
305  /*
306  * Either filename or program option is required for file_fdw foreign
307  * tables.
308  */
309  if (catalog == ForeignTableRelationId && filename == NULL)
310  ereport(ERROR,
311  (errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
312  errmsg("either filename or program is required for file_fdw foreign tables")));
313 
314  PG_RETURN_VOID();
315 }
316 
317 /*
318  * Check if the provided option is one of the valid options.
319  * context is the Oid of the catalog holding the object the option is for.
320  */
321 static bool
322 is_valid_option(const char *option, Oid context)
323 {
324  const struct FileFdwOption *opt;
325 
326  for (opt = valid_options; opt->optname; opt++)
327  {
328  if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
329  return true;
330  }
331  return false;
332 }
333 
334 /*
335  * Fetch the options for a file_fdw foreign table.
336  *
337  * We have to separate out filename/program from the other options because
338  * those must not appear in the options list passed to the core COPY code.
339  */
340 static void
341 fileGetOptions(Oid foreigntableid,
342  char **filename, bool *is_program, List **other_options)
343 {
344  ForeignTable *table;
345  ForeignServer *server;
346  ForeignDataWrapper *wrapper;
347  List *options;
348  ListCell *lc,
349  *prev;
350 
351  /*
352  * Extract options from FDW objects. We ignore user mappings because
353  * file_fdw doesn't have any options that can be specified there.
354  *
355  * (XXX Actually, given the current contents of valid_options[], there's
356  * no point in examining anything except the foreign table's own options.
357  * Simplify?)
358  */
359  table = GetForeignTable(foreigntableid);
360  server = GetForeignServer(table->serverid);
361  wrapper = GetForeignDataWrapper(server->fdwid);
362 
363  options = NIL;
364  options = list_concat(options, wrapper->options);
365  options = list_concat(options, server->options);
366  options = list_concat(options, table->options);
367  options = list_concat(options, get_file_fdw_attribute_options(foreigntableid));
368 
369  /*
370  * Separate out the filename or program option (we assume there is only
371  * one).
372  */
373  *filename = NULL;
374  *is_program = false;
375  prev = NULL;
376  foreach(lc, options)
377  {
378  DefElem *def = (DefElem *) lfirst(lc);
379 
380  if (strcmp(def->defname, "filename") == 0)
381  {
382  *filename = defGetString(def);
383  options = list_delete_cell(options, lc, prev);
384  break;
385  }
386  else if (strcmp(def->defname, "program") == 0)
387  {
388  *filename = defGetString(def);
389  *is_program = true;
390  options = list_delete_cell(options, lc, prev);
391  break;
392  }
393  prev = lc;
394  }
395 
396  /*
397  * The validator should have checked that filename or program was included
398  * in the options, but check again, just in case.
399  */
400  if (*filename == NULL)
401  elog(ERROR, "either filename or program is required for file_fdw foreign tables");
402 
403  *other_options = options;
404 }
405 
406 /*
407  * Retrieve per-column generic options from pg_attribute and construct a list
408  * of DefElems representing them.
409  *
410  * At the moment we only have "force_not_null", and "force_null",
411  * which should each be combined into a single DefElem listing all such
412  * columns, since that's what COPY expects.
413  */
414 static List *
416 {
417  Relation rel;
419  AttrNumber natts;
420  AttrNumber attnum;
421  List *fnncolumns = NIL;
422  List *fncolumns = NIL;
423 
424  List *options = NIL;
425 
426  rel = heap_open(relid, AccessShareLock);
427  tupleDesc = RelationGetDescr(rel);
428  natts = tupleDesc->natts;
429 
430  /* Retrieve FDW options for all user-defined attributes. */
431  for (attnum = 1; attnum <= natts; attnum++)
432  {
433  Form_pg_attribute attr = tupleDesc->attrs[attnum - 1];
434  List *options;
435  ListCell *lc;
436 
437  /* Skip dropped attributes. */
438  if (attr->attisdropped)
439  continue;
440 
441  options = GetForeignColumnOptions(relid, attnum);
442  foreach(lc, options)
443  {
444  DefElem *def = (DefElem *) lfirst(lc);
445 
446  if (strcmp(def->defname, "force_not_null") == 0)
447  {
448  if (defGetBoolean(def))
449  {
450  char *attname = pstrdup(NameStr(attr->attname));
451 
452  fnncolumns = lappend(fnncolumns, makeString(attname));
453  }
454  }
455  else if (strcmp(def->defname, "force_null") == 0)
456  {
457  if (defGetBoolean(def))
458  {
459  char *attname = pstrdup(NameStr(attr->attname));
460 
461  fncolumns = lappend(fncolumns, makeString(attname));
462  }
463  }
464  /* maybe in future handle other options here */
465  }
466  }
467 
469 
470  /*
471  * Return DefElem only when some column(s) have force_not_null /
472  * force_null options set
473  */
474  if (fnncolumns != NIL)
475  options = lappend(options, makeDefElem("force_not_null", (Node *) fnncolumns, -1));
476 
477  if (fncolumns != NIL)
478  options = lappend(options, makeDefElem("force_null", (Node *) fncolumns, -1));
479 
480  return options;
481 }
482 
483 /*
484  * fileGetForeignRelSize
485  * Obtain relation size estimates for a foreign table
486  */
487 static void
489  RelOptInfo *baserel,
490  Oid foreigntableid)
491 {
492  FileFdwPlanState *fdw_private;
493 
494  /*
495  * Fetch options. We only need filename (or program) at this point, but
496  * we might as well get everything and not need to re-fetch it later in
497  * planning.
498  */
499  fdw_private = (FileFdwPlanState *) palloc(sizeof(FileFdwPlanState));
500  fileGetOptions(foreigntableid,
501  &fdw_private->filename,
502  &fdw_private->is_program,
503  &fdw_private->options);
504  baserel->fdw_private = (void *) fdw_private;
505 
506  /* Estimate relation size */
507  estimate_size(root, baserel, fdw_private);
508 }
509 
510 /*
511  * fileGetForeignPaths
512  * Create possible access paths for a scan on the foreign table
513  *
514  * Currently we don't support any push-down feature, so there is only one
515  * possible access path, which simply returns all records in the order in
516  * the data file.
517  */
518 static void
520  RelOptInfo *baserel,
521  Oid foreigntableid)
522 {
523  FileFdwPlanState *fdw_private = (FileFdwPlanState *) baserel->fdw_private;
524  Cost startup_cost;
525  Cost total_cost;
526  List *columns;
527  List *coptions = NIL;
528 
529  /* Decide whether to selectively perform binary conversion */
531  foreigntableid,
532  &columns))
533  coptions = list_make1(makeDefElem("convert_selectively",
534  (Node *) columns, -1));
535 
536  /* Estimate costs */
537  estimate_costs(root, baserel, fdw_private,
538  &startup_cost, &total_cost);
539 
540  /*
541  * Create a ForeignPath node and add it as only possible path. We use the
542  * fdw_private list of the path to carry the convert_selectively option;
543  * it will be propagated into the fdw_private list of the Plan node.
544  */
545  add_path(baserel, (Path *)
546  create_foreignscan_path(root, baserel,
547  NULL, /* default pathtarget */
548  baserel->rows,
549  startup_cost,
550  total_cost,
551  NIL, /* no pathkeys */
552  NULL, /* no outer rel either */
553  NULL, /* no extra plan */
554  coptions));
555 
556  /*
557  * If data file was sorted, and we knew it somehow, we could insert
558  * appropriate pathkeys into the ForeignPath node to tell the planner
559  * that.
560  */
561 }
562 
563 /*
564  * fileGetForeignPlan
565  * Create a ForeignScan plan node for scanning the foreign table
566  */
567 static ForeignScan *
569  RelOptInfo *baserel,
570  Oid foreigntableid,
571  ForeignPath *best_path,
572  List *tlist,
573  List *scan_clauses,
574  Plan *outer_plan)
575 {
576  Index scan_relid = baserel->relid;
577 
578  /*
579  * We have no native ability to evaluate restriction clauses, so we just
580  * put all the scan_clauses into the plan node's qual list for the
581  * executor to check. So all we have to do here is strip RestrictInfo
582  * nodes from the clauses and ignore pseudoconstants (which will be
583  * handled elsewhere).
584  */
585  scan_clauses = extract_actual_clauses(scan_clauses, false);
586 
587  /* Create the ForeignScan node */
588  return make_foreignscan(tlist,
589  scan_clauses,
590  scan_relid,
591  NIL, /* no expressions to evaluate */
592  best_path->fdw_private,
593  NIL, /* no custom tlist */
594  NIL, /* no remote quals */
595  outer_plan);
596 }
597 
598 /*
599  * fileExplainForeignScan
600  * Produce extra output for EXPLAIN
601  */
602 static void
604 {
605  char *filename;
606  bool is_program;
607  List *options;
608 
609  /* Fetch options --- we only need filename and is_program at this point */
611  &filename, &is_program, &options);
612 
613  if (is_program)
614  ExplainPropertyText("Foreign Program", filename, es);
615  else
616  ExplainPropertyText("Foreign File", filename, es);
617 
618  /* Suppress file size if we're not showing cost details */
619  if (es->costs)
620  {
621  struct stat stat_buf;
622 
623  if (!is_program &&
624  stat(filename, &stat_buf) == 0)
625  ExplainPropertyLong("Foreign File Size", (long) stat_buf.st_size,
626  es);
627  }
628 }
629 
630 /*
631  * fileBeginForeignScan
632  * Initiate access to the file by creating CopyState
633  */
634 static void
636 {
637  ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
638  char *filename;
639  bool is_program;
640  List *options;
641  CopyState cstate;
642  FileFdwExecutionState *festate;
643 
644  /*
645  * Do nothing in EXPLAIN (no ANALYZE) case. node->fdw_state stays NULL.
646  */
647  if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
648  return;
649 
650  /* Fetch options of foreign table */
652  &filename, &is_program, &options);
653 
654  /* Add any options from the plan (currently only convert_selectively) */
656 
657  /*
658  * Create CopyState from FDW options. We always acquire all columns, so
659  * as to match the expected ScanTupleSlot signature.
660  */
661  cstate = BeginCopyFrom(NULL,
662  node->ss.ss_currentRelation,
663  filename,
664  is_program,
665  NIL,
666  options);
667 
668  /*
669  * Save state in node->fdw_state. We must save enough information to call
670  * BeginCopyFrom() again.
671  */
672  festate = (FileFdwExecutionState *) palloc(sizeof(FileFdwExecutionState));
673  festate->filename = filename;
674  festate->is_program = is_program;
675  festate->options = options;
676  festate->cstate = cstate;
677 
678  node->fdw_state = (void *) festate;
679 }
680 
681 /*
682  * fileIterateForeignScan
683  * Read next record from the data file and store it into the
684  * ScanTupleSlot as a virtual tuple
685  */
686 static TupleTableSlot *
688 {
690  TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
691  bool found;
692  ErrorContextCallback errcallback;
693 
694  /* Set up callback to identify error line number. */
695  errcallback.callback = CopyFromErrorCallback;
696  errcallback.arg = (void *) festate->cstate;
697  errcallback.previous = error_context_stack;
698  error_context_stack = &errcallback;
699 
700  /*
701  * The protocol for loading a virtual tuple into a slot is first
702  * ExecClearTuple, then fill the values/isnull arrays, then
703  * ExecStoreVirtualTuple. If we don't find another row in the file, we
704  * just skip the last step, leaving the slot empty as required.
705  *
706  * We can pass ExprContext = NULL because we read all columns from the
707  * file, so no need to evaluate default expressions.
708  *
709  * We can also pass tupleOid = NULL because we don't allow oids for
710  * foreign tables.
711  */
712  ExecClearTuple(slot);
713  found = NextCopyFrom(festate->cstate, NULL,
714  slot->tts_values, slot->tts_isnull,
715  NULL);
716  if (found)
717  ExecStoreVirtualTuple(slot);
718 
719  /* Remove error callback. */
720  error_context_stack = errcallback.previous;
721 
722  return slot;
723 }
724 
725 /*
726  * fileReScanForeignScan
727  * Rescan table, possibly with new parameters
728  */
729 static void
731 {
733 
734  EndCopyFrom(festate->cstate);
735 
736  festate->cstate = BeginCopyFrom(NULL,
737  node->ss.ss_currentRelation,
738  festate->filename,
739  festate->is_program,
740  NIL,
741  festate->options);
742 }
743 
744 /*
745  * fileEndForeignScan
746  * Finish scanning foreign table and dispose objects used for this scan
747  */
748 static void
750 {
752 
753  /* if festate is NULL, we are in EXPLAIN; nothing to do */
754  if (festate)
755  EndCopyFrom(festate->cstate);
756 }
757 
758 /*
759  * fileAnalyzeForeignTable
760  * Test whether analyzing this foreign table is supported
761  */
762 static bool
764  AcquireSampleRowsFunc *func,
765  BlockNumber *totalpages)
766 {
767  char *filename;
768  bool is_program;
769  List *options;
770  struct stat stat_buf;
771 
772  /* Fetch options of foreign table */
773  fileGetOptions(RelationGetRelid(relation), &filename, &is_program, &options);
774 
775  /*
776  * If this is a program instead of a file, just return false to skip
777  * analyzing the table. We could run the program and collect stats on
778  * whatever it currently returns, but it seems likely that in such cases
779  * the output would be too volatile for the stats to be useful. Maybe
780  * there should be an option to enable doing this?
781  */
782  if (is_program)
783  return false;
784 
785  /*
786  * Get size of the file. (XXX if we fail here, would it be better to just
787  * return false to skip analyzing the table?)
788  */
789  if (stat(filename, &stat_buf) < 0)
790  ereport(ERROR,
792  errmsg("could not stat file \"%s\": %m",
793  filename)));
794 
795  /*
796  * Convert size to pages. Must return at least 1 so that we can tell
797  * later on that pg_class.relpages is not default.
798  */
799  *totalpages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
800  if (*totalpages < 1)
801  *totalpages = 1;
802 
803  *func = file_acquire_sample_rows;
804 
805  return true;
806 }
807 
808 /*
809  * fileIsForeignScanParallelSafe
810  * Reading a file, or external program, in a parallel worker should work
811  * just the same as reading it in the leader, so mark scans safe.
812  */
813 static bool
815  RangeTblEntry *rte)
816 {
817  return true;
818 }
819 
820 /*
821  * check_selective_binary_conversion
822  *
823  * Check to see if it's useful to convert only a subset of the file's columns
824  * to binary. If so, construct a list of the column names to be converted,
825  * return that at *columns, and return TRUE. (Note that it's possible to
826  * determine that no columns need be converted, for instance with a COUNT(*)
827  * query. So we can't use returning a NIL list to indicate failure.)
828  */
829 static bool
831  Oid foreigntableid,
832  List **columns)
833 {
834  ForeignTable *table;
835  ListCell *lc;
836  Relation rel;
838  AttrNumber attnum;
839  Bitmapset *attrs_used = NULL;
840  bool has_wholerow = false;
841  int numattrs;
842  int i;
843 
844  *columns = NIL; /* default result */
845 
846  /*
847  * Check format of the file. If binary format, this is irrelevant.
848  */
849  table = GetForeignTable(foreigntableid);
850  foreach(lc, table->options)
851  {
852  DefElem *def = (DefElem *) lfirst(lc);
853 
854  if (strcmp(def->defname, "format") == 0)
855  {
856  char *format = defGetString(def);
857 
858  if (strcmp(format, "binary") == 0)
859  return false;
860  break;
861  }
862  }
863 
864  /* Collect all the attributes needed for joins or final output. */
865  pull_varattnos((Node *) baserel->reltarget->exprs, baserel->relid,
866  &attrs_used);
867 
868  /* Add all the attributes used by restriction clauses. */
869  foreach(lc, baserel->baserestrictinfo)
870  {
871  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
872 
873  pull_varattnos((Node *) rinfo->clause, baserel->relid,
874  &attrs_used);
875  }
876 
877  /* Convert attribute numbers to column names. */
878  rel = heap_open(foreigntableid, AccessShareLock);
879  tupleDesc = RelationGetDescr(rel);
880 
881  while ((attnum = bms_first_member(attrs_used)) >= 0)
882  {
883  /* Adjust for system attributes. */
885 
886  if (attnum == 0)
887  {
888  has_wholerow = true;
889  break;
890  }
891 
892  /* Ignore system attributes. */
893  if (attnum < 0)
894  continue;
895 
896  /* Get user attributes. */
897  if (attnum > 0)
898  {
899  Form_pg_attribute attr = tupleDesc->attrs[attnum - 1];
900  char *attname = NameStr(attr->attname);
901 
902  /* Skip dropped attributes (probably shouldn't see any here). */
903  if (attr->attisdropped)
904  continue;
905  *columns = lappend(*columns, makeString(pstrdup(attname)));
906  }
907  }
908 
909  /* Count non-dropped user attributes while we have the tupdesc. */
910  numattrs = 0;
911  for (i = 0; i < tupleDesc->natts; i++)
912  {
913  Form_pg_attribute attr = tupleDesc->attrs[i];
914 
915  if (attr->attisdropped)
916  continue;
917  numattrs++;
918  }
919 
921 
922  /* If there's a whole-row reference, fail: we need all the columns. */
923  if (has_wholerow)
924  {
925  *columns = NIL;
926  return false;
927  }
928 
929  /* If all the user attributes are needed, fail. */
930  if (numattrs == list_length(*columns))
931  {
932  *columns = NIL;
933  return false;
934  }
935 
936  return true;
937 }
938 
939 /*
940  * Estimate size of a foreign table.
941  *
942  * The main result is returned in baserel->rows. We also set
943  * fdw_private->pages and fdw_private->ntuples for later use in the cost
944  * calculation.
945  */
946 static void
948  FileFdwPlanState *fdw_private)
949 {
950  struct stat stat_buf;
951  BlockNumber pages;
952  double ntuples;
953  double nrows;
954 
955  /*
956  * Get size of the file. It might not be there at plan time, though, in
957  * which case we have to use a default estimate. We also have to fall
958  * back to the default if using a program as the input.
959  */
960  if (fdw_private->is_program || stat(fdw_private->filename, &stat_buf) < 0)
961  stat_buf.st_size = 10 * BLCKSZ;
962 
963  /*
964  * Convert size to pages for use in I/O cost estimate later.
965  */
966  pages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
967  if (pages < 1)
968  pages = 1;
969  fdw_private->pages = pages;
970 
971  /*
972  * Estimate the number of tuples in the file.
973  */
974  if (baserel->pages > 0)
975  {
976  /*
977  * We have # of pages and # of tuples from pg_class (that is, from a
978  * previous ANALYZE), so compute a tuples-per-page estimate and scale
979  * that by the current file size.
980  */
981  double density;
982 
983  density = baserel->tuples / (double) baserel->pages;
984  ntuples = clamp_row_est(density * (double) pages);
985  }
986  else
987  {
988  /*
989  * Otherwise we have to fake it. We back into this estimate using the
990  * planner's idea of the relation width; which is bogus if not all
991  * columns are being read, not to mention that the text representation
992  * of a row probably isn't the same size as its internal
993  * representation. Possibly we could do something better, but the
994  * real answer to anyone who complains is "ANALYZE" ...
995  */
996  int tuple_width;
997 
998  tuple_width = MAXALIGN(baserel->reltarget->width) +
1000  ntuples = clamp_row_est((double) stat_buf.st_size /
1001  (double) tuple_width);
1002  }
1003  fdw_private->ntuples = ntuples;
1004 
1005  /*
1006  * Now estimate the number of rows returned by the scan after applying the
1007  * baserestrictinfo quals.
1008  */
1009  nrows = ntuples *
1011  baserel->baserestrictinfo,
1012  0,
1013  JOIN_INNER,
1014  NULL);
1015 
1016  nrows = clamp_row_est(nrows);
1017 
1018  /* Save the output-rows estimate for the planner */
1019  baserel->rows = nrows;
1020 }
1021 
1022 /*
1023  * Estimate costs of scanning a foreign table.
1024  *
1025  * Results are returned in *startup_cost and *total_cost.
1026  */
1027 static void
1029  FileFdwPlanState *fdw_private,
1030  Cost *startup_cost, Cost *total_cost)
1031 {
1032  BlockNumber pages = fdw_private->pages;
1033  double ntuples = fdw_private->ntuples;
1034  Cost run_cost = 0;
1035  Cost cpu_per_tuple;
1036 
1037  /*
1038  * We estimate costs almost the same way as cost_seqscan(), thus assuming
1039  * that I/O costs are equivalent to a regular table file of the same size.
1040  * However, we take per-tuple CPU costs as 10x of a seqscan, to account
1041  * for the cost of parsing records.
1042  *
1043  * In the case of a program source, this calculation is even more divorced
1044  * from reality, but we have no good alternative; and it's not clear that
1045  * the numbers we produce here matter much anyway, since there's only one
1046  * access path for the rel.
1047  */
1048  run_cost += seq_page_cost * pages;
1049 
1050  *startup_cost = baserel->baserestrictcost.startup;
1051  cpu_per_tuple = cpu_tuple_cost * 10 + baserel->baserestrictcost.per_tuple;
1052  run_cost += cpu_per_tuple * ntuples;
1053  *total_cost = *startup_cost + run_cost;
1054 }
1055 
1056 /*
1057  * file_acquire_sample_rows -- acquire a random sample of rows from the table
1058  *
1059  * Selected rows are returned in the caller-allocated array rows[],
1060  * which must have at least targrows entries.
1061  * The actual number of rows selected is returned as the function result.
1062  * We also count the total number of rows in the file and return it into
1063  * *totalrows. Note that *totaldeadrows is always set to 0.
1064  *
1065  * Note that the returned list of rows is not always in order by physical
1066  * position in the file. Therefore, correlation estimates derived later
1067  * may be meaningless, but it's OK because we don't use the estimates
1068  * currently (the planner only pays attention to correlation for indexscans).
1069  */
1070 static int
1072  HeapTuple *rows, int targrows,
1073  double *totalrows, double *totaldeadrows)
1074 {
1075  int numrows = 0;
1076  double rowstoskip = -1; /* -1 means not set yet */
1077  ReservoirStateData rstate;
1078  TupleDesc tupDesc;
1079  Datum *values;
1080  bool *nulls;
1081  bool found;
1082  char *filename;
1083  bool is_program;
1084  List *options;
1085  CopyState cstate;
1086  ErrorContextCallback errcallback;
1087  MemoryContext oldcontext = CurrentMemoryContext;
1088  MemoryContext tupcontext;
1089 
1090  Assert(onerel);
1091  Assert(targrows > 0);
1092 
1093  tupDesc = RelationGetDescr(onerel);
1094  values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
1095  nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
1096 
1097  /* Fetch options of foreign table */
1098  fileGetOptions(RelationGetRelid(onerel), &filename, &is_program, &options);
1099 
1100  /*
1101  * Create CopyState from FDW options.
1102  */
1103  cstate = BeginCopyFrom(NULL, onerel, filename, is_program, NIL, options);
1104 
1105  /*
1106  * Use per-tuple memory context to prevent leak of memory used to read
1107  * rows from the file with Copy routines.
1108  */
1110  "file_fdw temporary context",
1112 
1113  /* Prepare for sampling rows */
1114  reservoir_init_selection_state(&rstate, targrows);
1115 
1116  /* Set up callback to identify error line number. */
1117  errcallback.callback = CopyFromErrorCallback;
1118  errcallback.arg = (void *) cstate;
1119  errcallback.previous = error_context_stack;
1120  error_context_stack = &errcallback;
1121 
1122  *totalrows = 0;
1123  *totaldeadrows = 0;
1124  for (;;)
1125  {
1126  /* Check for user-requested abort or sleep */
1128 
1129  /* Fetch next row */
1130  MemoryContextReset(tupcontext);
1131  MemoryContextSwitchTo(tupcontext);
1132 
1133  found = NextCopyFrom(cstate, NULL, values, nulls, NULL);
1134 
1135  MemoryContextSwitchTo(oldcontext);
1136 
1137  if (!found)
1138  break;
1139 
1140  /*
1141  * The first targrows sample rows are simply copied into the
1142  * reservoir. Then we start replacing tuples in the sample until we
1143  * reach the end of the relation. This algorithm is from Jeff Vitter's
1144  * paper (see more info in commands/analyze.c).
1145  */
1146  if (numrows < targrows)
1147  {
1148  rows[numrows++] = heap_form_tuple(tupDesc, values, nulls);
1149  }
1150  else
1151  {
1152  /*
1153  * t in Vitter's paper is the number of records already processed.
1154  * If we need to compute a new S value, we must use the
1155  * not-yet-incremented value of totalrows as t.
1156  */
1157  if (rowstoskip < 0)
1158  rowstoskip = reservoir_get_next_S(&rstate, *totalrows, targrows);
1159 
1160  if (rowstoskip <= 0)
1161  {
1162  /*
1163  * Found a suitable tuple, so save it, replacing one old tuple
1164  * at random
1165  */
1166  int k = (int) (targrows * sampler_random_fract(rstate.randstate));
1167 
1168  Assert(k >= 0 && k < targrows);
1169  heap_freetuple(rows[k]);
1170  rows[k] = heap_form_tuple(tupDesc, values, nulls);
1171  }
1172 
1173  rowstoskip -= 1;
1174  }
1175 
1176  *totalrows += 1;
1177  }
1178 
1179  /* Remove error callback. */
1180  error_context_stack = errcallback.previous;
1181 
1182  /* Clean up. */
1183  MemoryContextDelete(tupcontext);
1184 
1185  EndCopyFrom(cstate);
1186 
1187  pfree(values);
1188  pfree(nulls);
1189 
1190  /*
1191  * Emit some interesting relation info
1192  */
1193  ereport(elevel,
1194  (errmsg("\"%s\": file contains %.0f rows; "
1195  "%d rows in sample",
1196  RelationGetRelationName(onerel),
1197  *totalrows, numrows)));
1198 
1199  return numrows;
1200 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:175
Value * makeString(char *str)
Definition: value.c:53
BeginForeignScan_function BeginForeignScan
Definition: fdwapi.h:176
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define NIL
Definition: pg_list.h:69
ScanState ss
Definition: execnodes.h:1631
static bool fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: file_fdw.c:814
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:855
static List * get_file_fdw_attribute_options(Oid relid)
Definition: file_fdw.c:415
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
Oid optcontext
Definition: file_fdw.c:47
int errhint(const char *fmt,...)
Definition: elog.c:987
static void fileEndForeignScan(ForeignScanState *node)
Definition: file_fdw.c:749
static const struct FileFdwOption valid_options[]
Definition: file_fdw.c:59
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
ExplainForeignScan_function ExplainForeignScan
Definition: fdwapi.h:212
void CopyFromErrorCallback(void *arg)
Definition: copy.c:2184
BlockNumber pages
Definition: file_fdw.c:93
#define RelationGetDescr(relation)
Definition: rel.h:425
List * options
Definition: file_fdw.c:91
AnalyzeForeignTable_function AnalyzeForeignTable
Definition: fdwapi.h:217
Datum file_fdw_handler(PG_FUNCTION_ARGS)
Definition: file_fdw.c:169
void ExplainPropertyLong(const char *qlabel, long value, ExplainState *es)
Definition: explain.c:3067
PG_MODULE_MAGIC
Definition: file_fdw.c:39
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
int(* AcquireSampleRowsFunc)(Relation relation, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: fdwapi.h:134
char * pstrdup(const char *in)
Definition: mcxt.c:1165
double tuples
Definition: relation.h:529
List * baserestrictinfo
Definition: relation.h:544
List * fdw_private
Definition: plannodes.h:557
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
Form_pg_attribute * attrs
Definition: tupdesc.h:74
void ProcessCopyOptions(ParseState *pstate, CopyState cstate, bool is_from, List *options)
Definition: copy.c:1012
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:508
#define AttributeRelationId
Definition: pg_attribute.h:33
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:216
List * list_concat(List *list1, List *list2)
Definition: list.c:321
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1290
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
uint32 BlockNumber
Definition: block.h:31
void reservoir_init_selection_state(ReservoirState rs, int n)
Definition: sampling.c:129
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
Relation ss_currentRelation
Definition: execnodes.h:1288
Datum file_fdw_validator(PG_FUNCTION_ARGS)
Definition: file_fdw.c:194
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
bool costs
Definition: explain.h:34
struct ErrorContextCallback * previous
Definition: elog.h:238
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:543
int natts
Definition: tupdesc.h:73
Cost startup
Definition: relation.h:45
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
double ntuples
Definition: file_fdw.c:94
static void estimate_size(PlannerInfo *root, RelOptInfo *baserel, FileFdwPlanState *fdw_private)
Definition: file_fdw.c:947
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define list_make1(x1)
Definition: pg_list.h:133
static int file_acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: file_fdw.c:1071
PlanState ps
Definition: execnodes.h:1287
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:3046
bool defGetBoolean(DefElem *def)
Definition: define.c:111
Cost per_tuple
Definition: relation.h:46
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition: foreign.c:35
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:254
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
IsForeignScanParallelSafe_function IsForeignScanParallelSafe
Definition: fdwapi.h:223
#define ERROR
Definition: elog.h:43
struct FileFdwPlanState FileFdwPlanState
CopyState BeginCopyFrom(ParseState *pstate, Relation rel, const char *filename, bool is_program, List *attnamelist, List *options)
Definition: copy.c:2876
PG_FUNCTION_INFO_V1(file_fdw_handler)
char * defGetString(DefElem *def)
Definition: define.c:49
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
static TupleTableSlot * fileIterateForeignScan(ForeignScanState *node)
Definition: file_fdw.c:687
static char * buf
Definition: pg_test_fsync.c:65
ForeignPath * create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, double rows, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer, Path *fdw_outerpath, List *fdw_private)
Definition: pathnode.c:1844
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
int errcode_for_file_access(void)
Definition: elog.c:598
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:173
EndForeignScan_function EndForeignScan
Definition: fdwapi.h:179
#define RelationGetRelationName(relation)
Definition: rel.h:433
static bool fileAnalyzeForeignTable(Relation relation, AcquireSampleRowsFunc *func, BlockNumber *totalpages)
Definition: file_fdw.c:763
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
char * filename
Definition: file_fdw.c:89
static void fileBeginForeignScan(ForeignScanState *node, int eflags)
Definition: file_fdw.c:635
#define ereport(elevel, rest)
Definition: elog.h:122
static void fileGetOptions(Oid foreigntableid, char **filename, bool *is_program, List **other_options)
Definition: file_fdw.c:341
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
GetForeignPaths_function GetForeignPaths
Definition: fdwapi.h:174
Expr * clause
Definition: relation.h:1637
static char ** options
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
List * options
Definition: foreign.h:42
List * exprs
Definition: relation.h:824
const char * optname
Definition: file_fdw.c:46
static int elevel
Definition: vacuumlazy.c:136
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
static void fileGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
Definition: file_fdw.c:519
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:93
uintptr_t Datum
Definition: postgres.h:374
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
unsigned int Index
Definition: c.h:361
List * untransformRelOptions(Datum options)
Definition: reloptions.c:861
Plan * plan
Definition: execnodes.h:1046
double rows
Definition: relation.h:493
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: relation.h:541
#define PG_RETURN_VOID()
Definition: fmgr.h:293
void EndCopyFrom(CopyState cstate)
Definition: copy.c:3418
#define makeNode(_type_)
Definition: nodes.h:556
BlockNumber pages
Definition: relation.h:528
#define NULL
Definition: c.h:226
static void fileGetForeignRelSize(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
Definition: file_fdw.c:488
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
ReScanForeignScan_function ReScanForeignScan
Definition: fdwapi.h:178
static bool check_selective_binary_conversion(RelOptInfo *baserel, Oid foreigntableid, List **columns)
Definition: file_fdw.c:830
IterateForeignScan_function IterateForeignScan
Definition: fdwapi.h:177
static bool is_valid_option(const char *option, Oid context)
Definition: file_fdw.c:322
static void fileExplainForeignScan(ForeignScanState *node, ExplainState *es)
Definition: file_fdw.c:603
Oid serverid
Definition: foreign.h:67
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
#define MAXALIGN(LEN)
Definition: c.h:583
ForeignScan * make_foreignscan(List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
Definition: createplan.c:4972
static void fileReScanForeignScan(ForeignScanState *node)
Definition: file_fdw.c:730
static ForeignScan * fileGetForeignPlan(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid, ForeignPath *best_path, List *tlist, List *scan_clauses, Plan *outer_plan)
Definition: file_fdw.c:568
double cpu_tuple_cost
Definition: costsize.c:106
static void estimate_costs(PlannerInfo *root, RelOptInfo *baserel, FileFdwPlanState *fdw_private, Cost *startup_cost, Cost *total_cost)
Definition: file_fdw.c:1028
static Datum values[MAXATTR]
Definition: bootstrap.c:162
List * options
Definition: foreign.h:68
struct FileFdwExecutionState FileFdwExecutionState
static char * filename
Definition: pg_dumpall.c:84
int width
Definition: relation.h:827
void(* callback)(void *arg)
Definition: elog.h:239
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * fdw_private
Definition: relation.h:1073
#define ForeignTableRelationId
bool NextCopyFrom(CopyState cstate, ExprContext *econtext, Datum *values, bool *nulls, Oid *tupleOid)
Definition: copy.c:3177
int i
#define NameStr(name)
Definition: c.h:494
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
char * defname
Definition: parsenodes.h:675
static char format
Definition: pg_basebackup.c:83
#define elog
Definition: elog.h:219
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:92
void vacuum_delay_point(void)
Definition: vacuum.c:1515
double clamp_row_est(double nrows)
Definition: costsize.c:172
double seq_page_cost
Definition: costsize.c:104
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:501
List * options
Definition: foreign.h:53
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:413
QualCost baserestrictcost
Definition: relation.h:546
Definition: relation.h:888
double Cost
Definition: nodes.h:632
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:488
SamplerRandomState randstate
Definition: sampling.h:50
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:142