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)
521 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
522 errmsg(
"argument to option \"%s\" must be a list of column names",
526 else if (strcmp(defel->
defname,
"force_null") == 0)
534 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
535 errmsg(
"argument to option \"%s\" must be a list of column names",
539 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
553 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
554 errmsg(
"argument to option \"%s\" must be a list of column names",
558 else if (strcmp(defel->
defname,
"encoding") == 0)
565 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
566 errmsg(
"argument to option \"%s\" must be a valid encoding name",
572 (
errcode(ERRCODE_SYNTAX_ERROR),
573 errmsg(
"option \"%s\" not recognized",
584 (
errcode(ERRCODE_SYNTAX_ERROR),
585 errmsg(
"cannot specify DELIMITER in BINARY mode")));
589 (
errcode(ERRCODE_SYNTAX_ERROR),
590 errmsg(
"cannot specify NULL in BINARY mode")));
594 (
errcode(ERRCODE_SYNTAX_ERROR),
595 errmsg(
"cannot specify DEFAULT in BINARY mode")));
598 if (!opts_out->
delim)
607 if (!opts_out->
quote)
608 opts_out->
quote =
"\"";
614 if (strlen(opts_out->
delim) != 1)
616 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
617 errmsg(
"COPY delimiter must be a single one-byte character")));
620 if (strchr(opts_out->
delim,
'\r') != NULL ||
621 strchr(opts_out->
delim,
'\n') != NULL)
623 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
624 errmsg(
"COPY delimiter cannot be newline or carriage return")));
626 if (strchr(opts_out->
null_print,
'\r') != NULL ||
629 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
630 errmsg(
"COPY null representation cannot use newline or carriage return")));
639 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
640 errmsg(
"COPY default representation cannot use newline or carriage return")));
654 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
655 opts_out->
delim[0]) != NULL)
657 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
658 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
663 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
664 errmsg(
"cannot specify HEADER in BINARY mode")));
669 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
670 errmsg(
"COPY quote available only in CSV mode")));
674 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
675 errmsg(
"COPY quote must be a single one-byte character")));
679 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
680 errmsg(
"COPY delimiter and quote must be different")));
685 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
686 errmsg(
"COPY escape available only in CSV mode")));
690 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
691 errmsg(
"COPY escape must be a single one-byte character")));
696 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
697 errmsg(
"COPY force quote available only in CSV mode")));
700 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
701 errmsg(
"COPY force quote only available using COPY TO")));
706 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
707 errmsg(
"COPY force not null available only in CSV mode")));
710 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
711 errmsg(
"COPY force not null only available using COPY FROM")));
716 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
717 errmsg(
"COPY force null available only in CSV mode")));
721 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
722 errmsg(
"COPY force null only available using COPY FROM")));
727 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
728 errmsg(
"COPY delimiter must not appear in the NULL specification")));
734 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
735 errmsg(
"CSV quote character must not appear in the NULL specification")));
741 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
742 errmsg(
"COPY DEFAULT only available using COPY FROM")));
747 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
748 errmsg(
"COPY delimiter must not appear in the DEFAULT specification")));
754 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
755 errmsg(
"CSV quote character must not appear in the DEFAULT specification")));
762 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
763 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
786 if (attnamelist ==
NIL)
789 int attr_count = tupDesc->
natts;
792 for (
i = 0;
i < attr_count;
i++)
806 foreach(l, attnamelist)
814 for (
i = 0;
i < tupDesc->
natts;
i++)
818 if (att->attisdropped)
822 if (att->attgenerated)
824 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
825 errmsg(
"column \"%s\" is a generated column",
827 errdetail(
"Generated columns cannot be used in COPY.")));
836 (
errcode(ERRCODE_UNDEFINED_COLUMN),
837 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
841 (
errcode(ERRCODE_UNDEFINED_COLUMN),
842 errmsg(
"column \"%s\" does not exist",
848 (
errcode(ERRCODE_DUPLICATE_COLUMN),
849 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,...)
int pg_char_to_encoding(const char *name)
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)
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)