PostgreSQL Source Code git master
Loading...
Searching...
No Matches
copy.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include "access/sysattr.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/pg_authid.h"
#include "commands/copy.h"
#include "commands/defrem.h"
#include "executor/executor.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/miscnodes.h"
#include "optimizer/optimizer.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/rls.h"
Include dependency graph for copy.c:

Go to the source code of this file.

Functions

void DoCopy (ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
 
static int defGetCopyHeaderOption (DefElem *def, bool is_from)
 
static CopyOnErrorChoice defGetCopyOnErrorChoice (DefElem *def, ParseState *pstate, bool is_from)
 
static int64 defGetCopyRejectLimitOption (DefElem *def)
 
static CopyLogVerbosityChoice defGetCopyLogVerbosityChoice (DefElem *def, ParseState *pstate)
 
void ProcessCopyOptions (ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
 
ListCopyGetAttnums (TupleDesc tupDesc, Relation rel, List *attnamelist)
 

Function Documentation

◆ CopyGetAttnums()

List * CopyGetAttnums ( TupleDesc  tupDesc,
Relation  rel,
List attnamelist 
)

Definition at line 1048 of file copy.c.

1049{
1050 List *attnums = NIL;
1051
1052 if (attnamelist == NIL)
1053 {
1054 /* Generate default column list */
1055 int attr_count = tupDesc->natts;
1056 int i;
1057
1058 for (i = 0; i < attr_count; i++)
1059 {
1060 CompactAttribute *attr = TupleDescCompactAttr(tupDesc, i);
1061
1062 if (attr->attisdropped || attr->attgenerated)
1063 continue;
1064 attnums = lappend_int(attnums, i + 1);
1065 }
1066 }
1067 else
1068 {
1069 /* Validate the user-supplied list and extract attnums */
1070 ListCell *l;
1071
1072 foreach(l, attnamelist)
1073 {
1074 char *name = strVal(lfirst(l));
1075 int attnum;
1076 int i;
1077
1078 /* Lookup column name */
1080 for (i = 0; i < tupDesc->natts; i++)
1081 {
1083
1084 if (att->attisdropped)
1085 continue;
1086 if (namestrcmp(&(att->attname), name) == 0)
1087 {
1088 if (att->attgenerated)
1089 ereport(ERROR,
1091 errmsg("column \"%s\" is a generated column",
1092 name),
1093 errdetail("Generated columns cannot be used in COPY.")));
1094 attnum = att->attnum;
1095 break;
1096 }
1097 }
1099 {
1100 if (rel != NULL)
1101 ereport(ERROR,
1103 errmsg("column \"%s\" of relation \"%s\" does not exist",
1105 else
1106 ereport(ERROR,
1108 errmsg("column \"%s\" does not exist",
1109 name)));
1110 }
1111 /* Check for duplicates */
1112 if (list_member_int(attnums, attnum))
1113 ereport(ERROR,
1115 errmsg("column \"%s\" specified more than once",
1116 name)));
1117 attnums = lappend_int(attnums, attnum);
1118 }
1119 }
1120
1121 return attnums;
1122}
#define InvalidAttrNumber
Definition attnum.h:23
int errcode(int sqlerrcode)
Definition elog.c:874
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
int i
Definition isn.c:77
List * lappend_int(List *list, int datum)
Definition list.c:357
bool list_member_int(const List *list, int datum)
Definition list.c:702
int namestrcmp(Name name, const char *str)
Definition name.c:247
static char * errmsg
int16 attnum
FormData_pg_attribute * Form_pg_attribute
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
static int fb(int x)
#define RelationGetRelationName(relation)
Definition rel.h:548
bool attgenerated
Definition tupdesc.h:79
bool attisdropped
Definition tupdesc.h:78
Definition pg_list.h:54
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193
#define strVal(v)
Definition value.h:82
const char * name

References CompactAttribute::attgenerated, CompactAttribute::attisdropped, attnum, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), i, InvalidAttrNumber, lappend_int(), lfirst, list_member_int(), name, namestrcmp(), TupleDescData::natts, NIL, RelationGetRelationName, strVal, TupleDescAttr(), and TupleDescCompactAttr().

Referenced by BeginCopyFrom(), BeginCopyTo(), and DoCopy().

◆ defGetCopyHeaderOption()

static int defGetCopyHeaderOption ( DefElem def,
bool  is_from 
)
static

Definition at line 376 of file copy.c.

