LCOV - differential code coverage report
Current view: top level - contrib/file_fdw - file_fdw.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 89.7 % 350 314 36 314
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 22 22 22
Baseline: lcov-20250906-005545-baseline Branches: 64.7 % 224 145 79 145
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 97.3 % 37 36 1 36
(360..) days: 88.8 % 313 278 35 278
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 21 21 21
Branch coverage date bins:
(30,360] days: 70.0 % 40 28 12 28
(360..) days: 63.6 % 184 117 67 117

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

Generated by: LCOV version 2.4-beta