According to the standard, the first two characters of an error code
denote a class of errors, while the last three characters indicate
a specific condition within that class. Thus, an application that
- does not recognize the specific error code can still be able to infer
+ does not recognize the specific error code might still be able to infer
what to do from the error class.
</para>
</para>
<para>
- The symbol shown in the column <quote>Condition Name</quote> is also
+ The symbol shown in the column <quote>Condition Name</quote> is
the condition name to use in <application>PL/pgSQL</>. Condition
names can be written in either upper or lower case. (Note that
<application>PL/pgSQL</> does not recognize warning, as opposed to error,
condition names; those are classes 00, 01, and 02.)
</para>
+ <para>
+ For some types of errors, the server reports the name of a database object
+ (a table, table column, data type, or constraint) associated with the error;
+ for example, the name of the unique constraint that caused a
+ <symbol>unique_violation</> error. Such names are supplied in separate
+ fields of the error report message so that applications need not try to
+ extract them from the possibly-localized human-readable text of the message.
+ As of <productname>PostgreSQL</> 9.3, complete coverage for this feature
+ exists only for errors in SQLSTATE class 23 (integrity constraint
+ violation), but this is likely to be expanded in future.
+ </para>
+
<table id="errcodes-table">
<title><productname>PostgreSQL</productname> Error Codes</title>
</listitem>
</varlistentry>
+ <varlistentry id="libpq-pg-diag-schema-name">
+ <term><symbol>PG_DIAG_SCHEMA_NAME</></term>
+ <listitem>
+ <para>
+ If the error was associated with a specific database object,
+ the name of the schema containing that object, if any.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry id="libpq-pg-diag-table-name">
+ <term><symbol>PG_DIAG_TABLE_NAME</></term>
+ <listitem>
+ <para>
+ If the error was associated with a specific table, the name of
+ the table. (When this field is present, the schema name field
+ provides the name of the table's schema.)
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry id="libpq-pg-diag-column-name">
+ <term><symbol>PG_DIAG_COLUMN_NAME</></term>
+ <listitem>
+ <para>
+ If the error was associated with a specific table column, the
+ name of the column. (When this field is present, the schema
+ and table name fields identify the table.)
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry id="libpq-pg-diag-datatype-name">
+ <term><symbol>PG_DIAG_DATATYPE_NAME</></term>
+ <listitem>
+ <para>
+ If the error was associated with a specific datatype, the name
+ of the datatype. (When this field is present, the schema name
+ field provides the name of the datatype's schema.)
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry id="libpq-pg-diag-constraint-name">
+ <term><symbol>PG_DIAG_CONSTRAINT_NAME</></term>
+ <listitem>
+ <para>
+ If the error was associated with a specific constraint,
+ the name of the constraint. The table or domain that the
+ constraint belongs to is reported using the fields listed
+ above. (For this purpose, indexes are treated as constraints,
+ even if they weren't created with constraint syntax.)
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry id="libpq-pg-diag-source-file">
<term><symbol>PG_DIAG_SOURCE_FILE</></term>
<listitem>
</variablelist>
</para>
+ <note>
+ <para>
+ The fields for schema name, table name, column name, datatype
+ name, and constraint name are supplied only for a limited number
+ of error types; see <xref linkend="errcodes-appendix">.
+ </para>
+ </note>
+
<para>
The client is responsible for formatting displayed information to meet
its needs; in particular it should break long lines as needed.
</listitem>
</varlistentry>
+<varlistentry>
+<term>
+<literal>s</>
+</term>
+<listitem>
+<para>
+ Schema name: if the error was associated with a specific database
+ object, the name of the schema containing that object, if any.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+<literal>t</>
+</term>
+<listitem>
+<para>
+ Table name: if the error was associated with a specific table, the
+ name of the table. (When this field is present, the schema name field
+ provides the name of the table's schema.)
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+<literal>c</>
+</term>
+<listitem>
+<para>
+ Column name: if the error was associated with a specific table column,
+ the name of the column. (When this field is present, the schema and
+ table name fields identify the table.)
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+<literal>d</>
+</term>
+<listitem>
+<para>
+ Datatype name: if the error was associated with a specific datatype,
+ the name of the datatype. (When this field is present, the schema
+ name field provides the name of the datatype's schema.)
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+<literal>n</>
+</term>
+<listitem>
+<para>
+ Constraint name: if the error was associated with a specific
+ constraint, the name of the constraint. The table or domain that the
+ constraint belongs to is reported using the fields listed above. (For
+ this purpose, indexes are treated as constraints, even if they weren't
+ created with constraint syntax.)
+</para>
+</listitem>
+</varlistentry>
+
<varlistentry>
<term>
<literal>F</>
</variablelist>
+<note>
+ <para>
+ The fields for schema name, table name, column name, datatype name, and
+ constraint name are supplied only for a limited number of error types;
+ see <xref linkend="errcodes-appendix">.
+ </para>
+</note>
+
<para>
The client is responsible for formatting displayed information to meet its
needs; in particular it should break long lines as needed. Newline characters
query processing.
</para>
</listitem>
+ <listitem>
+ <para>
+ <function>errtable(Relation rel)</function> specifies a relation whose
+ name and schema name should be included as auxiliary fields in the error
+ report.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ <function>errtablecol(Relation rel, int attnum)</function> specifies
+ a column whose name, table name, and schema name should be included as
+ auxiliary fields in the error report.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ <function>errtableconstraint(Relation rel, const char *conname)</function>
+ specifies a table constraint whose name, table name, and schema name
+ should be included as auxiliary fields in the error report. Indexes
+ should be considered to be constraints for this purpose, whether or
+ not they have an associated <structname>pg_constraint</> entry. Be
+ careful to pass the underlying heap relation, not the index itself, as
+ <literal>rel</>.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ <function>errdatatype(Oid datatypeOid)</function> specifies a data
+ type whose name and schema name should be included as auxiliary fields
+ in the error report.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ <function>errdomainconstraint(Oid datatypeOid, const char *conname)</function>
+ specifies a domain constraint whose name, domain name, and schema name
+ should be included as auxiliary fields in the error report.
+ </para>
+ </listitem>
<listitem>
<para>
<function>errcode_for_file_access()</> is a convenience function that
</itemizedlist>
</para>
+ <note>
+ <para>
+ At most one of the functions <function>errtable</>,
+ <function>errtablecol</>, <function>errtableconstraint</>,
+ <function>errdatatype</>, or <function>errdomainconstraint</> should
+ be used in an <function>ereport</> call. These functions exist to
+ allow applications to extract the name of a database object associated
+ with the error condition without having to examine the
+ potentially-localized error message text.
+ These functions should be used in error reports for which it's likely
+ that applications would wish to have automatic error handling. As of
+ <productname>PostgreSQL</> 9.3, complete coverage exists only for
+ errors in SQLSTATE class 23 (integrity constraint violation), but this
+ is likely to be expanded in future.
+ </para>
+ </note>
+
<para>
There is an older function <function>elog</> that is still heavily used.
An <function>elog</> call:
* one page.
*/
if (num_buckets >= (uint32) NBuffers)
- buildstate.spool = _h_spoolinit(index, num_buckets);
+ buildstate.spool = _h_spoolinit(heap, index, num_buckets);
else
buildstate.spool = NULL;
* create and initialize a spool structure
*/
HSpool *
-_h_spoolinit(Relation index, uint32 num_buckets)
+_h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
{
HSpool *hspool = (HSpool *) palloc0(sizeof(HSpool));
uint32 hash_mask;
* speed index creation. This should be OK since a single backend can't
* run multiple index creations in parallel.
*/
- hspool->sortstate = tuplesort_begin_index_hash(index,
+ hspool->sortstate = tuplesort_begin_index_hash(heap,
+ index,
hash_mask,
maintenance_work_mem,
false);
RelationGetRelationName(rel)),
errdetail("Key %s already exists.",
BuildIndexValueDescription(rel,
- values, isnull))));
+ values, isnull)),
+ errtableconstraint(heapRel,
+ RelationGetRelationName(rel))));
}
}
else if (all_dead)
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("failed to re-find tuple within index \"%s\"",
RelationGetRelationName(rel)),
- errhint("This may be because of a non-immutable index expression.")));
+ errhint("This may be because of a non-immutable index expression."),
+ errtableconstraint(heapRel,
+ RelationGetRelationName(rel))));
if (nbuf != InvalidBuffer)
_bt_relbuf(rel, nbuf);
* able to fit three items on every page, so restrict any one item to 1/3
* the per-page available space. Note that at this point, itemsz doesn't
* include the ItemId.
+ *
+ * NOTE: if you change this, see also the similar code in _bt_buildadd().
*/
if (itemsz > BTMaxItemSize(page))
ereport(ERROR,
RelationGetRelationName(rel)),
errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
"Consider a function index of an MD5 hash of the value, "
- "or use full text indexing.")));
+ "or use full text indexing."),
+ errtableconstraint(heapRel,
+ RelationGetRelationName(rel))));
/*----------
* If we will need to split the page to put the item on this page,
elog(ERROR, "index \"%s\" already contains data",
RelationGetRelationName(index));
- buildstate.spool = _bt_spoolinit(index, indexInfo->ii_Unique, false);
+ buildstate.spool = _bt_spoolinit(heap, index, indexInfo->ii_Unique, false);
/*
* If building a unique index, put dead tuples in a second spool to keep
* them out of the uniqueness check.
*/
if (indexInfo->ii_Unique)
- buildstate.spool2 = _bt_spoolinit(index, false, true);
+ buildstate.spool2 = _bt_spoolinit(heap, index, false, true);
/* do the heap scan */
reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
struct BTSpool
{
Tuplesortstate *sortstate; /* state data for tuplesort.c */
+ Relation heap;
Relation index;
bool isunique;
};
*/
typedef struct BTWriteState
{
+ Relation heap;
Relation index;
bool btws_use_wal; /* dump pages to WAL? */
BlockNumber btws_pages_alloced; /* # pages allocated */
* create and initialize a spool structure
*/
BTSpool *
-_bt_spoolinit(Relation index, bool isunique, bool isdead)
+_bt_spoolinit(Relation heap, Relation index, bool isunique, bool isdead)
{
BTSpool *btspool = (BTSpool *) palloc0(sizeof(BTSpool));
int btKbytes;
+ btspool->heap = heap;
btspool->index = index;
btspool->isunique = isunique;
* work_mem.
*/
btKbytes = isdead ? work_mem : maintenance_work_mem;
- btspool->sortstate = tuplesort_begin_index_btree(index, isunique,
+ btspool->sortstate = tuplesort_begin_index_btree(heap, index, isunique,
btKbytes, false);
return btspool;
if (btspool2)
tuplesort_performsort(btspool2->sortstate);
+ wstate.heap = btspool->heap;
wstate.index = btspool->index;
/*
RelationGetRelationName(wstate->index)),
errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
"Consider a function index of an MD5 hash of the value, "
- "or use full text indexing.")));
+ "or use full text indexing."),
+ errtableconstraint(wstate->heap,
+ RelationGetRelationName(wstate->index))));
/*
* Check to see if page is "full". It's definitely full if the item won't
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)
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 */
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);
}
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);
* to pg_constraint.
*/
- ccbin = domainAddConstraint(HeapTupleGetOid(tup), typTup->typnamespace,
+ ccbin = domainAddConstraint(domainoid, typTup->typnamespace,
typTup->typbasetype, typTup->typtypmod,
constr, NameStr(typTup->typname));
&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);
/*
* ExecRelCheck --- check that tuple meets constraints for result relation
+ *
+ * Returns NULL if OK, else name of failed check constraint
*/
static const char *
ExecRelCheck(ResultRelInfo *resultRelInfo,
errmsg("null value in column \"%s\" violates not-null constraint",
NameStr(rel->rd_att->attrs[attrChk - 1]->attname)),
errdetail("Failing row contains %s.",
- ExecBuildSlotValueDescription(slot, 64))));
+ ExecBuildSlotValueDescription(slot, 64)),
+ errtablecol(rel, attrChk)));
}
}
errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
RelationGetRelationName(rel), failed),
errdetail("Failing row contains %s.",
- ExecBuildSlotValueDescription(slot, 64))));
+ ExecBuildSlotValueDescription(slot, 64)),
+ errtableconstraint(rel, failed)));
}
}
ereport(ERROR,
(errcode(ERRCODE_NOT_NULL_VIOLATION),
errmsg("domain %s does not allow null values",
- format_type_be(ctest->resulttype))));
+ format_type_be(ctest->resulttype)),
+ errdatatype(ctest->resulttype)));
break;
case DOM_CONSTRAINT_CHECK:
{
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("value for domain %s violates check constraint \"%s\"",
format_type_be(ctest->resulttype),
- con->name)));
+ con->name),
+ errdomainconstraint(ctest->resulttype,
+ con->name)));
econtext->domainValue_datum = save_datum;
econtext->domainValue_isNull = save_isNull;
errmsg("could not create exclusion constraint \"%s\"",
RelationGetRelationName(index)),
errdetail("Key %s conflicts with key %s.",
- error_new, error_existing)));
+ error_new, error_existing),
+ errtableconstraint(heap,
+ RelationGetRelationName(index))));
else
ereport(ERROR,
(errcode(ERRCODE_EXCLUSION_VIOLATION),
errmsg("conflicting key value violates exclusion constraint \"%s\"",
RelationGetRelationName(index)),
errdetail("Key %s conflicts with existing key %s.",
- error_new, error_existing)));
+ error_new, error_existing),
+ errtableconstraint(heap,
+ RelationGetRelationName(index))));
}
index_endscan(index_scan);
*/
#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"
/*
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:
{
(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:
* 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;
*/
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 */
+}
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);
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
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),
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))));
}
}
+/*
+ * 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
*
/* 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;
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);
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
*
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);
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);
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);
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);
* 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: */
}
Tuplesortstate *
-tuplesort_begin_index_btree(Relation indexRel,
+tuplesort_begin_index_btree(Relation heapRel,
+ Relation indexRel,
bool enforceUnique,
int workMem, bool randomAccess)
{
state->readtup = readtup_index;
state->reversedirection = reversedirection_index_btree;
+ state->heapRel = heapRel;
state->indexRel = indexRel;
state->indexScanKey = _bt_mkscankey_nodata(indexRel);
state->enforceUnique = enforceUnique;
}
Tuplesortstate *
-tuplesort_begin_index_hash(Relation indexRel,
+tuplesort_begin_index_hash(Relation heapRel,
+ Relation indexRel,
uint32 hash_mask,
int workMem, bool randomAccess)
{
state->readtup = readtup_index;
state->reversedirection = reversedirection_index_hash;
+ state->heapRel = heapRel;
state->indexRel = indexRel;
state->hash_mask = hash_mask;
RelationGetRelationName(state->indexRel)),
errdetail("Key %s is duplicated.",
BuildIndexValueDescription(state->indexRel,
- values, isnull))));
+ values, isnull)),
+ errtableconstraint(state->heapRel,
+ RelationGetRelationName(state->indexRel))));
}
/*
/* hashsort.c */
typedef struct HSpool HSpool; /* opaque struct in hashsort.c */
-extern HSpool *_h_spoolinit(Relation index, uint32 num_buckets);
+extern HSpool *_h_spoolinit(Relation heap, Relation index, uint32 num_buckets);
extern void _h_spooldestroy(HSpool *hspool);
extern void _h_spool(IndexTuple itup, HSpool *hspool);
extern void _h_indexbuild(HSpool *hspool);
*/
typedef struct BTSpool BTSpool; /* opaque type known only within nbtsort.c */
-extern BTSpool *_bt_spoolinit(Relation index, bool isunique, bool isdead);
+extern BTSpool *_bt_spoolinit(Relation heap, Relation index,
+ bool isunique, bool isdead);
extern void _bt_spooldestroy(BTSpool *btspool);
extern void _bt_spool(IndexTuple itup, BTSpool *btspool);
extern void _bt_leafbuild(BTSpool *btspool, BTSpool *spool2);
#define PG_DIAG_INTERNAL_POSITION 'p'
#define PG_DIAG_INTERNAL_QUERY 'q'
#define PG_DIAG_CONTEXT 'W'
+#define PG_DIAG_SCHEMA_NAME 's'
+#define PG_DIAG_TABLE_NAME 't'
+#define PG_DIAG_COLUMN_NAME 'c'
+#define PG_DIAG_DATATYPE_NAME 'd'
+#define PG_DIAG_CONSTRAINT_NAME 'n'
#define PG_DIAG_SOURCE_FILE 'F'
#define PG_DIAG_SOURCE_LINE 'L'
#define PG_DIAG_SOURCE_FUNCTION 'R'
/* domains.c */
extern Datum domain_in(PG_FUNCTION_ARGS);
extern Datum domain_recv(PG_FUNCTION_ARGS);
-extern void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt);
+extern void domain_check(Datum value, bool isnull, Oid domainType,
+ void **extra, MemoryContext mcxt);
+extern int errdatatype(Oid datatypeOid);
+extern int errdomainconstraint(Oid datatypeOid, const char *conname);
/* encode.c */
extern Datum binary_encode(PG_FUNCTION_ARGS);
extern int internalerrposition(int cursorpos);
extern int internalerrquery(const char *query);
+extern int err_generic_string(int field, const char *str);
+
extern int geterrcode(void);
extern int geterrposition(void);
extern int getinternalerrposition(void);
char *detail_log; /* detail error message for server log only */
char *hint; /* hint message */
char *context; /* context message */
+ char *schema_name; /* name of schema */
+ char *table_name; /* name of table */
+ char *column_name; /* name of column */
+ char *datatype_name; /* name of datatype */
+ char *constraint_name; /* name of constraint */
int cursorpos; /* cursor index into query string */
int internalpos; /* cursor index into internalquery */
char *internalquery; /* text of internally-generated query */
extern void RelationInitIndexAccessInfo(Relation relation);
+/*
+ * Routines to support ereport() reports of relation-related errors
+ */
+extern int errtable(Relation rel);
+extern int errtablecol(Relation rel, int attnum);
+extern int errtablecolname(Relation rel, const char *colname);
+extern int errtableconstraint(Relation rel, const char *conname);
+
/*
* Routines for backend startup
*/
extern Tuplesortstate *tuplesort_begin_cluster(TupleDesc tupDesc,
Relation indexRel,
int workMem, bool randomAccess);
-extern Tuplesortstate *tuplesort_begin_index_btree(Relation indexRel,
+extern Tuplesortstate *tuplesort_begin_index_btree(Relation heapRel,
+ Relation indexRel,
bool enforceUnique,
int workMem, bool randomAccess);
-extern Tuplesortstate *tuplesort_begin_index_hash(Relation indexRel,
+extern Tuplesortstate *tuplesort_begin_index_hash(Relation heapRel,
+ Relation indexRel,
uint32 hash_mask,
int workMem, bool randomAccess);
extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
appendPQExpBuffer(&workBuf, libpq_gettext("CONTEXT: %s\n"), val);
}
if (conn->verbosity == PQERRORS_VERBOSE)
+ {
+ val = PQresultErrorField(res, PG_DIAG_SCHEMA_NAME);
+ if (val)
+ appendPQExpBuffer(&workBuf,
+ libpq_gettext("SCHEMA NAME: %s\n"), val);
+ val = PQresultErrorField(res, PG_DIAG_TABLE_NAME);
+ if (val)
+ appendPQExpBuffer(&workBuf,
+ libpq_gettext("TABLE NAME: %s\n"), val);
+ val = PQresultErrorField(res, PG_DIAG_COLUMN_NAME);
+ if (val)
+ appendPQExpBuffer(&workBuf,
+ libpq_gettext("COLUMN NAME: %s\n"), val);
+ val = PQresultErrorField(res, PG_DIAG_DATATYPE_NAME);
+ if (val)
+ appendPQExpBuffer(&workBuf,
+ libpq_gettext("DATATYPE NAME: %s\n"), val);
+ val = PQresultErrorField(res, PG_DIAG_CONSTRAINT_NAME);
+ if (val)
+ appendPQExpBuffer(&workBuf,
+ libpq_gettext("CONSTRAINT NAME: %s\n"), val);
+ }
+ if (conn->verbosity == PQERRORS_VERBOSE)
{
const char *valf;
const char *vall;