LCOV - code coverage report
Current view: top level - contrib/file_fdw - file_fdw.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 239 314 76.1 %
Date: 2020-05-25 06:06:29 Functions: 20 22 90.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13