Skip to content

orca: fallback to Postgres optimizer on cross-slice replicated CTE Consumer#375

Merged
Alena0704 merged 2 commits intoOPENGPDB_STABLEfrom
bugfix-cross-slice-shared-scan-cte-v3
May 6, 2026
Merged

orca: fallback to Postgres optimizer on cross-slice replicated CTE Consumer#375
Alena0704 merged 2 commits intoOPENGPDB_STABLEfrom
bugfix-cross-slice-shared-scan-cte-v3

Conversation

@Alena0704
Copy link
Copy Markdown
Contributor

@Alena0704 Alena0704 commented Apr 13, 2026

orca: fallback to Postgres optimizer on cross-slice replicated CTE Consumer
Inspired by greengage 51fe92e: before Expr->DXL translation,
walk the physical tree and track which slice each CTE Producer
and Consumer lives on. If a Consumer is on a different slice
than its Producer and the Producer's distribution is replicated,
force a fallback to the Postgres optimizer.

The replicated filter is essential: ordinary cross-slice CTE plans
(non-replicated Producer with Gather/Redistribute Consumer) are a
normal ORCA pattern and must not trigger fallback.

51fe92e doesn't trigger when a CTE over a replicated table is
referenced from a scalar subquery, so the query hangs. This commit
replaces the single-point check with a whole-tree walker that
catches both cases.

Tests: bring the original qp_orca_fallback case from 51fe92e and add
a scalar-subquery reproducer in shared_scan guarded by
statement_timeout.

@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 3 times, most recently from 2afc2dc to abb8c9d Compare April 13, 2026 10:19
@Alena0704 Alena0704 marked this pull request as draft April 13, 2026 10:41
@Alena0704 Alena0704 requested a review from andr-sokolov April 13, 2026 10:42
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch from abb8c9d to 481f8c9 Compare April 13, 2026 11:03
@Alena0704 Alena0704 marked this pull request as ready for review April 13, 2026 12:18
Copy link
Copy Markdown
Contributor

@andr-sokolov andr-sokolov left a comment

Choose a reason for hiding this comment

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

Test doesn't reproduce the error without the patch

@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch from 481f8c9 to de87aaf Compare April 13, 2026 14:01
Comment thread src/test/regress/sql/shared_scan.sql Outdated
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch from de87aaf to e85d781 Compare April 13, 2026 14:32
Alena0704 added a commit that referenced this pull request Apr 13, 2026
Increase ss_t1 to 40000 rows and simplify the query so that ORCA picks
the cross-slice plan where the unpaired-Consumer path is exercised.
With the previous 10-row input, plan costs favored the same-slice
variant and the test would silently pass even without the fix.

Cherry-picked from #375 (de87aaf).
Alena0704 added a commit that referenced this pull request Apr 13, 2026
Increase ss_t1 to 40000 rows and simplify the query so that ORCA picks
the cross-slice plan where the unpaired-Consumer path is exercised.
With the previous 10-row input, plan costs favored the same-slice
variant and the test would silently pass even without the fix.

Cherry-picked from #375 (de87aaf).
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
@Alena0704 Alena0704 changed the title Add a fallback to PO when a CTE consumer under hazard motion is found Orca: fallback to Postgres optimizer on unpaired CTE consumer in SubPlan Apr 14, 2026
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 4 times, most recently from 26c3e52 to 2b85c19 Compare April 20, 2026 07:48
@Alena0704 Alena0704 changed the title Orca: fallback to Postgres optimizer on unpaired CTE consumer in SubPlan orca: fallback to Postgres optimizer on cross-slice replicated CTE Consumer Apr 20, 2026
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 2 times, most recently from fe3ff28 to 5494df3 Compare April 21, 2026 13:24
Comment thread src/test/regress/sql/qp_orca_fallback.sql Outdated
Comment thread src/backend/gporca/libgpopt/src/translate/CTranslatorExprToDXL.cpp Outdated
Comment thread src/backend/gporca/libgpopt/src/translate/CTranslatorExprToDXL.cpp Outdated
Comment thread src/backend/gporca/libgpopt/src/translate/CTranslatorExprToDXL.cpp Outdated
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 3 times, most recently from 736b175 to fda691e Compare April 22, 2026 14:53
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 3 times, most recently from f26a8bc to a2e1973 Compare May 4, 2026 10:58
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
Comment thread src/backend/gporca/libgpopt/src/base/CUtils.cpp Outdated
…nsumer.

