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),
439 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
441 errmsg(
"COPY %s \"%s\" not recognized",
"LOG_VERBOSITY", sval),
468 bool format_specified =
false;
469 bool freeze_specified =
false;
470 bool header_specified =
false;
471 bool on_error_specified =
false;
472 bool log_verbosity_specified =
false;
476 if (opts_out == NULL)
486 if (strcmp(defel->
defname,
"format") == 0)
490 if (format_specified)
492 format_specified =
true;
493 if (strcmp(
fmt,
"text") == 0)
495 else if (strcmp(
fmt,
"csv") == 0)
497 else if (strcmp(
fmt,
"binary") == 0)
501 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
502 errmsg(
"COPY format \"%s\" not recognized",
fmt),
505 else if (strcmp(defel->
defname,
"freeze") == 0)
507 if (freeze_specified)
509 freeze_specified =
true;
512 else if (strcmp(defel->
defname,
"delimiter") == 0)
518 else if (strcmp(defel->
defname,
"null") == 0)
524 else if (strcmp(defel->
defname,
"default") == 0)
530 else if (strcmp(defel->
defname,
"header") == 0)
532 if (header_specified)
534 header_specified =
true;
537 else if (strcmp(defel->
defname,
"quote") == 0)
543 else if (strcmp(defel->
defname,
"escape") == 0)
549 else if (strcmp(defel->
defname,
"force_quote") == 0)
559 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
560 errmsg(
"argument to option \"%s\" must be a list of column names",
564 else if (strcmp(defel->
defname,
"force_not_null") == 0)
574 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
575 errmsg(
"argument to option \"%s\" must be a list of column names",
579 else if (strcmp(defel->
defname,
"force_null") == 0)
589 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
590 errmsg(
"argument to option \"%s\" must be a list of column names",
594 else if (strcmp(defel->
defname,
"convert_selectively") == 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,
"encoding") == 0)
620 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
621 errmsg(
"argument to option \"%s\" must be a valid encoding name",
625 else if (strcmp(defel->
defname,
"on_error") == 0)
627 if (on_error_specified)
629 on_error_specified =
true;
632 else if (strcmp(defel->
defname,
"log_verbosity") == 0)
634 if (log_verbosity_specified)
636 log_verbosity_specified =
true;
641 (
errcode(ERRCODE_SYNTAX_ERROR),
642 errmsg(
"option \"%s\" not recognized",
653 (
errcode(ERRCODE_SYNTAX_ERROR),
655 errmsg(
"cannot specify %s in BINARY mode",
"DELIMITER")));
659 (
errcode(ERRCODE_SYNTAX_ERROR),
660 errmsg(
"cannot specify %s in BINARY mode",
"NULL")));
664 (
errcode(ERRCODE_SYNTAX_ERROR),
665 errmsg(
"cannot specify %s in BINARY mode",
"DEFAULT")));
669 (
errcode(ERRCODE_SYNTAX_ERROR),
670 errmsg(
"only ON_ERROR STOP is allowed in BINARY mode")));
673 if (!opts_out->
delim)
682 if (!opts_out->
quote)
683 opts_out->
quote =
"\"";
689 if (strlen(opts_out->
delim) != 1)
691 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
692 errmsg(
"COPY delimiter must be a single one-byte character")));
695 if (strchr(opts_out->
delim,
'\r') != NULL ||
696 strchr(opts_out->
delim,
'\n') != NULL)
698 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
699 errmsg(
"COPY delimiter cannot be newline or carriage return")));
701 if (strchr(opts_out->
null_print,
'\r') != NULL ||
704 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
705 errmsg(
"COPY null representation cannot use newline or carriage return")));
714 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
715 errmsg(
"COPY default representation cannot use newline or carriage return")));
729 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
730 opts_out->
delim[0]) != NULL)
732 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
733 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
738 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
740 errmsg(
"cannot specify %s in BINARY mode",
"HEADER")));
745 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
747 errmsg(
"COPY %s requires CSV mode",
"QUOTE")));
751 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
752 errmsg(
"COPY quote must be a single one-byte character")));
756 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
757 errmsg(
"COPY delimiter and quote must be different")));
762 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
764 errmsg(
"COPY %s requires CSV mode",
"ESCAPE")));
768 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
769 errmsg(
"COPY escape must be a single one-byte character")));
774 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
776 errmsg(
"COPY %s requires CSV mode",
"FORCE_QUOTE")));
779 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
782 errmsg(
"COPY %s cannot be used with %s",
"FORCE_QUOTE",
788 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
790 errmsg(
"COPY %s requires CSV mode",
"FORCE_NOT_NULL")));
793 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
796 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NOT_NULL",
802 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
804 errmsg(
"COPY %s requires CSV mode",
"FORCE_NULL")));
808 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
811 errmsg(
"COPY %s cannot be used with %s",
"FORCE_NULL",
817 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
819 errmsg(
"COPY delimiter character must not appear in the %s specification",
826 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
828 errmsg(
"CSV quote character must not appear in the %s specification",
832 if (opts_out->
freeze && !is_from)
834 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
837 errmsg(
"COPY %s cannot be used with %s",
"FREEZE",
844 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
847 errmsg(
"COPY %s cannot be used with %s",
"DEFAULT",
853 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
855 errmsg(
"COPY delimiter character must not appear in the %s specification",
862 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
864 errmsg(
"CSV quote character must not appear in the %s specification",
872 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
873 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
896 if (attnamelist ==
NIL)
899 int attr_count = tupDesc->
natts;
902 for (
i = 0;
i < attr_count;
i++)
916 foreach(l, attnamelist)
924 for (
i = 0;
i < tupDesc->
natts;
i++)
928 if (att->attisdropped)
932 if (att->attgenerated)
934 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
935 errmsg(
"column \"%s\" is a generated column",
937 errdetail(
"Generated columns cannot be used in COPY.")));
946 (
errcode(ERRCODE_UNDEFINED_COLUMN),
947 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
951 (
errcode(ERRCODE_UNDEFINED_COLUMN),
952 errmsg(
"column \"%s\" does not exist",
958 (
errcode(ERRCODE_DUPLICATE_COLUMN),
959 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 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)
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_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)
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)