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),
400 errmsg(
"COPY ON_ERROR cannot be used with COPY TO"),
406 if (def->
arg == NULL)
419 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
420 errmsg(
"COPY ON_ERROR \"%s\" not recognized", sval),
447 bool format_specified =
false;
448 bool freeze_specified =
false;
449 bool header_specified =
false;
450 bool on_error_specified =
false;
454 if (opts_out == NULL)
464 if (strcmp(defel->
defname,
"format") == 0)
468 if (format_specified)
470 format_specified =
true;
471 if (strcmp(
fmt,
"text") == 0)
473 else if (strcmp(
fmt,
"csv") == 0)
475 else if (strcmp(
fmt,
"binary") == 0)
479 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
480 errmsg(
"COPY format \"%s\" not recognized",
fmt),
483 else if (strcmp(defel->
defname,
"freeze") == 0)
485 if (freeze_specified)
487 freeze_specified =
true;
490 else if (strcmp(defel->
defname,
"delimiter") == 0)
496 else if (strcmp(defel->
defname,
"null") == 0)
502 else if (strcmp(defel->
defname,
"default") == 0)
508 else if (strcmp(defel->
defname,
"header") == 0)
510 if (header_specified)
512 header_specified =
true;
515 else if (strcmp(defel->
defname,
"quote") == 0)
521 else if (strcmp(defel->
defname,
"escape") == 0)
527 else if (strcmp(defel->
defname,
"force_quote") == 0)
537 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
538 errmsg(
"argument to option \"%s\" must be a list of column names",
542 else if (strcmp(defel->
defname,
"force_not_null") == 0)
552 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
553 errmsg(
"argument to option \"%s\" must be a list of column names",
557 else if (strcmp(defel->
defname,
"force_null") == 0)
567 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
568 errmsg(
"argument to option \"%s\" must be a list of column names",
572 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
586 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
587 errmsg(
"argument to option \"%s\" must be a list of column names",
591 else if (strcmp(defel->
defname,
"encoding") == 0)
598 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
599 errmsg(
"argument to option \"%s\" must be a valid encoding name",
603 else if (strcmp(defel->
defname,
"on_error") == 0)
605 if (on_error_specified)
607 on_error_specified =
true;
612 (
errcode(ERRCODE_SYNTAX_ERROR),
613 errmsg(
"option \"%s\" not recognized",
624 (
errcode(ERRCODE_SYNTAX_ERROR),
625 errmsg(
"cannot specify DELIMITER in BINARY mode")));
629 (
errcode(ERRCODE_SYNTAX_ERROR),
630 errmsg(
"cannot specify NULL in BINARY mode")));
634 (
errcode(ERRCODE_SYNTAX_ERROR),
635 errmsg(
"cannot specify DEFAULT in BINARY mode")));
639 (
errcode(ERRCODE_SYNTAX_ERROR),
640 errmsg(
"only ON_ERROR STOP is allowed in BINARY mode")));
643 if (!opts_out->
delim)
652 if (!opts_out->
quote)
653 opts_out->
quote =
"\"";
659 if (strlen(opts_out->
delim) != 1)
661 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
662 errmsg(
"COPY delimiter must be a single one-byte character")));
665 if (strchr(opts_out->
delim,
'\r') != NULL ||
666 strchr(opts_out->
delim,
'\n') != NULL)
668 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
669 errmsg(
"COPY delimiter cannot be newline or carriage return")));
671 if (strchr(opts_out->
null_print,
'\r') != NULL ||
674 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
675 errmsg(
"COPY null representation cannot use newline or carriage return")));
684 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
685 errmsg(
"COPY default representation cannot use newline or carriage return")));
699 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
700 opts_out->
delim[0]) != NULL)
702 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
703 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
708 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
709 errmsg(
"cannot specify HEADER in BINARY mode")));
714 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
715 errmsg(
"COPY QUOTE requires CSV mode")));
719 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
720 errmsg(
"COPY quote must be a single one-byte character")));
724 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
725 errmsg(
"COPY delimiter and quote must be different")));
730 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
731 errmsg(
"COPY ESCAPE requires CSV mode")));
735 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
736 errmsg(
"COPY escape must be a single one-byte character")));
741 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
742 errmsg(
"COPY FORCE_QUOTE requires CSV mode")));
745 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
746 errmsg(
"COPY FORCE_QUOTE cannot be used with COPY FROM")));
751 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
752 errmsg(
"COPY FORCE_NOT_NULL requires CSV mode")));
755 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
756 errmsg(
"COPY FORCE_NOT_NULL cannot be used with COPY TO")));
761 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
762 errmsg(
"COPY FORCE_NULL requires CSV mode")));
766 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
767 errmsg(
"COPY FORCE_NULL cannot be used with COPY TO")));
772 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
773 errmsg(
"COPY delimiter character must not appear in the NULL specification")));
779 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
780 errmsg(
"CSV quote character must not appear in the NULL specification")));
783 if (opts_out->
freeze && !is_from)
785 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
786 errmsg(
"COPY FREEZE cannot be used with COPY TO")));
792 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
793 errmsg(
"COPY DEFAULT only available using COPY FROM")));
798 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
799 errmsg(
"COPY delimiter must not appear in the DEFAULT specification")));
805 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
806 errmsg(
"CSV quote character must not appear in the DEFAULT specification")));
813 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
814 errmsg(
"NULL specification and DEFAULT specification cannot be the same")));
837 if (attnamelist ==
NIL)
840 int attr_count = tupDesc->
natts;
843 for (
i = 0;
i < attr_count;
i++)
857 foreach(l, attnamelist)
865 for (
i = 0;
i < tupDesc->
natts;
i++)
869 if (att->attisdropped)
873 if (att->attgenerated)
875 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
876 errmsg(
"column \"%s\" is a generated column",
878 errdetail(
"Generated columns cannot be used in COPY.")));
887 (
errcode(ERRCODE_UNDEFINED_COLUMN),
888 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
892 (
errcode(ERRCODE_UNDEFINED_COLUMN),
893 errmsg(
"column \"%s\" does not exist",
899 (
errcode(ERRCODE_DUPLICATE_COLUMN),
900 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)
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,...)
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)
#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)