65 int stmt_location,
int stmt_len,
68 bool is_from =
stmt->is_from;
69 bool pipe = (
stmt->filename == NULL);
73 Node *whereClause = NULL;
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.")));
96 (
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.")));
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 errhint(
"Anyone can COPY to stdout or from stdin. "
110 "psql's \\copy command also works for anyone.")));
136 if (
stmt->whereClause)
158 foreach(
cur, attnums)
194 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
195 errmsg(
"COPY FROM not supported with row-level security"),
196 errhint(
"Use INSERT statements instead.")));
228 foreach(lc,
stmt->attlist)
247 targetList =
lappend(targetList, target);
264 select->targetList = targetList;
289 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
290 errmsg(
"MERGE not supported in COPY"));
313 NULL,
stmt->attlist,
stmt->options);
323 NULL,
stmt->attlist,
stmt->options);
342 if (def->
arg == NULL)
382 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
383 errmsg(
"cannot use \"%s\" with HEADER in COPY TO",
391 (
errcode(ERRCODE_SYNTAX_ERROR),
392 errmsg(
"%s requires a Boolean value or \"match\"",
419 bool format_specified =
false;
420 bool freeze_specified =
false;
421 bool header_specified =
false;
425 if (opts_out == NULL)
435 if (strcmp(defel->
defname,
"format") == 0)
439 if (format_specified)
441 format_specified =
true;
442 if (strcmp(
fmt,
"text") == 0)
444 else if (strcmp(
fmt,
"csv") == 0)
446 else if (strcmp(
fmt,
"binary") == 0)
450 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
451 errmsg(
"COPY format \"%s\" not recognized",
fmt),
454 else if (strcmp(defel->
defname,
"freeze") == 0)
456 if (freeze_specified)
458 freeze_specified =
true;
461 else if (strcmp(defel->
defname,
"delimiter") == 0)
467 else if (strcmp(defel->
defname,
"null") == 0)
473 else if (strcmp(defel->
defname,
"default") == 0)
479 else if (strcmp(defel->
defname,
"header") == 0)
481 if (header_specified)
483 header_specified =
true;
486 else if (strcmp(defel->
defname,
"quote") == 0)
492 else if (strcmp(defel->
defname,
"escape") == 0)
498 else if (strcmp(defel->
defname,
"force_quote") == 0)
508 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
509 errmsg(
"argument to option \"%s\" must be a list of column names",
513 else if (strcmp(defel->
defname,
"force_not_null") == 0)
523 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
524 errmsg(
"argument to option \"%s\" must be a list of column names",
528 else if (strcmp(defel->
defname,
"force_null") == 0)
538 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
539 errmsg(
"argument to option \"%s\" must be a list of column names",
543 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
557 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
558 errmsg(
"argument to option \"%s\" must be a list of column names",
562 else if (strcmp(defel->
defname,
"encoding") == 0)
569 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
570 errmsg(
"argument to option \"%s\" must be a valid encoding name",
576 (
errcode(ERRCODE_SYNTAX_ERROR),
577 errmsg(
"option \"%s\" not recognized",
588 (
errcode(ERRCODE_SYNTAX_ERROR),
589 errmsg(
"cannot specify DELIMITER in BINARY mode")));
593 (
errcode(ERRCODE_SYNTAX_ERROR),
594 errmsg(
"cannot specify NULL in BINARY mode")));
598 (
errcode(ERRCODE_SYNTAX_ERROR),
599 errmsg(
"cannot specify DEFAULT in BINARY mode")));
602 if (!opts_out->
delim)
611 if (!opts_out->
quote)
612 opts_out->
quote =
"\"";
618 if (strlen(opts_out->
delim) != 1)
620 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
621 errmsg(
"COPY delimiter must be a single one-byte character")));
624 if (strchr(opts_out->
delim,
'\r') != NULL ||
625 strchr(opts_out->
delim,
'\n') != NULL)
627 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
628 errmsg(
"COPY delimiter cannot be newline or carriage return")));
630 if (strchr(opts_out->
null_print,
'\r') != NULL ||
633 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
634 errmsg(
"COPY null representation cannot use newline or carriage return")));
643 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
644 errmsg(
"COPY default representation cannot use newline or carriage return")));
658 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
659 opts_out->
delim[0]) != NULL)
661 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
662 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
667 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
668 errmsg(
"cannot specify HEADER in BINARY mode")));
673 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
674 errmsg(
"COPY QUOTE requires CSV mode")));
678 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
679 errmsg(
"COPY quote must be a single one-byte character")));
683 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
684 errmsg(
"COPY delimiter and quote must be different")));
689 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
690 errmsg(
"COPY ESCAPE requires CSV mode")));
694 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
695 errmsg(
"COPY escape must be a single one-byte character")));
700 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
701 errmsg(
"COPY FORCE_QUOTE requires CSV mode")));
704 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
705 errmsg(
"COPY FORCE_QUOTE cannot be used with COPY FROM")));
710 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
711 errmsg(
"COPY FORCE_NOT_NULL requires CSV mode")));
714 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
715 errmsg(
"COPY FORCE_NOT_NULL cannot be used with COPY TO")));
720 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
721 errmsg(
"COPY FORCE_NULL requires CSV mode")));
725 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
726 errmsg(
"COPY FORCE_NULL cannot be used with COPY TO")));
731 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
732 errmsg(
"COPY delimiter character must not appear in the NULL specification")));
738 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
739 errmsg(
"CSV quote character must not appear in the NULL specification")));
742 if (opts_out->
freeze && !is_from)
744 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
745 errmsg(
"COPY FREEZE cannot be used with COPY TO")));
751 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
752 errmsg(
"COPY DEFAULT only available using COPY FROM")));
757 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
758 errmsg(
"COPY delimiter must not appear in the DEFAULT specification")));
764 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
765 errmsg(
"CSV quote character must not appear in the DEFAULT specification")));
772 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
773 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
796 if (attnamelist ==
NIL)
799 int attr_count = tupDesc->
natts;
802 for (
i = 0;
i < attr_count;
i++)
816 foreach(l, attnamelist)
824 for (
i = 0;
i < tupDesc->
natts;
i++)
828 if (att->attisdropped)
832 if (att->attgenerated)
834 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
835 errmsg(
"column \"%s\" is a generated column",
837 errdetail(
"Generated columns cannot be used in COPY.")));
846 (
errcode(ERRCODE_UNDEFINED_COLUMN),
847 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
851 (
errcode(ERRCODE_UNDEFINED_COLUMN),
852 errmsg(
"column \"%s\" does not exist",
858 (
errcode(ERRCODE_DUPLICATE_COLUMN),
859 errmsg(
"column \"%s\" specified more than once",
bool has_privs_of_role(Oid member, Oid role)
#define InvalidAttrNumber
static CopyHeaderChoice defGetCopyHeaderChoice(DefElem *def, bool is_from)
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
void DoCopy(ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Node * eval_const_expressions(PlannerInfo *root, Node *node)
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)
uint64 CopyFrom(CopyFromState cstate)
void EndCopyFrom(CopyFromState cstate)
uint64 DoCopyTo(CopyToState cstate)
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)
void EndCopyTo(CopyToState cstate)
bool defGetBoolean(DefElem *def)
char * defGetString(DefElem *def)
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool ExecCheckPermissions(List *rangeTable, List *rteperminfos, bool ereport_on_violation)
static void const char * fmt
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
bool list_member_int(const List *list, int datum)
char * get_namespace_name(Oid nspid)
List * make_ands_implicit(Expr *clause)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
char * pstrdup(const char *in)
void * palloc0(Size size)
int namestrcmp(Name name, const char *str)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
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)
int parser_errposition(ParseState *pstate, int location)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
FormData_pg_attribute * Form_pg_attribute
#define lfirst_node(type, lc)
#define pg_char_to_encoding
int pg_strcasecmp(const char *s1, const char *s2)
Expr * canonicalize_qual(Expr *qual, bool is_check)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define RelationGetNamespace(relation)
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
RTEPermissionInfo * p_perminfo
#define FirstLowInvalidHeapAttributeNumber
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
#define TupleDescAttr(tupdesc, i)
void PreventCommandIfReadOnly(const char *cmdname)
#define select(n, r, w, e, timeout)