377{
378 int ival = COPY_HEADER_FALSE;
379
380 /*
381 * If no parameter value given, assume "true" is meant.
382 */
383 if (def->arg == NULL)
384 return COPY_HEADER_TRUE;
385
386 /*
387 * Allow an integer value greater than or equal to zero (integers
388 * specified as strings are also accepted, mainly for file_fdw foreign
389 * table options), "true", "false", "on", "off", or "match".
390 */
391 switch (nodeTag(def->arg))
392 {
393 case T_Integer:
394 ival = intVal(def->arg);
395 break;
396 default:
397 {
398 char *sval = defGetString(def);
399
400 /*
401 * The set of strings accepted here should match up with the
402 * grammar's opt_boolean_or_string production.
403 */
404 if (pg_strcasecmp(sval, "true") == 0)
405 return COPY_HEADER_TRUE;
406 if (pg_strcasecmp(sval, "false") == 0)
407 return COPY_HEADER_FALSE;
408 if (pg_strcasecmp(sval, "on") == 0)
409 return COPY_HEADER_TRUE;
410 if (pg_strcasecmp(sval, "off") == 0)
411 return COPY_HEADER_FALSE;
412 if (pg_strcasecmp(sval, "match") == 0)
413 {
414 if (!is_from)
417 errmsg("cannot use \"%s\" with HEADER in COPY TO",
418 sval)));
419 return COPY_HEADER_MATCH;
420 }
421 else
422 {
424
425 /* Check if the header is a valid integer */
426 ival = pg_strtoint32_safe(sval, (Node *) &escontext);
427 if (escontext.error_occurred)
430 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
431 second %s is the special value "match" for that option */
432 errmsg("%s requires a Boolean value, an integer "
433 "value greater than or equal to zero, "
434 "or the string \"%s\"",
435 def->defname, "match")));
436 }
437 }
438 break;
439 }
440
441 if (ival < 0)
444 errmsg("a negative integer value cannot be "
445 "specified for %s", def->defname)));
446
447 if (!is_from && ival > 1)
450 errmsg("cannot use multi-line header in COPY TO")));
451
452 return ival;
453}
char * defGetString(DefElem *def)
Definition define.c:34
#define COPY_HEADER_MATCH
Definition copy.h:26
#define COPY_HEADER_FALSE
Definition copy.h:27
#define COPY_HEADER_TRUE
Definition copy.h:28
#define nodeTag(nodeptr)
Definition nodes.h:139
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition numutils.c:388
int pg_strcasecmp(const char *s1, const char *s2)
char * defname
Definition parsenodes.h:857
Node * arg
Definition parsenodes.h:858
Definition nodes.h:135
#define intVal(v)
Definition value.h:79

References DefElem::arg, COPY_HEADER_FALSE, COPY_HEADER_MATCH, COPY_HEADER_TRUE, defGetString(), DefElem::defname, ereport, errcode(), errmsg, ERROR, ErrorSaveContext::error_occurred, fb(), intVal, nodeTag, pg_strcasecmp(), and pg_strtoint32_safe().

Referenced by ProcessCopyOptions().

◆ defGetCopyLogVerbosityChoice()

static CopyLogVerbosityChoice defGetCopyLogVerbosityChoice ( DefElem def,
ParseState pstate 
)
static

Definition at line 521 of file copy.c.

522{
523 char *sval;
524
525 /*
526 * Allow "silent", "default", or "verbose" values.
527 */
528 sval = defGetString(def);
529 if (pg_strcasecmp(sval, "silent") == 0)
531 if (pg_strcasecmp(sval, "default") == 0)
533 if (pg_strcasecmp(sval, "verbose") == 0)
535
538 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
539 errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
540 parser_errposition(pstate, def->location)));
541 return COPY_LOG_VERBOSITY_DEFAULT; /* keep compiler quiet */
542}
@ COPY_LOG_VERBOSITY_SILENT
Definition copy.h:46
@ COPY_LOG_VERBOSITY_VERBOSE
Definition copy.h:49
@ COPY_LOG_VERBOSITY_DEFAULT
Definition copy.h:47
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
ParseLoc location
Definition parsenodes.h:861

References COPY_LOG_VERBOSITY_DEFAULT, COPY_LOG_VERBOSITY_SILENT, COPY_LOG_VERBOSITY_VERBOSE, defGetString(), ereport, errcode(), errmsg, ERROR, fb(), DefElem::location, parser_errposition(), and pg_strcasecmp().

Referenced by ProcessCopyOptions().

◆ defGetCopyOnErrorChoice()

static CopyOnErrorChoice defGetCopyOnErrorChoice ( DefElem def,
ParseState pstate,
bool  is_from 
)
static

Definition at line 459 of file copy.c.

460{
461 char *sval = defGetString(def);
462
463 if (!is_from)
466 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
467 second %s is a COPY with direction, e.g. COPY TO */
468 errmsg("COPY %s cannot be used with %s", "ON_ERROR", "COPY TO"),
469 parser_errposition(pstate, def->location)));
470
471 if (pg_strcasecmp(sval, "stop") == 0)
472 return COPY_ON_ERROR_STOP;
473 if (pg_strcasecmp(sval, "ignore") == 0)
475 if (pg_strcasecmp(sval, "set_null") == 0)
477
480 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
481 errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
482 parser_errposition(pstate, def->location)));
483 return COPY_ON_ERROR_STOP; /* keep compiler quiet */
484}
@ COPY_ON_ERROR_IGNORE
Definition copy.h:37
@ COPY_ON_ERROR_SET_NULL
Definition copy.h:38
@ COPY_ON_ERROR_STOP
Definition copy.h:36

References COPY_ON_ERROR_IGNORE, COPY_ON_ERROR_SET_NULL, COPY_ON_ERROR_STOP, defGetString(), ereport, errcode(), errmsg, ERROR, fb(), DefElem::location, parser_errposition(), and pg_strcasecmp().

Referenced by ProcessCopyOptions().

◆ defGetCopyRejectLimitOption()

static int64 defGetCopyRejectLimitOption ( DefElem def)
static

Definition at line 494 of file copy.c.

495{
496 int64 reject_limit;
497
498 if (def->arg == NULL)
501 errmsg("%s requires a numeric value",
502 def->defname)));
503 else if (IsA(def->arg, String))
504 reject_limit = pg_strtoint64(strVal(def->arg));
505 else
506 reject_limit = defGetInt64(def);
507
508 if (reject_limit <= 0)
511 errmsg("REJECT_LIMIT (%" PRId64 ") must be greater than zero",
512 reject_limit)));
513
514 return reject_limit;
515}
int64_t int64
Definition c.h:615
int64 defGetInt64(DefElem *def)
Definition define.c:172
#define IsA(nodeptr, _type_)
Definition nodes.h:164
int64 pg_strtoint64(const char *s)
Definition numutils.c:643
Definition value.h:64

References DefElem::arg, defGetInt64(), DefElem::defname, ereport, errcode(), errmsg, ERROR, fb(), IsA, pg_strtoint64(), and strVal.

Referenced by ProcessCopyOptions().

◆ DoCopy()

void DoCopy ( ParseState pstate,
const CopyStmt stmt,
int  stmt_location,
int  stmt_len,
uint64 processed 
)

Definition at line 63 of file copy.c.

