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.")));
151 foreach(
cur, attnums)
187 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
188 errmsg(
"COPY FROM not supported with row-level security"),
189 errhint(
"Use INSERT statements instead.")));
240 targetList =
lappend(targetList, target);
254 select->targetList = targetList;
326 if (def->
arg == NULL)
368 (
errcode(ERRCODE_SYNTAX_ERROR),
369 errmsg(
"%s requires a Boolean value or \"match\"",
396 bool format_specified =
false;
397 bool freeze_specified =
false;
398 bool header_specified =
false;
402 if (opts_out == NULL)
412 if (strcmp(defel->
defname,
"format") == 0)
416 if (format_specified)
418 format_specified =
true;
419 if (strcmp(
fmt,
"text") == 0)
421 else if (strcmp(
fmt,
"csv") == 0)
423 else if (strcmp(
fmt,
"binary") == 0)
427 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
428 errmsg(
"COPY format \"%s\" not recognized",
fmt),
431 else if (strcmp(defel->
defname,
"freeze") == 0)
433 if (freeze_specified)
435 freeze_specified =
true;
438 else if (strcmp(defel->
defname,
"delimiter") == 0)
444 else if (strcmp(defel->
defname,
"null") == 0)
450 else if (strcmp(defel->
defname,
"header") == 0)
452 if (header_specified)
454 header_specified =
true;
457 else if (strcmp(defel->
defname,
"quote") == 0)
463 else if (strcmp(defel->
defname,
"escape") == 0)
469 else if (strcmp(defel->
defname,
"force_quote") == 0)
479 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
480 errmsg(
"argument to option \"%s\" must be a list of column names",
484 else if (strcmp(defel->
defname,
"force_not_null") == 0)
492 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
493 errmsg(
"argument to option \"%s\" must be a list of column names",
497 else if (strcmp(defel->
defname,
"force_null") == 0)
505 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
506 errmsg(
"argument to option \"%s\" must be a list of column names",
510 else if (strcmp(defel->
defname,
"convert_selectively") == 0)
524 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
525 errmsg(
"argument to option \"%s\" must be a list of column names",
529 else if (strcmp(defel->
defname,
"encoding") == 0)
536 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
537 errmsg(
"argument to option \"%s\" must be a valid encoding name",
543 (
errcode(ERRCODE_SYNTAX_ERROR),
544 errmsg(
"option \"%s\" not recognized",
555 (
errcode(ERRCODE_SYNTAX_ERROR),
556 errmsg(
"cannot specify DELIMITER in BINARY mode")));
560 (
errcode(ERRCODE_SYNTAX_ERROR),
561 errmsg(
"cannot specify NULL in BINARY mode")));
564 if (!opts_out->
delim)
573 if (!opts_out->
quote)
574 opts_out->
quote =
"\"";
580 if (strlen(opts_out->
delim) != 1)
582 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
583 errmsg(
"COPY delimiter must be a single one-byte character")));
586 if (strchr(opts_out->
delim,
'\r') != NULL ||
587 strchr(opts_out->
delim,
'\n') != NULL)
589 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
590 errmsg(
"COPY delimiter cannot be newline or carriage return")));
592 if (strchr(opts_out->
null_print,
'\r') != NULL ||
595 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
596 errmsg(
"COPY null representation cannot use newline or carriage return")));
609 strchr(
"\\.abcdefghijklmnopqrstuvwxyz0123456789",
610 opts_out->
delim[0]) != NULL)
612 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
613 errmsg(
"COPY delimiter cannot be \"%s\"", opts_out->
delim)));
618 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
619 errmsg(
"cannot specify HEADER in BINARY mode")));
624 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
625 errmsg(
"COPY quote available only in CSV mode")));
629 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
630 errmsg(
"COPY quote must be a single one-byte character")));
634 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
635 errmsg(
"COPY delimiter and quote must be different")));
640 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
641 errmsg(
"COPY escape available only in CSV mode")));
645 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
646 errmsg(
"COPY escape must be a single one-byte character")));
651 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
652 errmsg(
"COPY force quote available only in CSV mode")));
655 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
656 errmsg(
"COPY force quote only available using COPY TO")));
661 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
662 errmsg(
"COPY force not null available only in CSV mode")));
665 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
666 errmsg(
"COPY force not null only available using COPY FROM")));
671 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
672 errmsg(
"COPY force null available only in CSV mode")));
676 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
677 errmsg(
"COPY force null only available using COPY FROM")));
682 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
683 errmsg(
"COPY delimiter must not appear in the NULL specification")));
689 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
690 errmsg(
"CSV quote character must not appear in the NULL specification")));
712 if (attnamelist ==
NIL)
715 int attr_count = tupDesc->
natts;
718 for (
i = 0;
i < attr_count;
i++)
732 foreach(l, attnamelist)
740 for (
i = 0;
i < tupDesc->
natts;
i++)
744 if (att->attisdropped)
748 if (att->attgenerated)
750 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
751 errmsg(
"column \"%s\" is a generated column",
753 errdetail(
"Generated columns cannot be used in COPY.")));
762 (
errcode(ERRCODE_UNDEFINED_COLUMN),
763 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
767 (
errcode(ERRCODE_UNDEFINED_COLUMN),
768 errmsg(
"column \"%s\" does not exist",
774 (
errcode(ERRCODE_DUPLICATE_COLUMN),
775 errmsg(
"column \"%s\" specified more than once",
bool has_privs_of_role(Oid member, Oid role)
#define InvalidAttrNumber
List * CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
void DoCopy(ParseState *pstate, const CopyStmt *stmt, int stmt_location, int stmt_len, uint64 *processed)
static CopyHeaderChoice defGetCopyHeaderChoice(DefElem *def)
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)
void EndCopyTo(CopyToState cstate)
CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *raw_query, Oid queryRelId, const char *filename, bool is_program, List *attnamelist, List *options)
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 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
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)
#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)