From f3b3ef17335822f50c3ab209d996ff626f61eec4 Mon Sep 17 00:00:00 2001 From: amit Date: Wed, 6 Jun 2018 16:46:46 +0900 Subject: [PATCH v3] Fix bug regarding partition column option inheritance It appears that the coding pattern in MergeAttributes causes the NOT NULL constraint and default value of a column from not being properly inherited from the parent's definition. --- src/backend/commands/tablecmds.c | 34 +++++++++++++++++++++++++----- src/test/regress/expected/create_table.out | 13 ++++++++++++ src/test/regress/sql/create_table.sql | 7 ++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index f4745f3a9a..ae0ba9deb1 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2208,14 +2208,38 @@ MergeAttributes(List *schema, List *supers, char relpersistence, if (strcmp(coldef->colname, restdef->colname) == 0) { /* - * merge the column options into the column from the - * parent + * Merge the column options specified for the partition + * into the column definition inherited from the parent. */ if (coldef->is_from_parent) { - coldef->is_not_null = restdef->is_not_null; - coldef->raw_default = restdef->raw_default; - coldef->cooked_default = restdef->cooked_default; + coldef->is_not_null = (coldef->is_not_null || + restdef->is_not_null); + + /* + * If the partition's definition specifies a default, + * it's in restdef->raw_default, which if non-NULL, + * overrides the parent's default that's in + * coldef->cooked_default. + */ + if (restdef->raw_default) + { + coldef->raw_default = restdef->raw_default; + coldef->cooked_default = NULL; + } + + /* + * coldef->cooked_default would contain the inherited + * default, unless overridden above. Don't try to + * override it with NULL. + */ + Assert(restdef->cooked_default == NULL); + + /* + * Parent's constraints, if any, have been saved in + * 'constraints', which are passed back to the caller, + * so it's okay to overwrite the variable like this. + */ coldef->constraints = restdef->constraints; coldef->is_from_parent = false; list_delete_cell(schema, rest, prev); diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index 660398bdbe..9e771e58a2 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -657,6 +657,19 @@ 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 that NOT NULL and default value are inherited correctly +create table parted_notnull_inh_test (a int default 1, b int not null default 0) partition by list (a); +create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (a not null, b default 1) for values in (1); +-- note that while b's default is overriden, a's default is preserved +\d parted_notnull_inh_test1 + Table "public.parted_notnull_inh_test1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | not null | 1 + 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 16f2edb897..bde03f194f 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -604,6 +604,13 @@ 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 that NOT NULL and default value are inherited correctly +create table parted_notnull_inh_test (a int default 1, b int not null default 0) partition by list (a); +create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (a not null, b default 1) for values in (1); +-- note that while b's default is overriden, a's default is preserved +\d parted_notnull_inh_test1 +drop table parted_notnull_inh_test; + -- Partition bound in describe output \d+ part_b -- 2.11.0