Skip to content

Conversation

@colm-mchugh
Copy link
Contributor

DESCRIPTION: tighten distributed plan check to cover subplans.

The distributed_planner() hook checks, after calling standard_planner(), if the query still requires distributed planning - this was necessitated by issue #7782, #7783 where the citus tables in a query are optimized away by standard_planner().

However, issue #8313 exposed a case where we incorrectly flag a query as no long needing distributed planning; in this case, the citus relation(s) had been reduced to a read_intermediate_result() call after recursive planning of CTEs and subqueries. The fix involves including the plan's subplan list when checking the plan for a distributed relation - if a distributed subplan is found, the check can conclude the query requires distributed planning.

Fixes issue: #8313

@codecov
Copy link

codecov bot commented Dec 18, 2025

Codecov Report

❌ Patch coverage is 61.90476% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.78%. Comparing base (ee3812d) to head (81c19f1).

❌ Your patch check has failed because the patch coverage (61.90%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8388      +/-   ##
==========================================
- Coverage   88.79%   88.78%   -0.01%     
==========================================
  Files         287      287              
  Lines       63237    63256      +19     
  Branches     7928     7932       +4     
==========================================
+ Hits        56149    56163      +14     
- Misses       4756     4759       +3     
- Partials     2332     2334       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #8313 where queries with Citus tables that were reduced to read_intermediate_result() calls after recursive planning of CTEs and subqueries were incorrectly flagged as not requiring distributed planning. The fix enhances the distributed planning check in distributed_planner() hook to inspect both the range table and the plan's subplan list for distributed relations.

  • Enhanced distributed planning check to include subplan inspection for read_intermediate_result() function calls
  • Made IsReadIntermediateResultFunction() publicly accessible for reuse
  • Added comprehensive regression tests covering various scenarios with distributed subplans

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/backend/distributed/planner/distributed_planner.c Added PlanContainsDistributedSubPlanRTE() function to check for distributed subplans and updated CheckPostPlanDistribution() to use it
src/backend/distributed/planner/multi_logical_planner.c Changed IsReadIntermediateResultFunction() from static to public visibility
src/include/distributed/multi_logical_planner.h Added declaration for the newly exported IsReadIntermediateResultFunction()
src/test/regress/sql/subquery_in_where.sql Added test cases for queries with distributed subplans including minimal repro and variants with redundant WHERE clauses
src/test/regress/expected/subquery_in_where.out Added expected output for the new test cases verifying correct results

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@colm-mchugh colm-mchugh marked this pull request as draft December 18, 2025 19:55
DESCRIPTION: tighten distributed plan check to cover subplans.

The `distributed_planner()` hook checks, after calling `standard_planner()`,
if the query still requires distributed planning - this was necessitated by
issue #7782, #7783 where the citus tables in a query are optimized away by
`standard_planner()`.

However, issue #8313 exposed a case where we incorrectly flag a query as no
long needing distributed planning; in this case, the citus relation(s) had
been reduced to a read_intermediate_result() call after recursive planning
of CTEs and subqueries. The fix involves including the plan's subplan list
when checking the plan for a distributed relation - if a distributed subplan
is found, the check can conclude the query requires distributed planning.

Fixes issue: #8313
@colm-mchugh colm-mchugh marked this pull request as ready for review December 18, 2025 20:28
FROM
users_table);
users_table)
ORDER BY id;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: this change is not relevant to the fix, but this regress failed flakyness tests because of differences in the order of the results of this query. The ORDER BY stabilizes that.

@imranzaheer612
Copy link
Contributor

imranzaheer612 commented Dec 19, 2025

@colm-mchugh looks like this is also an related issue: #8312

@colm-mchugh
Copy link
Contributor Author

@colm-mchugh looks like this is also an related issue: #8312

Yes it does, and the PR also fixes #8312, from testing locally. Thanks for pointing out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants