Skip to content

Commit 05f18c6

Browse files
author
Amit Kapila
committed
Added relation name in error messages for constraint checks.
This gives more information to the user about the error and it makes such messages consistent with the other similar messages in the code. Reported-by: Simon Riggs Author: Mahendra Singh and Simon Riggs Reviewed-by: Beena Emerson and Amit Kapila Discussion: https://postgr.es/m/CANP8+j+7YUvQvGxTrCiw77R23enMJ7DFmyA3buR+fa2pKs4XhA@mail.gmail.com
1 parent ff8ca5f commit 05f18c6

17 files changed

+82
-76
lines changed

src/backend/commands/tablecmds.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5288,8 +5288,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
52885288

52895289
ereport(ERROR,
52905290
(errcode(ERRCODE_NOT_NULL_VIOLATION),
5291-
errmsg("column \"%s\" contains null values",
5292-
NameStr(attr->attname)),
5291+
errmsg("column \"%s\" of relation \"%s\" contains null values",
5292+
NameStr(attr->attname),
5293+
RelationGetRelationName(oldrel)),
52935294
errtablecol(oldrel, attn + 1)));
52945295
}
52955296
}
@@ -5304,8 +5305,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
53045305
if (!ExecCheck(con->qualstate, econtext))
53055306
ereport(ERROR,
53065307
(errcode(ERRCODE_CHECK_VIOLATION),
5307-
errmsg("check constraint \"%s\" is violated by some row",
5308-
con->name),
5308+
errmsg("check constraint \"%s\" of relation \"%s\" is violated by some row",
5309+
con->name,
5310+
RelationGetRelationName(oldrel)),
53095311
errtableconstraint(oldrel, con->name)));
53105312
break;
53115313
case CONSTR_FOREIGN:
@@ -5322,11 +5324,13 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
53225324
if (tab->validate_default)
53235325
ereport(ERROR,
53245326
(errcode(ERRCODE_CHECK_VIOLATION),
5325-
errmsg("updated partition constraint for default partition would be violated by some row")));
5327+
errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
5328+
RelationGetRelationName(oldrel))));
53265329
else
53275330
ereport(ERROR,
53285331
(errcode(ERRCODE_CHECK_VIOLATION),
5329-
errmsg("partition constraint is violated by some row")));
5332+
errmsg("partition constraint of relation \"%s\" is violated by some row",
5333+
RelationGetRelationName(oldrel))));
53305334
}
53315335

53325336
/* Write the tuple out to the new relation */
@@ -10160,8 +10164,9 @@ validateCheckConstraint(Relation rel, HeapTuple constrtup)
1016010164
if (!ExecCheck(exprstate, econtext))
1016110165
ereport(ERROR,
1016210166
(errcode(ERRCODE_CHECK_VIOLATION),
10163-
errmsg("check constraint \"%s\" is violated by some row",
10164-
NameStr(constrForm->conname)),
10167+
errmsg("check constraint \"%s\" of relation \"%s\" is violated by some row",
10168+
NameStr(constrForm->conname),
10169+
RelationGetRelationName(rel)),
1016510170
errtableconstraint(rel, NameStr(constrForm->conname))));
1016610171

1016710172
ResetExprContext(econtext);

src/backend/executor/execMain.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,8 +1957,9 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
19571957

19581958
ereport(ERROR,
19591959
(errcode(ERRCODE_NOT_NULL_VIOLATION),
1960-
errmsg("null value in column \"%s\" violates not-null constraint",
1961-
NameStr(att->attname)),
1960+
errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint",
1961+
NameStr(att->attname),
1962+
RelationGetRelationName(orig_rel)),
19621963
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
19631964
errtablecol(orig_rel, attrChk)));
19641965
}

src/test/regress/expected/alter_table.out

Lines changed: 38 additions & 38 deletions
Large diffs are not rendered by default.

