mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to extract object names from possibly-localized textual error messages, if they want to know for example which index caused a UNIQUE_VIOLATION failure. It adds new error message fields to the wire protocol, which can carry the name of a table, table column, data type, or constraint associated with the error. (Since the protocol spec has always instructed clients to ignore unrecognized field types, this should not create any compatibility problem.) Support for providing these new fields has been added to just a limited set of error reports (mainly, those in the "integrity constraint violation" SQLSTATE class), but we will doubtless add them to more calls in future. Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with additional hacking by Tom Lane.
This commit is contained in:
@ -31,11 +31,14 @@
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/htup_details.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/typecmds.h"
|
||||
#include "executor/executor.h"
|
||||
#include "lib/stringinfo.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -126,7 +129,8 @@ domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NOT_NULL_VIOLATION),
|
||||
errmsg("domain %s does not allow null values",
|
||||
format_type_be(my_extra->domain_type))));
|
||||
format_type_be(my_extra->domain_type)),
|
||||
errdatatype(my_extra->domain_type)));
|
||||
break;
|
||||
case DOM_CONSTRAINT_CHECK:
|
||||
{
|
||||
@ -163,7 +167,9 @@ domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
|
||||
(errcode(ERRCODE_CHECK_VIOLATION),
|
||||
errmsg("value for domain %s violates check constraint \"%s\"",
|
||||
format_type_be(my_extra->domain_type),
|
||||
con->name)));
|
||||
con->name),
|
||||
errdomainconstraint(my_extra->domain_type,
|
||||
con->name)));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -310,7 +316,8 @@ domain_recv(PG_FUNCTION_ARGS)
|
||||
* setup is repeated for each call.
|
||||
*/
|
||||
void
|
||||
domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
|
||||
domain_check(Datum value, bool isnull, Oid domainType,
|
||||
void **extra, MemoryContext mcxt)
|
||||
{
|
||||
DomainIOData *my_extra = NULL;
|
||||
|
||||
@ -339,3 +346,40 @@ domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryConte
|
||||
*/
|
||||
domain_check_input(value, isnull, my_extra);
|
||||
}
|
||||
|
||||
/*
|
||||
* errdatatype --- stores schema_name and datatype_name of a datatype
|
||||
* within the current errordata.
|
||||
*/
|
||||
int
|
||||
errdatatype(Oid datatypeOid)
|
||||
{
|
||||
HeapTuple tup;
|
||||
Form_pg_type typtup;
|
||||
|
||||
tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(datatypeOid));
|
||||
if (!HeapTupleIsValid(tup))
|
||||
elog(ERROR, "cache lookup failed for type %u", datatypeOid);
|
||||
typtup = (Form_pg_type) GETSTRUCT(tup);
|
||||
|
||||
err_generic_string(PG_DIAG_SCHEMA_NAME,
|
||||
get_namespace_name(typtup->typnamespace));
|
||||
err_generic_string(PG_DIAG_DATATYPE_NAME, NameStr(typtup->typname));
|
||||
|
||||
ReleaseSysCache(tup);
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
/*
|
||||
* errdomainconstraint --- stores schema_name, datatype_name and
|
||||
* constraint_name of a domain-related constraint within the current errordata.
|
||||
*/
|
||||
int
|
||||
errdomainconstraint(Oid datatypeOid, const char *conname)
|
||||
{
|
||||
errdatatype(datatypeOid);
|
||||
err_generic_string(PG_DIAG_CONSTRAINT_NAME, conname);
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
@ -337,9 +337,11 @@ RI_FKey_check(TriggerData *trigdata)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
|
||||
errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"",
|
||||
RelationGetRelationName(trigdata->tg_relation),
|
||||
RelationGetRelationName(fk_rel),
|
||||
NameStr(riinfo->conname)),
|
||||
errdetail("MATCH FULL does not allow mixing of null and nonnull key values.")));
|
||||
errdetail("MATCH FULL does not allow mixing of null and nonnull key values."),
|
||||
errtableconstraint(fk_rel,
|
||||
NameStr(riinfo->conname))));
|
||||
heap_close(pk_rel, RowShareLock);
|
||||
return PointerGetDatum(NULL);
|
||||
|
||||
@ -2470,7 +2472,9 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
|
||||
errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"",
|
||||
RelationGetRelationName(fk_rel),
|
||||
NameStr(fake_riinfo.conname)),
|
||||
errdetail("MATCH FULL does not allow mixing of null and nonnull key values.")));
|
||||
errdetail("MATCH FULL does not allow mixing of null and nonnull key values."),
|
||||
errtableconstraint(fk_rel,
|
||||
NameStr(fake_riinfo.conname))));
|
||||
|
||||
/*
|
||||
* We tell ri_ReportViolation we were doing the RI_PLAN_CHECK_LOOKUPPK
|
||||
@ -3222,7 +3226,8 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
|
||||
NameStr(riinfo->conname)),
|
||||
errdetail("Key (%s)=(%s) is not present in table \"%s\".",
|
||||
key_names.data, key_values.data,
|
||||
RelationGetRelationName(pk_rel))));
|
||||
RelationGetRelationName(pk_rel)),
|
||||
errtableconstraint(fk_rel, NameStr(riinfo->conname))));
|
||||
else
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
|
||||
@ -3232,7 +3237,8 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
|
||||
RelationGetRelationName(fk_rel)),
|
||||
errdetail("Key (%s)=(%s) is still referenced from table \"%s\".",
|
||||
key_names.data, key_values.data,
|
||||
RelationGetRelationName(fk_rel))));
|
||||
RelationGetRelationName(fk_rel)),
|
||||
errtableconstraint(fk_rel, NameStr(riinfo->conname))));
|
||||
}
|
||||
|
||||
|
||||
|
76
src/backend/utils/cache/relcache.c
vendored
76
src/backend/utils/cache/relcache.c
vendored
@ -3998,6 +3998,82 @@ RelationGetExclusionInfo(Relation indexRelation,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Routines to support ereport() reports of relation-related errors
|
||||
*
|
||||
* These could have been put into elog.c, but it seems like a module layering
|
||||
* violation to have elog.c calling relcache or syscache stuff --- and we
|
||||
* definitely don't want elog.h including rel.h. So we put them here.
|
||||
*/
|
||||
|
||||
/*
|
||||
* errtable --- stores schema_name and table_name of a table
|
||||
* within the current errordata.
|
||||
*/
|
||||
int
|
||||
errtable(Relation rel)
|
||||
{
|
||||
err_generic_string(PG_DIAG_SCHEMA_NAME,
|
||||
get_namespace_name(RelationGetNamespace(rel)));
|
||||
err_generic_string(PG_DIAG_TABLE_NAME, RelationGetRelationName(rel));
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
/*
|
||||
* errtablecol --- stores schema_name, table_name and column_name
|
||||
* of a table column within the current errordata.
|
||||
*
|
||||
* The column is specified by attribute number --- for most callers, this is
|
||||
* easier and less error-prone than getting the column name for themselves.
|
||||
*/
|
||||
int
|
||||
errtablecol(Relation rel, int attnum)
|
||||
{
|
||||
TupleDesc reldesc = RelationGetDescr(rel);
|
||||
const char *colname;
|
||||
|
||||
/* Use reldesc if it's a user attribute, else consult the catalogs */
|
||||
if (attnum > 0 && attnum <= reldesc->natts)
|
||||
colname = NameStr(reldesc->attrs[attnum - 1]->attname);
|
||||
else
|
||||
colname = get_relid_attribute_name(RelationGetRelid(rel), attnum);
|
||||
|
||||
return errtablecolname(rel, colname);
|
||||
}
|
||||
|
||||
/*
|
||||
* errtablecolname --- stores schema_name, table_name and column_name
|
||||
* of a table column within the current errordata, where the column name is
|
||||
* given directly rather than extracted from the relation's catalog data.
|
||||
*
|
||||
* Don't use this directly unless errtablecol() is inconvenient for some
|
||||
* reason. This might possibly be needed during intermediate states in ALTER
|
||||
* TABLE, for instance.
|
||||
*/
|
||||
int
|
||||
errtablecolname(Relation rel, const char *colname)
|
||||
{
|
||||
errtable(rel);
|
||||
err_generic_string(PG_DIAG_COLUMN_NAME, colname);
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
/*
|
||||
* errtableconstraint --- stores schema_name, table_name and constraint_name
|
||||
* of a table-related constraint within the current errordata.
|
||||
*/
|
||||
int
|
||||
errtableconstraint(Relation rel, const char *conname)
|
||||
{
|
||||
errtable(rel);
|
||||
err_generic_string(PG_DIAG_CONSTRAINT_NAME, conname);
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* load_relcache_init_file, write_relcache_init_file
|
||||
*
|
||||
|
@ -87,6 +87,7 @@ err_gettext(const char *str)
|
||||
/* This extension allows gcc to check the format string for consistency with
|
||||
the supplied arguments. */
|
||||
__attribute__((format_arg(1)));
|
||||
static void set_errdata_field(char **ptr, const char *str);
|
||||
|
||||
/* Global variables */
|
||||
ErrorContextCallback *error_context_stack = NULL;
|
||||
@ -475,6 +476,16 @@ errfinish(int dummy,...)
|
||||
pfree(edata->hint);
|
||||
if (edata->context)
|
||||
pfree(edata->context);
|
||||
if (edata->schema_name)
|
||||
pfree(edata->schema_name);
|
||||
if (edata->table_name)
|
||||
pfree(edata->table_name);
|
||||
if (edata->column_name)
|
||||
pfree(edata->column_name);
|
||||
if (edata->datatype_name)
|
||||
pfree(edata->datatype_name);
|
||||
if (edata->constraint_name)
|
||||
pfree(edata->constraint_name);
|
||||
if (edata->internalquery)
|
||||
pfree(edata->internalquery);
|
||||
|
||||
@ -1100,6 +1111,59 @@ internalerrquery(const char *query)
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
/*
|
||||
* err_generic_string -- used to set individual ErrorData string fields
|
||||
* identified by PG_DIAG_xxx codes.
|
||||
*
|
||||
* This intentionally only supports fields that don't use localized strings,
|
||||
* so that there are no translation considerations.
|
||||
*
|
||||
* Most potential callers should not use this directly, but instead prefer
|
||||
* higher-level abstractions, such as errtablecol() (see relcache.c).
|
||||
*/
|
||||
int
|
||||
err_generic_string(int field, const char *str)
|
||||
{
|
||||
ErrorData *edata = &errordata[errordata_stack_depth];
|
||||
|
||||
/* we don't bother incrementing recursion_depth */
|
||||
CHECK_STACK_DEPTH();
|
||||
|
||||
switch (field)
|
||||
{
|
||||
case PG_DIAG_SCHEMA_NAME:
|
||||
set_errdata_field(&edata->schema_name, str);
|
||||
break;
|
||||
case PG_DIAG_TABLE_NAME:
|
||||
set_errdata_field(&edata->table_name, str);
|
||||
break;
|
||||
case PG_DIAG_COLUMN_NAME:
|
||||
set_errdata_field(&edata->column_name, str);
|
||||
break;
|
||||
case PG_DIAG_DATATYPE_NAME:
|
||||
set_errdata_field(&edata->datatype_name, str);
|
||||
break;
|
||||
case PG_DIAG_CONSTRAINT_NAME:
|
||||
set_errdata_field(&edata->constraint_name, str);
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unsupported ErrorData field id: %d", field);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0; /* return value does not matter */
|
||||
}
|
||||
|
||||
/*
|
||||
* set_errdata_field --- set an ErrorData string field
|
||||
*/
|
||||
static void
|
||||
set_errdata_field(char **ptr, const char *str)
|
||||
{
|
||||
Assert(*ptr == NULL);
|
||||
*ptr = MemoryContextStrdup(ErrorContext, str);
|
||||
}
|
||||
|
||||
/*
|
||||
* geterrcode --- return the currently set SQLSTATE error code
|
||||
*
|
||||
@ -1373,6 +1437,16 @@ CopyErrorData(void)
|
||||
newedata->hint = pstrdup(newedata->hint);
|
||||
if (newedata->context)
|
||||
newedata->context = pstrdup(newedata->context);
|
||||
if (newedata->schema_name)
|
||||
newedata->schema_name = pstrdup(newedata->schema_name);
|
||||
if (newedata->table_name)
|
||||
newedata->table_name = pstrdup(newedata->table_name);
|
||||
if (newedata->column_name)
|
||||
newedata->column_name = pstrdup(newedata->column_name);
|
||||
if (newedata->datatype_name)
|
||||
newedata->datatype_name = pstrdup(newedata->datatype_name);
|
||||
if (newedata->constraint_name)
|
||||
newedata->constraint_name = pstrdup(newedata->constraint_name);
|
||||
if (newedata->internalquery)
|
||||
newedata->internalquery = pstrdup(newedata->internalquery);
|
||||
|
||||
@ -1398,6 +1472,16 @@ FreeErrorData(ErrorData *edata)
|
||||
pfree(edata->hint);
|
||||
if (edata->context)
|
||||
pfree(edata->context);
|
||||
if (edata->schema_name)
|
||||
pfree(edata->schema_name);
|
||||
if (edata->table_name)
|
||||
pfree(edata->table_name);
|
||||
if (edata->column_name)
|
||||
pfree(edata->column_name);
|
||||
if (edata->datatype_name)
|
||||
pfree(edata->datatype_name);
|
||||
if (edata->constraint_name)
|
||||
pfree(edata->constraint_name);
|
||||
if (edata->internalquery)
|
||||
pfree(edata->internalquery);
|
||||
pfree(edata);
|
||||
@ -1470,6 +1554,16 @@ ReThrowError(ErrorData *edata)
|
||||
newedata->hint = pstrdup(newedata->hint);
|
||||
if (newedata->context)
|
||||
newedata->context = pstrdup(newedata->context);
|
||||
if (newedata->schema_name)
|
||||
newedata->schema_name = pstrdup(newedata->schema_name);
|
||||
if (newedata->table_name)
|
||||
newedata->table_name = pstrdup(newedata->table_name);
|
||||
if (newedata->column_name)
|
||||
newedata->column_name = pstrdup(newedata->column_name);
|
||||
if (newedata->datatype_name)
|
||||
newedata->datatype_name = pstrdup(newedata->datatype_name);
|
||||
if (newedata->constraint_name)
|
||||
newedata->constraint_name = pstrdup(newedata->constraint_name);
|
||||
if (newedata->internalquery)
|
||||
newedata->internalquery = pstrdup(newedata->internalquery);
|
||||
|
||||
@ -2657,6 +2751,36 @@ send_message_to_frontend(ErrorData *edata)
|
||||
err_sendstring(&msgbuf, edata->context);
|
||||
}
|
||||
|
||||
if (edata->schema_name)
|
||||
{
|
||||
pq_sendbyte(&msgbuf, PG_DIAG_SCHEMA_NAME);
|
||||
err_sendstring(&msgbuf, edata->schema_name);
|
||||
}
|
||||
|
||||
if (edata->table_name)
|
||||
{
|
||||
pq_sendbyte(&msgbuf, PG_DIAG_TABLE_NAME);
|
||||
err_sendstring(&msgbuf, edata->table_name);
|
||||
}
|
||||
|
||||
if (edata->column_name)
|
||||
{
|
||||
pq_sendbyte(&msgbuf, PG_DIAG_COLUMN_NAME);
|
||||
err_sendstring(&msgbuf, edata->column_name);
|
||||
}
|
||||
|
||||
if (edata->datatype_name)
|
||||
{
|
||||
pq_sendbyte(&msgbuf, PG_DIAG_DATATYPE_NAME);
|
||||
err_sendstring(&msgbuf, edata->datatype_name);
|
||||
}
|
||||
|
||||
if (edata->constraint_name)
|
||||
{
|
||||
pq_sendbyte(&msgbuf, PG_DIAG_CONSTRAINT_NAME);
|
||||
err_sendstring(&msgbuf, edata->constraint_name);
|
||||
}
|
||||
|
||||
if (edata->cursorpos > 0)
|
||||
{
|
||||
snprintf(tbuf, sizeof(tbuf), "%d", edata->cursorpos);
|
||||
|
@ -364,6 +364,7 @@ struct Tuplesortstate
|
||||
* These variables are specific to the IndexTuple case; they are set by
|
||||
* tuplesort_begin_index_xxx and used only by the IndexTuple routines.
|
||||
*/
|
||||
Relation heapRel; /* table the index is being built on */
|
||||
Relation indexRel; /* index being built */
|
||||
|
||||
/* These are specific to the index_btree subcase: */
|
||||
@ -720,7 +721,8 @@ tuplesort_begin_cluster(TupleDesc tupDesc,
|
||||
}
|
||||
|
||||
Tuplesortstate *
|
||||
tuplesort_begin_index_btree(Relation indexRel,
|
||||
tuplesort_begin_index_btree(Relation heapRel,
|
||||
Relation indexRel,
|
||||
bool enforceUnique,
|
||||
int workMem, bool randomAccess)
|
||||
{
|
||||
@ -751,6 +753,7 @@ tuplesort_begin_index_btree(Relation indexRel,
|
||||
state->readtup = readtup_index;
|
||||
state->reversedirection = reversedirection_index_btree;
|
||||
|
||||
state->heapRel = heapRel;
|
||||
state->indexRel = indexRel;
|
||||
state->indexScanKey = _bt_mkscankey_nodata(indexRel);
|
||||
state->enforceUnique = enforceUnique;
|
||||
@ -761,7 +764,8 @@ tuplesort_begin_index_btree(Relation indexRel,
|
||||
}
|
||||
|
||||
Tuplesortstate *
|
||||
tuplesort_begin_index_hash(Relation indexRel,
|
||||
tuplesort_begin_index_hash(Relation heapRel,
|
||||
Relation indexRel,
|
||||
uint32 hash_mask,
|
||||
int workMem, bool randomAccess)
|
||||
{
|
||||
@ -786,6 +790,7 @@ tuplesort_begin_index_hash(Relation indexRel,
|
||||
state->readtup = readtup_index;
|
||||
state->reversedirection = reversedirection_index_hash;
|
||||
|
||||
state->heapRel = heapRel;
|
||||
state->indexRel = indexRel;
|
||||
|
||||
state->hash_mask = hash_mask;
|
||||
@ -3171,7 +3176,9 @@ comparetup_index_btree(const SortTuple *a, const SortTuple *b,
|
||||
RelationGetRelationName(state->indexRel)),
|
||||
errdetail("Key %s is duplicated.",
|
||||
BuildIndexValueDescription(state->indexRel,
|
||||
values, isnull))));
|
||||
values, isnull)),
|
||||
errtableconstraint(state->heapRel,
|
||||
RelationGetRelationName(state->indexRel))));
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user