You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by ma...@apache.org on 2025/02/18 05:40:08 UTC

(cloudberry) 29/31: FIX: num of pages always be invalid when relation is root partition table

This is an automated email from the ASF dual-hosted git repository.

maxyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 44b65975de0751bc7949d27bc74c4bd2451aa72a
Author: zhoujiaqi <zh...@hashdata.cn>
AuthorDate: Thu Feb 13 16:58:40 2025 +0800

    FIX: num of pages always be invalid when relation is root partition table
    
    `rel->rd_rel->relpages` always be -1 if current relation is root partition table.
    But the `totalpages` in `PageEstimate` is a uint32. So the pages of current
    relation will be the UINT32_MAX - 1 which will make ORCA can't estimate the
    reasonable cost of the `Index Only Scan`/`Dynamic Index Only Scan`.
    
    example:
    ```
    CREATE TABLE test_cover_index_on_pt(a int, b int, c int)
    DISTRIBUTED BY (a)
    PARTITION BY RANGE (b)
    (
        START (0) END (4) EVERY (1)
    );
    CREATE INDEX i_test_cover_index_scan_on_partition_table ON test_cover_index_on_pt(a) INCLUDE(b);
    INSERT INTO test_cover_index_on_pt SELECT i+i, i%4 FROM generate_series(1, 10)i;
    VACUUM ANALYZE test_cover_index_on_pt;
    
    explain SELECT a, b FROM test_cover_index_on_pt WHERE a=10 and b=2;
    ```
    
    The `Dynamic Index Scan` and `Dynamic Index Only Scan` in this case is:
    - CPhysicalDynamicIndexScan: cost: 6.000388
    - CLogicalDynamicIndexOnlyGet: cost: 6.000389
    
    The function `CostIndexOnlyScan`(used to estimate the cost of
    the `Index Only Scan`/`Dynamic Index Only Scan`) will  calculate
    `dPartialVisFrac` (too high will make the final cost higher), which
    will always be 1 (because totalpages is wrong). This causes the
    cost of `Index Only Scan`/`Dynamic Index Only Scan` to always be
    higher than `Index Scan`/`Dynamic Only Scan`.
---
 src/backend/optimizer/util/plancat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index df97340642..372d97f014 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -744,7 +744,7 @@ cdb_estimate_partitioned_numpages(Relation rel)
 	ListCell   *lc;
 
 	PageEstimate estimate = {
-		.totalpages = rel->rd_rel->relpages,
+		.totalpages = rel->rd_rel->relpages >= 0 ? (BlockNumber) rel->rd_rel->relpages : 0,
 		.totalallvisiblepages = rel->rd_rel->relallvisible
 	};
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org