From a6d26ae5ca55b3bc45bfb8f933201aee51376159 Mon Sep 17 00:00:00 2001 From: Narayanan Venkateswaran Date: Thu, 26 Nov 2026 14:55:51 +0000 Subject: [PATCH v1] Map Vars before checking default subpartition constraints check_default_partition_contents() remapped the updated default partition constraint to each subpartition's attribute numbers before scanning it, but still passed the unmapped def_part_constraints list to PartConstraintImpliedByRelConstraint(). When a subpartition has reordered or dropped columns relative to the default partition, a CHECK or NOT NULL constraint on a different column at the same attribute number can falsely imply the updated default partition constraint. This skips scanning that subpartition and allows creating the new partition, hiding existing conflicting rows from queries on the parent table. In the opposite case, a valid constraint on the actual partition key column fails to skip the scan. Fix by mapping def_part_constraints directly before calling PartConstraintImpliedByRelConstraint(), and calling make_ands_explicit() only when a leaf partition needs to be scanned. --- src/backend/partitioning/partbounds.c | 14 +++---- .../test_misc/t/001_constraint_validation.pl | 27 ++++++++++++ src/test/regress/expected/create_table.out | 41 +++++++++++++++++++ src/test/regress/sql/create_table.sql | 31 ++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c index 9da8d0000bb..b4e3efa59ef 100644 --- a/src/backend/partitioning/partbounds.c +++ b/src/backend/partitioning/partbounds.c @@ -3285,6 +3285,7 @@ check_default_partition_contents(Relation parent, Relation default_rel, { Oid part_relid = lfirst_oid(lc); Relation part_rel; + List *part_constraints; Expr *partition_constraint; EState *estate; ExprState *partqualstate = NULL; @@ -3300,13 +3301,11 @@ check_default_partition_contents(Relation parent, Relation default_rel, part_rel = table_open(part_relid, NoLock); /* - * Map the Vars in the constraint expression from default_rel's + * Map the Vars in the constraint expression from default_rel's to * the sub-partition's. */ - partition_constraint = make_ands_explicit(def_part_constraints); - partition_constraint = (Expr *) - map_partition_varattnos((List *) partition_constraint, 1, - part_rel, default_rel); + part_constraints = map_partition_varattnos(def_part_constraints, 1, + part_rel, default_rel); /* * If the partition constraints on default partition child imply @@ -3314,7 +3313,7 @@ check_default_partition_contents(Relation parent, Relation default_rel, * partition, we can avoid scanning the child table. */ if (PartConstraintImpliedByRelConstraint(part_rel, - def_part_constraints)) + part_constraints)) { ereport(DEBUG1, (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints", @@ -3327,7 +3326,7 @@ check_default_partition_contents(Relation parent, Relation default_rel, else { part_rel = default_rel; - partition_constraint = make_ands_explicit(def_part_constraints); + part_constraints = def_part_constraints; } /* @@ -3349,6 +3348,7 @@ check_default_partition_contents(Relation parent, Relation default_rel, continue; } + partition_constraint = make_ands_explicit(part_constraints); estate = CreateExecutorState(); /* Build expression execution states for partition check quals */ diff --git a/src/test/modules/test_misc/t/001_constraint_validation.pl b/src/test/modules/test_misc/t/001_constraint_validation.pl index 6121c5bcae5..4ffacaefc4a 100644 --- a/src/test/modules/test_misc/t/001_constraint_validation.pl +++ b/src/test/modules/test_misc/t/001_constraint_validation.pl @@ -333,6 +333,33 @@ like( 'updated partition constraint for default partition quuux_default1'); run_sql_command('DROP TABLE quuux;'); +# Check default subpartitions whose attribute numbers differ from the default +# partition: a constraint on an unrelated column sharing an attnum must not +# skip scanning, while a constraint on the actual partition key column should. +run_sql_command( + 'CREATE TABLE quuux_reord (a int, b int) PARTITION BY RANGE (a); + CREATE TABLE quuux_reord_def PARTITION OF quuux_reord DEFAULT PARTITION BY RANGE (b); + CREATE TABLE quuux_reord_leaf1 (b int CHECK (b >= 100 AND b < 200), a int); + ALTER TABLE quuux_reord_def ATTACH PARTITION quuux_reord_leaf1 + FOR VALUES FROM (100) TO (200); + CREATE TABLE quuux_reord_leaf2 (c int, a int CHECK (a >= 100), b int); + ALTER TABLE quuux_reord_leaf2 DROP COLUMN c; + ALTER TABLE quuux_reord_def ATTACH PARTITION quuux_reord_leaf2 + FOR VALUES FROM (200) TO (300);'); + +$output = run_sql_command( + 'CREATE TABLE quuux_reord_p1 PARTITION OF quuux_reord FOR VALUES FROM (0) TO (10);' +); +unlike( + $output, + qr/updated partition constraint for default partition "quuux_reord_leaf1" is implied by existing constraints/, + 'quuux_reord_leaf1 not falsely implied by unrelated column'); +like( + $output, + qr/updated partition constraint for default partition "quuux_reord_leaf2" is implied by existing constraints/, + 'quuux_reord_leaf2 implied by existing constraint after attno mapping'); +run_sql_command('DROP TABLE quuux_reord;'); + $node->stop('fast'); done_testing(); diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index 2df8761ae8e..c22f172d329 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -1103,6 +1103,47 @@ insert into defcheck_def values (0, 0); create table defcheck_0 partition of defcheck for values in (0); ERROR: updated partition constraint for default partition "defcheck_def" would be violated by some row drop table defcheck; +-- test that subpartitioned default partitions map column numbers before +-- checking whether a child's constraints imply the updated default constraint +create table defcheck_sub (a int, b int) partition by range (a); +create table defcheck_sub_def partition of defcheck_sub default + partition by range (b); +create table defcheck_sub_leaf1 (b int check (b >= 100 and b < 200), a int); +alter table defcheck_sub_def attach partition defcheck_sub_leaf1 + for values from (100) to (200); +create table defcheck_sub_leaf2 (c int, a int check (a >= 100), b int); +alter table defcheck_sub_leaf2 drop column c; +alter table defcheck_sub_def attach partition defcheck_sub_leaf2 + for values from (200) to (300); +insert into defcheck_sub values (5, 150); +create table defcheck_sub_p1 partition of defcheck_sub + for values from (0) to (10); +ERROR: updated partition constraint for default partition "defcheck_sub_def" would be violated by some row +select tableoid::regclass, a, b from defcheck_sub where a = 5; + tableoid | a | b +--------------------+---+----- + defcheck_sub_leaf1 | 5 | 150 +(1 row) + +drop table defcheck_sub; +-- same for NOT NULL on a reordered column of a default subpartition +create table defcheck_sub_nn (a int, b int) partition by list (a); +create table defcheck_sub_nn_def partition of defcheck_sub_nn default + partition by list (b); +create table defcheck_sub_nn_leaf (b int not null, a int); +alter table defcheck_sub_nn_def attach partition defcheck_sub_nn_leaf + for values in (1); +insert into defcheck_sub_nn values (null, 1); +create table defcheck_sub_nn_null partition of defcheck_sub_nn + for values in (null); +ERROR: updated partition constraint for default partition "defcheck_sub_nn_def" would be violated by some row +select tableoid::regclass, a, b from defcheck_sub_nn where a is null; + tableoid | a | b +----------------------+---+--- + defcheck_sub_nn_leaf | | 1 +(1 row) + +drop table defcheck_sub_nn; -- tests of column drop with partition tables and indexes using -- predicates and expressions. create table part_column_drop ( diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql index 80e424e6bda..361ef20c569 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -729,6 +729,37 @@ insert into defcheck_def values (0, 0); create table defcheck_0 partition of defcheck for values in (0); drop table defcheck; +-- test that subpartitioned default partitions map column numbers before +-- checking whether a child's constraints imply the updated default constraint +create table defcheck_sub (a int, b int) partition by range (a); +create table defcheck_sub_def partition of defcheck_sub default + partition by range (b); +create table defcheck_sub_leaf1 (b int check (b >= 100 and b < 200), a int); +alter table defcheck_sub_def attach partition defcheck_sub_leaf1 + for values from (100) to (200); +create table defcheck_sub_leaf2 (c int, a int check (a >= 100), b int); +alter table defcheck_sub_leaf2 drop column c; +alter table defcheck_sub_def attach partition defcheck_sub_leaf2 + for values from (200) to (300); +insert into defcheck_sub values (5, 150); +create table defcheck_sub_p1 partition of defcheck_sub + for values from (0) to (10); +select tableoid::regclass, a, b from defcheck_sub where a = 5; +drop table defcheck_sub; + +-- same for NOT NULL on a reordered column of a default subpartition +create table defcheck_sub_nn (a int, b int) partition by list (a); +create table defcheck_sub_nn_def partition of defcheck_sub_nn default + partition by list (b); +create table defcheck_sub_nn_leaf (b int not null, a int); +alter table defcheck_sub_nn_def attach partition defcheck_sub_nn_leaf + for values in (1); +insert into defcheck_sub_nn values (null, 1); +create table defcheck_sub_nn_null partition of defcheck_sub_nn + for values in (null); +select tableoid::regclass, a, b from defcheck_sub_nn where a is null; +drop table defcheck_sub_nn; + -- tests of column drop with partition tables and indexes using -- predicates and expressions. create table part_column_drop ( -- 2.56.1.839.g22a8290db9-goog