orca: fallback to Postgres optimizer on cross-slice replicated CTE Consumer
Inspired by greengage 51fe92e: before Expr->DXL translation,
walk the physical tree and track which slice each CTE Producer
and Consumer lives on. If a Consumer is on a different slice
than its Producer and the Producer's distribution is replicated,
force a fallback to the Postgres optimizer.

The replicated filter is essential: ordinary cross-slice CTE plans
(non-replicated Producer with Gather/Redistribute Consumer) are a
normal ORCA pattern and must not trigger fallback.

51fe92e doesn't trigger when a CTE over a replicated table is
referenced from a scalar subquery, so the query hangs. This commit
replaces the single-point check with a whole-tree walker that
catches both cases.

Tests: bring the original qp_orca_fallback case from 51fe92e and add
a scalar-subquery reproducer in shared_scan guarded by
statement_timeout.
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch 2 times, most recently from a019316 to 381ca97 Compare May 5, 2026 18:35
Co-authored-by: Andrey Sokolov <sokolov.andrey.yurevich@gmail.com>
@Alena0704 Alena0704 force-pushed the bugfix-cross-slice-shared-scan-cte-v3 branch from 381ca97 to f5349ea Compare May 6, 2026 05:27
@Alena0704 Alena0704 merged commit 3a9aebf into OPENGPDB_STABLE May 6, 2026
41 of 43 checks passed
@Alena0704 Alena0704 deleted the bugfix-cross-slice-shared-scan-cte-v3 branch May 6, 2026 07:32
Alena0704 added a commit that referenced this pull request May 6, 2026
…nsumer (#375)

Inspired by greengage 51fe92e: before Expr->DXL translation,
walk the physical tree and track which slice each CTE Producer
and Consumer lives on. If a Consumer is on a different slice
than its Producer and the Producer's distribution is replicated,
force a fallback to the Postgres optimizer.

The replicated filter is essential: ordinary cross-slice CTE plans
(non-replicated Producer with Gather/Redistribute Consumer) are a
normal ORCA pattern and must not trigger fallback.

51fe92e doesn't trigger when a CTE over a replicated table is
referenced from a scalar subquery, so the query hangs. This commit
replaces the single-point check with a whole-tree walker that
catches both cases.

Tests: bring the original qp_orca_fallback case from 51fe92e and add
a scalar-subquery reproducer in shared_scan guarded by
statement_timeout.

cherry-pick from 3a9aebf
Alena0704 added a commit that referenced this pull request May 6, 2026
…nsumer (#375)

Inspired by greengage 51fe92e: before Expr->DXL translation,
walk the physical tree and track which slice each CTE Producer
and Consumer lives on. If a Consumer is on a different slice
than its Producer and the Producer's distribution is replicated,
force a fallback to the Postgres optimizer.

The replicated filter is essential: ordinary cross-slice CTE plans
(non-replicated Producer with Gather/Redistribute Consumer) are a
normal ORCA pattern and must not trigger fallback.

51fe92e doesn't trigger when a CTE over a replicated table is
referenced from a scalar subquery, so the query hangs. This commit
replaces the single-point check with a whole-tree walker that
catches both cases.

Tests: bring the original qp_orca_fallback case from 51fe92e and add
a scalar-subquery reproducer in shared_scan guarded by
statement_timeout.

cherry-pick from 3a9aebf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants