65 int stmt_location,
int stmt_len,
69 bool pipe = (stmt->
filename == NULL);
73 Node *whereClause = NULL;
85 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
86 errmsg(
"must be superuser or a member of the pg_execute_server_program role to COPY to or from an external program"),
87 errhint(
"Anyone can COPY to stdout or from stdin. " 88 "psql's \\copy command also works for anyone.")));
94 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
95 errmsg(
"must be superuser or a member of the pg_read_server_files role to COPY from a file"),
96 errhint(
"Anyone can COPY to stdout or from stdin. " 97 "psql's \\copy command also works for anyone.")));
101 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
102 errmsg(
"must be superuser or a member of the pg_write_server_files role to COPY to a file"),
103 errhint(
"Anyone can COPY to stdout or from stdin. " 104 "psql's \\copy command also works for anyone.")));
151 foreach(cur, attnums)
187 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
188 errmsg(
"COPY FROM not supported with row-level security"),
189 errhint(
"Use INSERT statements instead.")));
241 targetList =
lappend(targetList, target);
339 bool format_specified =
false;
340 bool freeze_specified =
false;
341 bool header_specified =
false;
345 if (opts_out == NULL)
351 foreach(option, options)
355 if (strcmp(defel->
defname,
"format") == 0)
359 if (format_specified)
361 (
errcode(ERRCODE_SYNTAX_ERROR),
362 errmsg(
"conflicting or redundant options"),
364 format_specified =
true;
365 if (strcmp(fmt,
"text") == 0)
367 else if (strcmp(fmt,
"csv") == 0)
369 else if (strcmp(fmt,
"binary") == 0)
373 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
374 errmsg(
"COPY format \"%s\" not recognized", fmt),
377 else if (strcmp(defel->
defname,
"freeze") == 0)
379 if (freeze_specified)
381 (
errcode(ERRCODE_SYNTAX_ERROR),
382 errmsg(
"conflicting or redundant options"),
384 freeze_specified =
true;
387 else if (strcmp(defel->
defname,
"delimiter") == 0)
391 (
errcode(ERRCODE_SYNTAX_ERROR),
392 errmsg(
"conflicting or redundant options"),
396 else if (strcmp(defel->
defname,
"null") == 0)
400 (
errcode(ERRCODE_SYNTAX_ERROR),
401 errmsg(
"conflicting or redundant options"),
405 else if (strcmp(defel->
defname,
"header") == 0)
407 if (header_specified)
409 (
errcode(ERRCODE_SYNTAX_ERROR),
410 errmsg(
"conflicting or redundant options"),
412 header_specified =
true;
415 else if (strcmp(defel->
defname,
"quote") == 0)
419 (
errcode(ERRCODE_SYNTAX_ERROR),
420 errmsg(
"conflicting or redundant options"),
424 else if (strcmp(defel->
defname,
"escape") == 0)
428 (
errcode(ERRCODE_SYNTAX_ERROR),
429 errmsg(
"conflicting or redundant options"),
433 else if (strcmp(defel->
defname,
"force_quote") == 0)
437 (
errcode(ERRCODE_SYNTAX_ERROR),
438 errmsg(
"conflicting or redundant options"),
446 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
447 errmsg(
"argument to option \"%s\" must be a list of column names",
451 else if (strcmp(defel->
defname,
"force_not_null") == 0)
455 (
errcode(ERRCODE_SYNTAX_ERROR),
456 errmsg(
"conflicting or redundant options"),
462 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
463 errmsg(
"argument to option \"%s\" must be a list of column names",
467 else if (strcmp(defel->
defname,
"force_null") == 0)
471 (
errcode(ERRCODE_SYNTAX_ERROR),
472 errmsg(
"conflicting or redundant options")));
477 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
478 errmsg(
"argument to option \"%s\" must be a list of column names",
482 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
491 (
errcode(ERRCODE_SYNTAX_ERROR),
492 errmsg(
"conflicting or redundant options"),
499 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
500 errmsg(
"argument to option \"%s\" must be a list of column names",
504 else if (strcmp(defel->
defname,
"encoding") == 0)
508 (
errcode(ERRCODE_SYNTAX_ERROR),
509 errmsg(
"conflicting or redundant options"),
514 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
515 errmsg(
"argument to option \"%s\" must be a valid encoding name",
521 (
errcode(ERRCODE_SYNTAX_ERROR),
522 errmsg(
"option \"%s\" not recognized",
533 (
errcode(ERRCODE_SYNTAX_ERROR),
534 errmsg(
"cannot specify DELIMITER in BINARY mode")));
538 (
errcode(ERRCODE_SYNTAX_ERROR),
539 errmsg(
"cannot specify NULL in BINARY mode")));
542 if (!opts_out->
delim)
551 if (!opts_out->
quote)
552 opts_out->
quote =
"\"";
558 if (strlen(opts_out->
delim) != 1)
560 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
561 errmsg(
"COPY delimiter must be a single one-byte character")));
564 if (strchr(opts_out->
delim,
'\r') != NULL ||
565 strchr(opts_out->
delim,
'\n') != NULL)
567 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
568 errmsg(
"COPY delimiter cannot be newline or carriage return")));
570 if (strchr(opts_out->
null_print,
'\r') != NULL ||
573 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
574 errmsg(
"COPY null representation cannot use newline or carriage return")));
587 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
588 opts_out->
delim[0]) != NULL)
590 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
591 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
596 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
597 errmsg(
"COPY HEADER available only in CSV mode")));
602 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
603 errmsg(
"COPY quote available only in CSV mode")));
607 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
608 errmsg(
"COPY quote must be a single one-byte character")));
612 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
613 errmsg(
"COPY delimiter and quote must be different")));
618 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
619 errmsg(
"COPY escape available only in CSV mode")));
623 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
624 errmsg(
"COPY escape must be a single one-byte character")));
629 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
630 errmsg(
"COPY force quote available only in CSV mode")));
633 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
634 errmsg(
"COPY force quote only available using COPY TO")));
639 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
640 errmsg(
"COPY force not null available only in CSV mode")));
643 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
644 errmsg(
"COPY force not null only available using COPY FROM")));
649 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
650 errmsg(
"COPY force null available only in CSV mode")));
654 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
655 errmsg(
"COPY force null only available using COPY FROM")));
660 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
661 errmsg(
"COPY delimiter must not appear in the NULL specification")));
667 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
668 errmsg(
"CSV quote character must not appear in the NULL specification")));
690 if (attnamelist ==
NIL)
693 int attr_count = tupDesc->
natts;
696 for (i = 0; i < attr_count; i++)
710 foreach(l, attnamelist)
718 for (i = 0; i < tupDesc->
natts; i++)
722 if (att->attisdropped)
726 if (att->attgenerated)
728 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
729 errmsg(
"column \"%s\" is a generated column",
731 errdetail(
"Generated columns cannot be used in COPY.")));
732 attnum = att->attnum;
740 (
errcode(ERRCODE_UNDEFINED_COLUMN),
741 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
745 (
errcode(ERRCODE_UNDEFINED_COLUMN),
746 errmsg(
"column \"%s\" does not exist",
752 (
errcode(ERRCODE_DUPLICATE_COLUMN),
753 errmsg(
"column \"%s\" specified more than once",
#define IsA(nodeptr, _type_)
void table_close(Relation relation, LOCKMODE lockmode)
int errhint(const char *fmt,...)
int pg_char_to_encoding(const char *name)
#define RelationGetDescr(relation)
#define castNode(_type_, nodeptr)
#define TupleDescAttr(tupdesc, i)
uint64 CopyFrom(CopyFromState cstate)
char * pstrdup(const char *in)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
int errcode(int sqlerrcode)
int namestrcmp(Name name, const char *str)
Node * eval_const_expressions(PlannerInfo *root, Node *node)
#define FirstLowInvalidHeapAttributeNumber
void EndCopyTo(CopyToState cstate)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void assign_expr_collations(ParseState *pstate, Node *expr)
bool defGetBoolean(DefElem *def)
char * defGetString(DefElem *def)
#define lfirst_node(type, lc)
bool list_member_int(const List *list, int datum)
char * get_namespace_name(Oid nspid)
int errdetail(const char *fmt,...)
Expr * canonicalize_qual(Expr *qual, bool is_check)
#define select(n, r, w, e, timeout)
#define RelationGetRelationName(relation)
FormData_pg_attribute * Form_pg_attribute
List * lappend_int(List *list, int datum)
List * lappend(List *list, void *datum)
void * palloc0(Size size)
List * make_ands_implicit(Expr *clause)
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
#define ereport(elevel,...)
bool is_member_of_role(Oid member, Oid role)
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
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)
void DoCopy(ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
#define Assert(condition)
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
int parser_errposition(ParseState *pstate, int location)
Bitmapset * bms_add_member(Bitmapset *a, int x)
void PreventCommandIfReadOnly(const char *cmdname)
void EndCopyFrom(CopyFromState cstate)
#define InvalidAttrNumber
int errmsg(const char *fmt,...)
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
uint64 DoCopyTo(CopyToState cstate)
#define RelationGetRelid(relation)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
#define RelationGetNamespace(relation)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)