From b680ae4bdbf1c7fd78e6988bbe8f89e1e1b5db86 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 1 Aug 2009 19:59:41 +0000 Subject: Improve unique-constraint-violation error messages to include the exact values being complained of. In passing, also remove the arbitrary length limitation in the similar error detail message for foreign key violations. Itagaki Takahiro --- src/backend/utils/adt/ri_triggers.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/backend/utils/adt/ri_triggers.c') diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index 5bcbd633043..c89553ce0c0 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -15,7 +15,7 @@ * * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.113 2009/06/11 14:49:04 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.114 2009/08/01 19:59:41 tgl Exp $ * * ---------- */ @@ -3413,11 +3413,8 @@ ri_ReportViolation(RI_QueryKey *qkey, const char *constrname, HeapTuple violator, TupleDesc tupdesc, bool spi_err) { -#define BUFLENGTH 512 - char key_names[BUFLENGTH]; - char key_values[BUFLENGTH]; - char *name_ptr = key_names; - char *val_ptr = key_values; + StringInfoData key_names; + StringInfoData key_values; bool onfk; int idx, key_idx; @@ -3465,6 +3462,8 @@ ri_ReportViolation(RI_QueryKey *qkey, const char *constrname, } /* Get printable versions of the keys involved */ + initStringInfo(&key_names); + initStringInfo(&key_values); for (idx = 0; idx < qkey->nkeypairs; idx++) { int fnum = qkey->keypair[idx][key_idx]; @@ -3476,20 +3475,13 @@ ri_ReportViolation(RI_QueryKey *qkey, const char *constrname, if (!val) val = "null"; - /* - * Go to "..." if name or value doesn't fit in buffer. We reserve 5 - * bytes to ensure we can add comma, "...", null. - */ - if (strlen(name) >= (key_names + BUFLENGTH - 5) - name_ptr || - strlen(val) >= (key_values + BUFLENGTH - 5) - val_ptr) + if (idx > 0) { - sprintf(name_ptr, "..."); - sprintf(val_ptr, "..."); - break; + appendStringInfoString(&key_names, ", "); + appendStringInfoString(&key_values, ", "); } - - name_ptr += sprintf(name_ptr, "%s%s", idx > 0 ? "," : "", name); - val_ptr += sprintf(val_ptr, "%s%s", idx > 0 ? "," : "", val); + appendStringInfoString(&key_names, name); + appendStringInfoString(&key_values, val); } if (onfk) @@ -3498,7 +3490,7 @@ ri_ReportViolation(RI_QueryKey *qkey, const char *constrname, errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"", RelationGetRelationName(fk_rel), constrname), errdetail("Key (%s)=(%s) is not present in table \"%s\".", - key_names, key_values, + key_names.data, key_values.data, RelationGetRelationName(pk_rel)))); else ereport(ERROR, @@ -3507,7 +3499,7 @@ ri_ReportViolation(RI_QueryKey *qkey, const char *constrname, RelationGetRelationName(pk_rel), constrname, RelationGetRelationName(fk_rel)), errdetail("Key (%s)=(%s) is still referenced from table \"%s\".", - key_names, key_values, + key_names.data, key_values.data, RelationGetRelationName(fk_rel)))); } -- cgit v1.2.3