Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc370910

Browse files
committed
Fix propagating attnotnull in multiple inheritance
In one of the many strange corner cases of multiple inheritance beingused, commitb0e96f3 missed a CommandCounterIncrement() call afterupdating the attnotnull flag during ALTER TABLE ADD COLUMN, which causeda catalog tuple to be update attempted twice in the same command, givingrise to a "tuple already updated by self" error. Add the missing callto solve that, and a test case that reproduces the scenario.As a (perhaps surprising) secondary effect, this CCI addition triggersanother behavior change: when a primary key is added to a parentpartitioned table and the column in an existing partition does not havea not-null constraint, we no longer error out. This will probably be awelcome change by some users, and I think it's unlikely that anybodywill miss the old behavior.Reported-by: Alexander Lakhin <exclusion@gmail.com>Discussion:http://postgr.es/m/045dec3f-9b3d-aa44-0c99-85f6992306c7@gmail.com
1 parent6ff21c0 commitc370910

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7757,6 +7757,10 @@ set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, bool recurse,
77577757
List *children;
77587758
ListCell *lc;
77597759

7760+
/* Make above update visible, for multiple inheritance cases */
7761+
if (retval)
7762+
CommandCounterIncrement();
7763+
77607764
children = find_inheritance_children(RelationGetRelid(rel), lockmode);
77617765
foreach(lc, children)
77627766
{

‎src/test/regress/expected/constraints.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,13 +1010,21 @@ ERROR: constraint "cnn_parent_pkey" of relation "cnn_parent" does not exist
10101010
create table cnn2_parted(a int primary key) partition by list (a);
10111011
create table cnn2_part1(a int);
10121012
alter table cnn2_parted attach partition cnn2_part1 for values in (1);
1013-
ERROR: primary key column "a" is not marked NOT NULL
1013+
insert into cnn2_part1 values (null);
1014+
ERROR: null value in column "a" of relation "cnn2_part1" violates not-null constraint
1015+
DETAIL: Failing row contains (null).
10141016
drop table cnn2_parted, cnn2_part1;
10151017
create table cnn2_parted(a int not null) partition by list (a);
10161018
create table cnn2_part1(a int primary key);
10171019
alter table cnn2_parted attach partition cnn2_part1 for values in (1);
10181020
ERROR: column "a" in child table must be marked NOT NULL
10191021
drop table cnn2_parted, cnn2_part1;
1022+
create table cnn2_parted(a int) partition by list (a);
1023+
create table cnn_part1 partition of cnn2_parted for values in (1, null);
1024+
insert into cnn_part1 values (null);
1025+
alter table cnn2_parted add primary key (a);
1026+
ERROR: column "a" of relation "cnn_part1" contains null values
1027+
drop table cnn2_parted;
10201028
-- columns in regular and LIKE inheritance should be marked not-nullable
10211029
-- for primary keys, even if those are deferred
10221030
CREATE TABLE notnull_tbl4 (a INTEGER PRIMARY KEY INITIALLY DEFERRED);

‎src/test/regress/expected/inherit.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,14 @@ Child tables: cc1,
21772177

21782178
alter table pp1 add primary key (f1);
21792179
-- Leave these tables around, for pg_upgrade testing
2180+
-- Test a not-null addition that must walk down the hierarchy
2181+
CREATE TABLE inh_parent ();
2182+
CREATE TABLE inh_child (i int) INHERITS (inh_parent);
2183+
CREATE TABLE inh_grandchild () INHERITS (inh_parent, inh_child);
2184+
ALTER TABLE inh_parent ADD COLUMN i int NOT NULL;
2185+
NOTICE: merging definition of column "i" for child "inh_child"
2186+
NOTICE: merging definition of column "i" for child "inh_grandchild"
2187+
drop table inh_parent, inh_child, inh_grandchild;
21802188
-- Test the same constraint name for different columns in different parents
21812189
create table inh_parent1(a int constraint nn not null);
21822190
create table inh_parent2(b int constraint nn not null);

‎src/test/regress/sql/constraints.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,20 @@ ALTER TABLE cnn_parent DROP CONSTRAINT cnn_parent_pkey;
661661
createtablecnn2_parted(aintprimary key) partition by list (a);
662662
createtablecnn2_part1(aint);
663663
altertable cnn2_parted attach partition cnn2_part1 forvaluesin (1);
664+
insert into cnn2_part1values (null);
664665
droptable cnn2_parted, cnn2_part1;
665666

666667
createtablecnn2_parted(aintnot null) partition by list (a);
667668
createtablecnn2_part1(aintprimary key);
668669
altertable cnn2_parted attach partition cnn2_part1 forvaluesin (1);
669670
droptable cnn2_parted, cnn2_part1;
670671

672+
createtablecnn2_parted(aint) partition by list (a);
673+
createtablecnn_part1 partition of cnn2_parted forvaluesin (1,null);
674+
insert into cnn_part1values (null);
675+
altertable cnn2_parted addprimary key (a);
676+
droptable cnn2_parted;
677+
671678
-- columns in regular and LIKE inheritance should be marked not-nullable
672679
-- for primary keys, even if those are deferred
673680
CREATETABLEnotnull_tbl4 (aINTEGERPRIMARY KEY INITIALLY DEFERRED);

‎src/test/regress/sql/inherit.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,13 @@ alter table pp1 alter column f1 drop not null;
804804
altertable pp1 addprimary key (f1);
805805
-- Leave these tables around, for pg_upgrade testing
806806

807+
-- Test a not-null addition that must walk down the hierarchy
808+
CREATETABLEinh_parent ();
809+
CREATETABLEinh_child (iint) INHERITS (inh_parent);
810+
CREATETABLEinh_grandchild () INHERITS (inh_parent, inh_child);
811+
ALTERTABLE inh_parent ADD COLUMN iintNOT NULL;
812+
droptable inh_parent, inh_child, inh_grandchild;
813+
807814
-- Test the same constraint name for different columns in different parents
808815
createtableinh_parent1(aintconstraint nnnot null);
809816
createtableinh_parent2(bintconstraint nnnot null);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp