From 5a5f5658d616e01108feeab7d8710e5f30620d88 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Wed, 18 Nov 2026 15:28:01 +0300 Subject: [PATCH 1/3] Use Split Update when partition key is updated and expand is in progress --- src/backend/optimizer/prep/preptlist.c | 122 +++++++++++++++++- src/test/regress/expected/bfv_planner.out | 111 ++++++++++++++++ .../expected/bfv_planner_optimizer.out | 111 ++++++++++++++++ src/test/regress/sql/bfv_planner.sql | 38 ++++++ 4 files changed, 381 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c index 0815a165142..51241125e71 100644 --- a/src/backend/optimizer/prep/preptlist.c +++ b/src/backend/optimizer/prep/preptlist.c @@ -47,6 +47,7 @@ #include "parser/parse_coerce.h" #include "parser/parsetree.h" #include "utils/lsyscache.h" +#include "utils/partcache.h" #include "utils/rel.h" #include "catalog/gp_distribution_policy.h" /* CDB: POLICYTYPE_PARTITIONED */ @@ -54,6 +55,7 @@ #include "optimizer/plancat.h" #include "parser/parse_relation.h" #include "utils/lsyscache.h" +#include "utils/syscache.h" static bool check_splitupdate(List *tlist, Index result_relation, Relation rel); static List *supplement_simply_updatable_targetlist(PlannerInfo *root, @@ -598,14 +600,81 @@ expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel, Index spl return new_tlist; } +/* + * Sub-partitions may have a different physical column layout than the + * root (dropped columns, ATTACH PARTITION), so attributes are mapped to + * the root by name. + */ +static bool +is_partkey_updated(Oid rootoid, List *inheritors, Bitmapset *changed_cols) +{ + ListCell *lc; + + foreach(lc, inheritors) + { + Relation r; + PartitionKey key; + Oid relid = lfirst_oid(lc); + Bitmapset *attnos = NULL; + + if (get_rel_relkind(relid) != RELKIND_PARTITIONED_TABLE) + continue; + + /* already locked by find_all_inheritors() */ + r = table_open(relid, NoLock); + key = RelationGetPartitionKey(r); + + for (int i = 0; i < key->partnatts; i++) + { + if (key->partattrs[i] != 0) + attnos = bms_add_member(attnos, + key->partattrs[i] - FirstLowInvalidHeapAttributeNumber); + } + /* partition expressions always use varno 1 */ + pull_varattnos((Node *) key->partexprs, 1, &attnos); + + table_close(r, NoLock); + + for (int x = -1; (x = bms_next_member(attnos, x)) >= 0; ) + { + AttrNumber attno = x + FirstLowInvalidHeapAttributeNumber; + AttrNumber rootattno; + + /* attno is not InvalidAttrNumber and not a system column */ + Assert (attno > 0); + + if (relid == rootoid) + rootattno = attno; + else + rootattno = get_attnum(rootoid, + get_attname(relid, attno, false)); + + Assert (rootattno > 0); + + if (bms_is_member(rootattno, changed_cols)) + { + bms_free(attnos); + return true; + } + } + + bms_free(attnos); + } + + return false; +} + static bool check_splitupdate(List *tlist, Index result_relation, Relation rel) { ListCell *lc; Bitmapset *changed_cols = NULL; GpPolicy *targetPolicy = rel->rd_cdbpolicy; + List *inheritors; - if (targetPolicy == NULL || targetPolicy->ptype != POLICYTYPE_PARTITIONED) + if (targetPolicy == NULL || + targetPolicy->ptype != POLICYTYPE_PARTITIONED || + targetPolicy->nattrs == 0) return false; foreach(lc, tlist) @@ -658,8 +727,59 @@ check_splitupdate(List *tlist, Index result_relation, Relation rel) } } + + if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) + { + bms_free(changed_cols); + return false; + } + + inheritors = find_all_inheritors(RelationGetRelid(rel), AccessShareLock, NULL); + + /* Check if partition key is updated */ + if (!is_partkey_updated(RelationGetRelid(rel), inheritors, changed_cols)) + { + list_free(inheritors); + bms_free(changed_cols); + return false; + } + bms_free(changed_cols); + /* + * Check if there are partitions which are distributed randomly. These + * partitions appear as a result of ALTER TABLE ... EXPAND PARTITION PREPARE + */ + foreach(lc, inheritors) + { + HeapTuple tuple; + int2vector *distkey; + bool isNull; + int nattrs; + Oid relid = lfirst_oid(lc); + + tuple = SearchSysCache1(GPPOLICYID, ObjectIdGetDatum(relid)); + if (!HeapTupleIsValid(tuple)) + { + list_free(inheritors); + return true; + } + + distkey = (int2vector *) DatumGetPointer( + SysCacheGetAttr(GPPOLICYID, tuple, + Anum_gp_distribution_policy_distkey, + &isNull)); + + nattrs = distkey->dim1; + ReleaseSysCache(tuple); + if (isNull || nattrs == 0) + { + list_free(inheritors); + return true; + } + } + + list_free(inheritors); return false; } diff --git a/src/test/regress/expected/bfv_planner.out b/src/test/regress/expected/bfv_planner.out index 3f9f5548093..6d54d7ee591 100644 --- a/src/test/regress/expected/bfv_planner.out +++ b/src/test/regress/expected/bfv_planner.out @@ -913,6 +913,117 @@ FROM t_out; (1 row) RESET optimizer_trace_fallback; +--Split Update should be used when partition key is updated and expand is in progress +select gp_debug_set_create_table_default_numsegments(2); + gp_debug_set_create_table_default_numsegments +----------------------------------------------- + 2 +(1 row) + +create table t_part (a int, b int, c int, d int) +distributed by (a) +partition by range (b) +subpartition by list (c) +subpartition template ( + subpartition sp1 values (1), + subpartition sp2 values (2) +) +(start (0) end (20) every (10)); +insert into t_part select i, 5, 1, 1 from generate_series(1, 20) i; +alter table t_part expand partition prepare; +alter table t_part_1_prt_2_2_prt_sp2 set with (REORGANIZE = TRUE) distributed by (a); +explain (costs off) update t_part set b = 15, c = 2; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set b = 15; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set c = 2; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set d = 2; + QUERY PLAN +----------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(11 rows) + +update t_part set b = 15, c = 2; +select gp_debug_set_create_table_default_numsegments(3); + gp_debug_set_create_table_default_numsegments +----------------------------------------------- + 3 +(1 row) + +create table t as select generate_series(1, 20) a distributed by (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); + count +------- + 20 +(1 row) + +--cleanup +select gp_debug_reset_create_table_default_numsegments(); + gp_debug_reset_create_table_default_numsegments +------------------------------------------------- + +(1 row) + +drop extension gp_debug_numsegments; -- start_ignore drop table if exists bfv_planner_x; drop table if exists testbadsql; diff --git a/src/test/regress/expected/bfv_planner_optimizer.out b/src/test/regress/expected/bfv_planner_optimizer.out index 37df6054375..1368d95c1e5 100644 --- a/src/test/regress/expected/bfv_planner_optimizer.out +++ b/src/test/regress/expected/bfv_planner_optimizer.out @@ -929,6 +929,117 @@ FROM t_out; (1 row) RESET optimizer_trace_fallback; +--Split Update should be used when partition key is updated and expand is in progress +select gp_debug_set_create_table_default_numsegments(2); + gp_debug_set_create_table_default_numsegments +----------------------------------------------- + 2 +(1 row) + +create table t_part (a int, b int, c int, d int) +distributed by (a) +partition by range (b) +subpartition by list (c) +subpartition template ( + subpartition sp1 values (1), + subpartition sp2 values (2) +) +(start (0) end (20) every (10)); +insert into t_part select i, 5, 1, 1 from generate_series(1, 20) i; +alter table t_part expand partition prepare; +alter table t_part_1_prt_2_2_prt_sp2 set with (REORGANIZE = TRUE) distributed by (a); +explain (costs off) update t_part set b = 15, c = 2; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set b = 15; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set c = 2; + QUERY PLAN +----------------------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Explicit Redistribute Motion 3:3 (slice1; segments: 3) + -> Split Update + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(13 rows) + +explain (costs off) update t_part set d = 2; + QUERY PLAN +----------------------------------------------------------- + Update on t_part + Update on t_part_1_prt_1_2_prt_sp1 t_part_1 + Update on t_part_1_prt_1_2_prt_sp2 t_part_2 + Update on t_part_1_prt_2_2_prt_sp1 t_part_3 + Update on t_part_1_prt_2_2_prt_sp2 t_part_4 + -> Append + -> Seq Scan on t_part_1_prt_1_2_prt_sp1 t_part_1 + -> Seq Scan on t_part_1_prt_1_2_prt_sp2 t_part_2 + -> Seq Scan on t_part_1_prt_2_2_prt_sp1 t_part_3 + -> Seq Scan on t_part_1_prt_2_2_prt_sp2 t_part_4 + Optimizer: Postgres query optimizer +(11 rows) + +update t_part set b = 15, c = 2; +select gp_debug_set_create_table_default_numsegments(3); + gp_debug_set_create_table_default_numsegments +----------------------------------------------- + 3 +(1 row) + +create table t as select generate_series(1, 20) a distributed by (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); + count +------- + 20 +(1 row) + +--cleanup +select gp_debug_reset_create_table_default_numsegments(); + gp_debug_reset_create_table_default_numsegments +------------------------------------------------- + +(1 row) + +drop extension gp_debug_numsegments; -- start_ignore drop table if exists bfv_planner_x; drop table if exists testbadsql; diff --git a/src/test/regress/sql/bfv_planner.sql b/src/test/regress/sql/bfv_planner.sql index 1cdfe9c888c..634d5c25400 100644 --- a/src/test/regress/sql/bfv_planner.sql +++ b/src/test/regress/sql/bfv_planner.sql @@ -538,6 +538,44 @@ FROM t_out; RESET optimizer_trace_fallback; +--Split Update should be used when partition key is updated and expand is in progress +--start_ignore +create extension if not exists gp_debug_numsegments; +drop table if exists t_part, t; +--end_ignore +select gp_debug_set_create_table_default_numsegments(2); + +create table t_part (a int, b int, c int, d int) +distributed by (a) +partition by range (b) +subpartition by list (c) +subpartition template ( + subpartition sp1 values (1), + subpartition sp2 values (2) +) +(start (0) end (20) every (10)); + +insert into t_part select i, 5, 1, 1 from generate_series(1, 20) i; +alter table t_part expand partition prepare; + +alter table t_part_1_prt_2_2_prt_sp2 set with (REORGANIZE = TRUE) distributed by (a); + +explain (costs off) update t_part set b = 15, c = 2; +explain (costs off) update t_part set b = 15; +explain (costs off) update t_part set c = 2; +explain (costs off) update t_part set d = 2; +update t_part set b = 15, c = 2; + +select gp_debug_set_create_table_default_numsegments(3); + +create table t as select generate_series(1, 20) a distributed by (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); + +--cleanup +select gp_debug_reset_create_table_default_numsegments(); +drop extension gp_debug_numsegments; + + -- start_ignore drop table if exists bfv_planner_x; drop table if exists testbadsql; From 8d4073554b5c27c2be4fa0641f16f9e3c47c7c97 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Fri, 20 Nov 2026 13:14:03 +0300 Subject: [PATCH 2/3] Fix tests --- src/test/regress/expected/bfv_planner.out | 2 +- src/test/regress/expected/bfv_planner_optimizer.out | 2 +- src/test/regress/sql/bfv_planner.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/regress/expected/bfv_planner.out b/src/test/regress/expected/bfv_planner.out index 6d54d7ee591..a28624df543 100644 --- a/src/test/regress/expected/bfv_planner.out +++ b/src/test/regress/expected/bfv_planner.out @@ -1010,7 +1010,7 @@ select gp_debug_set_create_table_default_numsegments(3); (1 row) create table t as select generate_series(1, 20) a distributed by (a); -select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t using (a); count ------- 20 diff --git a/src/test/regress/expected/bfv_planner_optimizer.out b/src/test/regress/expected/bfv_planner_optimizer.out index 1368d95c1e5..4ade35b0f99 100644 --- a/src/test/regress/expected/bfv_planner_optimizer.out +++ b/src/test/regress/expected/bfv_planner_optimizer.out @@ -1026,7 +1026,7 @@ select gp_debug_set_create_table_default_numsegments(3); (1 row) create table t as select generate_series(1, 20) a distributed by (a); -select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t using (a); count ------- 20 diff --git a/src/test/regress/sql/bfv_planner.sql b/src/test/regress/sql/bfv_planner.sql index 634d5c25400..5f0979f7115 100644 --- a/src/test/regress/sql/bfv_planner.sql +++ b/src/test/regress/sql/bfv_planner.sql @@ -569,7 +569,7 @@ update t_part set b = 15, c = 2; select gp_debug_set_create_table_default_numsegments(3); create table t as select generate_series(1, 20) a distributed by (a); -select count(*) from t_part_1_prt_2_2_prt_sp2 join t_part using (a); +select count(*) from t_part_1_prt_2_2_prt_sp2 join t using (a); --cleanup select gp_debug_reset_create_table_default_numsegments(); From 482abcb657191a4cb2c53bc5c85eebc6f6df3760 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Fri, 20 Nov 2026 14:10:15 +0300 Subject: [PATCH 3/3] Fix after review --- src/backend/optimizer/prep/preptlist.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c index 51241125e71..059e9206750 100644 --- a/src/backend/optimizer/prep/preptlist.c +++ b/src/backend/optimizer/prep/preptlist.c @@ -755,7 +755,7 @@ check_splitupdate(List *tlist, Index result_relation, Relation rel) HeapTuple tuple; int2vector *distkey; bool isNull; - int nattrs; + bool noDistKeys; Oid relid = lfirst_oid(lc); tuple = SearchSysCache1(GPPOLICYID, ObjectIdGetDatum(relid)); @@ -770,9 +770,9 @@ check_splitupdate(List *tlist, Index result_relation, Relation rel) Anum_gp_distribution_policy_distkey, &isNull)); - nattrs = distkey->dim1; + noDistKeys = isNull || distkey->dim1 == 0; ReleaseSysCache(tuple); - if (isNull || nattrs == 0) + if (noDistKeys) { list_free(inheritors); return true;