src/test/regress/expected/copy2.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ SELECT c, d FROM forcetest WHERE a = 2;
440440
-- should fail with not-null constraint violation
441441
BEGIN;
442442
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NULL(b), FORCE_NOT_NULL(c));
443-
ERROR: null value in column "b" violates not-null constraint
443+
ERROR: null value in column "b" of relation "forcetest" violates not-null constraint
444444
DETAIL: Failing row contains (3, null, , null, null).
445445
CONTEXT: COPY forcetest, line 1: "3,,"""
446446
ROLLBACK;

src/test/regress/expected/create_table.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10);
955955
create table parted_notnull_inh_test (a int default 1, b int not null default 0) partition by list (a);
956956
create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (a not null, b default 1) for values in (1);
957957
insert into parted_notnull_inh_test (b) values (null);
958-
ERROR: null value in column "b" violates not-null constraint
958+
ERROR: null value in column "b" of relation "parted_notnull_inh_test1" violates not-null constraint
959959
DETAIL: Failing row contains (1, null).
960960
-- note that while b's default is overriden, a's default is preserved
961961
\d parted_notnull_inh_test1

src/test/regress/expected/create_table_like.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ CREATE TABLE test_like_id_2 (LIKE test_like_id_1);
9090
b | text | | |
9191

9292
INSERT INTO test_like_id_2 (b) VALUES ('b2');
93-
ERROR: null value in column "a" violates not-null constraint
93+
ERROR: null value in column "a" of relation "test_like_id_2" violates not-null constraint
9494
DETAIL: Failing row contains (null, b2).
9595
SELECT * FROM test_like_id_2; -- identity was not copied
9696
a | b

src/test/regress/expected/domain.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,12 @@ ERROR: domain dnotnull does not allow null values
542542
INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
543543
ERROR: domain dnotnull does not allow null values
544544
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
545-
ERROR: null value in column "col3" violates not-null constraint
545+
ERROR: null value in column "col3" of relation "nulltest" violates not-null constraint
546546
DETAIL: Failing row contains (a, b, null, d, c).
547547
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
548548
-- Test copy
549549
COPY nulltest FROM stdin; --fail
550-
ERROR: null value in column "col3" violates not-null constraint
550+
ERROR: null value in column "col3" of relation "nulltest" violates not-null constraint
551551
DETAIL: Failing row contains (a, b, null, d, d).
552552
CONTEXT: COPY nulltest, line 1: "a b \N d d"
553553
COPY nulltest FROM stdin; --fail
@@ -601,14 +601,14 @@ create table defaulttest
601601
, col8 ddef5
602602
);
603603
insert into defaulttest(col4) values(0); -- fails, col5 defaults to null
604-
ERROR: null value in column "col5" violates not-null constraint
604+
ERROR: null value in column "col5" of relation "defaulttest" violates not-null constraint
605605
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
606606
alter table defaulttest alter column col5 drop default;
607607
insert into defaulttest default values; -- succeeds, inserts domain default
608608
-- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrong
609609
alter table defaulttest alter column col5 set default null;
610610
insert into defaulttest(col4) values(0); -- fails
611-
ERROR: null value in column "col5" violates not-null constraint
611+
ERROR: null value in column "col5" of relation "defaulttest" violates not-null constraint
612612
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
613613
alter table defaulttest alter column col5 drop default;
614614
insert into defaulttest default values;

