64 int stmt_location,
int stmt_len,
67 bool is_from =
stmt->is_from;
68 bool pipe = (
stmt->filename == NULL);
72 Node *whereClause = NULL;
84 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
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.")));
95 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
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.")));
104 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
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.")));
135 if (
stmt->whereClause)
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.",
196 foreach(
cur, attnums)
232 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
233 errmsg(
"COPY FROM not supported with row-level security"),
234 errhint(
"Use INSERT statements instead.")));
266 foreach(lc,
stmt->attlist)
285 targetList =
lappend(targetList, target);
302 from->
inh = (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
306 select->targetList = targetList;
349 NULL,
stmt->attlist,
stmt->options);
359 NULL,
stmt->attlist,
stmt->options);
383 if (def->
arg == NULL)
416 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
417 errmsg(
"cannot use \"%s\" with HEADER in COPY TO",
429 (
errcode(ERRCODE_SYNTAX_ERROR),
432 errmsg(
"%s requires a Boolean value, an integer "
433 "value greater than or equal to zero, "
434 "or the string \"%s\"",
443 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
444 errmsg(
"a negative integer value cannot be "
445 "specified for %s", def->
defname)));
447 if (!is_from && ival > 1)
449 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
450 errmsg(
"cannot use multi-line header in COPY TO")));
465 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
468 errmsg(
"COPY %s cannot be used with %s",
"ON_ERROR",
"COPY TO"),
480 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
482 errmsg(
"COPY %s \"%s\" not recognized",
"ON_ERROR", sval),
499 if (def->
arg == NULL)
501 (
errcode(ERRCODE_SYNTAX_ERROR),
502 errmsg(
"%s requires a numeric value",
509 if (reject_limit <= 0)
511 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
512 errmsg(
"REJECT_LIMIT (%" PRId64
") must be greater than zero",
538 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
540 errmsg(
"COPY %s \"%s\" not recognized",
"LOG_VERBOSITY", sval),
567 bool format_specified =
false;
568 bool freeze_specified =
false;
569 bool header_specified =
false;
570 bool on_error_specified =
false;
571 bool log_verbosity_specified =
false;
572 bool reject_limit_specified =
false;
576 if (opts_out == NULL)
586 if (strcmp(defel->
defname,
"format") == 0)
590 if (format_specified)
592 format_specified =
true;
593 if (strcmp(fmt,
"text") == 0)
595 else if (strcmp(fmt,
"csv") == 0)
597 else if (strcmp(fmt,
"binary") == 0)
601 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
602 errmsg(
"COPY format \"%s\" not recognized", fmt),
605 else if (strcmp(defel->
defname,
"freeze") == 0)
607 if (freeze_specified)
609 freeze_specified =
true;
612 else if (strcmp(defel->
defname,
"delimiter") == 0)
618 else if (strcmp(defel->
defname,
"null") == 0)
624 else if (strcmp(defel->
defname,
"default") == 0)
630 else if (strcmp(defel->
defname,
"header") == 0)
632 if (header_specified)
634 header_specified =
true;
637 else if (strcmp(defel->
defname,
"quote") == 0)
643 else if (strcmp(defel->
defname,
"escape") == 0)
649 else if (strcmp(defel->
defname,
"force_quote") == 0)
659 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
660 errmsg(
"argument to option \"%s\" must be a list of column names",
664 else if (strcmp(defel->
defname,
"force_not_null") == 0)
674 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
675 errmsg(
"argument to option \"%s\" must be a list of column names",
679 else if (strcmp(defel->
defname,
"force_null") == 0)
689 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
690 errmsg(
"argument to option \"%s\" must be a list of column names",
694 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
708 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
709 errmsg(
"argument to option \"%s\" must be a list of column names",
713 else if (strcmp(defel->
defname,
"encoding") == 0)
720 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
721 errmsg(
"argument to option \"%s\" must be a valid encoding name",
725 else if (strcmp(defel->
defname,
"on_error") == 0)
727 if (on_error_specified)
729 on_error_specified =
true;
732 else if (strcmp(defel->
defname,
"log_verbosity") == 0)
734 if (log_verbosity_specified)
736 log_verbosity_specified =
true;
739 else if (strcmp(defel->
defname,
"reject_limit") == 0)
741 if (reject_limit_specified)
743 reject_limit_specified =
true;
748 (
errcode(ERRCODE_SYNTAX_ERROR),
749 errmsg(
"option \"%s\" not recognized",
760 (
errcode(ERRCODE_SYNTAX_ERROR),
762 errmsg(
"cannot specify %s in BINARY mode",
"DELIMITER")));
766 (
errcode(ERRCODE_SYNTAX_ERROR),
767 errmsg(
"cannot specify %s in BINARY mode",
"NULL")));
771 (
errcode(ERRCODE_SYNTAX_ERROR),
772 errmsg(
"cannot specify %s in BINARY mode",
"DEFAULT")));
775 if (!opts_out->
delim)
784 if (!opts_out->
quote)
785 opts_out->
quote =
"\"";
791 if (strlen(opts_out->
delim) != 1)
793 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
794 errmsg(
"COPY delimiter must be a single one-byte character")));
797 if (strchr(opts_out->
delim,
'\r') != NULL ||
798 strchr(opts_out->
delim,
'\n') != NULL)
800 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
801 errmsg(
"COPY delimiter cannot be newline or carriage return")));
803 if (strchr(opts_out->
null_print,
'\r') != NULL ||
806 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
807 errmsg(
"COPY null representation cannot use newline or carriage return")));
816 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
817 errmsg(
"COPY default representation cannot use newline or carriage return")));
831 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
832 opts_out->
delim[0]) != NULL)
834 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
835 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
840 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
842 errmsg(
"cannot specify %s in BINARY mode",
"HEADER")));
847 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
849 errmsg(
"COPY %s requires CSV mode",
"QUOTE")));
853 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
854 errmsg(
"COPY quote must be a single one-byte character")));
858 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
859 errmsg(
"COPY delimiter and quote must be different")));
864 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
866 errmsg(
"COPY %s requires CSV mode",
"ESCAPE")));
870 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
871 errmsg(
"COPY escape must be a single one-byte character")));
876 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
878 errmsg(
"COPY %s requires CSV mode",
"FORCE_QUOTE")));
881 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
884 errmsg(
"COPY %s cannot be used with %s",
"FORCE_QUOTE",
891 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
893 errmsg(
"COPY %s requires CSV mode",
"FORCE_NOT_NULL")));
897 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
900 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NOT_NULL",
907 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
909 errmsg(
"COPY %s requires CSV mode",
"FORCE_NULL")));
914 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
917 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NULL",
923 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
925 errmsg(
"COPY delimiter character must not appear in the %s specification",
932 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
934 errmsg(
"CSV quote character must not appear in the %s specification",
938 if (opts_out->
freeze && !is_from)
940 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
943 errmsg(
"COPY %s cannot be used with %s",
"FREEZE",
950 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
953 errmsg(
"COPY %s cannot be used with %s",
"DEFAULT",
959 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
961 errmsg(
"COPY delimiter character must not appear in the %s specification",
968 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
970 errmsg(
"CSV quote character must not appear in the %s specification",
978 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
979 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
984 (
errcode(ERRCODE_SYNTAX_ERROR),
985 errmsg(
"only ON_ERROR STOP is allowed in BINARY mode")));
989 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
992 errmsg(
"COPY %s requires %s to be set to %s",
993 "REJECT_LIMIT",
"ON_ERROR",
"IGNORE")));
1015 if (attnamelist ==
NIL)
1018 int attr_count = tupDesc->
natts;
1021 for (
i = 0;
i < attr_count;
i++)
1035 foreach(l, attnamelist)
1043 for (
i = 0;
i < tupDesc->
natts;
i++)
1047 if (att->attisdropped)
1051 if (att->attgenerated)
1053 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1054 errmsg(
"column \"%s\" is a generated column",
1056 errdetail(
"Generated columns cannot be used in COPY.")));
1065 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1066 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1070 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1071 errmsg(
"column \"%s\" does not exist",
1077 (
errcode(ERRCODE_DUPLICATE_COLUMN),
1078 errmsg(
"column \"%s\" specified more than once",
bool has_privs_of_role(Oid member, Oid role)
#define InvalidAttrNumber
void DoCopy(ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
static int defGetCopyHeaderOption(DefElem *def, bool is_from)
static int64 defGetCopyRejectLimitOption(DefElem *def)
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
static CopyOnErrorChoice defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
static CopyLogVerbosityChoice defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
int bms_next_member(const Bitmapset *a, int prevbit)
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Bitmapset * bms_del_member(Bitmapset *a, int x)
bool bms_is_member(int x, const Bitmapset *a)
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)
char * defGetString(DefElem *def)
bool defGetBoolean(DefElem *def)
int64 defGetInt64(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)
#define palloc0_object(type)
Assert(PointerIsAligned(start, uint64))
@ COPY_LOG_VERBOSITY_SILENT
@ COPY_LOG_VERBOSITY_VERBOSE
@ COPY_LOG_VERBOSITY_DEFAULT
#define COPY_HEADER_MATCH
#define COPY_HEADER_FALSE
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
bool list_member_int(const List *list, int datum)
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
char * get_namespace_name(Oid nspid)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
List * make_ands_implicit(Expr *clause)
char * pstrdup(const char *in)
int namestrcmp(Name name, const char *str)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
int64 pg_strtoint64(const char *s)
int32 pg_strtoint32_safe(const char *s, Node *escontext)
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 RelationGetNumberOfAttributes(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)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
void PreventCommandIfReadOnly(const char *cmdname)
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
#define select(n, r, w, e, timeout)