From 23b2a585516be2110b1593dff390d6b5f16843d3 Mon Sep 17 00:00:00 2001 From: syj <904673462@qq.com> Date: Sat, 13 Mar 2021 16:47:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=8C=BA=E8=A1=A8=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=82=20=20=201.=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=88=86=E5=8C=BA=E8=A1=A8=E5=89=AA=E6=9E=9D=E6=AF=94=E4=BE=8B?= =?UTF-8?q?=E7=9A=84=E8=AE=A1=E7=AE=97=E3=80=82=20=20=202.=E5=88=A0?= =?UTF-8?q?=E9=99=A4GetPartitionInfo=E5=87=BD=E6=95=B0=E4=B8=AD=E5=AF=B9?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=E7=9A=84=E5=89=AA=E6=9E=9D=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=B7=B1=E6=8B=B7=E8=B4=9D=E7=9A=84=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E3=80=82=20=20=203.=E4=BC=98=E5=8C=96=E5=88=86?= =?UTF-8?q?=E5=8C=BA=E8=A1=A8SQLByPass=E5=AF=B9=E5=88=86=E5=8C=BAIndexOid?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/optimizer/path/allpaths.cpp | 2 +- src/gausskernel/optimizer/util/pruning.cpp | 6 +- .../runtime/executor/opfusion_scan.cpp | 15 +++-- src/test/regress/expected/gpi_build_index.out | 6 +- src/test/regress/expected/gpi_clean_wait.out | 12 ++-- src/test/regress/expected/gpi_index.out | 41 +++++++------ .../regress/expected/sqlbypass_partition.out | 60 +++++++++---------- 7 files changed, 72 insertions(+), 70 deletions(-) diff --git a/src/gausskernel/optimizer/path/allpaths.cpp b/src/gausskernel/optimizer/path/allpaths.cpp index 9f12379f0..6d47380a3 100644 --- a/src/gausskernel/optimizer/path/allpaths.cpp +++ b/src/gausskernel/optimizer/path/allpaths.cpp @@ -801,7 +801,7 @@ static void set_plain_rel_size(PlannerInfo* root, RelOptInfo* rel, RangeTblEntry if (relation->partMap != NULL && PartitionMapIsRange(relation->partMap)) { RangePartitionMap *partMmap = (RangePartitionMap *)relation->partMap; - pruningRatio = rel->partItrs / partMmap->rangeElementsNum; + pruningRatio = (double)rel->partItrs / partMmap->rangeElementsNum; } heap_close(relation, NoLock); diff --git a/src/gausskernel/optimizer/util/pruning.cpp b/src/gausskernel/optimizer/util/pruning.cpp index 3d929d49b..5f977aba3 100644 --- a/src/gausskernel/optimizer/util/pruning.cpp +++ b/src/gausskernel/optimizer/util/pruning.cpp @@ -89,7 +89,6 @@ static PartitionMap* GetRelPartitionMap(Relation relation) */ PruningResult* GetPartitionInfo(PruningResult* result, EState* estate, Relation current_relation) { - PruningResult* res = NULL; PruningResult* resPartition = NULL; PruningContext context; @@ -100,11 +99,10 @@ PruningResult* GetPartitionInfo(PruningResult* result, EState* estate, Relation context.estate = estate; context.relation = current_relation; - res = copyPruningResult(result); if (current_relation->partMap->type == PART_TYPE_LIST || current_relation->partMap->type == PART_TYPE_HASH) { - resPartition = partitionEqualPruningWalker(current_relation->partMap->type, res->expr, &context); + resPartition = partitionEqualPruningWalker(current_relation->partMap->type, result->expr, &context); } else { - resPartition = partitionPruningWalker(res->expr, &context); + resPartition = partitionPruningWalker(result->expr, &context); } partitionPruningFromBoundary(&context, resPartition); if (PruningResultIsFull(resPartition) || diff --git a/src/gausskernel/runtime/executor/opfusion_scan.cpp b/src/gausskernel/runtime/executor/opfusion_scan.cpp index 05b3d2e62..41dabb972 100644 --- a/src/gausskernel/runtime/executor/opfusion_scan.cpp +++ b/src/gausskernel/runtime/executor/opfusion_scan.cpp @@ -353,10 +353,15 @@ Oid GetRelOidForPartitionTable(Scan scan, const Relation rel, ParamListInfo para } /* init the index in construct */ -Relation InitPartitionIndexInFusion(Oid parentIndexOid, Oid partOid, Partition* partIndex, Relation* parentIndex) +Relation InitPartitionIndexInFusion(Oid parentIndexOid, Oid partOid, Partition* partIndex, Relation* parentIndex, Relation rel) { *parentIndex = relation_open(parentIndexOid, AccessShareLock); - Oid partIndexOid = getPartitionIndexOid(parentIndexOid, partOid); + Oid partIndexOid; + if (list_length(rel->rd_indexlist) == 1) { + partIndexOid = lfirst_oid(rel->rd_indexlist->head); + } else { + partIndexOid = getPartitionIndexOid(parentIndexOid, partOid); + } *partIndex = partitionOpen(*parentIndex, partIndexOid, AccessShareLock); Relation index = partitionGetRelation(*parentIndex, *partIndex); return index; @@ -481,7 +486,7 @@ void IndexScanFusion::Init(long max_rows) /* get partition index */ Oid parentIndexOid = m_node->indexid; - m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex); + m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex, m_rel); } else { m_rel = heap_open(m_reloid, AccessShareLock); m_index = index_open(m_node->indexid, AccessShareLock); @@ -682,7 +687,7 @@ IndexOnlyScanFusion::IndexOnlyScanFusion(IndexOnlyScan* node, PlannedStmt* plans /* get Partition index */ Oid parentIndexOid = m_node->indexid; - m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex); + m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex, m_rel); } else { m_reloid = getrelid(m_node->scan.scanrelid, planstmt->rtable); m_index = index_open(m_node->indexid, AccessShareLock); @@ -721,7 +726,7 @@ void IndexOnlyScanFusion::Init(long max_rows) /* get partition index */ Oid parentIndexOid = m_node->indexid; - m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex); + m_index = InitPartitionIndexInFusion(parentIndexOid, m_reloid, &m_partIndex, &m_parentIndex, m_rel); } else { m_rel = heap_open(m_reloid, AccessShareLock); m_index = index_open(m_node->indexid, AccessShareLock); diff --git a/src/test/regress/expected/gpi_build_index.out b/src/test/regress/expected/gpi_build_index.out index 509d1ec71..1ecb18677 100644 --- a/src/test/regress/expected/gpi_build_index.out +++ b/src/test/regress/expected/gpi_build_index.out @@ -227,11 +227,11 @@ Number of partition: 5 (View pg_partition to check each partition range.) insert into test_gpi_create_like values(generate_series(0,9999), 1); explain (costs off) select * from test_gpi_create_like where c1 = 0; - QUERY PLAN --------------------------------------------------------------------- + QUERY PLAN +----------------------------------------------------------------- Bitmap Heap Scan on test_gpi_create_like Recheck Cond: (c1 = 0) - -> Bitmap Index Scan on test_gpi_create_like_c1_c2_tableoid_idx + -> Bitmap Index Scan on test_gpi_create_like_c1_tableoid_idx Index Cond: (c1 = 0) (4 rows) diff --git a/src/test/regress/expected/gpi_clean_wait.out b/src/test/regress/expected/gpi_clean_wait.out index a7ac796a2..93e775eef 100644 --- a/src/test/regress/expected/gpi_clean_wait.out +++ b/src/test/regress/expected/gpi_clean_wait.out @@ -21,9 +21,9 @@ select a.relname,a.parttype,a.reloptions from pg_partition a, pg_class b where a (2 rows) explain (costs off) select * from test_gpi_more_invalid where a >= 3000; - QUERY PLAN ------------------------------------------------------------------------------------- - Index Only Scan using global_index_gpi_more_invalid_a_b_c on test_gpi_more_invalid + QUERY PLAN +--------------------------------------------------------------------------- + Index Scan using global_index_gpi_more_invalid_b on test_gpi_more_invalid Index Cond: (a >= 3000) (2 rows) @@ -55,10 +55,10 @@ explain (costs off) select count(*) from test_gpi_more_invalid where a <= 100 an (4 rows) explain (costs off) select count(*) from test_gpi_more_invalid where a > 3000; - QUERY PLAN ------------------------------------------------------------------------------------------- + QUERY PLAN +-------------------------------------------------------------------------------------- Aggregate - -> Index Only Scan using global_index_gpi_more_invalid_a_b_c on test_gpi_more_invalid + -> Index Only Scan using global_index_gpi_more_invalid_b on test_gpi_more_invalid Index Cond: (a > 3000) (3 rows) diff --git a/src/test/regress/expected/gpi_index.out b/src/test/regress/expected/gpi_index.out index 6460ef81d..da2967b93 100644 --- a/src/test/regress/expected/gpi_index.out +++ b/src/test/regress/expected/gpi_index.out @@ -119,11 +119,12 @@ select * from gpi_index_test where a < 200 and b > 190 order by a; -- multi column index with global index create index gpi_index_test_global_a_b on gpi_index_test(a,b) global; explain (costs off) select * from gpi_index_test where a = 200 and b = 100; - QUERY PLAN --------------------------------------------------------------- - Index Scan using gpi_index_test_global_a_b on gpi_index_test - Index Cond: ((a = 200) AND (b = 100)) -(2 rows) + QUERY PLAN +------------------------------------------------------------ + Index Scan using gpi_index_test_global_b on gpi_index_test + Index Cond: (b = 100) + Filter: (a = 200) +(3 rows) select * from gpi_index_test where a = 100 and b = 100 order by a; a | b | c @@ -432,19 +433,17 @@ select count(*) from gpi_index_test as t1 NATURAL inner join gpi_index_test1 t2 (1 row) explain (costs off) select * from gpi_index_test t1 full join gpi_index_test1 t2 on t1.b >= t2.a where t1.b < 200 and t2.a = 50 and t1.a = 100; - QUERY PLAN --------------------------------------------------------------------------------------- + QUERY PLAN +----------------------------------------------------------------------- Nested Loop - Join Filter: (t1.b >= t2.a) - -> Partition Iterator - Iterations: 1 - -> Partitioned Index Scan using gpi_index_test_local_a on gpi_index_test t1 - Index Cond: (a = 100) - Filter: (b < 200) - Selected Partitions: 2 -> Index Scan using gpi_index_test1_global_a on gpi_index_test1 t2 Index Cond: (a = 50) -(10 rows) + -> Bitmap Heap Scan on gpi_index_test t1 + Recheck Cond: ((b >= t2.a) AND (b < 200)) + Filter: (a = 100) + -> Bitmap Index Scan on gpi_index_test_global_b + Index Cond: ((b >= t2.a) AND (b < 200)) +(8 rows) select * from gpi_index_test t1 full join gpi_index_test1 t2 on t1.b >= t2.a where t1.b < 200 and t2.a = 50 and t1.a = 100; a | b | c | a | b | c @@ -470,15 +469,15 @@ select count(*) from gpi_index_test t1 left join gpi_index_test1 t2 on (t1.b >= (1 row) explain (costs off) select * from gpi_index_test t1 right join gpi_index_test1 t2 on (t1.b >= t2.a and (t1.a < 60 or t2.b != 30)) where t1.b < 200 and t2.a < 100; - QUERY PLAN ------------------------------------------------------------------------------ + QUERY PLAN +--------------------------------------------------------------------------- Nested Loop Join Filter: ((t1.b >= t2.a) AND ((t1.a < 60) OR (t2.b <> 30))) - -> Index Scan using gpi_index_test_global_b on gpi_index_test t1 - Index Cond: (b < 200) + -> Index Scan using gpi_index_test1_global_a on gpi_index_test1 t2 + Index Cond: (a < 100) -> Materialize - -> Index Scan using gpi_index_test1_global_a on gpi_index_test1 t2 - Index Cond: (a < 100) + -> Index Scan using gpi_index_test_global_b on gpi_index_test t1 + Index Cond: (b < 200) (7 rows) select count(*) from gpi_index_test t1 left join gpi_index_test1 t2 on (t1.b >= t2.a and (t1.a != 60 or t1.b != 30)) where t1.b < 200 and t2.a < 100; diff --git a/src/test/regress/expected/sqlbypass_partition.out b/src/test/regress/expected/sqlbypass_partition.out index 2f38efa94..3377a2759 100644 --- a/src/test/regress/expected/sqlbypass_partition.out +++ b/src/test/regress/expected/sqlbypass_partition.out @@ -1299,9 +1299,9 @@ explain select col1,col2 from test_bypass_sql_partition where col1=10 and col2=1 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Partition Iterator (cost=10000000000.00..1000000000827.00 rows=1 width=8) + Partition Iterator (cost=10000000000.00..1000000000827.01 rows=1 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.00 rows=1 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.01 rows=1 width=8) Index Cond: ((col1 = 10) AND (col2 = 10)) Selected Partitions: 2 (6 rows) @@ -1316,10 +1316,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1=10 and col2=1 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Limit (cost=10000000000.00..1000000000827.00 rows=1 width=8) - -> Partition Iterator (cost=10000000000.00..1000000000827.00 rows=1 width=8) + Limit (cost=10000000000.00..1000000000827.01 rows=1 width=8) + -> Partition Iterator (cost=10000000000.00..1000000000827.01 rows=1 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.00 rows=1 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.01 rows=1 width=8) Index Cond: ((col1 = 10) AND (col2 = 10)) Selected Partitions: 2 (7 rows) @@ -1334,10 +1334,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1=10 and col2=1 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Limit (cost=10000000000.00..1000000000827.00 rows=1 width=8) - -> Partition Iterator (cost=10000000000.00..1000000000827.00 rows=1 width=8) + Limit (cost=10000000000.00..1000000000827.01 rows=1 width=8) + -> Partition Iterator (cost=10000000000.00..1000000000827.01 rows=1 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.00 rows=1 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000000827.01 rows=1 width=8) Index Cond: ((col1 = 10) AND (col2 = 10)) Selected Partitions: 2 (7 rows) @@ -1414,10 +1414,10 @@ explain select col1, col2 from test_bypass_sql_partition where col1 <= 30 and co QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------ [No Bypass]reason: Bypass not support query in multiple partitions. - Limit (cost=0.00..24.37 rows=6 width=8) - -> Partition Iterator (cost=0.00..24.37 rows=6 width=8) + Limit (cost=0.00..28.37 rows=6 width=8) + -> Partition Iterator (cost=0.00..28.37 rows=6 width=8) Iterations: 3 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=0.00..24.37 rows=6 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=0.00..28.37 rows=6 width=8) Index Cond: ((col1 <= 30) AND (col1 >= 10)) Selected Partitions: 2..4 (7 rows) @@ -1494,10 +1494,10 @@ explain select col1, col2 from test_bypass_sql_partition where col1 < 20 order b QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- [No Bypass]reason: Bypass not support query in multiple partitions. - Limit (cost=0.00..1.42 rows=10 width=8) - -> Partition Iterator (cost=0.00..55.06 rows=389 width=8) + Limit (cost=0.00..1.83 rows=10 width=8) + -> Partition Iterator (cost=0.00..71.06 rows=389 width=8) Iterations: 2 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=0.00..55.06 rows=389 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=0.00..71.06 rows=389 width=8) Index Cond: (col1 < 20) Selected Partitions: 1..2 (7 rows) @@ -1902,9 +1902,9 @@ explain select col1,col2 from test_bypass_sql_partition where col1=0 order by c QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Partition Iterator (cost=10000000000.00..1000000002435.50 rows=6 width=8) + Partition Iterator (cost=10000000000.00..1000000002435.51 rows=6 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002435.50 rows=6 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002435.51 rows=6 width=8) Index Cond: (col1 = 0) Selected Partitions: 1 (6 rows) @@ -1929,10 +1929,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1>0 and col1<10 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Limit (cost=10000000000.00..1000000002437.00 rows=6 width=8) - -> Partition Iterator (cost=10000000000.00..1000000002437.00 rows=6 width=8) + Limit (cost=10000000000.00..1000000002437.01 rows=6 width=8) + -> Partition Iterator (cost=10000000000.00..1000000002437.01 rows=6 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002437.00 rows=6 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002437.01 rows=6 width=8) Index Cond: ((col1 > 0) AND (col1 < 10)) Selected Partitions: 1 (7 rows) @@ -1957,9 +1957,9 @@ explain select col2,col1 from test_bypass_sql_partition where col1>0 and col1<10 --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] Limit (cost=10000000000.00..505000001218.50 rows=3 width=8) - -> Partition Iterator (cost=10000000000.00..1000000002437.00 rows=6 width=8) + -> Partition Iterator (cost=10000000000.00..1000000002437.01 rows=6 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002437.00 rows=6 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000002437.01 rows=6 width=8) Index Cond: ((col1 > 0) AND (col1 < 10)) Selected Partitions: 1 (7 rows) @@ -1976,10 +1976,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1>=0 and col1<1 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Limit (cost=10000000000.00..1000000001234.50 rows=2 width=8) - -> Partition Iterator (cost=10000000000.00..1000000001234.50 rows=2 width=8) + Limit (cost=10000000000.00..1000000001234.51 rows=2 width=8) + -> Partition Iterator (cost=10000000000.00..1000000001234.51 rows=2 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.50 rows=2 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.51 rows=2 width=8) Index Cond: ((col1 >= 0) AND (col1 < 10) AND (col2 > 0)) Selected Partitions: 1 (7 rows) @@ -1996,10 +1996,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1>=0 and col1<1 QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [Bypass] - Limit (cost=10000000000.00..1000000001234.50 rows=2 width=8) - -> Partition Iterator (cost=10000000000.00..1000000001234.50 rows=2 width=8) + Limit (cost=10000000000.00..1000000001234.51 rows=2 width=8) + -> Partition Iterator (cost=10000000000.00..1000000001234.51 rows=2 width=8) Iterations: 1 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.50 rows=2 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.51 rows=2 width=8) Index Cond: ((col1 >= 0) AND (col1 < 10) AND (col2 > 0)) Selected Partitions: 1 (7 rows) @@ -2177,10 +2177,10 @@ explain select col1,col2 from test_bypass_sql_partition where col1>=0 and col1<= QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- [No Bypass]reason: Bypass not support query in multiple partitions. - Limit (cost=10000000000.00..1000000001234.50 rows=2 width=8) - -> Partition Iterator (cost=10000000000.00..1000000001234.50 rows=2 width=8) + Limit (cost=10000000000.00..1000000001234.52 rows=2 width=8) + -> Partition Iterator (cost=10000000000.00..1000000001234.52 rows=2 width=8) Iterations: 2 - -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.50 rows=2 width=8) + -> Partitioned Index Only Scan using itest_bypass_sql_partition on test_bypass_sql_partition (cost=10000000000.00..1000000001234.52 rows=2 width=8) Index Cond: ((col1 >= 0) AND (col1 <= 10) AND (col2 > 0)) Selected Partitions: 1..2 (7 rows)