diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c index 0815a165142..059e9206750 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; + bool noDistKeys; + 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)); + + noDistKeys = isNull || distkey->dim1 == 0; + ReleaseSysCache(tuple); + if (noDistKeys) + { + 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..a28624df543 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 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..4ade35b0f99 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 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..5f0979f7115 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 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;