66{
67 bool is_from = stmt->is_from;
68 bool pipe = (stmt->filename == NULL);
69 Relation rel;
70 Oid relid;
71 RawStmt *query = NULL;
72 Node *whereClause = NULL;
73
74 /*
75 * Disallow COPY to/from file or program except to users with the
76 * appropriate role.
77 */
78 if (!pipe)
79 {
80 if (stmt->is_program)
81 {
85 errmsg("permission denied to COPY to or from an external program"),
86 errdetail("Only roles with privileges of the \"%s\" role may COPY to or from an external program.",
87 "pg_execute_server_program"),
88 errhint("Anyone can COPY to stdout or from stdin. "
89 "psql's \\copy command also works for anyone.")));
90 }
91 else
92 {
96 errmsg("permission denied to COPY from a file"),
97 errdetail("Only roles with privileges of the \"%s\" role may COPY from a file.",
98 "pg_read_server_files"),
99 errhint("Anyone can COPY to stdout or from stdin. "
100 "psql's \\copy command also works for anyone.")));
101
105 errmsg("permission denied to COPY to a file"),
106 errdetail("Only roles with privileges of the \"%s\" role may COPY to a file.",
107 "pg_write_server_files"),
108 errhint("Anyone can COPY to stdout or from stdin. "
109 "psql's \\copy command also works for anyone.")));
110 }
111 }
112
113 if (stmt->relation)
114 {
115 LOCKMODE lockmode = is_from ? RowExclusiveLock : AccessShareLock;
118 TupleDesc tupDesc;
119 List *attnums;
120 ListCell *cur;
121
122 Assert(!stmt->query);
123
124 /* Open and lock the relation, using the appropriate lock type. */
125 rel = table_openrv(stmt->relation, lockmode);
126
127 relid = RelationGetRelid(rel);
128
129 nsitem = addRangeTableEntryForRelation(pstate, rel, lockmode,
130 NULL, false, false);
131
132 perminfo = nsitem->p_perminfo;
133 perminfo->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
134
135 if (stmt->whereClause)
136 {
138 int i;
139
140 /* add nsitem to query namespace */
141 addNSItemToQuery(pstate, nsitem, false, true, true);
142
143 /* Transform the raw expression tree */
144 whereClause = transformExpr(pstate, stmt->whereClause, EXPR_KIND_COPY_WHERE);
145
146 /* Make sure it yields a boolean result. */
147 whereClause = coerce_to_boolean(pstate, whereClause, "WHERE");
148
149 /* we have to fix its collations too */
150 assign_expr_collations(pstate, whereClause);
151
152 /*
153 * Examine all the columns in the WHERE clause expression. When
154 * the whole-row reference is present, examine all the columns of
155 * the table.
156 */
157 pull_varattnos(whereClause, 1, &expr_attrs);
159 {
164 }
165
166 i = -1;
167 while ((i = bms_next_member(expr_attrs, i)) >= 0)
168 {
170
171 Assert(attno != 0);
172
173 /*
174 * Prohibit generated columns in the WHERE clause. Stored
175 * generated columns are not yet computed when the filtering
176 * happens. Virtual generated columns could probably work (we
177 * would need to expand them somewhere around here), but for
178 * now we keep them consistent with the stored variant.
179 */
180 if (TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated)
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
202 bms = is_from ? &perminfo->insertedCols : &perminfo->selectedCols;
203
204 *bms = bms_add_member(*bms, attno);
205 }
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 {
225 ColumnRef *cr;
226 ResTarget *target;
227 RangeVar *from;
228 List *targetList = NIL;
229
230 if (is_from)
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 {
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 */
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 * However, when copying data from a partitioned table, we don't
296 * use "ONLY", since we need to retrieve rows from its descendant
297 * tables too.
298 */
301 -1);
302 from->inh = (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
303
304 /* Build query */
306 select->targetList = targetList;
307 select->fromClause = list_make1(from);
308
309 query = makeNode(RawStmt);
310 query->stmt = (Node *) select;
311 query->stmt_location = stmt_location;
312 query->stmt_len = stmt_len;
313
314 /*
315 * Close the relation for now, but keep the lock on it to prevent
316 * changes between now and when we start the query-based COPY.
317 *
318 * We'll reopen it later as part of the query-based COPY.
319 */
320 table_close(rel, NoLock);
321 rel = NULL;
322 }
323 }
324 else
325 {
326 Assert(stmt->query);
327
328 query = makeNode(RawStmt);
329 query->stmt = stmt->query;
330 query->stmt_location = stmt_location;
331 query->stmt_len = stmt_len;
332
333 relid = InvalidOid;
334 rel = NULL;
335 }
336
337 if (is_from)
338 {
339 CopyFromState cstate;
340
341 Assert(rel);
342
343 /* check read-only transaction and parallel mode */
344 if (XactReadOnly && !rel->rd_islocaltemp)
345 PreventCommandIfReadOnly("COPY FROM");
346
347 cstate = BeginCopyFrom(pstate, rel, whereClause,
348 stmt->filename, stmt->is_program,
349 NULL, stmt->attlist, stmt->options);
350 *processed = CopyFrom(cstate); /* copy from file to database */
351 EndCopyFrom(cstate);
352 }
353 else
354 {
355 CopyToState cstate;
356
357 cstate = BeginCopyTo(pstate, rel, query, relid,
358 stmt->filename, stmt->is_program,
359 NULL, stmt->attlist, stmt->options);
360 *processed = DoCopyTo(cstate); /* copy from database to file */
361 EndCopyTo(cstate);
362 }
363
364 if (rel != NULL)
365 table_close(rel, NoLock);
366}
bool has_privs_of_role(Oid member, Oid role)
Definition acl.c:5314
int16 AttrNumber
Definition attnum.h:21
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
Definition copy.c:1048
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition bitmapset.c:1003
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define Assert(condition)
Definition c.h:945
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition clauses.c:2498
CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition copyfrom.c:1535
uint64 CopyFrom(CopyFromState cstate)
Definition copyfrom.c:781
void EndCopyFrom(CopyFromState cstate)
Definition copyfrom.c:1942
uint64 DoCopyTo(CopyToState cstate)
Definition copyto.c:1241
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, copy_data_dest_cb data_dest_cb, List *attnamelist, List *options)
Definition copyto.c:769
void EndCopyTo(CopyToState cstate)
Definition copyto.c:1220
struct cursor * cur
Definition ecpg.c:29
int errhint(const char *fmt,...) pg_attribute_printf(1
bool ExecCheckPermissions(List *rangeTable, List *rteperminfos, bool ereport_on_violation)
Definition execMain.c:584
#define stmt
List * lappend(List *list, void *datum)
Definition list.c:339
int LOCKMODE
Definition lockdefs.h:26
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:946
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3588
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
List * make_ands_implicit(Expr *clause)
Definition makefuncs.c:810
char * pstrdup(const char *in)
Definition mcxt.c:1781
Oid GetUserId(void)
Definition miscinit.c:470
#define makeNode(_type_)
Definition nodes.h:161
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
@ EXPR_KIND_COPY_WHERE
Definition parse_node.h:82
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, LOCKMODE lockmode, Alias *alias, bool inh, bool inFromCl)
#define ACL_INSERT
Definition parsenodes.h:76
#define ACL_SELECT
Definition parsenodes.h:77
#define lfirst_int(lc)
Definition pg_list.h:173
#define list_make1(x1)
Definition pg_list.h:212
#define InvalidOid
unsigned int Oid
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition prepqual.c:293
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
#define RelationGetNamespace(relation)
Definition rel.h:555
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition rls.c:52
@ RLS_ENABLED
Definition rls.h:45
List * p_rtable
Definition parse_node.h:211
bool inh
Definition primnodes.h:87
ParseLoc stmt_location
ParseLoc stmt_len
Node * stmt
bool rd_islocaltemp
Definition rel.h:61
Form_pg_class rd_rel
Definition rel.h:111
Node * val
Definition parsenodes.h:547
ParseLoc location
Definition parsenodes.h:548
List * indirection
Definition parsenodes.h:546
char * name
Definition parsenodes.h:545
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition table.c:83
void PreventCommandIfReadOnly(const char *cmdname)
Definition utility.c:409
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition var.c:296
#define select(n, r, w, e, timeout)
Definition win32_port.h:500
bool XactReadOnly
Definition xact.c:84

