63 int stmt_location,
int stmt_len,
66 bool is_from =
stmt->is_from;
67 bool pipe = (
stmt->filename == NULL);
71 Node *whereClause = NULL;
83 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
84 errmsg(
"permission denied to COPY to or from an external program"),
85 errdetail(
"Only roles with privileges of the \"%s\" role may COPY to or from an external program.",
86 "pg_execute_server_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(
"permission denied to COPY from a file"),
96 errdetail(
"Only roles with privileges of the \"%s\" role may COPY from a file.",
97 "pg_read_server_files"),
98 errhint(
"Anyone can COPY to stdout or from stdin. "
99 "psql's \\copy command also works for anyone.")));
103 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
104 errmsg(
"permission denied to COPY to a file"),
105 errdetail(
"Only roles with privileges of the \"%s\" role may COPY to a file.",
106 "pg_write_server_files"),
107 errhint(
"Anyone can COPY to stdout or from stdin. "
108 "psql's \\copy command also works for anyone.")));
134 if (
stmt->whereClause)
181 errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
182 errmsg(
"generated columns are not supported in COPY FROM WHERE conditions"),
183 errdetail(
"Column \"%s\" is a generated column.",
195 foreach(
cur, attnums)
231 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
232 errmsg(
"COPY FROM not supported with row-level security"),
233 errhint(
"Use INSERT statements instead.")));
265 foreach(lc,
stmt->attlist)
284 targetList =
lappend(targetList, target);
301 from->
inh = (rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
305 select->targetList = targetList;
348 NULL,
stmt->attlist,
stmt->options);
358 NULL,
stmt->attlist,
stmt->options);
379 if (def->
arg == NULL)
394 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
395 errmsg(
"a negative integer value cannot be "
396 "specified for %s", def->
defname)));
398 if (!is_from && ival > 1)
400 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
401 errmsg(
"cannot use multi-line header in COPY TO")));
426 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
427 errmsg(
"cannot use \"%s\" with HEADER in COPY TO",
435 (
errcode(ERRCODE_SYNTAX_ERROR),
436 errmsg(
"%s requires a Boolean value, a non-negative integer, "
437 "or the string \"match\"",
452 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
455 errmsg(
"COPY %s cannot be used with %s",
"ON_ERROR",
"COPY TO"),
467 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
469 errmsg(
"COPY %s \"%s\" not recognized",
"ON_ERROR", sval),
486 if (def->
arg == NULL)
488 (
errcode(ERRCODE_SYNTAX_ERROR),
489 errmsg(
"%s requires a numeric value",
496 if (reject_limit <= 0)
498 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
499 errmsg(
"REJECT_LIMIT (%" PRId64
") must be greater than zero",
525 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
527 errmsg(
"COPY %s \"%s\" not recognized",
"LOG_VERBOSITY", sval),
554 bool format_specified =
false;
555 bool freeze_specified =
false;
556 bool header_specified =
false;
557 bool on_error_specified =
false;
558 bool log_verbosity_specified =
false;
559 bool reject_limit_specified =
false;
563 if (opts_out == NULL)
573 if (strcmp(defel->
defname,
"format") == 0)
577 if (format_specified)
579 format_specified =
true;
580 if (strcmp(fmt,
"text") == 0)
582 else if (strcmp(fmt,
"csv") == 0)
584 else if (strcmp(fmt,
"binary") == 0)
588 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
589 errmsg(
"COPY format \"%s\" not recognized", fmt),
592 else if (strcmp(defel->
defname,
"freeze") == 0)
594 if (freeze_specified)
596 freeze_specified =
true;
599 else if (strcmp(defel->
defname,
"delimiter") == 0)
605 else if (strcmp(defel->
defname,
"null") == 0)
611 else if (strcmp(defel->
defname,
"default") == 0)
617 else if (strcmp(defel->
defname,
"header") == 0)
619 if (header_specified)
621 header_specified =
true;
624 else if (strcmp(defel->
defname,
"quote") == 0)
630 else if (strcmp(defel->
defname,
"escape") == 0)
636 else if (strcmp(defel->
defname,
"force_quote") == 0)
646 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
647 errmsg(
"argument to option \"%s\" must be a list of column names",
651 else if (strcmp(defel->
defname,
"force_not_null") == 0)
661 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
662 errmsg(
"argument to option \"%s\" must be a list of column names",
666 else if (strcmp(defel->
defname,
"force_null") == 0)
676 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
677 errmsg(
"argument to option \"%s\" must be a list of column names",
681 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
695 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
696 errmsg(
"argument to option \"%s\" must be a list of column names",
700 else if (strcmp(defel->
defname,
"encoding") == 0)
707 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
708 errmsg(
"argument to option \"%s\" must be a valid encoding name",
712 else if (strcmp(defel->
defname,
"on_error") == 0)
714 if (on_error_specified)
716 on_error_specified =
true;
719 else if (strcmp(defel->
defname,
"log_verbosity") == 0)
721 if (log_verbosity_specified)
723 log_verbosity_specified =
true;
726 else if (strcmp(defel->
defname,
"reject_limit") == 0)
728 if (reject_limit_specified)
730 reject_limit_specified =
true;
735 (
errcode(ERRCODE_SYNTAX_ERROR),
736 errmsg(
"option \"%s\" not recognized",
747 (
errcode(ERRCODE_SYNTAX_ERROR),
749 errmsg(
"cannot specify %s in BINARY mode",
"DELIMITER")));
753 (
errcode(ERRCODE_SYNTAX_ERROR),
754 errmsg(
"cannot specify %s in BINARY mode",
"NULL")));
758 (
errcode(ERRCODE_SYNTAX_ERROR),
759 errmsg(
"cannot specify %s in BINARY mode",
"DEFAULT")));
762 if (!opts_out->
delim)
771 if (!opts_out->
quote)
772 opts_out->
quote =
"\"";
778 if (strlen(opts_out->
delim) != 1)
780 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
781 errmsg(
"COPY delimiter must be a single one-byte character")));
784 if (strchr(opts_out->
delim,
'\r') != NULL ||
785 strchr(opts_out->
delim,
'\n') != NULL)
787 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
788 errmsg(
"COPY delimiter cannot be newline or carriage return")));
790 if (strchr(opts_out->
null_print,
'\r') != NULL ||
793 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
794 errmsg(
"COPY null representation cannot use newline or carriage return")));
803 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
804 errmsg(
"COPY default representation cannot use newline or carriage return")));
818 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
819 opts_out->
delim[0]) != NULL)
821 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
822 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
827 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
829 errmsg(
"cannot specify %s in BINARY mode",
"HEADER")));
834 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
836 errmsg(
"COPY %s requires CSV mode",
"QUOTE")));
840 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
841 errmsg(
"COPY quote must be a single one-byte character")));
845 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
846 errmsg(
"COPY delimiter and quote must be different")));
851 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
853 errmsg(
"COPY %s requires CSV mode",
"ESCAPE")));
857 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
858 errmsg(
"COPY escape must be a single one-byte character")));
863 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
865 errmsg(
"COPY %s requires CSV mode",
"FORCE_QUOTE")));
868 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
871 errmsg(
"COPY %s cannot be used with %s",
"FORCE_QUOTE",
878 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
880 errmsg(
"COPY %s requires CSV mode",
"FORCE_NOT_NULL")));
884 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
887 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NOT_NULL",
894 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
896 errmsg(
"COPY %s requires CSV mode",
"FORCE_NULL")));
901 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
904 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NULL",
910 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
912 errmsg(
"COPY delimiter character must not appear in the %s specification",
919 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
921 errmsg(
"CSV quote character must not appear in the %s specification",
925 if (opts_out->
freeze && !is_from)
927 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
930 errmsg(
"COPY %s cannot be used with %s",
"FREEZE",
937 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
940 errmsg(
"COPY %s cannot be used with %s",
"DEFAULT",
946 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
948 errmsg(
"COPY delimiter character must not appear in the %s specification",
955 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
957 errmsg(
"CSV quote character must not appear in the %s specification",
965 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
966 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
971 (
errcode(ERRCODE_SYNTAX_ERROR),
972 errmsg(
"only ON_ERROR STOP is allowed in BINARY mode")));
976 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
979 errmsg(
"COPY %s requires %s to be set to %s",
980 "REJECT_LIMIT",
"ON_ERROR",
"IGNORE")));
1002 if (attnamelist ==
NIL)
1005 int attr_count = tupDesc->
natts;
1008 for (
i = 0;
i < attr_count;
i++)
1022 foreach(l, attnamelist)
1030 for (
i = 0;
i < tupDesc->
natts;
i++)
1034 if (att->attisdropped)
1038 if (att->attgenerated)
1040 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1041 errmsg(
"column \"%s\" is a generated column",
1043 errdetail(
"Generated columns cannot be used in COPY.")));
1052 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1053 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1057 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1058 errmsg(
"column \"%s\" does not exist",
1064 (
errcode(ERRCODE_DUPLICATE_COLUMN),
1065 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)
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)
void * palloc0(Size size)
int namestrcmp(Name name, const char *str)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
int64 pg_strtoint64(const char *s)
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)