diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/access/index/genam.c | 38 | ||||
-rw-r--r-- | src/backend/access/nbtree/nbtinsert.c | 12 | ||||
-rw-r--r-- | src/backend/utils/sort/tuplesort.c | 12 |
3 files changed, 38 insertions, 24 deletions
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c index 6482aaa6a70..860e5766034 100644 --- a/src/backend/access/index/genam.c +++ b/src/backend/access/index/genam.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/index/genam.c,v 1.75 2009/08/01 19:59:41 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/index/genam.c,v 1.76 2009/08/01 20:59:17 tgl Exp $ * * NOTES * many of the old access method routines have been turned into @@ -133,13 +133,16 @@ IndexScanEnd(IndexScanDesc scan) } /* - * ReportUniqueViolation -- Report a unique-constraint violation. + * BuildIndexValueDescription * - * The index entry represented by values[]/isnull[] violates the unique - * constraint enforced by this index. Throw a suitable error. + * Construct a string describing the contents of an index entry, in the + * form "(key_name, ...)=(key_value, ...)". This is currently used + * only for building unique-constraint error messages, but we don't want + * to hardwire the spelling of the messages here. */ -void -ReportUniqueViolation(Relation indexRelation, Datum *values, bool *isnull) +char * +BuildIndexValueDescription(Relation indexRelation, + Datum *values, bool *isnull) { /* * XXX for the moment we use the index's tupdesc as a guide to the @@ -148,14 +151,14 @@ ReportUniqueViolation(Relation indexRelation, Datum *values, bool *isnull) * are ever to support non-btree unique indexes. */ TupleDesc tupdesc = RelationGetDescr(indexRelation); - char *key_names; - StringInfoData key_values; + StringInfoData buf; int i; - key_names = pg_get_indexdef_columns(RelationGetRelid(indexRelation), true); + initStringInfo(&buf); + appendStringInfo(&buf, "(%s)=(", + pg_get_indexdef_columns(RelationGetRelid(indexRelation), + true)); - /* Get printable versions of the key values */ - initStringInfo(&key_values); for (i = 0; i < tupdesc->natts; i++) { char *val; @@ -173,16 +176,13 @@ ReportUniqueViolation(Relation indexRelation, Datum *values, bool *isnull) } if (i > 0) - appendStringInfoString(&key_values, ", "); - appendStringInfoString(&key_values, val); + appendStringInfoString(&buf, ", "); + appendStringInfoString(&buf, val); } - ereport(ERROR, - (errcode(ERRCODE_UNIQUE_VIOLATION), - errmsg("duplicate key value violates unique constraint \"%s\"", - RelationGetRelationName(indexRelation)), - errdetail("Key (%s)=(%s) already exists.", - key_names, key_values.data))); + appendStringInfoChar(&buf, ')'); + + return buf.data; } diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c index ee3d89fd066..dd3736ad8a9 100644 --- a/src/backend/access/nbtree/nbtinsert.c +++ b/src/backend/access/nbtree/nbtinsert.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.172 2009/08/01 19:59:41 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.173 2009/08/01 20:59:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -365,7 +365,7 @@ _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel, * This is a definite conflict. Break the tuple down * into datums and report the error. But first, make * sure we release the buffer locks we're holding --- - * the error reporting code could make catalog accesses, + * BuildIndexValueDescription could make catalog accesses, * which in the worst case might touch this same index * and cause deadlocks. */ @@ -379,7 +379,13 @@ _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel, index_deform_tuple(itup, RelationGetDescr(rel), values, isnull); - ReportUniqueViolation(rel, values, isnull); + ereport(ERROR, + (errcode(ERRCODE_UNIQUE_VIOLATION), + errmsg("duplicate key value violates unique constraint \"%s\"", + RelationGetRelationName(rel)), + errdetail("Key %s already exists.", + BuildIndexValueDescription(rel, + values, isnull)))); } } else if (all_dead) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 6d07296d596..cb89f65a914 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -91,7 +91,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.91 2009/06/11 14:49:06 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.92 2009/08/01 20:59:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -2801,11 +2801,19 @@ comparetup_index_btree(const SortTuple *a, const SortTuple *b, * error in that case. */ if (state->enforceUnique && !equal_hasnull && tuple1 != tuple2) + { + Datum values[INDEX_MAX_KEYS]; + bool isnull[INDEX_MAX_KEYS]; + + index_deform_tuple(tuple1, tupDes, values, isnull); ereport(ERROR, (errcode(ERRCODE_UNIQUE_VIOLATION), errmsg("could not create unique index \"%s\"", RelationGetRelationName(state->indexRel)), - errdetail("Table contains duplicated values."))); + errdetail("Key %s is duplicated.", + BuildIndexValueDescription(state->indexRel, + values, isnull)))); + } /* * If key values are equal, we sort on ItemPointer. This does not affect |