diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-01-29 17:06:26 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-01-29 17:08:26 -0500 |
commit | 991f3e5ab3f8196d18d5b313c81a5f744f3baaea (patch) | |
tree | 376f7a4bc5541156a3c270304dc22333f2ba6955 /src/backend/utils/adt/domains.c | |
parent | 89d00cbe01447fd36edbc3bed659f869b18172d1 (diff) | |
download | postgresql-991f3e5ab3f8196d18d5b313c81a5f744f3baaea.tar.gz postgresql-991f3e5ab3f8196d18d5b313c81a5f744f3baaea.zip |
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.
Diffstat (limited to 'src/backend/utils/adt/domains.c')
-rw-r--r-- | src/backend/utils/adt/domains.c | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/src/backend/utils/adt/domains.c b/src/backend/utils/adt/domains.c index 9d2fb1e600e..0a26222c39b 100644 --- a/src/backend/utils/adt/domains.c +++ b/src/backend/utils/adt/domains.c @@ -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 */ +} |