References AccessShareLock, ACL_INSERT, ACL_SELECT, addNSItemToQuery(), addRangeTableEntryForRelation(), Assert, assign_expr_collations(), BeginCopyFrom(), BeginCopyTo(), bms_add_member(), bms_add_range(), bms_del_member(), bms_is_member(), bms_next_member(), canonicalize_qual(), check_enable_rls(), coerce_to_boolean(), CopyFrom(), CopyGetAttnums(), cur, DoCopyTo(), EndCopyFrom(), EndCopyTo(), ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, eval_const_expressions(), ExecCheckPermissions(), EXPR_KIND_COPY_WHERE, fb(), FirstLowInvalidHeapAttributeNumber, get_attname(), get_namespace_name(), GetUserId(), has_privs_of_role(), i, ResTarget::indirection, RangeVar::inh, InvalidOid, lappend(), lfirst, lfirst_int, list_make1, ResTarget::location, make_ands_implicit(), makeNode, makeRangeVar(), ResTarget::name, NIL, NoLock, ParseState::p_rtable, PreventCommandIfReadOnly(), pstrdup(), pull_varattnos(), RelationData::rd_islocaltemp, RelationData::rd_rel, RelationGetDescr, RelationGetNamespace, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, RLS_ENABLED, RowExclusiveLock, select, RawStmt::stmt, stmt, RawStmt::stmt_len, RawStmt::stmt_location, table_close(), table_openrv(), transformExpr(), TupleDescAttr(), ResTarget::val, and XactReadOnly.

Referenced by standard_ProcessUtility().

◆ ProcessCopyOptions()

void ProcessCopyOptions ( ParseState pstate,
CopyFormatOptions opts_out,
bool  is_from,
List options 
)

Definition at line 561 of file copy.c.

