[Bug] reject_partition_fullscan does not reject full scans planned by GPORCA, and threshold 0 rejects partial scans under the planner

未关闭
#2,342 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
44/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, postgresql
领域
backend, databases

调研方向

Start by reading gpcontrib/reject_partition_fullscan and the partition_fullscan_reject regression SQL; the issue says its expected output is missing. Trace check_dynamic_scan_fullscan() and check_append_no_pruneinfo() to understand the GPORCA range table and planner pruning cases. Add regression coverage for full and partial scans with both optimizers and threshold 0; done means the full scan is rejected consistently while the documented partial scan remains allowed.

由索引模型根据 Issue 内容生成。

描述

type: Bug
Apache Cloudberry version

Reproduced on:

  • REL_2_STABLE at 91582af9baf (PostgreSQL 14.12, 2.2.0-incubating build dev)
  • main at eb9bf338855 (PostgreSQL 16.9, 3.0.0-devel)
What happened

gpcontrib/reject_partition_fullscan (added in #1713) behaves differently depending on the optimizer.

  1. GPORCA (the default optimizer): full scans are not rejected. A SELECT that scans all
    partitions (Dynamic Seq Scan, "Number of partitions to scan: 3 (out of 3)") runs normally.
    The dynamic scan has no part_prune_info, so the walker calls check_dynamic_scan_fullscan() as
    intended, but that function returns early at
    rte->relkind != RELKIND_PARTITIONED_TABLE: in the range table that GPORCA produces, the entry
    of the partitioned table has relkind unset. debug_print_plan for the query below shows
    :relid 17047 :relkind <> with GPORCA, and :relid 17047 :relkind p with the Postgres-based
    planner. UPDATE and DELETE on partitioned tables are planned by the Postgres-based planner
    and are rejected as expected.
  2. Postgres-based planner with partition_fullscan_threshold = 0: partial scans are rejected.
    The source comment and the GUC description say that 0 rejects only true full scans. For a
    planner Append without PartitionPruneInfo (pruning done at plan time), check_append_no_pruneinfo()
    rejects whenever more than one partition remains, so a range filter that keeps 2 of 3 partitions
    fails with "2 partitions would be scanned".
What you think should happen instead
  • A full partition scan should be rejected regardless of the optimizer.
  • With partition_fullscan_threshold = 0, only scans of all partitions should be rejected,
    as documented, or the documentation of 0 should be changed to match the behavior.
How to reproduce
LOAD 'reject_partition_fullscan';
CREATE TABLE pfr (id int, dt date) PARTITION BY RANGE (dt) DISTRIBUTED BY (id);
CREATE TABLE pfr_p1 PARTITION OF pfr FOR VALUES FROM ('2025-01-01') TO ('2025-04-01');
CREATE TABLE pfr_p2 PARTITION OF pfr FOR VALUES FROM ('2025-04-01') TO ('2025-07-01');
CREATE TABLE pfr_p3 PARTITION OF pfr FOR VALUES FROM ('2025-07-01') TO ('2025-10-01');
INSERT INTO pfr SELECT g, '2025-01-01'::date + (g % 270) FROM generate_series(1, 3000) g;
ANALYZE pfr;

-- defaults: reject_partition_fullscan = on, partition_fullscan_threshold = 0
SET optimizer = on;
EXPLAIN (COSTS OFF) SELECT count(*) FROM pfr;
SELECT count(*) FROM pfr;                       -- expected: rejected

SET optimizer = off;
SELECT count(*) FROM pfr;                       -- rejected (correct)
SELECT count(*) FROM pfr WHERE dt = '2025-02-01';                          -- allowed (correct)
SELECT count(*) FROM pfr WHERE dt >= '2025-01-01' AND dt < '2025-07-01';   -- expected: allowed

Output (identical on both branches):

SET optimizer = on;
EXPLAIN (COSTS OFF) SELECT count(*) FROM pfr;
 Finalize Aggregate
   ->  Gather Motion 3:1  (slice1; segments: 3)
         ->  Partial Aggregate
               ->  Dynamic Seq Scan on pfr
                     Number of partitions to scan: 3 (out of 3)
 Optimizer: GPORCA
SELECT count(*) FROM pfr;
 count
-------
  3000

SET optimizer = off;
SELECT count(*) FROM pfr;
ERROR:  partitioned table "public.pfr" full partition scan is not allowed, 3 partitions would be scanned
SELECT count(*) FROM pfr WHERE dt = '2025-02-01';
 count
-------
    11
SELECT count(*) FROM pfr WHERE dt >= '2025-01-01' AND dt < '2025-07-01';
ERROR:  partitioned table "public.pfr" full partition scan is not allowed, 2 partitions would be scanned
Operating System

Rocky Linux 9.7 (apache/incubator-cloudberry:cbdb-build-rocky9-latest), built with devops/build/automation/cloudberry/scripts.

Anything else

Candidate fix for item 1 (tested on REL_2_STABLE only): look the relkind up from the catalog
instead of trusting the range table entry, in both places that check it:

-			rte->relkind == RELKIND_PARTITIONED_TABLE)
+			get_rel_relkind(rte->relid) == RELKIND_PARTITIONED_TABLE)
...
-		rte->relkind != RELKIND_PARTITIONED_TABLE)
+		get_rel_relkind(rte->relid) != RELKIND_PARTITIONED_TABLE)

With this change and GPORCA on, SELECT count(*) FROM pfr is rejected ("3 partitions would be
scanned"), a filter that keeps one partition is allowed, a filter that keeps 2 of 3 partitions is
allowed with threshold 0 and with threshold 2, and an unfiltered query is rejected with
threshold 2. Results under the Postgres-based planner do not change. Item 2 is not addressed by
this change.

The module's Makefile declares REGRESS = partition_fullscan_reject, but there is no
expected/ directory, so the regression test cannot pass and none of the behavior above is
covered. The test SQL does not exercise GPORCA dynamic scans with static pruning.

Bug found by Claude Code. Please review it again.

主要语言
C
星标
1.4k
派生
273
平均合并
4 天 6 小时
30 天内合并 PR
105

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/cloudberry 的其他 Issue

查看 apache/cloudberry 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。