lqriu opened a new pull request, #1711:
URL: https://github.com/apache/cloudberry/pull/1711

   ## What does this PR do?
   
   Add a plan-time check that rejects queries on partitioned tables when no
   effective partition pruning occurs, preventing unintended full partition
   scans in production environments.
   
   Two new GUC parameters:
   - `reject_partition_fullscan` (bool, default ON) — enable/disable
   - `partition_fullscan_threshold` (int, default 0) — max partitions allowed
     after pruning; 0 means reject only true fullscans
   
   ### Planner path (`inherit.c`)
   - Check inserted in `expand_partitioned_rtentry()` after
     `prune_append_rel_partitions()` returns
   - Compares `num_live_parts` vs `relinfo->nparts`
   - Exempts queries with Param nodes (prepared statements, subquery params)
   
   ### ORCA path (`orca.c`)
   - Post-plan check in `optimize_query()` via `plan_tree_walker`
   - Inspects `part_prune_info` on 7 node types (Append, MergeAppend,
     DynamicSeqScan, DynamicIndexScan, DynamicIndexOnlyScan,
     DynamicBitmapHeapScan, DynamicForeignScan)
   - Skips PartitionSelector (JOIN dynamic pruning)
   - Exempts nodes with `initial_pruning_steps` or `exec_pruning_steps`
     (runtime pruning capable)
   
   ### Exemptions
   | Scenario | Behavior |
   |----------|----------|
   | `reject_partition_fullscan = off` | Allowed |
   | `enable_partition_pruning = off` | Allowed |
   | Single-partition table (nparts <= 1) | Allowed |
   | Parameterized query ($1, PARAM_EXEC) | Allowed (runtime pruning) |
   | PartitionSelector (ORCA JOIN pruning) | Allowed |
   | No WHERE / WHERE 1=1 | **Rejected** |
   | WHERE on non-partition-key column | **Rejected** |
   
   ### Error message
   ```
   ERROR: partitioned table "schema.table" full partition scan is not
          allowed, N partitions would be scanned
   HINT:  Add a WHERE clause on the partition key to enable partition pruning.
   ```
   
   ## Type of Change
   - [ ] Bug fix
   - [x] New feature
   - [ ] Breaking change
   - [ ] Documentation update
   
   ## Breaking Changes
   
   Queries that previously performed full partition scans will now be
   rejected by default. Users can disable with:
   `SET reject_partition_fullscan = off;`
   
   ## Test Plan
   - [x] New regression test: `partition_fullscan_reject.sql` (12 scenarios)
     - Basic rejection (no WHERE, WHERE 1=1, non-partition-key WHERE)
     - Pruning passes (WHERE on partition key)
     - GUC on/off toggle
     - enable_partition_pruning=off exemption
     - Single-partition table exemption
     - Threshold mode (threshold=2)
     - Prepared statement Param exemption
     - UPDATE/DELETE rejection
     - Subquery propagation
     - ORCA path verification
   - [ ] `make installcheck`
   - [ ] `make -C src/test installcheck-cbdb-parallel`
   
   ## Impact
   - **Performance**: negligible — Planner path adds one integer comparison
     per partitioned table; ORCA path adds one lightweight plan_tree_walker
   - **User-facing**: new ERROR for unfiltered partition queries; two new GUCs
   - **Risk**: low — read-only checks, GUC-controllable, zero ORCA C++ changes
   
   ## Compliance Checklist
   - [x] I have read the [contribution 
guide](https://cloudberry.apache.org/contribute/code)
   - [x] My code follows PostgreSQL coding conventions
   - [x] New functionality includes tests
   - [x] I have reviewed my changes for security implications
   - [x] I have requested appropriate reviewers
   
   ## Files Changed
   
   | File | Change |
   |------|--------|
   | `src/backend/optimizer/path/costsize.c` | GUC variable definitions (+2) |
   | `src/include/optimizer/cost.h` | extern declarations (+2) |
   | `src/backend/utils/misc/guc.c` | GUC registration (+27) |
   | `src/include/utils/unsync_guc_name.h` | GUC name registration (+2) |
   | `src/backend/optimizer/util/inherit.c` | Planner path check (+85) |
   | `src/backend/optimizer/plan/orca.c` | ORCA path check (+164) |
   | `src/test/regress/sql/partition_fullscan_reject.sql` | New test (+127) |
   | `src/test/regress/greenplum_schedule` | Register test (+2) |


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to