src/test/regress/expected/generated.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,24 +406,24 @@ CREATE TABLE gtest20a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STOR
406406
INSERT INTO gtest20a (a) VALUES (10);
407407
INSERT INTO gtest20a (a) VALUES (30);
408408
ALTER TABLE gtest20a ADD CHECK (b < 50); -- fails on existing row
409-
ERROR: check constraint "gtest20a_b_check" is violated by some row
409+
ERROR: check constraint "gtest20a_b_check" of relation "gtest20a" is violated by some row
410410
CREATE TABLE gtest20b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
411411
INSERT INTO gtest20b (a) VALUES (10);
412412
INSERT INTO gtest20b (a) VALUES (30);
413413
ALTER TABLE gtest20b ADD CONSTRAINT chk CHECK (b < 50) NOT VALID;
414414
ALTER TABLE gtest20b VALIDATE CONSTRAINT chk; -- fails on existing row
415-
ERROR: check constraint "chk" is violated by some row
415+
ERROR: check constraint "chk" of relation "gtest20b" is violated by some row
416416
-- not-null constraints
417417
CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED NOT NULL);
418418
INSERT INTO gtest21a (a) VALUES (1); -- ok
419419
INSERT INTO gtest21a (a) VALUES (0); -- violates constraint
420-
ERROR: null value in column "b" violates not-null constraint
420+
ERROR: null value in column "b" of relation "gtest21a" violates not-null constraint
421421
DETAIL: Failing row contains (0, null).
422422
CREATE TABLE gtest21b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
423423
ALTER TABLE gtest21b ALTER COLUMN b SET NOT NULL;
424424
INSERT INTO gtest21b (a) VALUES (1); -- ok
425425
INSERT INTO gtest21b (a) VALUES (0); -- violates constraint
426-
ERROR: null value in column "b" violates not-null constraint
426+
ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
427427
DETAIL: Failing row contains (0, null).
428428
ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
429429
INSERT INTO gtest21b (a) VALUES (0); -- ok now

src/test/regress/expected/identity.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ERROR: column "a" of relation "itest4" is not an identity column
186186
ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY IF EXISTS; -- noop
187187
NOTICE: column "a" of relation "itest4" is not an identity column, skipping
188188
INSERT INTO itest4 DEFAULT VALUES; -- fails because NOT NULL is not dropped
189-
ERROR: null value in column "a" violates not-null constraint
189+
ERROR: null value in column "a" of relation "itest4" violates not-null constraint
190190
DETAIL: Failing row contains (null, ).
191191
ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL;
192192
INSERT INTO itest4 DEFAULT VALUES;
@@ -414,7 +414,7 @@ INSERT INTO itest8 VALUES(0), (1);
414414
ALTER TABLE itest8
415415
ADD COLUMN f22 int NOT NULL,
416416
ALTER COLUMN f22 ADD GENERATED ALWAYS AS IDENTITY;
417-
ERROR: column "f22" contains null values
417+
ERROR: column "f22" of relation "itest8" contains null values
418418
TABLE itest8;
419419
f1 | f2 | f3 | f4 | f5
420420
----+----+----+----+----

src/test/regress/expected/index_including.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x
124124
ERROR: duplicate key value violates unique constraint "covering"
125125
DETAIL: Key (c1, c2)=(1, 2) already exists.
126126
INSERT INTO tbl SELECT 1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
127-
ERROR: null value in column "c2" violates not-null constraint
127+
ERROR: null value in column "c2" of relation "tbl" violates not-null constraint
128128
DETAIL: Failing row contains (1, null, 3, (4,4),(4,4)).
129129
INSERT INTO tbl SELECT x, 2*x, NULL, NULL FROM generate_series(1,300) AS x;
130130
explain (costs off)
@@ -202,7 +202,7 @@ INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x
202202
ERROR: duplicate key value violates unique constraint "tbl_pkey"
203203
DETAIL: Key (c1, c2)=(1, 2) already exists.
204204
INSERT INTO tbl SELECT 1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
205-
ERROR: null value in column "c2" violates not-null constraint
205+
ERROR: null value in column "c2" of relation "tbl" violates not-null constraint
206206
DETAIL: Failing row contains (1, null, 3, (4,4),(4,4)).
207207
INSERT INTO tbl SELECT x, 2*x, NULL, NULL FROM generate_series(1,10) AS x;
208208
DROP TABLE tbl;

0 commit comments

Comments
 (0)