Debian Codegraph @

src/backend/commands/copy.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * copy.c
4 * Implements the COPY utility command
5 *
6 * Portions Copyright (c) 1996-2025, 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 "utils/acl.h"
37 #include "utils/builtins.h"
38 #include "utils/lsyscache.h"
39 #include "utils/rel.h"
40 #include "utils/rls.h"
41
42 /*
43 * DoCopy executes the SQL COPY statement
44 *
45 * Either unload or reload contents of table <relation>, depending on <from>.
46 * (<from> = true means we are inserting into the table.) In the "TO" case
47 * we also support copying the output of an arbitrary SELECT, INSERT, UPDATE
48 * or DELETE query.
49 *
50 * If <pipe> is false, transfer is between the table and the file named
51 * <filename>. Otherwise, transfer is between the table and our regular
52 * input/output stream. The latter could be either stdin/stdout or a
53 * socket, depending on whether we're running under Postmaster control.
54 *
55 * Do not allow a Postgres user without the 'pg_read_server_files' or
56 * 'pg_write_server_files' role to read from or write to a file.
57 *
58 * Do not allow the copy if user doesn't have proper permission to access
59 * the table or the specifically requested columns.
60 */
61 void
62 DoCopy(ParseState *pstate, const CopyStmt *stmt,
63 int stmt_location, int stmt_len,
64 uint64 *processed)
65 {
66 bool is_from = stmt->is_from;
67 bool pipe = (stmt->filename == NULL);
68 Relation rel;
69 Oid relid;
70 RawStmt *query = NULL;
71 Node *whereClause = NULL;
72
73 /*
74 * Disallow COPY to/from file or program except to users with the
75 * appropriate role.
76 */
77 if (!pipe)
78 {
79 if (stmt->is_program)
80 {
81 if (!has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
82 ereport(ERROR,
83 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
84 errmsg("permission denied to COPY to or from an external program"),
85 errdetail("Only roles with privileges of the \"%s\" role may COPY to or from an external program.",
86 "pg_execute_server_program"),
87 errhint("Anyone can COPY to stdout or from stdin. "
88 "psql's \\copy command also works for anyone.")));
89 }
90 else
91 {
92 if (is_from && !has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
93 ereport(ERROR,
94 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
95 errmsg("permission denied to COPY from a file"),
96 errdetail("Only roles with privileges of the \"%s\" role may COPY from a file.",
97 "pg_read_server_files"),
98 errhint("Anyone can COPY to stdout or from stdin. "
99 "psql's \\copy command also works for anyone.")));
100
101 if (!is_from && !has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
102 ereport(ERROR,
103 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
104 errmsg("permission denied to COPY to a file"),
105 errdetail("Only roles with privileges of the \"%s\" role may COPY to a file.",
106 "pg_write_server_files"),
107 errhint("Anyone can COPY to stdout or from stdin. "
108 "psql's \\copy command also works for anyone.")));
109 }
110 }
111
112 if (stmt->relation)
113 {
114 LOCKMODE lockmode = is_from ? RowExclusiveLock : AccessShareLock;
115 ParseNamespaceItem *nsitem;
116 RTEPermissionInfo *perminfo;
117 TupleDesc tupDesc;
118 List *attnums;
119 ListCell *cur;
120
121 Assert(!stmt->query);
122
123 /* Open and lock the relation, using the appropriate lock type. */
124 rel = table_openrv(stmt->relation, lockmode);
125
126 relid = RelationGetRelid(rel);
127
128 nsitem = addRangeTableEntryForRelation(pstate, rel, lockmode,
129 NULL, false, false);
130
131 perminfo = nsitem->p_perminfo;
132 perminfo->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
133
134 if (stmt->whereClause)
135 {
136 Bitmapset *expr_attrs = NULL;
137 int i;
138
139 /* add nsitem to query namespace */
140 addNSItemToQuery(pstate, nsitem, false, true, true);
141
142 /* Transform the raw expression tree */
143 whereClause = transformExpr(pstate, stmt->whereClause, EXPR_KIND_COPY_WHERE);
144
145 /* Make sure it yields a boolean result. */
146 whereClause = coerce_to_boolean(pstate, whereClause, "WHERE");
147
148 /* we have to fix its collations too */
149 assign_expr_collations(pstate, whereClause);
150
151 /*
152 * Examine all the columns in the WHERE clause expression. When
153 * the whole-row reference is present, examine all the columns of
154 * the table.
155 */
156 pull_varattnos(whereClause, 1, &expr_attrs);
157 if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, expr_attrs))
158 {
159 expr_attrs = bms_add_range(expr_attrs,
160 1 - FirstLowInvalidHeapAttributeNumber,
161 RelationGetNumberOfAttributes(rel) - FirstLowInvalidHeapAttributeNumber);
162 expr_attrs = bms_del_member(expr_attrs, 0 - FirstLowInvalidHeapAttributeNumber);
163 }
164
165 i = -1;
166 while ((i = bms_next_member(expr_attrs, i)) >= 0)
167 {
168 AttrNumber attno = i + FirstLowInvalidHeapAttributeNumber;
169
170 Assert(attno != 0);
171
172 /*
173 * Prohibit generated columns in the WHERE clause. Stored
174 * generated columns are not yet computed when the filtering
175 * happens. Virtual generated columns could probably work (we
176 * would need to expand them somewhere around here), but for
177 * now we keep them consistent with the stored variant.
178 */
179 if (attno > 0 &&
180 TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated)
181 ereport(ERROR,
182 errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
183 errmsg("generated columns are not supported in COPY FROM WHERE conditions"),
184 errdetail("Column \"%s\" is a generated column.",
185 get_attname(RelationGetRelid(rel), attno, false)));
186 }
187
188 whereClause = eval_const_expressions(NULL, whereClause);
189
190 whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
191 whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
192 }
193
194 tupDesc = RelationGetDescr(rel);
195 attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
196 foreach(cur, attnums)
197 {
198 int attno;
199 Bitmapset **bms;
200
201 attno = lfirst_int(cur) - FirstLowInvalidHeapAttributeNumber;
202 bms = is_from ? &perminfo->insertedCols : &perminfo->selectedCols;
203
204 *bms = bms_add_member(*bms, attno);
205 }
206 ExecCheckPermissions(pstate->p_rtable, list_make1(perminfo), true);
207
208 /*
209 * Permission check for row security policies.
210 *
211 * check_enable_rls will ereport(ERROR) if the user has requested
212 * something invalid and will otherwise indicate if we should enable
213 * RLS (returns RLS_ENABLED) or not for this COPY statement.
214 *
215 * If the relation has a row security policy and we are to apply it
216 * then perform a "query" copy and allow the normal query processing
217 * to handle the policies.
218 *
219 * If RLS is not enabled for this, then just fall through to the
220 * normal non-filtering relation handling.
221 */
222 if (check_enable_rls(relid, InvalidOid, false) == RLS_ENABLED)
223 {
224 SelectStmt *select;
225 ColumnRef *cr;
226 ResTarget *target;
227 RangeVar *from;
228 List *targetList = NIL;
229
230 if (is_from)
231 ereport(ERROR,
232 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
233 errmsg("COPY FROM not supported with row-level security"),
234 errhint("Use INSERT statements instead.")));
235
236 /*
237 * Build target list
238 *
239 * If no columns are specified in the attribute list of the COPY
240 * command, then the target list is 'all' columns. Therefore, '*'
241 * should be used as the target list for the resulting SELECT
242 * statement.
243 *
244 * In the case that columns are specified in the attribute list,
245 * create a ColumnRef and ResTarget for each column and add them
246 * to the target list for the resulting SELECT statement.
247 */
248 if (!stmt->attlist)
249 {
250 cr = makeNode(ColumnRef);
251 cr->fields = list_make1(makeNode(A_Star));
252 cr->location = -1;
253
254 target = makeNode(ResTarget);
255 target->name = NULL;
256 target->indirection = NIL;
257 target->val = (Node *) cr;
258 target->location = -1;
259
260 targetList = list_make1(target);
261 }
262 else
263 {
264 ListCell *lc;
265
266 foreach(lc, stmt->attlist)
267 {
268 /*
269 * Build the ColumnRef for each column. The ColumnRef
270 * 'fields' property is a String node that corresponds to
271 * the column name respectively.
272 */
273 cr = makeNode(ColumnRef);
274 cr->fields = list_make1(lfirst(lc));
275 cr->location = -1;
276
277 /* Build the ResTarget and add the ColumnRef to it. */
278 target = makeNode(ResTarget);
279 target->name = NULL;
280 target->indirection = NIL;
281 target->val = (Node *) cr;
282 target->location = -1;
283
284 /* Add each column to the SELECT statement's target list */
285 targetList = lappend(targetList, target);
286 }
287 }
288
289 /*
290 * Build RangeVar for from clause, fully qualified based on the
291 * relation which we have opened and locked. Use "ONLY" so that
292 * COPY retrieves rows from only the target table not any
293 * inheritance children, the same as when RLS doesn't apply.
294 */
295 from = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
296 pstrdup(RelationGetRelationName(rel)),
297 -1);
298 from->inh = false; /* apply ONLY */
299
300 /* Build query */
301 select = makeNode(SelectStmt);
302 select->targetList = targetList;
303 select->fromClause = list_make1(from);
304
305 query = makeNode(RawStmt);
306 query->stmt = (Node *) select;
307 query->stmt_location = stmt_location;
308 query->stmt_len = stmt_len;
309
310 /*
311 * Close the relation for now, but keep the lock on it to prevent
312 * changes between now and when we start the query-based COPY.
313 *
314 * We'll reopen it later as part of the query-based COPY.
315 */
316 table_close(rel, NoLock);
317 rel = NULL;
318 }
319 }
320 else
321 {
322 Assert(stmt->query);
323
324 query = makeNode(RawStmt);
325 query->stmt = stmt->query;
326 query->stmt_location = stmt_location;
327 query->stmt_len = stmt_len;
328
329 relid = InvalidOid;
330 rel = NULL;
331 }
332
333 if (is_from)
334 {
335 CopyFromState cstate;
336
337 Assert(rel);
338
339 /* check read-only transaction and parallel mode */
340 if (XactReadOnly && !rel->rd_islocaltemp)
341 PreventCommandIfReadOnly("COPY FROM");
342
343 cstate = BeginCopyFrom(pstate, rel, whereClause,
344 stmt->filename, stmt->is_program,
345 NULL, stmt->attlist, stmt->options);
346 *processed = CopyFrom(cstate); /* copy from file to database */
347 EndCopyFrom(cstate);
348 }
349 else
350 {
351 CopyToState cstate;
352
353 cstate = BeginCopyTo(pstate, rel, query, relid,
354 stmt->filename, stmt->is_program,
355 NULL, stmt->attlist, stmt->options);
356 *processed = DoCopyTo(cstate); /* copy from database to file */
357 EndCopyTo(cstate);
358 }
359
360 if (rel != NULL)
361 table_close(rel, NoLock);
362 }
363
364 /*
365 * Extract a CopyHeaderChoice value from a DefElem. This is like
366 * defGetBoolean() but also accepts the special value "match".
367 */
368 static CopyHeaderChoice
369 defGetCopyHeaderChoice(DefElem *def, bool is_from)
370 {
371 /*
372 * If no parameter value given, assume "true" is meant.
373 */
374 if (def->arg == NULL)
375 return COPY_HEADER_TRUE;
376
377 /*
378 * Allow 0, 1, "true", "false", "on", "off", or "match".
379 */
380 switch (nodeTag(def->arg))
381 {
382 case T_Integer:
383 switch (intVal(def->arg))
384 {
385 case 0:
386 return COPY_HEADER_FALSE;
387 case 1:
388 return COPY_HEADER_TRUE;
389 default:
390 /* otherwise, error out below */
391 break;
392 }
393 break;
394 default:
395 {
396 char *sval = defGetString(def);
397
398 /*
399 * The set of strings accepted here should match up with the
400 * grammar's opt_boolean_or_string production.
401 */
402 if (pg_strcasecmp(sval, "true") == 0)
403 return COPY_HEADER_TRUE;
404 if (pg_strcasecmp(sval, "false") == 0)
405 return COPY_HEADER_FALSE;
406 if (pg_strcasecmp(sval, "on") == 0)
407 return COPY_HEADER_TRUE;
408 if (pg_strcasecmp(sval, "off") == 0)
409 return COPY_HEADER_FALSE;
410 if (pg_strcasecmp(sval, "match") == 0)
411 {
412 if (!is_from)
413 ereport(ERROR,
414 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
415 errmsg("cannot use \"%s\" with HEADER in COPY TO",
416 sval)));
417 return COPY_HEADER_MATCH;
418 }
419 }
420 break;
421 }
422 ereport(ERROR,
423 (errcode(ERRCODE_SYNTAX_ERROR),
424 errmsg("%s requires a Boolean value or \"match\"",
425 def->defname)));
426 return COPY_HEADER_FALSE; /* keep compiler quiet */
427 }
428
429 /*
430 * Extract a CopyOnErrorChoice value from a DefElem.
431 */
432 static CopyOnErrorChoice
433 defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
434 {
435 char *sval = defGetString(def);
436
437 if (!is_from)
438 ereport(ERROR,
439 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
440 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
441 second %s is a COPY with direction, e.g. COPY TO */
442 errmsg("COPY %s cannot be used with %s", "ON_ERROR", "COPY TO"),
443 parser_errposition(pstate, def->location)));
444
445 /*
446 * Allow "stop", or "ignore" values.
447 */
448 if (pg_strcasecmp(sval, "stop") == 0)
449 return COPY_ON_ERROR_STOP;
450 if (pg_strcasecmp(sval, "ignore") == 0)
451 return COPY_ON_ERROR_IGNORE;
452
453 ereport(ERROR,
454 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
455 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
456 errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
457 parser_errposition(pstate, def->location)));
458 return COPY_ON_ERROR_STOP; /* keep compiler quiet */
459 }
460
461 /*
462 * Extract REJECT_LIMIT value from a DefElem.
463 *
464 * REJECT_LIMIT can be specified in two ways: as an int64 for the COPY command
465 * option or as a single-quoted string for the foreign table option using
466 * file_fdw. Therefore this function needs to handle both formats.
467 */
468 static int64
469 defGetCopyRejectLimitOption(DefElem *def)
470 {
471 int64 reject_limit;
472
473 if (def->arg == NULL)
474 ereport(ERROR,
475 (errcode(ERRCODE_SYNTAX_ERROR),
476 errmsg("%s requires a numeric value",
477 def->defname)));
478 else if (nodeTag(def->arg) == T_String)
479 reject_limit = pg_strtoint64(strVal(def->arg));
480 else
481 reject_limit = defGetInt64(def);
482
483 if (reject_limit <= 0)
484 ereport(ERROR,
485 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
486 errmsg("REJECT_LIMIT (%" PRId64 ") must be greater than zero",
487 reject_limit)));
488
489 return reject_limit;
490 }
491
492 /*
493 * Extract a CopyLogVerbosityChoice value from a DefElem.
494 */
495 static CopyLogVerbosityChoice
496 defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
497 {
498 char *sval;
499
500 /*
501 * Allow "silent", "default", or "verbose" values.
502 */
503 sval = defGetString(def);
504 if (pg_strcasecmp(sval, "silent") == 0)
505 return COPY_LOG_VERBOSITY_SILENT;
506 if (pg_strcasecmp(sval, "default") == 0)
507 return COPY_LOG_VERBOSITY_DEFAULT;
508 if (pg_strcasecmp(sval, "verbose") == 0)
509 return COPY_LOG_VERBOSITY_VERBOSE;
510
511 ereport(ERROR,
512 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
513 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
514 errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
515 parser_errposition(pstate, def->location)));
516 return COPY_LOG_VERBOSITY_DEFAULT; /* keep compiler quiet */
517 }
518
519 /*
520 * Process the statement option list for COPY.
521 *
522 * Scan the options list (a list of DefElem) and transpose the information
523 * into *opts_out, applying appropriate error checking.
524 *
525 * If 'opts_out' is not NULL, it is assumed to be filled with zeroes initially.
526 *
527 * This is exported so that external users of the COPY API can sanity-check
528 * a list of options. In that usage, 'opts_out' can be passed as NULL and
529 * the collected data is just leaked until CurrentMemoryContext is reset.
530 *
531 * Note that additional checking, such as whether column names listed in FORCE
532 * QUOTE actually exist, has to be applied later. This just checks for
533 * self-consistency of the options list.
534 */
535 void
536 ProcessCopyOptions(ParseState *pstate,
537 CopyFormatOptions *opts_out,
538 bool is_from,
539 List *options)
540 {
541 bool format_specified = false;
542 bool freeze_specified = false;
543 bool header_specified = false;
544 bool on_error_specified = false;
545 bool log_verbosity_specified = false;
546 bool reject_limit_specified = false;
547 ListCell *option;
548
549 /* Support external use for option sanity checking */
550 if (opts_out == NULL)
551 opts_out = (CopyFormatOptions *) palloc0(sizeof(CopyFormatOptions));
552
553 opts_out->file_encoding = -1;
554
555 /* Extract options from the statement node tree */
556 foreach(option, options)
557 {
558 DefElem *defel = lfirst_node(DefElem, option);
559
560 if (strcmp(defel->defname, "format") == 0)
561 {
562 char *fmt = defGetString(defel);
563
564 if (format_specified)
565 errorConflictingDefElem(defel, pstate);
566 format_specified = true;
567 if (strcmp(fmt, "text") == 0)
568 /* default format */ ;
569 else if (strcmp(fmt, "csv") == 0)
570 opts_out->csv_mode = true;
571 else if (strcmp(fmt, "binary") == 0)
572 opts_out->binary = true;
573 else
574 ereport(ERROR,
575 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
576 errmsg("COPY format \"%s\" not recognized", fmt),
577 parser_errposition(pstate, defel->location)));
578 }
579 else if (strcmp(defel->defname, "freeze") == 0)
580 {
581 if (freeze_specified)
582 errorConflictingDefElem(defel, pstate);
583 freeze_specified = true;
584 opts_out->freeze = defGetBoolean(defel);
585 }
586 else if (strcmp(defel->defname, "delimiter") == 0)
587 {
588 if (opts_out->delim)
589 errorConflictingDefElem(defel, pstate);
590 opts_out->delim = defGetString(defel);
591 }
592 else if (strcmp(defel->defname, "null") == 0)
593 {
594 if (opts_out->null_print)
595 errorConflictingDefElem(defel, pstate);
596 opts_out->null_print = defGetString(defel);
597 }
598 else if (strcmp(defel->defname, "default") == 0)
599 {
600 if (opts_out->default_print)
601 errorConflictingDefElem(defel, pstate);
602 opts_out->default_print = defGetString(defel);
603 }
604 else if (strcmp(defel->defname, "header") == 0)
605 {
606 if (header_specified)
607 errorConflictingDefElem(defel, pstate);
608 header_specified = true;
609 opts_out->header_line = defGetCopyHeaderChoice(defel, is_from);
610 }
611 else if (strcmp(defel->defname, "quote") == 0)
612 {
613 if (opts_out->quote)
614 errorConflictingDefElem(defel, pstate);
615 opts_out->quote = defGetString(defel);
616 }
617 else if (strcmp(defel->defname, "escape") == 0)
618 {
619 if (opts_out->escape)
620 errorConflictingDefElem(defel, pstate);
621 opts_out->escape = defGetString(defel);
622 }
623 else if (strcmp(defel->defname, "force_quote") == 0)
624 {
625 if (opts_out->force_quote || opts_out->force_quote_all)
626 errorConflictingDefElem(defel, pstate);
627 if (defel->arg && IsA(defel->arg, A_Star))
628 opts_out->force_quote_all = true;
629 else if (defel->arg && IsA(defel->arg, List))
630 opts_out->force_quote = castNode(List, defel->arg);
631 else
632 ereport(ERROR,
633 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
634 errmsg("argument to option \"%s\" must be a list of column names",
635 defel->defname),
636 parser_errposition(pstate, defel->location)));
637 }
638 else if (strcmp(defel->defname, "force_not_null") == 0)
639 {
640 if (opts_out->force_notnull || opts_out->force_notnull_all)
641 errorConflictingDefElem(defel, pstate);
642 if (defel->arg && IsA(defel->arg, A_Star))
643 opts_out->force_notnull_all = true;
644 else if (defel->arg && IsA(defel->arg, List))
645 opts_out->force_notnull = castNode(List, defel->arg);
646 else
647 ereport(ERROR,
648 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
649 errmsg("argument to option \"%s\" must be a list of column names",
650 defel->defname),
651 parser_errposition(pstate, defel->location)));
652 }
653 else if (strcmp(defel->defname, "force_null") == 0)
654 {
655 if (opts_out->force_null || opts_out->force_null_all)
656 errorConflictingDefElem(defel, pstate);
657 if (defel->arg && IsA(defel->arg, A_Star))
658 opts_out->force_null_all = true;
659 else if (defel->arg && IsA(defel->arg, List))
660 opts_out->force_null = castNode(List, defel->arg);
661 else
662 ereport(ERROR,
663 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
664 errmsg("argument to option \"%s\" must be a list of column names",
665 defel->defname),
666 parser_errposition(pstate, defel->location)));
667 }
668 else if (strcmp(defel->defname, "convert_selectively") == 0)
669 {
670 /*
671 * Undocumented, not-accessible-from-SQL option: convert only the
672 * named columns to binary form, storing the rest as NULLs. It's
673 * allowed for the column list to be NIL.
674 */
675 if (opts_out->convert_selectively)
676 errorConflictingDefElem(defel, pstate);
677 opts_out->convert_selectively = true;
678 if (defel->arg == NULL || IsA(defel->arg, List))
679 opts_out->convert_select = castNode(List, defel->arg);
680 else
681 ereport(ERROR,
682 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
683 errmsg("argument to option \"%s\" must be a list of column names",
684 defel->defname),
685 parser_errposition(pstate, defel->location)));
686 }
687 else if (strcmp(defel->defname, "encoding") == 0)
688 {
689 if (opts_out->file_encoding >= 0)
690 errorConflictingDefElem(defel, pstate);
691 opts_out->file_encoding = pg_char_to_encoding(defGetString(defel));
692 if (opts_out->file_encoding < 0)
693 ereport(ERROR,
694 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
695 errmsg("argument to option \"%s\" must be a valid encoding name",
696 defel->defname),
697 parser_errposition(pstate, defel->location)));
698 }
699 else if (strcmp(defel->defname, "on_error") == 0)
700 {
701 if (on_error_specified)
702 errorConflictingDefElem(defel, pstate);
703 on_error_specified = true;
704 opts_out->on_error = defGetCopyOnErrorChoice(defel, pstate, is_from);
705 }
706 else if (strcmp(defel->defname, "log_verbosity") == 0)
707 {
708 if (log_verbosity_specified)
709 errorConflictingDefElem(defel, pstate);
710 log_verbosity_specified = true;
711 opts_out->log_verbosity = defGetCopyLogVerbosityChoice(defel, pstate);
712 }
713 else if (strcmp(defel->defname, "reject_limit") == 0)
714 {
715 if (reject_limit_specified)
716 errorConflictingDefElem(defel, pstate);
717 reject_limit_specified = true;
718 opts_out->reject_limit = defGetCopyRejectLimitOption(defel);
719 }
720 else
721 ereport(ERROR,
722 (errcode(ERRCODE_SYNTAX_ERROR),
723 errmsg("option \"%s\" not recognized",
724 defel->defname),
725 parser_errposition(pstate, defel->location)));
726 }
727
728 /*
729 * Check for incompatible options (must do these three before inserting
730 * defaults)
731 */
732 if (opts_out->binary && opts_out->delim)
733 ereport(ERROR,
734 (errcode(ERRCODE_SYNTAX_ERROR),
735 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
736 errmsg("cannot specify %s in BINARY mode", "DELIMITER")));
737
738 if (opts_out->binary && opts_out->null_print)
739 ereport(ERROR,
740 (errcode(ERRCODE_SYNTAX_ERROR),
741 errmsg("cannot specify %s in BINARY mode", "NULL")));
742
743 if (opts_out->binary && opts_out->default_print)
744 ereport(ERROR,
745 (errcode(ERRCODE_SYNTAX_ERROR),
746 errmsg("cannot specify %s in BINARY mode", "DEFAULT")));
747
748 /* Set defaults for omitted options */
749 if (!opts_out->delim)
750 opts_out->delim = opts_out->csv_mode ? "," : "\t";
751
752 if (!opts_out->null_print)
753 opts_out->null_print = opts_out->csv_mode ? "" : "\\N";
754 opts_out->null_print_len = strlen(opts_out->null_print);
755
756 if (opts_out->csv_mode)
757 {
758 if (!opts_out->quote)
759 opts_out->quote = "\"";
760 if (!opts_out->escape)
761 opts_out->escape = opts_out->quote;
762 }
763
764 /* Only single-byte delimiter strings are supported. */
765 if (strlen(opts_out->delim) != 1)
766 ereport(ERROR,
767 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
768 errmsg("COPY delimiter must be a single one-byte character")));
769
770 /* Disallow end-of-line characters */
771 if (strchr(opts_out->delim, '\r') != NULL ||
772 strchr(opts_out->delim, '\n') != NULL)
773 ereport(ERROR,
774 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
775 errmsg("COPY delimiter cannot be newline or carriage return")));
776
777 if (strchr(opts_out->null_print, '\r') != NULL ||
778 strchr(opts_out->null_print, '\n') != NULL)
779 ereport(ERROR,
780 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
781 errmsg("COPY null representation cannot use newline or carriage return")));
782
783 if (opts_out->default_print)
784 {
785 opts_out->default_print_len = strlen(opts_out->default_print);
786
787 if (strchr(opts_out->default_print, '\r') != NULL ||
788 strchr(opts_out->default_print, '\n') != NULL)
789 ereport(ERROR,
790 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
791 errmsg("COPY default representation cannot use newline or carriage return")));
792 }
793
794 /*
795 * Disallow unsafe delimiter characters in non-CSV mode. We can't allow
796 * backslash because it would be ambiguous. We can't allow the other
797 * cases because data characters matching the delimiter must be
798 * backslashed, and certain backslash combinations are interpreted
799 * non-literally by COPY IN. Disallowing all lower case ASCII letters is
800 * more than strictly necessary, but seems best for consistency and
801 * future-proofing. Likewise we disallow all digits though only octal
802 * digits are actually dangerous.
803 */
804 if (!opts_out->csv_mode &&
805 strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
806 opts_out->delim[0]) != NULL)
807 ereport(ERROR,
808 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
809 errmsg("COPY delimiter cannot be \"%s\"", opts_out->delim)));
810
811 /* Check header */
812 if (opts_out->binary && opts_out->header_line)
813 ereport(ERROR,
814 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
815 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
816 errmsg("cannot specify %s in BINARY mode", "HEADER")));
817
818 /* Check quote */
819 if (!opts_out->csv_mode && opts_out->quote != NULL)
820 ereport(ERROR,
821 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
822 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
823 errmsg("COPY %s requires CSV mode", "QUOTE")));
824
825 if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
826 ereport(ERROR,
827 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
828 errmsg("COPY quote must be a single one-byte character")));
829
830 if (opts_out->csv_mode && opts_out->delim[0] == opts_out->quote[0])
831 ereport(ERROR,
832 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
833 errmsg("COPY delimiter and quote must be different")));
834
835 /* Check escape */
836 if (!opts_out->csv_mode && opts_out->escape != NULL)
837 ereport(ERROR,
838 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
839 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
840 errmsg("COPY %s requires CSV mode", "ESCAPE")));
841
842 if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
843 ereport(ERROR,
844 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
845 errmsg("COPY escape must be a single one-byte character")));
846
847 /* Check force_quote */
848 if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
849 ereport(ERROR,
850 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
851 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
852 errmsg("COPY %s requires CSV mode", "FORCE_QUOTE")));
853 if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
854 ereport(ERROR,
855 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
856 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
857 second %s is a COPY with direction, e.g. COPY TO */
858 errmsg("COPY %s cannot be used with %s", "FORCE_QUOTE",
859 "COPY FROM")));
860
861 /* Check force_notnull */
862 if (!opts_out->csv_mode && (opts_out->force_notnull != NIL ||
863 opts_out->force_notnull_all))
864 ereport(ERROR,
865 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
866 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
867 errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
868 if ((opts_out->force_notnull != NIL || opts_out->force_notnull_all) &&
869 !is_from)
870 ereport(ERROR,
871 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
872 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
873 second %s is a COPY with direction, e.g. COPY TO */
874 errmsg("COPY %s cannot be used with %s", "FORCE_NOT_NULL",
875 "COPY TO")));
876
877 /* Check force_null */
878 if (!opts_out->csv_mode && (opts_out->force_null != NIL ||
879 opts_out->force_null_all))
880 ereport(ERROR,
881 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
882 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
883 errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
884
885 if ((opts_out->force_null != NIL || opts_out->force_null_all) &&
886 !is_from)
887 ereport(ERROR,
888 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
889 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
890 second %s is a COPY with direction, e.g. COPY TO */
891 errmsg("COPY %s cannot be used with %s", "FORCE_NULL",
892 "COPY TO")));
893
894 /* Don't allow the delimiter to appear in the null string. */
895 if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
896 ereport(ERROR,
897 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
898 /*- translator: %s is the name of a COPY option, e.g. NULL */
899 errmsg("COPY delimiter character must not appear in the %s specification",
900 "NULL")));
901
902 /* Don't allow the CSV quote char to appear in the null string. */
903 if (opts_out->csv_mode &&
904 strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
905 ereport(ERROR,
906 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
907 /*- translator: %s is the name of a COPY option, e.g. NULL */
908 errmsg("CSV quote character must not appear in the %s specification",
909 "NULL")));
910
911 /* Check freeze */
912 if (opts_out->freeze && !is_from)
913 ereport(ERROR,
914 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
915 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
916 second %s is a COPY with direction, e.g. COPY TO */
917 errmsg("COPY %s cannot be used with %s", "FREEZE",
918 "COPY TO")));
919
920 if (opts_out->default_print)
921 {
922 if (!is_from)
923 ereport(ERROR,
924 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
925 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
926 second %s is a COPY with direction, e.g. COPY TO */
927 errmsg("COPY %s cannot be used with %s", "DEFAULT",
928 "COPY TO")));
929
930 /* Don't allow the delimiter to appear in the default string. */
931 if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
932 ereport(ERROR,
933 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
934 /*- translator: %s is the name of a COPY option, e.g. NULL */
935 errmsg("COPY delimiter character must not appear in the %s specification",
936 "DEFAULT")));
937
938 /* Don't allow the CSV quote char to appear in the default string. */
939 if (opts_out->csv_mode &&
940 strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
941 ereport(ERROR,
942 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
943 /*- translator: %s is the name of a COPY option, e.g. NULL */
944 errmsg("CSV quote character must not appear in the %s specification",
945 "DEFAULT")));
946
947 /* Don't allow the NULL and DEFAULT string to be the same */
948 if (opts_out->null_print_len == opts_out->default_print_len &&
949 strncmp(opts_out->null_print, opts_out->default_print,
950 opts_out->null_print_len) == 0)
951 ereport(ERROR,
952 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
953 errmsg("NULL specification and DEFAULT specification cannot be the same")));
954 }
955 /* Check on_error */
956 if (opts_out->binary && opts_out->on_error != COPY_ON_ERROR_STOP)
957 ereport(ERROR,
958 (errcode(ERRCODE_SYNTAX_ERROR),
959 errmsg("only ON_ERROR STOP is allowed in BINARY mode")));
960
961 if (opts_out->reject_limit && !opts_out->on_error)
962 ereport(ERROR,
963 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
964 /*- translator: first and second %s are the names of COPY option, e.g.
965 * ON_ERROR, third is the value of the COPY option, e.g. IGNORE */
966 errmsg("COPY %s requires %s to be set to %s",
967 "REJECT_LIMIT", "ON_ERROR", "IGNORE")));
968 }
969
970 /*
971 * CopyGetAttnums - build an integer list of attnums to be copied
972 *
973 * The input attnamelist is either the user-specified column list,
974 * or NIL if there was none (in which case we want all the non-dropped
975 * columns).
976 *
977 * We don't include generated columns in the generated full list and we don't
978 * allow them to be specified explicitly. They don't make sense for COPY
979 * FROM, but we could possibly allow them for COPY TO. But this way it's at
980 * least ensured that whatever we copy out can be copied back in.
981 *
982 * rel can be NULL ... it's only used for error reports.
983 */
984 List *
985 CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
986 {
987 List *attnums = NIL;
988
989 if (attnamelist == NIL)
990 {
991 /* Generate default column list */
992 int attr_count = tupDesc->natts;
993 int i;
994
995 for (i = 0; i < attr_count; i++)
996 {
997 CompactAttribute *attr = TupleDescCompactAttr(tupDesc, i);
998
999 if (attr->attisdropped || attr->attgenerated)
1000 continue;
1001 attnums = lappend_int(attnums, i + 1);
1002 }
1003 }
1004 else
1005 {
1006 /* Validate the user-supplied list and extract attnums */
1007 ListCell *l;
1008
1009 foreach(l, attnamelist)
1010 {
1011 char *name = strVal(lfirst(l));
1012 int attnum;
1013 int i;
1014
1015 /* Lookup column name */
1016 attnum = InvalidAttrNumber;
1017 for (i = 0; i < tupDesc->natts; i++)
1018 {
1019 Form_pg_attribute att = TupleDescAttr(tupDesc, i);
1020
1021 if (att->attisdropped)
1022 continue;
1023 if (namestrcmp(&(att->attname), name) == 0)
1024 {
1025 if (att->attgenerated)
1026 ereport(ERROR,
1027 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1028 errmsg("column \"%s\" is a generated column",
1029 name),
1030 errdetail("Generated columns cannot be used in COPY.")));
1031 attnum = att->attnum;
1032 break;
1033 }
1034 }
1035 if (attnum == InvalidAttrNumber)
1036 {
1037 if (rel != NULL)
1038 ereport(ERROR,
1039 (errcode(ERRCODE_UNDEFINED_COLUMN),
1040 errmsg("column \"%s\" of relation \"%s\" does not exist",
1041 name, RelationGetRelationName(rel))));
1042 else
1043 ereport(ERROR,
1044 (errcode(ERRCODE_UNDEFINED_COLUMN),
1045 errmsg("column \"%s\" does not exist",
1046 name)));
1047 }
1048 /* Check for duplicates */
1049 if (list_member_int(attnums, attnum))
1050 ereport(ERROR,
1051 (errcode(ERRCODE_DUPLICATE_COLUMN),
1052 errmsg("column \"%s\" specified more than once",
1053 name)));
1054 attnums = lappend_int(attnums, attnum);
1055 }
1056 }
1057
1058 return attnums;
1059 }