565{
566 bool format_specified = false;
567 bool freeze_specified = false;
568 bool header_specified = false;
569 bool on_error_specified = false;
570 bool log_verbosity_specified = false;
571 bool reject_limit_specified = false;
572 bool force_array_specified = false;
574
575 /* Support external use for option sanity checking */
576 if (opts_out == NULL)
578
579 opts_out->file_encoding = -1;
580 /* default format */
581 opts_out->format = COPY_FORMAT_TEXT;
582
583 /* Extract options from the statement node tree */
584 foreach(option, options)
585 {
587
588 if (strcmp(defel->defname, "format") == 0)
589 {
590 char *fmt = defGetString(defel);
591
594 format_specified = true;
595 if (strcmp(fmt, "text") == 0)
596 opts_out->format = COPY_FORMAT_TEXT;
597 else if (strcmp(fmt, "csv") == 0)
598 opts_out->format = COPY_FORMAT_CSV;
599 else if (strcmp(fmt, "binary") == 0)
601 else if (strcmp(fmt, "json") == 0)
602 opts_out->format = COPY_FORMAT_JSON;
603 else
606 errmsg("COPY format \"%s\" not recognized", fmt),
607 parser_errposition(pstate, defel->location)));
608 }
609 else if (strcmp(defel->defname, "freeze") == 0)
610 {
613 freeze_specified = true;
614 opts_out->freeze = defGetBoolean(defel);
615 }
616 else if (strcmp(defel->defname, "delimiter") == 0)
617 {
618 if (opts_out->delim)
620 opts_out->delim = defGetString(defel);
621 }
622 else if (strcmp(defel->defname, "null") == 0)
623 {
624 if (opts_out->null_print)
626 opts_out->null_print = defGetString(defel);
627 }
628 else if (strcmp(defel->defname, "default") == 0)
629 {
630 if (opts_out->default_print)
632 opts_out->default_print = defGetString(defel);
633 }
634 else if (strcmp(defel->defname, "header") == 0)
635 {
638 header_specified = true;
639 opts_out->header_line = defGetCopyHeaderOption(defel, is_from);
640 }
641 else if (strcmp(defel->defname, "quote") == 0)
642 {
643 if (opts_out->quote)
645 opts_out->quote = defGetString(defel);
646 }
647 else if (strcmp(defel->defname, "escape") == 0)
648 {
649 if (opts_out->escape)
651 opts_out->escape = defGetString(defel);
652 }
653 else if (strcmp(defel->defname, "force_quote") == 0)
654 {
655 if (opts_out->force_quote || opts_out->force_quote_all)
657 if (defel->arg && IsA(defel->arg, A_Star))
658 opts_out->force_quote_all = true;
659 else if (defel->arg && IsA(defel->arg, List))
660 opts_out->force_quote = castNode(List, defel->arg);
661 else
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, "force_not_null") == 0)
669 {
670 if (opts_out->force_notnull || opts_out->force_notnull_all)
672 if (defel->arg && IsA(defel->arg, A_Star))
673 opts_out->force_notnull_all = true;
674 else if (defel->arg && IsA(defel->arg, List))
675 opts_out->force_notnull = castNode(List, defel->arg);
676 else
679 errmsg("argument to option \"%s\" must be a list of column names",
680 defel->defname),
681 parser_errposition(pstate, defel->location)));
682 }
683 else if (strcmp(defel->defname, "force_null") == 0)
684 {
685 if (opts_out->force_null || opts_out->force_null_all)
687 if (defel->arg && IsA(defel->arg, A_Star))
688 opts_out->force_null_all = true;
689 else if (defel->arg && IsA(defel->arg, List))
690 opts_out->force_null = castNode(List, defel->arg);
691 else
694 errmsg("argument to option \"%s\" must be a list of column names",
695 defel->defname),
696 parser_errposition(pstate, defel->location)));
697 }
698 else if (strcmp(defel->defname, "convert_selectively") == 0)
699 {
700 /*
701 * Undocumented, not-accessible-from-SQL option: convert only the
702 * named columns to binary form, storing the rest as NULLs. It's
703 * allowed for the column list to be NIL.
704 */
705 if (opts_out->convert_selectively)
707 opts_out->convert_selectively = true;
708 if (defel->arg == NULL || IsA(defel->arg, List))
709 opts_out->convert_select = castNode(List, defel->arg);
710 else
713 errmsg("argument to option \"%s\" must be a list of column names",
714 defel->defname),
715 parser_errposition(pstate, defel->location)));
716 }
717 else if (strcmp(defel->defname, "encoding") == 0)
718 {
719 if (opts_out->file_encoding >= 0)
722 if (opts_out->file_encoding < 0)
725 errmsg("argument to option \"%s\" must be a valid encoding name",
726 defel->defname),
727 parser_errposition(pstate, defel->location)));
728 }
729 else if (strcmp(defel->defname, "force_array") == 0)
730 {
734 opts_out->force_array = defGetBoolean(defel);
735 }
736 else if (strcmp(defel->defname, "on_error") == 0)
737 {
740 on_error_specified = true;
741 opts_out->on_error = defGetCopyOnErrorChoice(defel, pstate, is_from);
742 }
743 else if (strcmp(defel->defname, "log_verbosity") == 0)
744 {
748 opts_out->log_verbosity = defGetCopyLogVerbosityChoice(defel, pstate);
749 }
750 else if (strcmp(defel->defname, "reject_limit") == 0)
751 {
756 }
757 else
760 errmsg("option \"%s\" not recognized",
761 defel->defname),
762 parser_errposition(pstate, defel->location)));
763 }
764
765 /*
766 * Check for incompatible options (must do these three before inserting
767 * defaults)
768 */
769 if (opts_out->delim &&
770 (opts_out->format == COPY_FORMAT_BINARY ||
771 opts_out->format == COPY_FORMAT_JSON))
775 ? errmsg("cannot specify %s in BINARY mode", "DELIMITER")
776 : errmsg("cannot specify %s in JSON mode", "DELIMITER"));
777
778 if (opts_out->null_print &&
779 (opts_out->format == COPY_FORMAT_BINARY ||
780 opts_out->format == COPY_FORMAT_JSON))
784 ? errmsg("cannot specify %s in BINARY mode", "NULL")
785 : errmsg("cannot specify %s in JSON mode", "NULL"));
786
787 if (opts_out->default_print &&
788 (opts_out->format == COPY_FORMAT_BINARY ||
789 opts_out->format == COPY_FORMAT_JSON))
793 ? errmsg("cannot specify %s in BINARY mode", "DEFAULT")
794 : errmsg("cannot specify %s in JSON mode", "DEFAULT"));
795
796 /* Set defaults for omitted options */
797 if (!opts_out->delim)
798 opts_out->delim = (opts_out->format == COPY_FORMAT_CSV) ? "," : "\t";
799
800 if (!opts_out->null_print)
801 opts_out->null_print = (opts_out->format == COPY_FORMAT_CSV) ? "" : "\\N";
802 opts_out->null_print_len = strlen(opts_out->null_print);
803
804 if (opts_out->format == COPY_FORMAT_CSV)
805 {
806 if (!opts_out->quote)
807 opts_out->quote = "\"";
808 if (!opts_out->escape)
809 opts_out->escape = opts_out->quote;
810 }
811
812 /* Only single-byte delimiter strings are supported. */
813 if (strlen(opts_out->delim) != 1)
816 errmsg("COPY delimiter must be a single one-byte character")));
817
818 /* Disallow end-of-line characters */
819 if (strchr(opts_out->delim, '\r') != NULL ||
820 strchr(opts_out->delim, '\n') != NULL)
823 errmsg("COPY delimiter cannot be newline or carriage return")));
824
825 if (strchr(opts_out->null_print, '\r') != NULL ||
826 strchr(opts_out->null_print, '\n') != NULL)
829 errmsg("COPY null representation cannot use newline or carriage return")));
830
831 if (opts_out->default_print)
832 {
833 opts_out->default_print_len = strlen(opts_out->default_print);
834
835 if (strchr(opts_out->default_print, '\r') != NULL ||
836 strchr(opts_out->default_print, '\n') != NULL)
839 errmsg("COPY default representation cannot use newline or carriage return")));
840 }
841
842 /*
843 * Disallow unsafe delimiter characters in non-CSV mode. We can't allow
844 * backslash because it would be ambiguous. We can't allow the other
845 * cases because data characters matching the delimiter must be
846 * backslashed, and certain backslash combinations are interpreted
847 * non-literally by COPY IN. Disallowing all lower case ASCII letters is
848 * more than strictly necessary, but seems best for consistency and
849 * future-proofing. Likewise we disallow all digits though only octal
850 * digits are actually dangerous.
851 */
852 if (opts_out->format != COPY_FORMAT_CSV &&
853 strchr("\\.abcdefghijklmnopqrstuvwxyz0123456789",
854 opts_out->delim[0]) != NULL)
857 errmsg("COPY delimiter cannot be \"%s\"", opts_out->delim)));
858
859 /* Check header */
860 if (opts_out->header_line != COPY_HEADER_FALSE &&
861 (opts_out->format == COPY_FORMAT_BINARY ||
862 opts_out->format == COPY_FORMAT_JSON))
865 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
867 ? errmsg("cannot specify %s in BINARY mode", "HEADER")
868 : errmsg("cannot specify %s in JSON mode", "HEADER"));
869
870 /* Check quote */
871 if (opts_out->format != COPY_FORMAT_CSV && opts_out->quote != NULL)
874 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
875 errmsg("COPY %s requires CSV mode", "QUOTE")));
876
877 if (opts_out->format == COPY_FORMAT_CSV && strlen(opts_out->quote) != 1)
880 errmsg("COPY quote must be a single one-byte character")));
881
882 if (opts_out->format == COPY_FORMAT_CSV && opts_out->delim[0] == opts_out->quote[0])
885 errmsg("COPY delimiter and quote must be different")));
886
887 /* Check escape */
888 if (opts_out->format != COPY_FORMAT_CSV && opts_out->escape != NULL)
891 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
892 errmsg("COPY %s requires CSV mode", "ESCAPE")));
893
894 if (opts_out->format == COPY_FORMAT_CSV && strlen(opts_out->escape) != 1)
897 errmsg("COPY escape must be a single one-byte character")));
898
899 /* Check force_quote */
900 if (opts_out->format != COPY_FORMAT_CSV && (opts_out->force_quote || opts_out->force_quote_all))
903 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
904 errmsg("COPY %s requires CSV mode", "FORCE_QUOTE")));
905 if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
908 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
909 second %s is a COPY with direction, e.g. COPY TO */
910 errmsg("COPY %s cannot be used with %s", "FORCE_QUOTE",
911 "COPY FROM")));
912
913 /* Check force_notnull */
914 if (opts_out->format != COPY_FORMAT_CSV && (opts_out->force_notnull != NIL ||
915 opts_out->force_notnull_all))
918 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
919 errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
920 if ((opts_out->force_notnull != NIL || opts_out->force_notnull_all) &&
921 !is_from)
924 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
925 second %s is a COPY with direction, e.g. COPY TO */
926 errmsg("COPY %s cannot be used with %s", "FORCE_NOT_NULL",
927 "COPY TO")));
928
929 /* Check force_null */
930 if (opts_out->format != COPY_FORMAT_CSV && (opts_out->force_null != NIL ||
931 opts_out->force_null_all))
934 /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
935 errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
936
937 if ((opts_out->force_null != NIL || opts_out->force_null_all) &&
938 !is_from)
941 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
942 second %s is a COPY with direction, e.g. COPY TO */
943 errmsg("COPY %s cannot be used with %s", "FORCE_NULL",
944 "COPY TO")));
945
946 /* Don't allow the delimiter to appear in the null string. */
947 if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
950 /*- translator: %s is the name of a COPY option, e.g. NULL */
951 errmsg("COPY delimiter character must not appear in the %s specification",
952 "NULL")));
953
954 /* Don't allow the CSV quote char to appear in the null string. */
955 if (opts_out->format == COPY_FORMAT_CSV &&
956 strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
959 /*- translator: %s is the name of a COPY option, e.g. NULL */
960 errmsg("CSV quote character must not appear in the %s specification",
961 "NULL")));
962
963 /* Check freeze */
964 if (opts_out->freeze && !is_from)
967 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
968 second %s is a COPY with direction, e.g. COPY TO */
969 errmsg("COPY %s cannot be used with %s", "FREEZE",
970 "COPY TO")));
971
972 /* Check json format */
973 if (opts_out->format == COPY_FORMAT_JSON && is_from)
976 errmsg("COPY %s is not supported for %s", "FORMAT JSON", "COPY FROM"));
977
978 if (opts_out->format != COPY_FORMAT_JSON && opts_out->force_array)
981 errmsg("COPY %s can only be used with JSON mode", "FORCE_ARRAY"));
982
983 if (opts_out->default_print)
984 {
985 if (!is_from)
988 /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
989 second %s is a COPY with direction, e.g. COPY TO */
990 errmsg("COPY %s cannot be used with %s", "DEFAULT",
991 "COPY TO")));
992
993 /* Don't allow the delimiter to appear in the default string. */
994 if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
997 /*- translator: %s is the name of a COPY option, e.g. NULL */
998 errmsg("COPY delimiter character must not appear in the %s specification",
999 "DEFAULT")));
1000
1001 /* Don't allow the CSV quote char to appear in the default string. */
1002 if (opts_out->format == COPY_FORMAT_CSV &&
1003 strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
1004 ereport(ERROR,
1006 /*- translator: %s is the name of a COPY option, e.g. NULL */
1007 errmsg("CSV quote character must not appear in the %s specification",
1008 "DEFAULT")));
1009
1010 /* Don't allow the NULL and DEFAULT string to be the same */
1011 if (opts_out->null_print_len == opts_out->default_print_len &&
1012 strncmp(opts_out->null_print, opts_out->default_print,
1013 opts_out->null_print_len) == 0)
1014 ereport(ERROR,
1016 errmsg("NULL specification and DEFAULT specification cannot be the same")));
1017 }
1018 /* Check on_error */
1019 if (opts_out->format == COPY_FORMAT_BINARY && opts_out->on_error != COPY_ON_ERROR_STOP)
1020 ereport(ERROR,
1022 errmsg("only ON_ERROR STOP is allowed in BINARY mode")));
1023
1024 if (opts_out->reject_limit && opts_out->on_error != COPY_ON_ERROR_IGNORE)
1025 ereport(ERROR,
1027 /*- translator: first and second %s are the names of COPY option, e.g.
1028 * ON_ERROR, third is the value of the COPY option, e.g. IGNORE */
1029 errmsg("COPY %s requires %s to be set to %s",
1030 "REJECT_LIMIT", "ON_ERROR", "IGNORE")));
1031}
static int defGetCopyHeaderOption(DefElem *def, bool is_from)
Definition copy.c:376
static int64 defGetCopyRejectLimitOption(DefElem *def)
Definition copy.c:494
static CopyOnErrorChoice defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
Definition copy.c:459
static CopyLogVerbosityChoice defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
Definition copy.c:521
bool defGetBoolean(DefElem *def)
Definition define.c:93
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition define.c:370
#define palloc0_object(type)
Definition fe_memutils.h:75
@ COPY_FORMAT_CSV
Definition copy.h:59
@ COPY_FORMAT_JSON
Definition copy.h:60
@ COPY_FORMAT_BINARY
Definition copy.h:58
@ COPY_FORMAT_TEXT
Definition copy.h:57
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#define lfirst_node(type, lc)
Definition pg_list.h:176
#define pg_char_to_encoding
Definition pg_wchar.h:629

References castNode, COPY_FORMAT_BINARY, COPY_FORMAT_CSV, COPY_FORMAT_JSON, COPY_FORMAT_TEXT, COPY_HEADER_FALSE, COPY_ON_ERROR_IGNORE, COPY_ON_ERROR_STOP, defGetBoolean(), defGetCopyHeaderOption(), defGetCopyLogVerbosityChoice(), defGetCopyOnErrorChoice(), defGetCopyRejectLimitOption(), defGetString(), ereport, errcode(), errmsg, ERROR, errorConflictingDefElem(), fb(), IsA, lfirst_node, NIL, palloc0_object, parser_errposition(), and pg_char_to_encoding.

Referenced by BeginCopyFrom(), BeginCopyTo(), and file_fdw_validator().