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

Generated by: LCOV version 1.14