mirror of
https://github.com/postgres/postgres.git
synced 2025-08-14 02:22:38 +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:
@@ -3828,7 +3828,8 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NOT_NULL_VIOLATION),
|
||||
errmsg("column \"%s\" contains null values",
|
||||
NameStr(newTupDesc->attrs[attn]->attname))));
|
||||
NameStr(newTupDesc->attrs[attn]->attname)),
|
||||
errtablecol(oldrel, attn + 1)));
|
||||
}
|
||||
|
||||
foreach(l, tab->constraints)
|
||||
@@ -3842,7 +3843,8 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_CHECK_VIOLATION),
|
||||
errmsg("check constraint \"%s\" is violated by some row",
|
||||
con->name)));
|
||||
con->name),
|
||||
errtableconstraint(oldrel, con->name)));
|
||||
break;
|
||||
case CONSTR_FOREIGN:
|
||||
/* Nothing to do here */
|
||||
@@ -6659,7 +6661,8 @@ validateCheckConstraint(Relation rel, HeapTuple constrtup)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_CHECK_VIOLATION),
|
||||
errmsg("check constraint \"%s\" is violated by some row",
|
||||
NameStr(constrForm->conname))));
|
||||
NameStr(constrForm->conname)),
|
||||
errtableconstraint(rel, NameStr(constrForm->conname))));
|
||||
|
||||
ResetExprContext(econtext);
|
||||
}
|
||||
|
@@ -2264,11 +2264,22 @@ AlterDomainNotNull(List *names, bool notNull)
|
||||
int attnum = rtc->atts[i];
|
||||
|
||||
if (heap_attisnull(tuple, attnum))
|
||||
{
|
||||
/*
|
||||
* In principle the auxiliary information for this
|
||||
* error should be errdatatype(), but errtablecol()
|
||||
* seems considerably more useful in practice. Since
|
||||
* this code only executes in an ALTER DOMAIN command,
|
||||
* the client should already know which domain is in
|
||||
* question.
|
||||
*/
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NOT_NULL_VIOLATION),
|
||||
errmsg("column \"%s\" of table \"%s\" contains null values",
|
||||
NameStr(tupdesc->attrs[attnum - 1]->attname),
|
||||
RelationGetRelationName(testrel))));
|
||||
RelationGetRelationName(testrel)),
|
||||
errtablecol(testrel, attnum)));
|
||||
}
|
||||
}
|
||||
}
|
||||
heap_endscan(scan);
|
||||
@@ -2469,7 +2480,7 @@ AlterDomainAddConstraint(List *names, Node *newConstraint)
|
||||
* to pg_constraint.
|
||||
*/
|
||||
|
||||
ccbin = domainAddConstraint(HeapTupleGetOid(tup), typTup->typnamespace,
|
||||
ccbin = domainAddConstraint(domainoid, typTup->typnamespace,
|
||||
typTup->typbasetype, typTup->typtypmod,
|
||||
constr, NameStr(typTup->typname));
|
||||
|
||||
@@ -2641,11 +2652,22 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
|
||||
&isNull, NULL);
|
||||
|
||||
if (!isNull && !DatumGetBool(conResult))
|
||||
{
|
||||
/*
|
||||
* In principle the auxiliary information for this error
|
||||
* should be errdomainconstraint(), but errtablecol()
|
||||
* seems considerably more useful in practice. Since this
|
||||
* code only executes in an ALTER DOMAIN command, the
|
||||
* client should already know which domain is in question,
|
||||
* and which constraint too.
|
||||
*/
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_CHECK_VIOLATION),
|
||||
errmsg("column \"%s\" of table \"%s\" contains values that violate the new constraint",
|
||||
NameStr(tupdesc->attrs[attnum - 1]->attname),
|
||||
RelationGetRelationName(testrel))));
|
||||
RelationGetRelationName(testrel)),
|
||||
errtablecol(testrel, attnum)));
|
||||
}
|
||||
}
|
||||
|
||||
ResetExprContext(econtext);
|
||||
|
Reference in New Issue
Block a user