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)
156 foreach(
cur, attnums)
192 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
193 errmsg(
"COPY FROM not supported with row-level security"),
194 errhint(
"Use INSERT statements instead.")));
226 foreach(lc,
stmt->attlist)
245 targetList =
lappend(targetList, target);
262 select->targetList = targetList;
305 NULL,
stmt->attlist,
stmt->options);
315 NULL,
stmt->attlist,
stmt->options);
334 if (def->
arg == NULL)
374 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
375 errmsg(
"cannot use \"%s\" with HEADER in COPY TO",
383 (
errcode(ERRCODE_SYNTAX_ERROR),
384 errmsg(
"%s requires a Boolean value or \"match\"",
399 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
402 errmsg(
"COPY %s cannot be used with %s",
"ON_ERROR",
"COPY TO"),
414 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
416 errmsg(
"COPY %s \"%s\" not recognized",
"ON_ERROR", sval),
433 if (def->
arg == NULL)
435 (
errcode(ERRCODE_SYNTAX_ERROR),
436 errmsg(
"%s requires a numeric value",
443 if (reject_limit <= 0)
445 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
446 errmsg(
"REJECT_LIMIT (%lld) must be greater than zero",
447 (
long long) reject_limit)));
472 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
474 errmsg(
"COPY %s \"%s\" not recognized",
"LOG_VERBOSITY", sval),
501 bool format_specified =
false;
502 bool freeze_specified =
false;
503 bool header_specified =
false;
504 bool on_error_specified =
false;
505 bool log_verbosity_specified =
false;
506 bool reject_limit_specified =
false;
510 if (opts_out == NULL)
520 if (strcmp(defel->
defname,
"format") == 0)
524 if (format_specified)
526 format_specified =
true;
527 if (strcmp(
fmt,
"text") == 0)
529 else if (strcmp(
fmt,
"csv") == 0)
531 else if (strcmp(
fmt,
"binary") == 0)
535 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
536 errmsg(
"COPY format \"%s\" not recognized",
fmt),
539 else if (strcmp(defel->
defname,
"freeze") == 0)
541 if (freeze_specified)
543 freeze_specified =
true;
546 else if (strcmp(defel->
defname,
"delimiter") == 0)
552 else if (strcmp(defel->
defname,
"null") == 0)
558 else if (strcmp(defel->
defname,
"default") == 0)
564 else if (strcmp(defel->
defname,
"header") == 0)
566 if (header_specified)
568 header_specified =
true;
571 else if (strcmp(defel->
defname,
"quote") == 0)
577 else if (strcmp(defel->
defname,
"escape") == 0)
583 else if (strcmp(defel->
defname,
"force_quote") == 0)
593 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
594 errmsg(
"argument to option \"%s\" must be a list of column names",
598 else if (strcmp(defel->
defname,
"force_not_null") == 0)
608 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
609 errmsg(
"argument to option \"%s\" must be a list of column names",
613 else if (strcmp(defel->
defname,
"force_null") == 0)
623 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
624 errmsg(
"argument to option \"%s\" must be a list of column names",
628 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
642 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
643 errmsg(
"argument to option \"%s\" must be a list of column names",
647 else if (strcmp(defel->
defname,
"encoding") == 0)
654 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
655 errmsg(
"argument to option \"%s\" must be a valid encoding name",
659 else if (strcmp(defel->
defname,
"on_error") == 0)
661 if (on_error_specified)
663 on_error_specified =
true;
666 else if (strcmp(defel->
defname,
"log_verbosity") == 0)
668 if (log_verbosity_specified)
670 log_verbosity_specified =
true;
673 else if (strcmp(defel->
defname,
"reject_limit") == 0)
675 if (reject_limit_specified)
677 reject_limit_specified =
true;
682 (
errcode(ERRCODE_SYNTAX_ERROR),
683 errmsg(
"option \"%s\" not recognized",
694 (
errcode(ERRCODE_SYNTAX_ERROR),
696 errmsg(
"cannot specify %s in BINARY mode",
"DELIMITER")));
700 (
errcode(ERRCODE_SYNTAX_ERROR),
701 errmsg(
"cannot specify %s in BINARY mode",
"NULL")));
705 (
errcode(ERRCODE_SYNTAX_ERROR),
706 errmsg(
"cannot specify %s in BINARY mode",
"DEFAULT")));
709 if (!opts_out->
delim)
718 if (!opts_out->
quote)
719 opts_out->
quote =
"\"";
725 if (strlen(opts_out->
delim) != 1)
727 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
728 errmsg(
"COPY delimiter must be a single one-byte character")));
731 if (strchr(opts_out->
delim,
'\r') != NULL ||
732 strchr(opts_out->
delim,
'\n') != NULL)
734 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
735 errmsg(
"COPY delimiter cannot be newline or carriage return")));
737 if (strchr(opts_out->
null_print,
'\r') != NULL ||
740 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
741 errmsg(
"COPY null representation cannot use newline or carriage return")));
750 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
751 errmsg(
"COPY default representation cannot use newline or carriage return")));
765 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
766 opts_out->
delim[0]) != NULL)
768 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
769 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
774 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
776 errmsg(
"cannot specify %s in BINARY mode",
"HEADER")));
781 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
783 errmsg(
"COPY %s requires CSV mode",
"QUOTE")));
787 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
788 errmsg(
"COPY quote must be a single one-byte character")));
792 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
793 errmsg(
"COPY delimiter and quote must be different")));
798 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
800 errmsg(
"COPY %s requires CSV mode",
"ESCAPE")));
804 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
805 errmsg(
"COPY escape must be a single one-byte character")));
810 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
812 errmsg(
"COPY %s requires CSV mode",
"FORCE_QUOTE")));
815 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
818 errmsg(
"COPY %s cannot be used with %s",
"FORCE_QUOTE",
825 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
827 errmsg(
"COPY %s requires CSV mode",
"FORCE_NOT_NULL")));
831 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
834 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NOT_NULL",
841 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
843 errmsg(
"COPY %s requires CSV mode",
"FORCE_NULL")));
848 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
851 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NULL",
857 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
859 errmsg(
"COPY delimiter character must not appear in the %s specification",
866 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
868 errmsg(
"CSV quote character must not appear in the %s specification",
872 if (opts_out->
freeze && !is_from)
874 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
877 errmsg(
"COPY %s cannot be used with %s",
"FREEZE",
884 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
887 errmsg(
"COPY %s cannot be used with %s",
"DEFAULT",
893 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
895 errmsg(
"COPY delimiter character must not appear in the %s specification",
902 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
904 errmsg(
"CSV quote character must not appear in the %s specification",
912 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
913 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
918 (
errcode(ERRCODE_SYNTAX_ERROR),
919 errmsg(
"only ON_ERROR STOP is allowed in BINARY mode")));
923 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
926 errmsg(
"COPY %s requires %s to be set to %s",
927 "REJECT_LIMIT",
"ON_ERROR",
"IGNORE")));
949 if (attnamelist ==
NIL)
952 int attr_count = tupDesc->
natts;
955 for (
i = 0;
i < attr_count;
i++)
969 foreach(l, attnamelist)
977 for (
i = 0;
i < tupDesc->
natts;
i++)
981 if (att->attisdropped)
985 if (att->attgenerated)
987 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
988 errmsg(
"column \"%s\" is a generated column",
990 errdetail(
"Generated columns cannot be used in COPY.")));
999 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1000 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1004 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1005 errmsg(
"column \"%s\" does not exist",
1011 (
errcode(ERRCODE_DUPLICATE_COLUMN),
1012 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)
static int64 defGetCopyRejectLimitOption(DefElem *def)
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)
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define Assert(condition)
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)
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)
@ COPY_LOG_VERBOSITY_SILENT
@ COPY_LOG_VERBOSITY_VERBOSE
@ COPY_LOG_VERBOSITY_DEFAULT
static void const char * fmt
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)
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 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)