@@ -679,14 +679,67 @@ INSERT INTO gtest21a (a) VALUES (1); -- ok
679679INSERT INTO gtest21a (a) VALUES (0); -- violates constraint
680680ERROR: null value in column "b" of relation "gtest21a" violates not-null constraint
681681DETAIL: Failing row contains (0, null).
682- CREATE TABLE gtest21b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
682+ -- also check with table constraint syntax
683+ CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED, CONSTRAINT cc NOT NULL b);
684+ INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
685+ ERROR: null value in column "b" of relation "gtest21ax" violates not-null constraint
686+ DETAIL: Failing row contains (0, null).
687+ INSERT INTO gtest21ax (a) VALUES (1); --ok
688+ -- SET EXPRESSION supports not null constraint
689+ ALTER TABLE gtest21ax ALTER COLUMN b SET EXPRESSION AS (nullif(a, 1)); --error
690+ ERROR: column "b" of relation "gtest21ax" contains null values
691+ DROP TABLE gtest21ax;
692+ CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
693+ ALTER TABLE gtest21ax ADD CONSTRAINT cc NOT NULL b;
694+ INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
695+ ERROR: null value in column "b" of relation "gtest21ax" violates not-null constraint
696+ DETAIL: Failing row contains (0, null).
697+ DROP TABLE gtest21ax;
698+ CREATE TABLE gtest21b (a int, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
683699ALTER TABLE gtest21b ALTER COLUMN b SET NOT NULL;
684700INSERT INTO gtest21b (a) VALUES (1); -- ok
685- INSERT INTO gtest21b (a) VALUES (0); -- violates constraint
701+ INSERT INTO gtest21b (a) VALUES (2), ( 0); -- violates constraint
686702ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
687703DETAIL: Failing row contains (0, null).
704+ INSERT INTO gtest21b (a) VALUES (NULL); -- error
705+ ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
706+ DETAIL: Failing row contains (null, null).
688707ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
689708INSERT INTO gtest21b (a) VALUES (0); -- ok now
709+ -- not-null constraint with partitioned table
710+ CREATE TABLE gtestnn_parent (
711+ f1 int,
712+ f2 bigint,
713+ f3 bigint GENERATED ALWAYS AS (nullif(f1, 1) + nullif(f2, 10)) STORED NOT NULL
714+ ) PARTITION BY RANGE (f1);
715+ CREATE TABLE gtestnn_child PARTITION OF gtestnn_parent FOR VALUES FROM (1) TO (5);
716+ CREATE TABLE gtestnn_childdef PARTITION OF gtestnn_parent default;
717+ -- check the error messages
718+ INSERT INTO gtestnn_parent VALUES (2, 2, default), (3, 5, default), (14, 12, default); -- ok
719+ INSERT INTO gtestnn_parent VALUES (1, 2, default); -- error
720+ ERROR: null value in column "f3" of relation "gtestnn_child" violates not-null constraint
721+ DETAIL: Failing row contains (1, 2, null).
722+ INSERT INTO gtestnn_parent VALUES (2, 10, default); -- error
723+ ERROR: null value in column "f3" of relation "gtestnn_child" violates not-null constraint
724+ DETAIL: Failing row contains (2, 10, null).
725+ ALTER TABLE gtestnn_parent ALTER COLUMN f3 SET EXPRESSION AS (nullif(f1, 2) + nullif(f2, 11)); -- error
726+ ERROR: column "f3" of relation "gtestnn_child" contains null values
727+ INSERT INTO gtestnn_parent VALUES (10, 11, default); -- ok
728+ SELECT * FROM gtestnn_parent ORDER BY f1, f2, f3;
729+ f1 | f2 | f3
730+ ----+----+----
731+ 2 | 2 | 4
732+ 3 | 5 | 8
733+ 10 | 11 | 21
734+ 14 | 12 | 26
735+ (4 rows)
736+
737+ -- test ALTER TABLE ADD COLUMN
738+ ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 14) + nullif(f2, 10)) STORED; -- error
739+ ERROR: column "c" of relation "gtestnn_childdef" contains null values
740+ ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 13) + nullif(f2, 5)) STORED; -- error
741+ ERROR: column "c" of relation "gtestnn_child" contains null values
742+ ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 4) + nullif(f2, 6)) STORED; -- ok
690743-- index constraints
691744CREATE TABLE gtest22a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a / 2) STORED UNIQUE);
692745INSERT INTO gtest22a VALUES (2);
@@ -847,6 +900,10 @@ CREATE TABLE gtest24r (a int PRIMARY KEY, b gtestdomain1range GENERATED ALWAYS A
847900INSERT INTO gtest24r (a) VALUES (4); -- ok
848901INSERT INTO gtest24r (a) VALUES (6); -- error
849902ERROR: value for domain gtestdomain1 violates check constraint "gtestdomain1_check"
903+ CREATE TABLE gtest24at (a int PRIMARY KEY);
904+ ALTER TABLE gtest24at ADD COLUMN b gtestdomain1 GENERATED ALWAYS AS (a * 2) STORED; -- ok
905+ CREATE TABLE gtest24ata (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
906+ ALTER TABLE gtest24ata ALTER COLUMN b TYPE gtestdomain1; -- ok
850907CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
851908CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) STORED);
852909INSERT INTO gtest24nn (a) VALUES (4); -- ok
@@ -1154,6 +1211,15 @@ DETAIL: Column "x" is a generated column.
11541211ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
11551212ERROR: column "x" of relation "gtest27" is a generated column
11561213HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.
1214+ -- test not-null checking during table rewrite
1215+ INSERT INTO gtest27 (a, b) VALUES (NULL, NULL);
1216+ ALTER TABLE gtest27
1217+ DROP COLUMN x,
1218+ ALTER COLUMN a TYPE bigint,
1219+ ALTER COLUMN b TYPE bigint,
1220+ ADD COLUMN x bigint GENERATED ALWAYS AS ((a + b) * 2) STORED NOT NULL; -- error
1221+ ERROR: column "x" of relation "gtest27" contains null values
1222+ DELETE FROM gtest27 WHERE a IS NULL AND b IS NULL;
11571223-- It's possible to alter the column types this way:
11581224ALTER TABLE gtest27
11591225 DROP COLUMN x,
0 commit comments