From eb4a1d734bd687a103da9d66ad27b14dd47cb541 Mon Sep 17 00:00:00 2001 From: liu qiren <13559537330@163.com> Date: Fri, 20 Nov 2026 21:18:22 +0800 Subject: [PATCH] fix(reject_partition_fullscan): look up relkind/partition count from catalog - ORCA dynamic scans: use get_rel_relkind() since ORCA leaves rte->relkind unset, so full scans were not rejected (issue #2342) - planner threshold=0: compare surviving subplans against catalog partition count so statically-pruned partial scans are not rejected (issue #2342) - add Test 12/13 regression cases and expected/ baseline (verified on gpdemo) Signed-off-by: liu qiren <13559537330@163.com> --- .../expected/partition_fullscan_reject.out | 240 ++++++++++++++++++ .../reject_partition_fullscan.c | 40 ++- .../sql/partition_fullscan_reject.sql | 33 +++ 3 files changed, 306 insertions(+), 7 deletions(-) create mode 100644 gpcontrib/reject_partition_fullscan/expected/partition_fullscan_reject.out diff --git a/gpcontrib/reject_partition_fullscan/expected/partition_fullscan_reject.out b/gpcontrib/reject_partition_fullscan/expected/partition_fullscan_reject.out new file mode 100644 index 00000000000..79e7ea52cfd --- /dev/null +++ b/gpcontrib/reject_partition_fullscan/expected/partition_fullscan_reject.out @@ -0,0 +1,240 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- +-- Test reject_partition_fullscan extension +-- +-- Load extension via LOAD (alternative to shared_preload_libraries) +LOAD 'reject_partition_fullscan'; +-- Create test partitioned table with 3 range partitions +CREATE TABLE pfr_test (id int, dt date, val text) + PARTITION BY RANGE (dt); +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. +HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. +CREATE TABLE pfr_test_p1 PARTITION OF pfr_test + FOR VALUES FROM ('2025-01-01') TO ('2025-04-01'); +NOTICE: table has parent, setting distribution columns to match parent table +CREATE TABLE pfr_test_p2 PARTITION OF pfr_test + FOR VALUES FROM ('2025-04-01') TO ('2025-07-01'); +NOTICE: table has parent, setting distribution columns to match parent table +CREATE TABLE pfr_test_p3 PARTITION OF pfr_test + FOR VALUES FROM ('2025-07-01') TO ('2025-10-01'); +NOTICE: table has parent, setting distribution columns to match parent table +-- Single-partition table for exemption test +CREATE TABLE pfr_single (id int, dt date) + PARTITION BY RANGE (dt); +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. +HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. +CREATE TABLE pfr_single_p1 PARTITION OF pfr_single + FOR VALUES FROM ('2025-01-01') TO ('2025-12-31'); +NOTICE: table has parent, setting distribution columns to match parent table +-- ============================== +-- Test 1: Basic rejection - no WHERE clause +-- ============================== +SET reject_partition_fullscan = on; +SET partition_fullscan_threshold = 0; +SELECT * FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +SELECT count(*) FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- ============================== +-- Test 2: Pruning passes - WHERE on partition key +-- ============================== +SELECT * FROM pfr_test WHERE dt = '2025-02-01'; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +SELECT * FROM pfr_test + WHERE dt >= '2025-01-01' AND dt < '2025-04-01'; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +-- ============================== +-- Test 3: WHERE not on partition key - should reject +-- ============================== +SELECT * FROM pfr_test WHERE val = 'x'; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +SELECT * FROM pfr_test WHERE id = 1; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- ============================== +-- Test 4: WHERE 1=1 - should reject (constant folded to NIL) +-- ============================== +SELECT * FROM pfr_test WHERE 1 = 1; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +SELECT * FROM pfr_test WHERE true; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- ============================== +-- Test 5: GUC off - allow full scan +-- ============================== +SET reject_partition_fullscan = off; +SELECT * FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +SET reject_partition_fullscan = on; +-- ============================== +-- Test 6: enable_partition_pruning=off exemption +-- ============================== +SET enable_partition_pruning = off; +SELECT * FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +SET enable_partition_pruning = on; +-- ============================== +-- Test 7: Single-partition table exemption +-- ============================== +SELECT * FROM pfr_single; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_single +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt +----+---- +(0 rows) + +-- ============================== +-- Test 8: Threshold mode +-- ============================== +SET partition_fullscan_threshold = 2; +-- Pruned to 2 partitions, within threshold, should pass +SELECT * FROM pfr_test + WHERE dt >= '2025-01-01' AND dt < '2025-07-01'; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +-- All 3 partitions exceed threshold of 2, should reject +SELECT * FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +SET partition_fullscan_threshold = 0; +-- ============================== +-- Test 9: Prepared statement with parameter (exemption) +-- ============================== +PREPARE pfr_q AS SELECT * FROM pfr_test WHERE dt = $1; +EXECUTE pfr_q('2025-02-01'); +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +DEALLOCATE pfr_q; +-- ============================== +-- Test 10: UPDATE/DELETE without WHERE - should reject +-- ============================== +UPDATE pfr_test SET val = 'y'; +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +DELETE FROM pfr_test; +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- UPDATE/DELETE with partition key - should pass +UPDATE pfr_test SET val = 'y' WHERE dt = '2025-02-01'; +DELETE FROM pfr_test WHERE dt = '2025-02-01'; +-- ============================== +-- Test 11: Subquery containing partitioned table +-- ============================== +SELECT * FROM (SELECT * FROM pfr_test) sub; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- ============================== +-- Test 12: threshold=0 partial scan must NOT be rejected (issue #2342) +-- Plan-time static pruning keeps 2 of 3 partitions under the Postgres +-- planner, producing an Append with no PartitionPruneInfo. This is a +-- legitimate partial scan and must pass, not be rejected as a full scan. +-- ============================== +SET optimizer = off; +SET partition_fullscan_threshold = 0; +-- keeps p1 and p2, prunes p3 -> partial scan, should pass +SELECT * FROM pfr_test + WHERE dt >= '2025-01-01' AND dt < '2025-07-01'; + id | dt | val +----+----+----- +(0 rows) + +-- all 3 partitions -> true full scan, should still reject +SELECT count(*) FROM pfr_test; +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- ============================== +-- Test 13: ORCA dynamic scan full scan must be rejected (issue #2342) +-- ORCA leaves relkind unset on the dynamic scan RTE; detection must look +-- up relkind from the catalog, otherwise full scans slip through. +-- ============================== +SET optimizer = on; +-- unfiltered full scan under ORCA -> should reject +SELECT count(*) FROM pfr_test; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. +ERROR: partitioned table "public.pfr_test" full partition scan is not allowed, 3 partitions would be scanned +HINT: Add a WHERE clause on the partition key to enable partition pruning. +-- pruning predicate keeping a single partition -> should pass +SELECT * FROM pfr_test WHERE dt = '2025-02-01'; +NOTICE: One or more columns in the following table(s) do not have statistics: pfr_test +HINT: For non-partitioned tables, run analyze (). For partitioned tables, run analyze rootpartition (). See log for columns missing statistics. + id | dt | val +----+----+----- +(0 rows) + +SET optimizer = off; +SET partition_fullscan_threshold = 0; +-- ============================== +-- Cleanup +-- ============================== +DROP TABLE pfr_test; +DROP TABLE pfr_single; +RESET reject_partition_fullscan; +RESET partition_fullscan_threshold; +RESET enable_partition_pruning; +RESET optimizer; diff --git a/gpcontrib/reject_partition_fullscan/reject_partition_fullscan.c b/gpcontrib/reject_partition_fullscan/reject_partition_fullscan.c index 0a38cfe7b0c..b57c963c2fc 100644 --- a/gpcontrib/reject_partition_fullscan/reject_partition_fullscan.c +++ b/gpcontrib/reject_partition_fullscan/reject_partition_fullscan.c @@ -242,14 +242,36 @@ check_append_no_pruneinfo(Plan *plan, List *rtable) if (nsubplans <= 1) return; - if (threshold == 0 || nsubplans > threshold) { RangeTblEntry *rte = rt_fetch(parent_rti, rtable); - char *relname = get_rel_name(rte->relid); - char *nspname = get_namespace_name( - get_rel_namespace(rte->relid)); + bool do_reject; + + if (threshold == 0) + { + /* + * threshold == 0: reject only true full scans. Plan-time static + * pruning can leave an Append covering a subset of partitions with + * no PartitionPruneInfo attached; comparing the surviving subplan + * count against the catalog partition count avoids rejecting such + * legitimate partial scans. + */ + List *children = find_inheritance_children(rte->relid, NoLock); + int ntotal = list_length(children); + + list_free(children); + do_reject = (ntotal > 1 && nsubplans >= ntotal); + } + else + do_reject = (nsubplans > threshold); - reject_fullscan(nspname, relname, nsubplans); + if (do_reject) + { + char *relname = get_rel_name(rte->relid); + char *nspname = get_namespace_name( + get_rel_namespace(rte->relid)); + + reject_fullscan(nspname, relname, nsubplans); + } } } @@ -319,10 +341,14 @@ check_dynamic_scan_fullscan(Plan *plan, List *rtable) if (nscanned <= 1) return; - /* Verify this is a partitioned table */ + /* + * Verify this is a partitioned table. ORCA does not set rte->relkind on + * the dynamic scan's RTE, so look it up from the catalog instead of + * trusting the (possibly unset) range-table field. + */ rte = rt_fetch(scanrelid, rtable); if (rte->rtekind != RTE_RELATION || - rte->relkind != RELKIND_PARTITIONED_TABLE) + get_rel_relkind(rte->relid) != RELKIND_PARTITIONED_TABLE) return; threshold = fullscan_threshold; diff --git a/gpcontrib/reject_partition_fullscan/sql/partition_fullscan_reject.sql b/gpcontrib/reject_partition_fullscan/sql/partition_fullscan_reject.sql index 537d70f7501..62a912be29d 100644 --- a/gpcontrib/reject_partition_fullscan/sql/partition_fullscan_reject.sql +++ b/gpcontrib/reject_partition_fullscan/sql/partition_fullscan_reject.sql @@ -122,6 +122,38 @@ DELETE FROM pfr_test WHERE dt = '2025-02-01'; -- ============================== SELECT * FROM (SELECT * FROM pfr_test) sub; +-- ============================== +-- Test 12: threshold=0 partial scan must NOT be rejected (issue #2342) +-- Plan-time static pruning keeps 2 of 3 partitions under the Postgres +-- planner, producing an Append with no PartitionPruneInfo. This is a +-- legitimate partial scan and must pass, not be rejected as a full scan. +-- ============================== +SET optimizer = off; +SET partition_fullscan_threshold = 0; + +-- keeps p1 and p2, prunes p3 -> partial scan, should pass +SELECT * FROM pfr_test + WHERE dt >= '2025-01-01' AND dt < '2025-07-01'; + +-- all 3 partitions -> true full scan, should still reject +SELECT count(*) FROM pfr_test; + +-- ============================== +-- Test 13: ORCA dynamic scan full scan must be rejected (issue #2342) +-- ORCA leaves relkind unset on the dynamic scan RTE; detection must look +-- up relkind from the catalog, otherwise full scans slip through. +-- ============================== +SET optimizer = on; + +-- unfiltered full scan under ORCA -> should reject +SELECT count(*) FROM pfr_test; + +-- pruning predicate keeping a single partition -> should pass +SELECT * FROM pfr_test WHERE dt = '2025-02-01'; + +SET optimizer = off; +SET partition_fullscan_threshold = 0; + -- ============================== -- Cleanup -- ============================== @@ -130,3 +162,4 @@ DROP TABLE pfr_single; RESET reject_partition_fullscan; RESET partition_fullscan_threshold; RESET enable_partition_pruning; +RESET optimizer;