Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f225e4b

Browse files
committed
When a row fails a not-null constraint, show row's contents in errdetail.
Simple extension of previous patch for CHECK constraints.
1 parent 8b08deb commit f225e4b

File tree

8 files changed

+23
-1
lines changed

8 files changed

+23
-1
lines changed

src/backend/executor/execMain.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,9 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
15761576
ereport(ERROR,
15771577
(errcode(ERRCODE_NOT_NULL_VIOLATION),
15781578
errmsg("null value in column \"%s\" violates not-null constraint",
1579-
NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
1579+
NameStr(rel->rd_att->attrs[attrChk - 1]->attname)),
1580+
errdetail("Failing row contains %s.",
1581+
ExecBuildSlotValueDescription(slot, 64))));
15801582
}
15811583
}
15821584

src/test/regress/expected/alter_table.out

+10
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ insert into atacc1 (test) values (4);
599599
-- inserting NULL should fail
600600
insert into atacc1 (test) values(NULL);
601601
ERROR: null value in column "test" violates not-null constraint
602+
DETAIL: Failing row contains (null).
602603
-- try adding a second primary key (should fail)
603604
alter table atacc1 add constraint atacc_oid1 primary key(oid);
604605
ERROR: multiple primary keys for table "atacc1" are not allowed
@@ -664,10 +665,13 @@ ERROR: duplicate key value violates unique constraint "atacc_test1"
664665
DETAIL: Key (test, test2)=(4, 4) already exists.
665666
insert into atacc1 (test,test2) values (NULL,3);
666667
ERROR: null value in column "test" violates not-null constraint
668+
DETAIL: Failing row contains (null, 3).
667669
insert into atacc1 (test,test2) values (3, NULL);
668670
ERROR: null value in column "test2" violates not-null constraint
671+
DETAIL: Failing row contains (3, null).
669672
insert into atacc1 (test,test2) values (NULL,NULL);
670673
ERROR: null value in column "test" violates not-null constraint
674+
DETAIL: Failing row contains (null, null).
671675
-- should all succeed
672676
insert into atacc1 (test,test2) values (4,5);
673677
insert into atacc1 (test,test2) values (5,4);
@@ -683,6 +687,7 @@ ERROR: duplicate key value violates unique constraint "atacc1_pkey"
683687
DETAIL: Key (test)=(3) already exists.
684688
insert into atacc1 (test2, test) values (1, NULL);
685689
ERROR: null value in column "test" violates not-null constraint
690+
DETAIL: Failing row contains (null, 1).
686691
drop table atacc1;
687692
-- alter table / alter column [set/drop] not null tests
688693
-- try altering system catalogs, should fail
@@ -733,8 +738,10 @@ create table child (b varchar(255)) inherits (parent);
733738
alter table parent alter a set not null;
734739
insert into parent values (NULL);
735740
ERROR: null value in column "a" violates not-null constraint
741+
DETAIL: Failing row contains (null).
736742
insert into child (a, b) values (NULL, 'foo');
737743
ERROR: null value in column "a" violates not-null constraint
744+
DETAIL: Failing row contains (null, foo).
738745
alter table parent alter a drop not null;
739746
insert into parent values (NULL);
740747
insert into child (a, b) values (NULL, 'foo');
@@ -746,13 +753,16 @@ delete from parent;
746753
alter table only parent alter a set not null;
747754
insert into parent values (NULL);
748755
ERROR: null value in column "a" violates not-null constraint
756+
DETAIL: Failing row contains (null).
749757
alter table child alter a set not null;
750758
insert into child (a, b) values (NULL, 'foo');
751759
ERROR: null value in column "a" violates not-null constraint
760+
DETAIL: Failing row contains (null, foo).
752761
delete from child;
753762
alter table child alter a set not null;
754763
insert into child (a, b) values (NULL, 'foo');
755764
ERROR: null value in column "a" violates not-null constraint
765+
DETAIL: Failing row contains (null, foo).
756766
drop table child;
757767
drop table parent;
758768
-- test setting and removing default values

src/test/regress/expected/domain.out

+4
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
206206
ERROR: domain dnotnull does not allow null values
207207
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
208208
ERROR: null value in column "col3" violates not-null constraint
209+
DETAIL: Failing row contains (a, b, null, d, c).
209210
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
210211
-- Test copy
211212
COPY nulltest FROM stdin; --fail
212213
ERROR: null value in column "col3" violates not-null constraint
214+
DETAIL: Failing row contains (a, b, null, d, d).
213215
CONTEXT: COPY nulltest, line 1: "a b \N d d"
214216
COPY nulltest FROM stdin; --fail
215217
ERROR: domain dcheck does not allow null values
@@ -264,12 +266,14 @@ create table defaulttest
264266
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "defaulttest_pkey" for table "defaulttest"
265267
insert into defaulttest(col4) values(0); -- fails, col5 defaults to null
266268
ERROR: null value in column "col5" violates not-null constraint
269+
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
267270
alter table defaulttest alter column col5 drop default;
268271
insert into defaulttest default values; -- succeeds, inserts domain default
269272
-- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrong
270273
alter table defaulttest alter column col5 set default null;
271274
insert into defaulttest(col4) values(0); -- fails
272275
ERROR: null value in column "col5" violates not-null constraint
276+
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
273277
alter table defaulttest alter column col5 drop default;
274278
insert into defaulttest default values;
275279
insert into defaulttest default values;

src/test/regress/expected/inherit.out

+1
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);
539539
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "z_pkey" for table "z"
540540
INSERT INTO z VALUES (NULL, 'text'); -- should fail
541541
ERROR: null value in column "aa" violates not-null constraint
542+
DETAIL: Failing row contains (null, text).
542543
-- Check UPDATE with inherited target and an inherited source table
543544
create temp table foo(f1 int, f2 int);
544545
create temp table foo2(f3 int) inherits (foo);

src/test/regress/expected/insert.out

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
create table inserttest (col1 int4, col2 int4 NOT NULL, col3 text default 'testing');
55
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT, DEFAULT);
66
ERROR: null value in column "col2" violates not-null constraint
7+
DETAIL: Failing row contains (null, null, testing).
78
insert into inserttest (col2, col3) values (3, DEFAULT);
89
insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
910
insert into inserttest values (DEFAULT, 5, 'test');

src/test/regress/expected/sequence.out

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ INSERT INTO serialTest VALUES ('bar');
88
INSERT INTO serialTest VALUES ('force', 100);
99
INSERT INTO serialTest VALUES ('wrong', NULL);
1010
ERROR: null value in column "f2" violates not-null constraint
11+
DETAIL: Failing row contains (wrong, null).
1112
SELECT * FROM serialTest;
1213
f1 | f2
1314
-------+-----

src/test/regress/expected/sequence_1.out

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ INSERT INTO serialTest VALUES ('bar');
88
INSERT INTO serialTest VALUES ('force', 100);
99
INSERT INTO serialTest VALUES ('wrong', NULL);
1010
ERROR: null value in column "f2" violates not-null constraint
11+
DETAIL: Failing row contains (wrong, null).
1112
SELECT * FROM serialTest;
1213
f1 | f2
1314
-------+-----

src/test/regress/output/constraints.source

+2
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ INSERT INTO PRIMARY_TBL VALUES (4, 'three');
320320
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
321321
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
322322
ERROR: null value in column "i" violates not-null constraint
323+
DETAIL: Failing row contains (null, six).
323324
SELECT '' AS four, * FROM PRIMARY_TBL;
324325
four | i | t
325326
------+---+-------
@@ -340,6 +341,7 @@ INSERT INTO PRIMARY_TBL VALUES (4, 'three');
340341
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
341342
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
342343
ERROR: null value in column "i" violates not-null constraint
344+
DETAIL: Failing row contains (null, six).
343345
SELECT '' AS three, * FROM PRIMARY_TBL;
344346
three | i | t
345347
-------+---+-------

0 commit comments

Comments
 (0)