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

Commit43085a4

Browse files
committed
Fix for dropped columns in a partitioned table's default partition
We forgot to map column numbers to/from the default partition forvarious operations, leading to valid cases failing with spuriouserrors, such asERROR: attribute N of type some_partition has been droppedIt was also possible that the search for conflicting rows in the defaultpartition when attaching another partition would fail to detect some.Secondarily, it was also possible that such a search should be skipped(because the constraint was implied) but wasn't.Fix all this by mapping column numbers when necessary.Reported by: Daniel WilchesAuthor: Amit LangoteDiscussion:https://postgr.es/m/15873-8c61945d6b3ef87c@postgresql.org
1 parent6827e46 commit43085a4

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14973,6 +14973,13 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1497314973
defaultrel=heap_open(defaultPartOid,NoLock);
1497414974
defPartConstraint=
1497514975
get_proposed_default_constraint(partBoundConstraint);
14976+
/*
14977+
* Map the Vars in the constraint expression from rel's attnos to
14978+
* defaultrel's.
14979+
*/
14980+
defPartConstraint=
14981+
map_partition_varattnos(defPartConstraint,
14982+
1,defaultrel,rel,NULL);
1497614983
QueuePartitionConstraintValidation(wqueue,defaultrel,
1497714984
defPartConstraint, true);
1497814985

‎src/backend/partitioning/partbounds.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ check_default_partition_contents(Relation parent, Relation default_rel,
609609
:get_qual_for_range(parent,new_spec, false);
610610
def_part_constraints=
611611
get_proposed_default_constraint(new_part_constraints);
612+
/*
613+
* Map the Vars in the constraint expression from parent's attnos to
614+
* default_rel's.
615+
*/
616+
def_part_constraints=
617+
map_partition_varattnos(def_part_constraints,1,default_rel,
618+
parent,NULL);
612619

613620
/*
614621
* If the existing constraints on the default partition imply that it will
@@ -637,7 +644,6 @@ check_default_partition_contents(Relation parent, Relation default_rel,
637644
{
638645
Oidpart_relid=lfirst_oid(lc);
639646
Relationpart_rel;
640-
Expr*constr;
641647
Expr*partition_constraint;
642648
EState*estate;
643649
HeapTupletuple;
@@ -654,6 +660,15 @@ check_default_partition_contents(Relation parent, Relation default_rel,
654660
{
655661
part_rel=heap_open(part_relid,NoLock);
656662

663+
/*
664+
* Map the Vars in the constraint expression from default_rel's
665+
* the sub-partition's.
666+
*/
667+
partition_constraint=make_ands_explicit(def_part_constraints);
668+
partition_constraint= (Expr*)
669+
map_partition_varattnos((List*)partition_constraint,1,
670+
part_rel,default_rel,NULL);
671+
657672
/*
658673
* If the partition constraints on default partition child imply
659674
* that it will not contain any row that would belong to the new
@@ -671,7 +686,10 @@ check_default_partition_contents(Relation parent, Relation default_rel,
671686
}
672687
}
673688
else
689+
{
674690
part_rel=default_rel;
691+
partition_constraint=make_ands_explicit(def_part_constraints);
692+
}
675693

676694
/*
677695
* Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
@@ -693,10 +711,6 @@ check_default_partition_contents(Relation parent, Relation default_rel,
693711
}
694712

695713
tupdesc=CreateTupleDescCopy(RelationGetDescr(part_rel));
696-
constr=linitial(def_part_constraints);
697-
partition_constraint= (Expr*)
698-
map_partition_varattnos((List*)constr,
699-
1,part_rel,parent,NULL);
700714
estate=CreateExecutorState();
701715

702716
/* Build expression execution states for partition check quals */

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,8 @@ DROP USER regress_alter_table_user1;
41094109
-- default partition
41104110
create table defpart_attach_test (a int) partition by list (a);
41114111
create table defpart_attach_test1 partition of defpart_attach_test for values in (1);
4112-
create table defpart_attach_test_d (like defpart_attach_test);
4112+
create table defpart_attach_test_d (b int, a int);
4113+
alter table defpart_attach_test_d drop b;
41134114
insert into defpart_attach_test_d values (1), (2);
41144115
-- error because its constraint as the default partition would be violated
41154116
-- by the row containing 1
@@ -4120,6 +4121,12 @@ alter table defpart_attach_test_d add check (a > 1);
41204121
-- should be attached successfully and without needing to be scanned
41214122
alter table defpart_attach_test attach partition defpart_attach_test_d default;
41224123
INFO: partition constraint for table "defpart_attach_test_d" is implied by existing constraints
4124+
-- check that attaching a partition correctly reports any rows in the default
4125+
-- partition that should not be there for the new partition to be attached
4126+
-- successfully
4127+
create table defpart_attach_test_2 (like defpart_attach_test_d);
4128+
alter table defpart_attach_test attach partition defpart_attach_test_2 for values in (2);
4129+
ERROR: updated partition constraint for default partition would be violated by some row
41234130
drop table defpart_attach_test;
41244131
-- check combinations of temporary and permanent relations when attaching
41254132
-- partitions.

‎src/test/regress/sql/alter_table.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2699,7 +2699,8 @@ DROP USER regress_alter_table_user1;
26992699
-- default partition
27002700
createtabledefpart_attach_test (aint) partition by list (a);
27012701
createtabledefpart_attach_test1 partition of defpart_attach_test forvaluesin (1);
2702-
createtabledefpart_attach_test_d (like defpart_attach_test);
2702+
createtabledefpart_attach_test_d (bint, aint);
2703+
altertable defpart_attach_test_d drop b;
27032704
insert into defpart_attach_test_dvalues (1), (2);
27042705

27052706
-- error because its constraint as the default partition would be violated
@@ -2711,6 +2712,12 @@ alter table defpart_attach_test_d add check (a > 1);
27112712
-- should be attached successfully and without needing to be scanned
27122713
altertable defpart_attach_test attach partition defpart_attach_test_d default;
27132714

2715+
-- check that attaching a partition correctly reports any rows in the default
2716+
-- partition that should not be there for the new partition to be attached
2717+
-- successfully
2718+
createtabledefpart_attach_test_2 (like defpart_attach_test_d);
2719+
altertable defpart_attach_test attach partition defpart_attach_test_2 forvaluesin (2);
2720+
27142721
droptable defpart_attach_test;
27152722

27162723
-- check combinations of temporary and permanent relations when attaching

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp