/* *DisallowCOPYto/fromfileorprogramexcepttouserswiththe *appropriaterole.
*/ if (!pipe)
{ if (stmt->is_program)
{ if (!has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied to COPY to or from an external program"),
errdetail("Only roles with privileges of the \"%s\" role may COPY to or from an external program.", "pg_execute_server_program"),
errhint("Anyone can COPY to stdout or from stdin. " "psql's \\copy command also works for anyone.")));
} else
{ if (is_from && !has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied to COPY from a file"),
errdetail("Only roles with privileges of the \"%s\" role may COPY from a file.", "pg_read_server_files"),
errhint("Anyone can COPY to stdout or from stdin. " "psql's \\copy command also works for anyone.")));
if (!is_from && !has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied to COPY to a file"),
errdetail("Only roles with privileges of the \"%s\" role may COPY to a file.", "pg_write_server_files"),
errhint("Anyone can COPY to stdout or from stdin. " "psql's \\copy command also works for anyone.")));
}
}
i = -1; while ((i = bms_next_member(expr_attrs, i)) >= 0)
{
AttrNumber attno = i + FirstLowInvalidHeapAttributeNumber;
Assert(attno != 0);
/* *ProhibitgeneratedcolumnsintheWHEREclause.Stored *generatedcolumnsarenotyetcomputedwhenthefiltering *happens.Virtualgeneratedcolumnscouldprobablywork(we *wouldneedtoexpandthemsomewherearoundhere),butfor *nowwekeepthemconsistentwiththestoredvariant.
*/ if (attno > 0 &&
TupleDescAttr(RelationGetDescr(rel), attno - 1)->attgenerated)
ereport(ERROR,
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("generated columns are not supported in COPY FROM WHERE conditions"),
errdetail("Column \"%s\" is a generated column.",
get_attname(RelationGetRelid(rel), attno, false)));
}
if (is_from)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY FROM not supported with row-level security"),
errhint("Use INSERT statements instead.")));
if (!is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "ON_ERROR", "COPY TO"),
parser_errposition(pstate, def->location)));
/* *Allow"stop",or"ignore"values.
*/ if (pg_strcasecmp(sval, "stop") == 0) return COPY_ON_ERROR_STOP; if (pg_strcasecmp(sval, "ignore") == 0) return COPY_ON_ERROR_IGNORE;
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
parser_errposition(pstate, def->location))); return COPY_ON_ERROR_STOP; /* keep compiler quiet */
}
if (reject_limit <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("REJECT_LIMIT (%" PRId64 ") must be greater than zero",
reject_limit)));
/* *Allow"silent","default",or"verbose"values.
*/
sval = defGetString(def); if (pg_strcasecmp(sval, "silent") == 0) return COPY_LOG_VERBOSITY_SILENT; if (pg_strcasecmp(sval, "default") == 0) return COPY_LOG_VERBOSITY_DEFAULT; if (pg_strcasecmp(sval, "verbose") == 0) return COPY_LOG_VERBOSITY_VERBOSE;
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
parser_errposition(pstate, def->location))); return COPY_LOG_VERBOSITY_DEFAULT; /* keep compiler quiet */
}
/* Support external use for option sanity checking */ if (opts_out == NULL)
opts_out = (CopyFormatOptions *) palloc0(sizeof(CopyFormatOptions));
opts_out->file_encoding = -1;
/* Extract options from the statement node tree */
foreach(option, options)
{
DefElem *defel = lfirst_node(DefElem, option);
if (strcmp(defel->defname, "format") == 0)
{ char *fmt = defGetString(defel);
if (format_specified)
errorConflictingDefElem(defel, pstate);
format_specified = true; if (strcmp(fmt, "text") == 0) /* default format */ ; elseif (strcmp(fmt, "csv") == 0)
opts_out->csv_mode = true; elseif (strcmp(fmt, "binary") == 0)
opts_out->binary = true; else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY format \"%s\" not recognized", fmt),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "freeze") == 0)
{ if (freeze_specified)
errorConflictingDefElem(defel, pstate);
freeze_specified = true;
opts_out->freeze = defGetBoolean(defel);
} elseif (strcmp(defel->defname, "delimiter") == 0)
{ if (opts_out->delim)
errorConflictingDefElem(defel, pstate);
opts_out->delim = defGetString(defel);
} elseif (strcmp(defel->defname, "null") == 0)
{ if (opts_out->null_print)
errorConflictingDefElem(defel, pstate);
opts_out->null_print = defGetString(defel);
} elseif (strcmp(defel->defname, "default") == 0)
{ if (opts_out->default_print)
errorConflictingDefElem(defel, pstate);
opts_out->default_print = defGetString(defel);
} elseif (strcmp(defel->defname, "header") == 0)
{ if (header_specified)
errorConflictingDefElem(defel, pstate);
header_specified = true;
opts_out->header_line = defGetCopyHeaderChoice(defel, is_from);
} elseif (strcmp(defel->defname, "quote") == 0)
{ if (opts_out->quote)
errorConflictingDefElem(defel, pstate);
opts_out->quote = defGetString(defel);
} elseif (strcmp(defel->defname, "escape") == 0)
{ if (opts_out->escape)
errorConflictingDefElem(defel, pstate);
opts_out->escape = defGetString(defel);
} elseif (strcmp(defel->defname, "force_quote") == 0)
{ if (opts_out->force_quote || opts_out->force_quote_all)
errorConflictingDefElem(defel, pstate); if (defel->arg && IsA(defel->arg, A_Star))
opts_out->force_quote_all = true; elseif (defel->arg && IsA(defel->arg, List))
opts_out->force_quote = castNode(List, defel->arg); else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "force_not_null") == 0)
{ if (opts_out->force_notnull || opts_out->force_notnull_all)
errorConflictingDefElem(defel, pstate); if (defel->arg && IsA(defel->arg, A_Star))
opts_out->force_notnull_all = true; elseif (defel->arg && IsA(defel->arg, List))
opts_out->force_notnull = castNode(List, defel->arg); else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "force_null") == 0)
{ if (opts_out->force_null || opts_out->force_null_all)
errorConflictingDefElem(defel, pstate); if (defel->arg && IsA(defel->arg, A_Star))
opts_out->force_null_all = true; elseif (defel->arg && IsA(defel->arg, List))
opts_out->force_null = castNode(List, defel->arg); else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "convert_selectively") == 0)
{ /* *Undocumented,not-accessible-from-SQLoption:convertonlythe *namedcolumnstobinaryform,storingtherestasNULLs.It's *allowedforthecolumnlisttobeNIL.
*/ if (opts_out->convert_selectively)
errorConflictingDefElem(defel, pstate);
opts_out->convert_selectively = true; if (defel->arg == NULL || IsA(defel->arg, List))
opts_out->convert_select = castNode(List, defel->arg); else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "encoding") == 0)
{ if (opts_out->file_encoding >= 0)
errorConflictingDefElem(defel, pstate);
opts_out->file_encoding = pg_char_to_encoding(defGetString(defel)); if (opts_out->file_encoding < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a valid encoding name",
defel->defname),
parser_errposition(pstate, defel->location)));
} elseif (strcmp(defel->defname, "on_error") == 0)
{ if (on_error_specified)
errorConflictingDefElem(defel, pstate);
on_error_specified = true;
opts_out->on_error = defGetCopyOnErrorChoice(defel, pstate, is_from);
} elseif (strcmp(defel->defname, "log_verbosity") == 0)
{ if (log_verbosity_specified)
errorConflictingDefElem(defel, pstate);
log_verbosity_specified = true;
opts_out->log_verbosity = defGetCopyLogVerbosityChoice(defel, pstate);
} elseif (strcmp(defel->defname, "reject_limit") == 0)
{ if (reject_limit_specified)
errorConflictingDefElem(defel, pstate);
reject_limit_specified = true;
opts_out->reject_limit = defGetCopyRejectLimitOption(defel);
} else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("option \"%s\" not recognized",
defel->defname),
parser_errposition(pstate, defel->location)));
}
/* *Checkforincompatibleoptions(mustdothesethreebeforeinserting *defaults)
*/ if (opts_out->binary && opts_out->delim)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("cannot specify %s in BINARY mode", "DELIMITER")));
if (opts_out->binary && opts_out->null_print)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify %s in BINARY mode", "NULL")));
if (opts_out->binary && opts_out->default_print)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify %s in BINARY mode", "DEFAULT")));
/* Set defaults for omitted options */ if (!opts_out->delim)
opts_out->delim = opts_out->csv_mode ? "," : "\t";
if (opts_out->csv_mode)
{ if (!opts_out->quote)
opts_out->quote = "\""; if (!opts_out->escape)
opts_out->escape = opts_out->quote;
}
/* Only single-byte delimiter strings are supported. */ if (strlen(opts_out->delim) != 1)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY delimiter must be a single one-byte character")));
/* Disallow end-of-line characters */ if (strchr(opts_out->delim, '\r') != NULL ||
strchr(opts_out->delim, '\n') != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY delimiter cannot be newline or carriage return")));
if (strchr(opts_out->null_print, '\r') != NULL ||
strchr(opts_out->null_print, '\n') != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY null representation cannot use newline or carriage return")));
if (opts_out->default_print)
{
opts_out->default_print_len = strlen(opts_out->default_print);
if (strchr(opts_out->default_print, '\r') != NULL ||
strchr(opts_out->default_print, '\n') != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY default representation cannot use newline or carriage return")));
}
/* Check header */ if (opts_out->binary && opts_out->header_line)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("cannot specify %s in BINARY mode", "HEADER")));
/* Check quote */ if (!opts_out->csv_mode && opts_out->quote != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "QUOTE")));
if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY quote must be a single one-byte character")));
if (opts_out->csv_mode && opts_out->delim[0] == opts_out->quote[0])
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY delimiter and quote must be different")));
/* Check escape */ if (!opts_out->csv_mode && opts_out->escape != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "ESCAPE")));
if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY escape must be a single one-byte character")));
/* Check force_quote */ if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_QUOTE"))); if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "FORCE_QUOTE", "COPY FROM")));
/* Check force_notnull */ if (!opts_out->csv_mode && (opts_out->force_notnull != NIL ||
opts_out->force_notnull_all))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL"))); if ((opts_out->force_notnull != NIL || opts_out->force_notnull_all) &&
!is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "FORCE_NOT_NULL", "COPY TO")));
/* Check force_null */ if (!opts_out->csv_mode && (opts_out->force_null != NIL ||
opts_out->force_null_all))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
if ((opts_out->force_null != NIL || opts_out->force_null_all) &&
!is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "FORCE_NULL", "COPY TO")));
/* Don't allow the delimiter to appear in the null string. */ if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("COPY delimiter character must not appear in the %s specification", "NULL")));
/* Don't allow the CSV quote char to appear in the null string. */ if (opts_out->csv_mode &&
strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("CSV quote character must not appear in the %s specification", "NULL")));
/* Check freeze */ if (opts_out->freeze && !is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "FREEZE", "COPY TO")));
if (opts_out->default_print)
{ if (!is_from)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
second %s is a COPY with direction, e.g. COPY TO */
errmsg("COPY %s cannot be used with %s", "DEFAULT", "COPY TO")));
/* Don't allow the delimiter to appear in the default string. */ if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("COPY delimiter character must not appear in the %s specification", "DEFAULT")));
/* Don't allow the CSV quote char to appear in the default string. */ if (opts_out->csv_mode &&
strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("CSV quote character must not appear in the %s specification", "DEFAULT")));
/* Don't allow the NULL and DEFAULT string to be the same */ if (opts_out->null_print_len == opts_out->default_print_len &&
strncmp(opts_out->null_print, opts_out->default_print,
opts_out->null_print_len) == 0)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("NULL specification and DEFAULT specification cannot be the same")));
} /* Check on_error */ if (opts_out->binary && opts_out->on_error != COPY_ON_ERROR_STOP)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("only ON_ERROR STOP is allowed in BINARY mode")));
if (opts_out->reject_limit && !opts_out->on_error)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), /*- translator: first and second %s are the names of COPY option, e.g.
* ON_ERROR, third is the value of the COPY option, e.g. IGNORE */
errmsg("COPY %s requires %s to be set to %s", "REJECT_LIMIT", "ON_ERROR", "IGNORE")));
}
/* *CopyGetAttnums-buildanintegerlistofattnumstobecopied * *Theinputattnamelistiseithertheuser-specifiedcolumnlist, *orNILiftherewasnone(inwhichcasewewantallthenon-dropped *columns). * *Wedon'tincludegeneratedcolumnsinthegeneratedfulllistandwedon't *allowthemtobespecifiedexplicitly.Theydon'tmakesenseforCOPY *FROM,butwecouldpossiblyallowthemforCOPYTO.Butthiswayit'sat *leastensuredthatwhateverwecopyoutcanbecopiedbackin. * *relcanbeNULL...it'sonlyusedforerrorreports.
*/
List *
CopyGetAttnums(TupleDesc tupDesc, Relation rel, List *attnamelist)
{
List *attnums = NIL;
if (attnamelist == NIL)
{ /* Generate default column list */ int attr_count = tupDesc->natts; int i;
for (i = 0; i < attr_count; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(tupDesc, i);
if (attr->attisdropped || attr->attgenerated) continue;
attnums = lappend_int(attnums, i + 1);
}
} else
{ /* Validate the user-supplied list and extract attnums */
ListCell *l;
foreach(l, attnamelist)
{ char *name = strVal(lfirst(l)); int attnum; int i;
/* Lookup column name */
attnum = InvalidAttrNumber; for (i = 0; i < tupDesc->natts; i++)
{
Form_pg_attribute att = TupleDescAttr(tupDesc, i);
if (att->attisdropped) continue; if (namestrcmp(&(att->attname), name) == 0)
{ if (att->attgenerated)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("column \"%s\" is a generated column",
name),
errdetail("Generated columns cannot be used in COPY.")));
attnum = att->attnum; break;
}
} if (attnum == InvalidAttrNumber)
{ if (rel != NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
name, RelationGetRelationName(rel)))); else
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" does not exist",
name)));
} /* Check for duplicates */ if (list_member_int(attnums, attnum))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_COLUMN),
errmsg("column \"%s\" specified more than once",
name)));
attnums = lappend_int(attnums, attnum);
}
}
return attnums;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.