65 int stmt_location,
int stmt_len,
69 bool pipe = (stmt->
filename == NULL);
73 Node *whereClause = NULL;
85 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
86 errmsg(
"must be superuser or have privileges of the pg_execute_server_program role to COPY to or from an external 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(
"must be superuser or have privileges of the pg_read_server_files role to COPY from a file"),
96 errhint(
"Anyone can COPY to stdout or from stdin. "
97 "psql's \\copy command also works for anyone.")));
101 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
102 errmsg(
"must be superuser or have privileges of the pg_write_server_files role to COPY to a file"),
103 errhint(
"Anyone can COPY to stdout or from stdin. "
104 "psql's \\copy command also works for anyone.")));
152 foreach(
cur, attnums)
188 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
189 errmsg(
"COPY FROM not supported with row-level security"),
190 errhint(
"Use INSERT statements instead.")));
241 targetList =
lappend(targetList, target);
255 select->targetList = targetList;
280 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
281 errmsg(
"MERGE not supported in COPY"));
333 if (def->
arg == NULL)
373 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
374 errmsg(
"cannot use \"%s\" with HEADER in COPY TO",
382 (
errcode(ERRCODE_SYNTAX_ERROR),
383 errmsg(
"%s requires a Boolean value or \"match\"",
410 bool format_specified =
false;
411 bool freeze_specified =
false;
412 bool header_specified =
false;
416 if (opts_out == NULL)
426 if (strcmp(defel->
defname,
"format") == 0)
430 if (format_specified)
432 format_specified =
true;
433 if (strcmp(
fmt,
"text") == 0)
435 else if (strcmp(
fmt,
"csv") == 0)
437 else if (strcmp(
fmt,
"binary") == 0)
441 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
442 errmsg(
"COPY format \"%s\" not recognized",
fmt),
445 else if (strcmp(defel->
defname,
"freeze") == 0)
447 if (freeze_specified)
449 freeze_specified =
true;
452 else if (strcmp(defel->
defname,
"delimiter") == 0)
458 else if (strcmp(defel->
defname,
"null") == 0)
464 else if (strcmp(defel->
defname,
"header") == 0)
466 if (header_specified)
468 header_specified =
true;
471 else if (strcmp(defel->
defname,
"quote") == 0)
477 else if (strcmp(defel->
defname,
"escape") == 0)
483 else if (strcmp(defel->
defname,
"force_quote") == 0)
493 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
494 errmsg(
"argument to option \"%s\" must be a list of column names",
498 else if (strcmp(defel->
defname,
"force_not_null") == 0)
506 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
507 errmsg(
"argument to option \"%s\" must be a list of column names",
511 else if (strcmp(defel->
defname,
"force_null") == 0)
519 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
520 errmsg(
"argument to option \"%s\" must be a list of column names",
524 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
538 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
539 errmsg(
"argument to option \"%s\" must be a list of column names",
543 else if (strcmp(defel->
defname,
"encoding") == 0)
550 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
551 errmsg(
"argument to option \"%s\" must be a valid encoding name",
557 (
errcode(ERRCODE_SYNTAX_ERROR),
558 errmsg(
"option \"%s\" not recognized",
569 (
errcode(ERRCODE_SYNTAX_ERROR),
570 errmsg(
"cannot specify DELIMITER in BINARY mode")));
574 (
errcode(ERRCODE_SYNTAX_ERROR),
575 errmsg(
"cannot specify NULL in BINARY mode")));
578 if (!opts_out->
delim)
587 if (!opts_out->
quote)
588 opts_out->
quote =
"\"";
594 if (strlen(opts_out->
delim) != 1)
596 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
597 errmsg(
"COPY delimiter must be a single one-byte character")));
600 if (strchr(opts_out->
delim,
'\r') != NULL ||
601 strchr(opts_out->
delim,
'\n') != NULL)
603 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
604 errmsg(
"COPY delimiter cannot be newline or carriage return")));
606 if (strchr(opts_out->
null_print,
'\r') != NULL ||
609 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
610 errmsg(
"COPY null representation cannot use newline or carriage return")));
623 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
624 opts_out->
delim[0]) != NULL)
626 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
627 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
632 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
633 errmsg(
"cannot specify HEADER in BINARY mode")));
638 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
639 errmsg(
"COPY quote available only in CSV mode")));
643 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
644 errmsg(
"COPY quote must be a single one-byte character")));
648 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
649 errmsg(
"COPY delimiter and quote must be different")));
654 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
655 errmsg(
"COPY escape available only in CSV mode")));
659 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
660 errmsg(
"COPY escape must be a single one-byte character")));
665 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
666 errmsg(
"COPY force quote available only in CSV mode")));
669 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
670 errmsg(
"COPY force quote only available using COPY TO")));
675 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
676 errmsg(
"COPY force not null available only in CSV mode")));
679 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
680 errmsg(
"COPY force not null only available using COPY FROM")));
685 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
686 errmsg(
"COPY force null available only in CSV mode")));
690 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
691 errmsg(
"COPY force null only available using COPY FROM")));
696 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
697 errmsg(
"COPY delimiter must not appear in the NULL specification")));
703 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
704 errmsg(
"CSV quote character must not appear in the NULL specification")));
726 if (attnamelist ==
NIL)
729 int attr_count = tupDesc->
natts;
732 for (
i = 0;
i < attr_count;
i++)
746 foreach(l, attnamelist)
754 for (
i = 0;
i < tupDesc->
natts;
i++)
758 if (att->attisdropped)
762 if (att->attgenerated)
764 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
765 errmsg(
"column \"%s\" is a generated column",
767 errdetail(
"Generated columns cannot be used in COPY.")));
776 (
errcode(ERRCODE_UNDEFINED_COLUMN),
777 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
781 (
errcode(ERRCODE_UNDEFINED_COLUMN),
782 errmsg(
"column \"%s\" does not exist",
788 (
errcode(ERRCODE_DUPLICATE_COLUMN),
789 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)
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,...)
int pg_char_to_encoding(const char *name)
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)
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)