LCOV - differential code coverage report
Current view: top level - src/backend/commands - copy.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 92.4 % 316 292 1 6 11 6 5 143 30 114 12 167 1 5
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 4 4 3 1 3
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * copy.c
       4                 :  *      Implements the COPY utility command
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/backend/commands/copy.c
      12                 :  *
      13                 :  *-------------------------------------------------------------------------
      14                 :  */
      15                 : #include "postgres.h"
      16                 : 
      17                 : #include <ctype.h>
      18                 : #include <unistd.h>
      19                 : #include <sys/stat.h>
      20                 : 
      21                 : #include "access/sysattr.h"
      22                 : #include "access/table.h"
      23                 : #include "access/xact.h"
      24                 : #include "catalog/pg_authid.h"
      25                 : #include "commands/copy.h"
      26                 : #include "commands/defrem.h"
      27                 : #include "executor/executor.h"
      28                 : #include "mb/pg_wchar.h"
      29                 : #include "miscadmin.h"
      30                 : #include "nodes/makefuncs.h"
      31                 : #include "optimizer/optimizer.h"
      32                 : #include "parser/parse_coerce.h"
      33                 : #include "parser/parse_collate.h"
      34                 : #include "parser/parse_expr.h"
      35                 : #include "parser/parse_relation.h"
      36                 : #include "rewrite/rewriteHandler.h"
      37                 : #include "utils/acl.h"
      38                 : #include "utils/builtins.h"
      39                 : #include "utils/lsyscache.h"
      40                 : #include "utils/memutils.h"
      41                 : #include "utils/rel.h"
      42                 : #include "utils/rls.h"
      43                 : 
      44                 : /*
      45                 :  *   DoCopy executes the SQL COPY statement
      46                 :  *
      47                 :  * Either unload or reload contents of table <relation>, depending on <from>.
      48                 :  * (<from> = true means we are inserting into the table.)  In the "TO" case
      49                 :  * we also support copying the output of an arbitrary SELECT, INSERT, UPDATE
      50                 :  * or DELETE query.
      51                 :  *
      52                 :  * If <pipe> is false, transfer is between the table and the file named
      53                 :  * <filename>.  Otherwise, transfer is between the table and our regular
      54                 :  * input/output stream. The latter could be either stdin/stdout or a
      55                 :  * socket, depending on whether we're running under Postmaster control.
      56                 :  *
      57                 :  * Do not allow a Postgres user without the 'pg_read_server_files' or
      58                 :  * 'pg_write_server_files' role to read from or write to a file.
      59                 :  *
      60                 :  * Do not allow the copy if user doesn't have proper permission to access
      61                 :  * the table or the specifically requested columns.
      62                 :  */
      63                 : void
      64 CBC        4500 : DoCopy(ParseState *pstate, const CopyStmt *stmt,
      65                 :        int stmt_location, int stmt_len,
      66                 :        uint64 *processed)
      67                 : {
      68            4500 :     bool        is_from = stmt->is_from;
      69            4500 :     bool        pipe = (stmt->filename == NULL);
      70                 :     Relation    rel;
      71                 :     Oid         relid;
      72            4500 :     RawStmt    *query = NULL;
      73            4500 :     Node       *whereClause = NULL;
      74                 : 
      75                 :     /*
      76                 :      * Disallow COPY to/from file or program except to users with the
      77                 :      * appropriate role.
      78                 :      */
      79            4500 :     if (!pipe)
      80                 :     {
      81             448 :         if (stmt->is_program)
      82                 :         {
      83 UBC           0 :             if (!has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
      84               0 :                 ereport(ERROR,
      85                 :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      86                 :                          errmsg("permission denied to COPY to or from an external program"),
      87                 :                          errdetail("Only roles with privileges of the \"%s\" role may COPY to or from an external program.",
      88                 :                                    "pg_execute_server_program"),
      89                 :                          errhint("Anyone can COPY to stdout or from stdin. "
      90                 :                                  "psql's \\copy command also works for anyone.")));
      91                 :         }
      92                 :         else
      93                 :         {
      94 GIC         448 :             if (is_from && !has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
      95 UIC           0 :                 ereport(ERROR,
      96 ECB             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      97                 :                          errmsg("permission denied to COPY from a file"),
      98                 :                          errdetail("Only roles with privileges of the \"%s\" role may COPY from a file.",
      99                 :                                    "pg_read_server_files"),
     100                 :                          errhint("Anyone can COPY to stdout or from stdin. "
     101                 :                                  "psql's \\copy command also works for anyone.")));
     102                 : 
     103 GIC         448 :             if (!is_from && !has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
     104 UIC           0 :                 ereport(ERROR,
     105                 :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     106                 :                          errmsg("permission denied to COPY to a file"),
     107                 :                          errdetail("Only roles with privileges of the \"%s\" role may COPY to a file.",
     108                 :                                    "pg_write_server_files"),
     109 ECB             :                          errhint("Anyone can COPY to stdout or from stdin. "
     110 EUB             :                                  "psql's \\copy command also works for anyone.")));
     111                 :         }
     112                 :     }
     113                 : 
     114 GIC        4500 :     if (stmt->relation)
     115                 :     {
     116            4307 :         LOCKMODE    lockmode = is_from ? RowExclusiveLock : AccessShareLock;
     117                 :         ParseNamespaceItem *nsitem;
     118                 :         RTEPermissionInfo *perminfo;
     119                 :         TupleDesc   tupDesc;
     120 ECB             :         List       *attnums;
     121                 :         ListCell   *cur;
     122                 : 
     123 GIC        4307 :         Assert(!stmt->query);
     124                 : 
     125                 :         /* Open and lock the relation, using the appropriate lock type. */
     126            4307 :         rel = table_openrv(stmt->relation, lockmode);
     127                 : 
     128            4306 :         relid = RelationGetRelid(rel);
     129 ECB             : 
     130 GIC        4306 :         nsitem = addRangeTableEntryForRelation(pstate, rel, lockmode,
     131                 :                                                NULL, false, false);
     132                 : 
     133 GNC        4306 :         perminfo = nsitem->p_perminfo;
     134            4306 :         perminfo->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
     135 ECB             : 
     136 GIC        4306 :         if (stmt->whereClause)
     137 ECB             :         {
     138                 :             /* add nsitem to query namespace */
     139 GIC          24 :             addNSItemToQuery(pstate, nsitem, false, true, true);
     140 ECB             : 
     141                 :             /* Transform the raw expression tree */
     142 GIC          24 :             whereClause = transformExpr(pstate, stmt->whereClause, EXPR_KIND_COPY_WHERE);
     143 ECB             : 
     144                 :             /* Make sure it yields a boolean result. */
     145 GIC           9 :             whereClause = coerce_to_boolean(pstate, whereClause, "WHERE");
     146 ECB             : 
     147                 :             /* we have to fix its collations too */
     148 GIC           9 :             assign_expr_collations(pstate, whereClause);
     149 ECB             : 
     150 GIC           9 :             whereClause = eval_const_expressions(NULL, whereClause);
     151                 : 
     152 CBC           9 :             whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
     153 GIC           9 :             whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
     154                 :         }
     155 ECB             : 
     156 GIC        4291 :         tupDesc = RelationGetDescr(rel);
     157 CBC        4291 :         attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
     158 GIC       20212 :         foreach(cur, attnums)
     159 ECB             :         {
     160                 :             int         attno;
     161                 :             Bitmapset **bms;
     162                 : 
     163 GNC       15951 :             attno = lfirst_int(cur) - FirstLowInvalidHeapAttributeNumber;
     164           15951 :             bms = is_from ? &perminfo->insertedCols : &perminfo->selectedCols;
     165                 : 
     166           15951 :             *bms = bms_add_member(*bms, attno);
     167                 :         }
     168            4261 :         ExecCheckPermissions(pstate->p_rtable, list_make1(perminfo), true);
     169                 : 
     170 ECB             :         /*
     171                 :          * Permission check for row security policies.
     172                 :          *
     173                 :          * check_enable_rls will ereport(ERROR) if the user has requested
     174                 :          * something invalid and will otherwise indicate if we should enable
     175                 :          * RLS (returns RLS_ENABLED) or not for this COPY statement.
     176                 :          *
     177                 :          * If the relation has a row security policy and we are to apply it
     178                 :          * then perform a "query" copy and allow the normal query processing
     179                 :          * to handle the policies.
     180                 :          *
     181                 :          * If RLS is not enabled for this, then just fall through to the
     182                 :          * normal non-filtering relation handling.
     183                 :          */
     184 GNC        4219 :         if (check_enable_rls(relid, InvalidOid, false) == RLS_ENABLED)
     185                 :         {
     186                 :             SelectStmt *select;
     187                 :             ColumnRef  *cr;
     188                 :             ResTarget  *target;
     189                 :             RangeVar   *from;
     190 GIC          30 :             List       *targetList = NIL;
     191 ECB             : 
     192 GIC          30 :             if (is_from)
     193               3 :                 ereport(ERROR,
     194                 :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     195                 :                          errmsg("COPY FROM not supported with row-level security"),
     196                 :                          errhint("Use INSERT statements instead.")));
     197 ECB             : 
     198                 :             /*
     199                 :              * Build target list
     200                 :              *
     201                 :              * If no columns are specified in the attribute list of the COPY
     202                 :              * command, then the target list is 'all' columns. Therefore, '*'
     203                 :              * should be used as the target list for the resulting SELECT
     204                 :              * statement.
     205                 :              *
     206                 :              * In the case that columns are specified in the attribute list,
     207                 :              * create a ColumnRef and ResTarget for each column and add them
     208                 :              * to the target list for the resulting SELECT statement.
     209                 :              */
     210 GIC          27 :             if (!stmt->attlist)
     211                 :             {
     212               9 :                 cr = makeNode(ColumnRef);
     213               9 :                 cr->fields = list_make1(makeNode(A_Star));
     214               9 :                 cr->location = -1;
     215                 : 
     216               9 :                 target = makeNode(ResTarget);
     217 CBC           9 :                 target->name = NULL;
     218 GIC           9 :                 target->indirection = NIL;
     219 CBC           9 :                 target->val = (Node *) cr;
     220               9 :                 target->location = -1;
     221 ECB             : 
     222 GIC           9 :                 targetList = list_make1(target);
     223 ECB             :             }
     224                 :             else
     225                 :             {
     226                 :                 ListCell   *lc;
     227                 : 
     228 GIC          51 :                 foreach(lc, stmt->attlist)
     229 ECB             :                 {
     230                 :                     /*
     231                 :                      * Build the ColumnRef for each column.  The ColumnRef
     232                 :                      * 'fields' property is a String node that corresponds to
     233                 :                      * the column name respectively.
     234                 :                      */
     235 CBC          33 :                     cr = makeNode(ColumnRef);
     236 GIC          33 :                     cr->fields = list_make1(lfirst(lc));
     237              33 :                     cr->location = -1;
     238                 : 
     239                 :                     /* Build the ResTarget and add the ColumnRef to it. */
     240              33 :                     target = makeNode(ResTarget);
     241              33 :                     target->name = NULL;
     242 CBC          33 :                     target->indirection = NIL;
     243              33 :                     target->val = (Node *) cr;
     244              33 :                     target->location = -1;
     245                 : 
     246                 :                     /* Add each column to the SELECT statement's target list */
     247              33 :                     targetList = lappend(targetList, target);
     248 ECB             :                 }
     249                 :             }
     250                 : 
     251                 :             /*
     252                 :              * Build RangeVar for from clause, fully qualified based on the
     253                 :              * relation which we have opened and locked.  Use "ONLY" so that
     254                 :              * COPY retrieves rows from only the target table not any
     255                 :              * inheritance children, the same as when RLS doesn't apply.
     256                 :              */
     257 GIC          27 :             from = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
     258              27 :                                 pstrdup(RelationGetRelationName(rel)),
     259                 :                                 -1);
     260              27 :             from->inh = false;   /* apply ONLY */
     261                 : 
     262                 :             /* Build query */
     263              27 :             select = makeNode(SelectStmt);
     264 CBC          27 :             select->targetList = targetList;
     265              27 :             select->fromClause = list_make1(from);
     266                 : 
     267              27 :             query = makeNode(RawStmt);
     268 GIC          27 :             query->stmt = (Node *) select;
     269              27 :             query->stmt_location = stmt_location;
     270 CBC          27 :             query->stmt_len = stmt_len;
     271 ECB             : 
     272                 :             /*
     273                 :              * Close the relation for now, but keep the lock on it to prevent
     274                 :              * changes between now and when we start the query-based COPY.
     275                 :              *
     276                 :              * We'll reopen it later as part of the query-based COPY.
     277                 :              */
     278 GIC          27 :             table_close(rel, NoLock);
     279              27 :             rel = NULL;
     280                 :         }
     281                 :     }
     282                 :     else
     283                 :     {
     284             193 :         Assert(stmt->query);
     285 ECB             : 
     286                 :         /* MERGE is allowed by parser, but unimplemented. Reject for now */
     287 GIC         193 :         if (IsA(stmt->query, MergeStmt))
     288               3 :             ereport(ERROR,
     289                 :                     errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     290                 :                     errmsg("MERGE not supported in COPY"));
     291 ECB             : 
     292 GIC         190 :         query = makeNode(RawStmt);
     293             190 :         query->stmt = stmt->query;
     294 CBC         190 :         query->stmt_location = stmt_location;
     295             190 :         query->stmt_len = stmt_len;
     296                 : 
     297 GIC         190 :         relid = InvalidOid;
     298             190 :         rel = NULL;
     299 ECB             :     }
     300                 : 
     301 CBC        4397 :     if (is_from)
     302 ECB             :     {
     303                 :         CopyFromState cstate;
     304                 : 
     305 CBC         903 :         Assert(rel);
     306                 : 
     307                 :         /* check read-only transaction and parallel mode */
     308             903 :         if (XactReadOnly && !rel->rd_islocaltemp)
     309 UIC           0 :             PreventCommandIfReadOnly("COPY FROM");
     310                 : 
     311 GIC         903 :         cstate = BeginCopyFrom(pstate, rel, whereClause,
     312 CBC         903 :                                stmt->filename, stmt->is_program,
     313 GIC         903 :                                NULL, stmt->attlist, stmt->options);
     314             837 :         *processed = CopyFrom(cstate);  /* copy from file to database */
     315 CBC         752 :         EndCopyFrom(cstate);
     316 EUB             :     }
     317                 :     else
     318 ECB             :     {
     319                 :         CopyToState cstate;
     320                 : 
     321 CBC        3494 :         cstate = BeginCopyTo(pstate, rel, query, relid,
     322            3494 :                              stmt->filename, stmt->is_program,
     323 GNC        3494 :                              NULL, stmt->attlist, stmt->options);
     324 GIC        3400 :         *processed = DoCopyTo(cstate);  /* copy from database to file */
     325            3399 :         EndCopyTo(cstate);
     326                 :     }
     327                 : 
     328 CBC        4151 :     if (rel != NULL)
     329            3998 :         table_close(rel, NoLock);
     330            4151 : }
     331 ECB             : 
     332                 : /*
     333                 :  * Extract a CopyHeaderChoice value from a DefElem.  This is like
     334                 :  * defGetBoolean() but also accepts the special value "match".
     335                 :  */
     336                 : static CopyHeaderChoice
     337 CBC          78 : defGetCopyHeaderChoice(DefElem *def, bool is_from)
     338                 : {
     339                 :     /*
     340                 :      * If no parameter value given, assume "true" is meant.
     341                 :      */
     342 GIC          78 :     if (def->arg == NULL)
     343               6 :         return COPY_HEADER_TRUE;
     344 ECB             : 
     345                 :     /*
     346                 :      * Allow 0, 1, "true", "false", "on", "off", or "match".
     347                 :      */
     348 GIC          72 :     switch (nodeTag(def->arg))
     349 ECB             :     {
     350 LBC           0 :         case T_Integer:
     351 UIC           0 :             switch (intVal(def->arg))
     352                 :             {
     353               0 :                 case 0:
     354               0 :                     return COPY_HEADER_FALSE;
     355 LBC           0 :                 case 1:
     356 UIC           0 :                     return COPY_HEADER_TRUE;
     357 UBC           0 :                 default:
     358 EUB             :                     /* otherwise, error out below */
     359 UIC           0 :                     break;
     360 EUB             :             }
     361 UBC           0 :             break;
     362 GBC          72 :         default:
     363 EUB             :             {
     364 GBC          72 :                 char       *sval = defGetString(def);
     365                 : 
     366 EUB             :                 /*
     367                 :                  * The set of strings accepted here should match up with the
     368                 :                  * grammar's opt_boolean_or_string production.
     369 ECB             :                  */
     370 GIC          72 :                 if (pg_strcasecmp(sval, "true") == 0)
     371 CBC          23 :                     return COPY_HEADER_TRUE;
     372 GIC          49 :                 if (pg_strcasecmp(sval, "false") == 0)
     373 UIC           0 :                     return COPY_HEADER_FALSE;
     374 GIC          49 :                 if (pg_strcasecmp(sval, "on") == 0)
     375 UIC           0 :                     return COPY_HEADER_TRUE;
     376 GIC          49 :                 if (pg_strcasecmp(sval, "off") == 0)
     377 CBC           3 :                     return COPY_HEADER_FALSE;
     378              46 :                 if (pg_strcasecmp(sval, "match") == 0)
     379 ECB             :                 {
     380 GBC          43 :                     if (!is_from)
     381 CBC           3 :                         ereport(ERROR,
     382 EUB             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     383 ECB             :                                  errmsg("cannot use \"%s\" with HEADER in COPY TO",
     384                 :                                         sval)));
     385 CBC          40 :                     return COPY_HEADER_MATCH;
     386                 :                 }
     387 ECB             :             }
     388 CBC           3 :             break;
     389                 :     }
     390 GIC           3 :     ereport(ERROR,
     391                 :             (errcode(ERRCODE_SYNTAX_ERROR),
     392 ECB             :              errmsg("%s requires a Boolean value or \"match\"",
     393                 :                     def->defname)));
     394                 :     return COPY_HEADER_FALSE;   /* keep compiler quiet */
     395                 : }
     396                 : 
     397                 : /*
     398                 :  * Process the statement option list for COPY.
     399                 :  *
     400                 :  * Scan the options list (a list of DefElem) and transpose the information
     401                 :  * into *opts_out, applying appropriate error checking.
     402                 :  *
     403                 :  * If 'opts_out' is not NULL, it is assumed to be filled with zeroes initially.
     404                 :  *
     405                 :  * This is exported so that external users of the COPY API can sanity-check
     406                 :  * a list of options.  In that usage, 'opts_out' can be passed as NULL and
     407                 :  * the collected data is just leaked until CurrentMemoryContext is reset.
     408                 :  *
     409                 :  * Note that additional checking, such as whether column names listed in FORCE
     410                 :  * QUOTE actually exist, has to be applied later.  This just checks for
     411                 :  * self-consistency of the options list.
     412                 :  */
     413                 : void
     414 GIC        4621 : ProcessCopyOptions(ParseState *pstate,
     415                 :                    CopyFormatOptions *opts_out,
     416                 :                    bool is_from,
     417                 :                    List *options)
     418                 : {
     419            4621 :     bool        format_specified = false;
     420            4621 :     bool        freeze_specified = false;
     421 CBC        4621 :     bool        header_specified = false;
     422                 :     ListCell   *option;
     423                 : 
     424                 :     /* Support external use for option sanity checking */
     425 GIC        4621 :     if (opts_out == NULL)
     426 CBC          43 :         opts_out = (CopyFormatOptions *) palloc0(sizeof(CopyFormatOptions));
     427 ECB             : 
     428 CBC        4621 :     opts_out->file_encoding = -1;
     429                 : 
     430                 :     /* Extract options from the statement node tree */
     431 GIC        5359 :     foreach(option, options)
     432 ECB             :     {
     433 CBC         781 :         DefElem    *defel = lfirst_node(DefElem, option);
     434                 : 
     435             781 :         if (strcmp(defel->defname, "format") == 0)
     436                 :         {
     437 GIC         243 :             char       *fmt = defGetString(defel);
     438 ECB             : 
     439 GIC         243 :             if (format_specified)
     440 CBC           3 :                 errorConflictingDefElem(defel, pstate);
     441 GIC         240 :             format_specified = true;
     442 CBC         240 :             if (strcmp(fmt, "text") == 0)
     443                 :                  /* default format */ ;
     444             211 :             else if (strcmp(fmt, "csv") == 0)
     445 GIC         182 :                 opts_out->csv_mode = true;
     446 CBC          29 :             else if (strcmp(fmt, "binary") == 0)
     447              28 :                 opts_out->binary = true;
     448 ECB             :             else
     449 CBC           1 :                 ereport(ERROR,
     450                 :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     451 ECB             :                          errmsg("COPY format \"%s\" not recognized", fmt),
     452                 :                          parser_errposition(pstate, defel->location)));
     453                 :         }
     454 CBC         538 :         else if (strcmp(defel->defname, "freeze") == 0)
     455                 :         {
     456              35 :             if (freeze_specified)
     457 GIC           3 :                 errorConflictingDefElem(defel, pstate);
     458              32 :             freeze_specified = true;
     459              32 :             opts_out->freeze = defGetBoolean(defel);
     460                 :         }
     461 CBC         503 :         else if (strcmp(defel->defname, "delimiter") == 0)
     462                 :         {
     463             132 :             if (opts_out->delim)
     464               3 :                 errorConflictingDefElem(defel, pstate);
     465             129 :             opts_out->delim = defGetString(defel);
     466 ECB             :         }
     467 GIC         371 :         else if (strcmp(defel->defname, "null") == 0)
     468 ECB             :         {
     469 GIC          63 :             if (opts_out->null_print)
     470 CBC           3 :                 errorConflictingDefElem(defel, pstate);
     471              60 :             opts_out->null_print = defGetString(defel);
     472 ECB             :         }
     473 GNC         308 :         else if (strcmp(defel->defname, "default") == 0)
     474                 :         {
     475              42 :             if (opts_out->default_print)
     476 UNC           0 :                 errorConflictingDefElem(defel, pstate);
     477 GNC          42 :             opts_out->default_print = defGetString(defel);
     478                 :         }
     479 GIC         266 :         else if (strcmp(defel->defname, "header") == 0)
     480 ECB             :         {
     481 GIC          81 :             if (header_specified)
     482 CBC           3 :                 errorConflictingDefElem(defel, pstate);
     483              78 :             header_specified = true;
     484              78 :             opts_out->header_line = defGetCopyHeaderChoice(defel, is_from);
     485                 :         }
     486             185 :         else if (strcmp(defel->defname, "quote") == 0)
     487                 :         {
     488              42 :             if (opts_out->quote)
     489 GBC           3 :                 errorConflictingDefElem(defel, pstate);
     490 CBC          39 :             opts_out->quote = defGetString(defel);
     491                 :         }
     492             143 :         else if (strcmp(defel->defname, "escape") == 0)
     493                 :         {
     494              38 :             if (opts_out->escape)
     495               3 :                 errorConflictingDefElem(defel, pstate);
     496              35 :             opts_out->escape = defGetString(defel);
     497 ECB             :         }
     498 GIC         105 :         else if (strcmp(defel->defname, "force_quote") == 0)
     499 ECB             :         {
     500 GIC          33 :             if (opts_out->force_quote || opts_out->force_quote_all)
     501 CBC           3 :                 errorConflictingDefElem(defel, pstate);
     502              30 :             if (defel->arg && IsA(defel->arg, A_Star))
     503               9 :                 opts_out->force_quote_all = true;
     504 GIC          21 :             else if (defel->arg && IsA(defel->arg, List))
     505 CBC          21 :                 opts_out->force_quote = castNode(List, defel->arg);
     506                 :             else
     507 LBC           0 :                 ereport(ERROR,
     508 ECB             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     509                 :                          errmsg("argument to option \"%s\" must be a list of column names",
     510                 :                                 defel->defname),
     511                 :                          parser_errposition(pstate, defel->location)));
     512                 :         }
     513 CBC          72 :         else if (strcmp(defel->defname, "force_not_null") == 0)
     514 ECB             :         {
     515 CBC          26 :             if (opts_out->force_notnull)
     516               3 :                 errorConflictingDefElem(defel, pstate);
     517              23 :             if (defel->arg && IsA(defel->arg, List))
     518              23 :                 opts_out->force_notnull = castNode(List, defel->arg);
     519                 :             else
     520 UBC           0 :                 ereport(ERROR,
     521                 :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     522                 :                          errmsg("argument to option \"%s\" must be a list of column names",
     523                 :                                 defel->defname),
     524                 :                          parser_errposition(pstate, defel->location)));
     525                 :         }
     526 CBC          46 :         else if (strcmp(defel->defname, "force_null") == 0)
     527                 :         {
     528              26 :             if (opts_out->force_null)
     529               3 :                 errorConflictingDefElem(defel, pstate);
     530              23 :             if (defel->arg && IsA(defel->arg, List))
     531              23 :                 opts_out->force_null = castNode(List, defel->arg);
     532                 :             else
     533 UBC           0 :                 ereport(ERROR,
     534                 :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     535                 :                          errmsg("argument to option \"%s\" must be a list of column names",
     536                 :                                 defel->defname),
     537                 :                          parser_errposition(pstate, defel->location)));
     538                 :         }
     539 CBC          20 :         else if (strcmp(defel->defname, "convert_selectively") == 0)
     540                 :         {
     541 ECB             :             /*
     542                 :              * Undocumented, not-accessible-from-SQL option: convert only the
     543                 :              * named columns to binary form, storing the rest as NULLs. It's
     544                 :              * allowed for the column list to be NIL.
     545                 :              */
     546 GBC           8 :             if (opts_out->convert_selectively)
     547 GIC           3 :                 errorConflictingDefElem(defel, pstate);
     548               5 :             opts_out->convert_selectively = true;
     549               5 :             if (defel->arg == NULL || IsA(defel->arg, List))
     550               5 :                 opts_out->convert_select = castNode(List, defel->arg);
     551                 :             else
     552 LBC           0 :                 ereport(ERROR,
     553                 :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     554                 :                          errmsg("argument to option \"%s\" must be a list of column names",
     555                 :                                 defel->defname),
     556                 :                          parser_errposition(pstate, defel->location)));
     557                 :         }
     558 GIC          12 :         else if (strcmp(defel->defname, "encoding") == 0)
     559 ECB             :         {
     560 CBC          12 :             if (opts_out->file_encoding >= 0)
     561               3 :                 errorConflictingDefElem(defel, pstate);
     562               9 :             opts_out->file_encoding = pg_char_to_encoding(defGetString(defel));
     563               9 :             if (opts_out->file_encoding < 0)
     564 UIC           0 :                 ereport(ERROR,
     565 EUB             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     566                 :                          errmsg("argument to option \"%s\" must be a valid encoding name",
     567                 :                                 defel->defname),
     568                 :                          parser_errposition(pstate, defel->location)));
     569                 :         }
     570                 :         else
     571 LBC           0 :             ereport(ERROR,
     572                 :                     (errcode(ERRCODE_SYNTAX_ERROR),
     573 ECB             :                      errmsg("option \"%s\" not recognized",
     574                 :                             defel->defname),
     575                 :                      parser_errposition(pstate, defel->location)));
     576                 :     }
     577 EUB             : 
     578                 :     /*
     579                 :      * Check for incompatible options (must do these two before inserting
     580                 :      * defaults)
     581                 :      */
     582 GIC        4578 :     if (opts_out->binary && opts_out->delim)
     583               3 :         ereport(ERROR,
     584 EUB             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     585                 :                  errmsg("cannot specify DELIMITER in BINARY mode")));
     586                 : 
     587 GIC        4575 :     if (opts_out->binary && opts_out->null_print)
     588               3 :         ereport(ERROR,
     589                 :                 (errcode(ERRCODE_SYNTAX_ERROR),
     590                 :                  errmsg("cannot specify NULL in BINARY mode")));
     591                 : 
     592 GNC        4572 :     if (opts_out->binary && opts_out->default_print)
     593               3 :         ereport(ERROR,
     594                 :                 (errcode(ERRCODE_SYNTAX_ERROR),
     595                 :                  errmsg("cannot specify DEFAULT in BINARY mode")));
     596                 : 
     597                 :     /* Set defaults for omitted options */
     598 GIC        4569 :     if (!opts_out->delim)
     599            4446 :         opts_out->delim = opts_out->csv_mode ? "," : "\t";
     600 ECB             : 
     601 CBC        4569 :     if (!opts_out->null_print)
     602 GIC        4515 :         opts_out->null_print = opts_out->csv_mode ? "" : "\\N";
     603            4569 :     opts_out->null_print_len = strlen(opts_out->null_print);
     604                 : 
     605 CBC        4569 :     if (opts_out->csv_mode)
     606 ECB             :     {
     607 GIC         179 :         if (!opts_out->quote)
     608             145 :             opts_out->quote = "\"";
     609             179 :         if (!opts_out->escape)
     610 CBC         150 :             opts_out->escape = opts_out->quote;
     611 ECB             :     }
     612                 : 
     613                 :     /* Only single-byte delimiter strings are supported. */
     614 GIC        4569 :     if (strlen(opts_out->delim) != 1)
     615               1 :         ereport(ERROR,
     616 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     617                 :                  errmsg("COPY delimiter must be a single one-byte character")));
     618                 : 
     619                 :     /* Disallow end-of-line characters */
     620 CBC        4568 :     if (strchr(opts_out->delim, '\r') != NULL ||
     621            4568 :         strchr(opts_out->delim, '\n') != NULL)
     622 GIC           1 :         ereport(ERROR,
     623 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     624                 :                  errmsg("COPY delimiter cannot be newline or carriage return")));
     625                 : 
     626 CBC        4567 :     if (strchr(opts_out->null_print, '\r') != NULL ||
     627            4567 :         strchr(opts_out->null_print, '\n') != NULL)
     628               1 :         ereport(ERROR,
     629                 :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     630                 :                  errmsg("COPY null representation cannot use newline or carriage return")));
     631                 : 
     632 GNC        4566 :     if (opts_out->default_print)
     633                 :     {
     634              39 :         opts_out->default_print_len = strlen(opts_out->default_print);
     635                 : 
     636              39 :         if (strchr(opts_out->default_print, '\r') != NULL ||
     637              36 :             strchr(opts_out->default_print, '\n') != NULL)
     638               6 :             ereport(ERROR,
     639                 :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     640                 :                      errmsg("COPY default representation cannot use newline or carriage return")));
     641                 :     }
     642                 : 
     643 ECB             :     /*
     644                 :      * Disallow unsafe delimiter characters in non-CSV mode.  We can't allow
     645                 :      * backslash because it would be ambiguous.  We can't allow the other
     646                 :      * cases because data characters matching the delimiter must be
     647                 :      * backslashed, and certain backslash combinations are interpreted
     648                 :      * non-literally by COPY IN.  Disallowing all lower case ASCII letters is
     649                 :      * more than strictly necessary, but seems best for consistency and
     650                 :      * future-proofing.  Likewise we disallow all digits though only octal
     651                 :      * digits are actually dangerous.
     652                 :      */
     653 GIC        4560 :     if (!opts_out->csv_mode &&
     654            4384 :         strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
     655 CBC        4384 :                opts_out->delim[0]) != NULL)
     656               5 :         ereport(ERROR,
     657 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     658                 :                  errmsg("COPY delimiter cannot be \"%s\"", opts_out->delim)));
     659                 : 
     660                 :     /* Check header */
     661 CBC        4555 :     if (opts_out->binary && opts_out->header_line)
     662 GIC           1 :         ereport(ERROR,
     663 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     664                 :                  errmsg("cannot specify HEADER in BINARY mode")));
     665                 : 
     666                 :     /* Check quote */
     667 CBC        4554 :     if (!opts_out->csv_mode && opts_out->quote != NULL)
     668 GIC           2 :         ereport(ERROR,
     669                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     670                 :                  errmsg("COPY quote available only in CSV mode")));
     671                 : 
     672            4552 :     if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
     673               1 :         ereport(ERROR,
     674                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     675                 :                  errmsg("COPY quote must be a single one-byte character")));
     676                 : 
     677            4551 :     if (opts_out->csv_mode && opts_out->delim[0] == opts_out->quote[0])
     678               1 :         ereport(ERROR,
     679                 :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     680                 :                  errmsg("COPY delimiter and quote must be different")));
     681                 : 
     682 ECB             :     /* Check escape */
     683 CBC        4550 :     if (!opts_out->csv_mode && opts_out->escape != NULL)
     684               3 :         ereport(ERROR,
     685 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     686                 :                  errmsg("COPY escape available only in CSV mode")));
     687                 : 
     688 GIC        4547 :     if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
     689               1 :         ereport(ERROR,
     690 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     691                 :                  errmsg("COPY escape must be a single one-byte character")));
     692                 : 
     693                 :     /* Check force_quote */
     694 GIC        4546 :     if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
     695               3 :         ereport(ERROR,
     696 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     697                 :                  errmsg("COPY force quote available only in CSV mode")));
     698 GIC        4543 :     if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
     699               3 :         ereport(ERROR,
     700                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     701 ECB             :                  errmsg("COPY force quote only available using COPY TO")));
     702                 : 
     703                 :     /* Check force_notnull */
     704 GIC        4540 :     if (!opts_out->csv_mode && opts_out->force_notnull != NIL)
     705               4 :         ereport(ERROR,
     706 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     707                 :                  errmsg("COPY force not null available only in CSV mode")));
     708 GIC        4536 :     if (opts_out->force_notnull != NIL && !is_from)
     709               3 :         ereport(ERROR,
     710                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     711                 :                  errmsg("COPY force not null only available using COPY FROM")));
     712 ECB             : 
     713                 :     /* Check force_null */
     714 GIC        4533 :     if (!opts_out->csv_mode && opts_out->force_null != NIL)
     715               3 :         ereport(ERROR,
     716                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     717 ECB             :                  errmsg("COPY force null available only in CSV mode")));
     718                 : 
     719 GIC        4530 :     if (opts_out->force_null != NIL && !is_from)
     720               3 :         ereport(ERROR,
     721                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     722                 :                  errmsg("COPY force null only available using COPY FROM")));
     723 ECB             : 
     724                 :     /* Don't allow the delimiter to appear in the null string. */
     725 GIC        4527 :     if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
     726               1 :         ereport(ERROR,
     727 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     728                 :                  errmsg("COPY delimiter must not appear in the NULL specification")));
     729                 : 
     730                 :     /* Don't allow the CSV quote char to appear in the null string. */
     731 GIC        4526 :     if (opts_out->csv_mode &&
     732             163 :         strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
     733 CBC           1 :         ereport(ERROR,
     734 ECB             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     735                 :                  errmsg("CSV quote character must not appear in the NULL specification")));
     736                 : 
     737 GNC        4525 :     if (opts_out->default_print)
     738                 :     {
     739              33 :         if (!is_from)
     740               3 :             ereport(ERROR,
     741                 :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     742                 :                      errmsg("COPY DEFAULT only available using COPY FROM")));
     743                 : 
     744                 :         /* Don't allow the delimiter to appear in the default string. */
     745              30 :         if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
     746               3 :             ereport(ERROR,
     747                 :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     748                 :                      errmsg("COPY delimiter must not appear in the DEFAULT specification")));
     749                 : 
     750                 :         /* Don't allow the CSV quote char to appear in the default string. */
     751              27 :         if (opts_out->csv_mode &&
     752              15 :             strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
     753               3 :             ereport(ERROR,
     754                 :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     755                 :                      errmsg("CSV quote character must not appear in the DEFAULT specification")));
     756                 : 
     757                 :         /* Don't allow the NULL and DEFAULT string to be the same */
     758              24 :         if (opts_out->null_print_len == opts_out->default_print_len &&
     759              12 :             strncmp(opts_out->null_print, opts_out->default_print,
     760              12 :                     opts_out->null_print_len) == 0)
     761               3 :             ereport(ERROR,
     762                 :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     763                 :                      errmsg("NULL specification and DEFAULT specification cannot be the same")));
     764                 :     }
     765 GIC        4513 : }
     766 ECB             : 
     767                 : /*
     768                 :  * CopyGetAttnums - build an integer list of attnums to be copied
     769                 :  *
     770                 :  * The input attnamelist is either the user-specified column list,
     771                 :  * or NIL if there was none (in which case we want all the non-dropped
     772                 :  * columns).
     773                 :  *
     774                 :  * We don't include generated columns in the generated full list and we don't
     775                 :  * allow them to be specified explicitly.  They don't make sense for COPY
     776                 :  * FROM, but we could possibly allow them for COPY TO.  But this way it's at
     777                 :  * least ensured that whatever we copy out can be copied back in.
     778                 :  *
     779                 :  * rel can be NULL ... it's only used for error reports.
     780                 :  */
     781                 : List *
     782 GIC        8760 : CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
     783 ECB             : {
     784 CBC        8760 :     List       *attnums = NIL;
     785                 : 
     786 GIC        8760 :     if (attnamelist == NIL)
     787                 :     {
     788                 :         /* Generate default column list */
     789 CBC        1585 :         int         attr_count = tupDesc->natts;
     790 ECB             :         int         i;
     791                 : 
     792 GIC        5336 :         for (i = 0; i < attr_count; i++)
     793                 :         {
     794            3751 :             if (TupleDescAttr(tupDesc, i)->attisdropped)
     795 CBC          98 :                 continue;
     796 GIC        3653 :             if (TupleDescAttr(tupDesc, i)->attgenerated)
     797 CBC          27 :                 continue;
     798            3626 :             attnums = lappend_int(attnums, i + 1);
     799                 :         }
     800                 :     }
     801                 :     else
     802                 :     {
     803 ECB             :         /* Validate the user-supplied list and extract attnums */
     804                 :         ListCell   *l;
     805                 : 
     806 GIC       35538 :         foreach(l, attnamelist)
     807                 :         {
     808           28393 :             char       *name = strVal(lfirst(l));
     809 ECB             :             int         attnum;
     810                 :             int         i;
     811                 : 
     812                 :             /* Lookup column name */
     813 GIC       28393 :             attnum = InvalidAttrNumber;
     814         4936742 :             for (i = 0; i < tupDesc->natts; i++)
     815                 :             {
     816 CBC     4936727 :                 Form_pg_attribute att = TupleDescAttr(tupDesc, i);
     817 ECB             : 
     818 CBC     4936727 :                 if (att->attisdropped)
     819             386 :                     continue;
     820 GIC     4936341 :                 if (namestrcmp(&(att->attname), name) == 0)
     821                 :                 {
     822           28378 :                     if (att->attgenerated)
     823 CBC          12 :                         ereport(ERROR,
     824                 :                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
     825                 :                                  errmsg("column \"%s\" is a generated column",
     826                 :                                         name),
     827                 :                                  errdetail("Generated columns cannot be used in COPY.")));
     828 GIC       28366 :                     attnum = att->attnum;
     829           28366 :                     break;
     830                 :                 }
     831                 :             }
     832           28381 :             if (attnum == InvalidAttrNumber)
     833                 :             {
     834              15 :                 if (rel != NULL)
     835              15 :                     ereport(ERROR,
     836                 :                             (errcode(ERRCODE_UNDEFINED_COLUMN),
     837                 :                              errmsg("column \"%s\" of relation \"%s\" does not exist",
     838                 :                                     name, RelationGetRelationName(rel))));
     839                 :                 else
     840 LBC           0 :                     ereport(ERROR,
     841                 :                             (errcode(ERRCODE_UNDEFINED_COLUMN),
     842 ECB             :                              errmsg("column \"%s\" does not exist",
     843                 :                                     name)));
     844                 :             }
     845                 :             /* Check for duplicates */
     846 GIC       28366 :             if (list_member_int(attnums, attnum))
     847 CBC           3 :                 ereport(ERROR,
     848                 :                         (errcode(ERRCODE_DUPLICATE_COLUMN),
     849                 :                          errmsg("column \"%s\" specified more than once",
     850 ECB             :                                 name)));
     851 GIC       28363 :             attnums = lappend_int(attnums, attnum);
     852 ECB             :         }
     853                 :     }
     854                 : 
     855 CBC        8730 :     return attnums;
     856 ECB             : }
        

Generated by: LCOV version v1.16-55-g56c0a2a