From 8c7cd78a7ede9cd16ef4393aca828704e11d2f1e Mon Sep 17 00:00:00 2001 From: amit Date: Wed, 6 Jun 2018 16:46:46 +0900 Subject: [PATCH] Fix bug that partition won't inherit NOT NULL if default specified --- src/backend/commands/tablecmds.c | 4 +++- src/test/regress/expected/create_table.out | 12 ++++++++++++ src/test/regress/sql/create_table.sql | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index cb7d866710..eeddca7190 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2200,7 +2200,9 @@ MergeAttributes(List *schema, List *supers, char relpersistence, */ if (coldef->is_from_parent) { - coldef->is_not_null = restdef->is_not_null; + /* Do not override parent's NOT NULL constraint. */ + if (restdef->is_not_null) + coldef->is_not_null = restdef->is_not_null; coldef->raw_default = restdef->raw_default; coldef->cooked_default = restdef->cooked_default; coldef->constraints = restdef->constraints; diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index 68dda4c0db..f801ea1e98 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -662,6 +662,18 @@ ERROR: column "c" named in partition key does not exist CREATE TABLE part_c PARTITION OF parted (b WITH OPTIONS NOT NULL DEFAULT 0) FOR VALUES IN ('c') PARTITION BY RANGE ((b)); -- create a level-2 partition CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10); +-- check inheriting NOT NULL constraint works correctly +create table parted_notnull_inh_test (a int, b int not null default 0) partition by list (a); +create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (b default 1) for values in (1); +\d parted_notnull_inh_test1 + Table "public.parted_notnull_inh_test1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | + b | integer | | not null | 1 +Partition of: parted_notnull_inh_test FOR VALUES IN (1) + +drop table parted_notnull_inh_test; -- Partition bound in describe output \d+ part_b Table "public.part_b" diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql index 89e1059a71..da9edc26df 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -609,6 +609,12 @@ CREATE TABLE part_c PARTITION OF parted (b WITH OPTIONS NOT NULL DEFAULT 0) FOR -- create a level-2 partition CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10); +-- check inheriting NOT NULL constraint works correctly +create table parted_notnull_inh_test (a int, b int not null default 0) partition by list (a); +create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (b default 1) for values in (1); +\d parted_notnull_inh_test1 +drop table parted_notnull_inh_test; + -- Partition bound in describe output \d+ part_b -- 2.11.0