Skip to content

Don't use Orca to plan SELECTs with an empty range table - #400

Open
Alena0704 wants to merge 1 commit into
OPENGPDB_STABLEfrom
orca-skip-constant-select
Open

Don't use Orca to plan SELECTs with an empty range table#400
Alena0704 wants to merge 1 commit into
OPENGPDB_STABLEfrom
orca-skip-constant-select

Conversation

@Alena0704

@Alena0704 Alena0704 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Skip Orca for a SELECT with an empty range table, no sublinks and no parent
utility statement. This covers FROM-less SQL function bodies too. The rule also
applies to the bodies of SQL functions. That is why some Orca fallback messages
disappear even for queries that do have a FROM clause: the message came from
planning the function body, not the query execution itself.

For some of these queries - the ones with parameters or with SIRV functions -
Orca does not produce a plan at all and falls back, so the attempt is wasted twice over.
Skipping it makes such queries about 16x faster.

Co-Authored-By: andr-sokolov sokolov.andrey.yurevich@gmail.com

@Alena0704
Alena0704 marked this pull request as draft July 7, 2026 08:21
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch from 25c8169 to 8e4ab9e Compare July 7, 2026 08:50
Comment thread src/backend/optimizer/plan/planner.c
Comment thread src/backend/optimizer/plan/planner.c Outdated
Comment thread src/backend/optimizer/plan/planner.c Outdated
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 6 times, most recently from 90844bd to 6b7ed8f Compare July 9, 2026 11:11
@Alena0704

Copy link
Copy Markdown
Contributor Author

Just in case, I'll write about changes in out files of regression tests. All changes stem from one cause: trivial constant SELECTs with no range table (SELECT 1, SELECT pg_column_size(...), SELECT pg_lock_status()) now bypass ORCA and are planned by the Postgres planner.

  • bfv_catalog_optimizer.out. In EXPLAIN, the two nested Result nodes and Pivotal Optimizer (GPORCA) are replaced by a single Result and Optimizer: Postgres query optimizer. Queries are simply planned by Postgres now, the plan is simpler (one node instead of two), and the result is identical.
  • qp_gist_indexes2_optimizer.source. The lines DETAIL: Feature not supported: SIRV functions and the second GPORCA failed to produce a plan are gone. Previously ORCA tried to plan the constant EXPLAIN sub-query and failed twice (first on SIRV functions, then on required properties), producing a doubled fallback message. Now that trivial query never goes to ORCA, so only the single honest fallback from the real query remains.
  • pgaudit.out. This is the subtlest one. The READ,SELECT,,,SELECT 1 line disappeared. The READ record is emitted by the ExecutorStart hook (commandText = sourceText), while AST_SEL is emitted by the planner hook at plan time. For SELECT 1 INTO test inside the DO block: ORCA produced a plan with two Result nodes, which PL/pgSQL does not recognize as a "simple expression", so the query ran through the executor → ExecutorStart fired → a READ line appeared. The Postgres planner produces a single Result, which PL/pgSQL recognizes as a simple expression and evaluates directly, bypassing the executor → ExecutorStart never fires → no READ line. The AST_SEL line stays because planning (and thus the planner hook) still happens in both cases.

@Alena0704
Alena0704 marked this pull request as ready for review July 12, 2026 23:02
Comment thread src/test/regress/expected/bfv_catalog_optimizer.out
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 3 times, most recently from 9466824 to 77b21f8 Compare August 25, 2026 17:08
@Alena0704 Alena0704 changed the title Don't use Orca to plan trivial constant queries Don't use Orca to plan FROM-less SELECT queries Sep 1, 2026
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch from 77b21f8 to d126fcd Compare September 1, 2026 07:30
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 2 times, most recently from 4216714 to 58c1e74 Compare September 8, 2026 13:54
Comment thread gpcontrib/pgaudit/expected/pgaudit.out
@Alena0704

Copy link
Copy Markdown
Contributor Author

For FROM-less SELECT queries ORCA has nothing to work with: there is no range table, nothing to distribute across segments, nothing to reorder. Its plan either matches the Postgres planner's but adds a redundant wrapping Result node and a worse cardinality estimate for set-returning functions, or it is not produced at all - ORCA fails on SIRV functions and falls back to the Postgres planner anyway. So bypassing ORCA here is not a "faster but worse plan" trade-off: the plan is strictly no worse, and better in node count and estimates, while we additionally save the entire optimization attempt - a FROM-less SELECT runs about 16x faster (0.33 ms -> 0.02 ms per query in a release build).

Query STABLE (ORCA) Patched (PG planner) Speedup
SELECT 1 3,048 TPS · 0.328 ms 51,563 TPS · 0.019 ms 16.9x
SELECT 42, 'abc'::text, 1+2*3 2,492 TPS · 0.401 ms 40,079 TPS · 0.025 ms 16.1x
SELECT pg_column_size('...') 2,859 TPS · 0.350 ms 45,546 TPS · 0.022 ms 15.9x
SELECT count(*) FROM bench_t WHERE a > 10 (control) 858 TPS 849 TPS 0.99x — unchanged

Comment thread src/backend/optimizer/plan/planner.c Outdated
Comment thread src/backend/optimizer/plan/planner.c Outdated
Comment thread src/backend/optimizer/plan/planner.c Outdated
Comment thread src/backend/optimizer/plan/planner.c
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch from 58c1e74 to 6d72571 Compare September 9, 2026 08:12
Comment thread src/backend/optimizer/plan/planner.c
Comment thread src/test/regress/expected/aggregates_optimizer.out
@Alena0704 Alena0704 changed the title Don't use Orca to plan FROM-less SELECT queries Skip Orca for a SELECT with an empty range table, no sublinks and no parent utility statement Sep 9, 2026
@Alena0704 Alena0704 changed the title Skip Orca for a SELECT with an empty range table, no sublinks and no parent utility statement Don't use Orca to plan SELECTs with an empty range table Sep 9, 2026
Comment thread src/backend/optimizer/plan/planner.c Outdated
Comment on lines +5886 to +5888
* statement (CTAS, COPY, REFRESH MATERIALIZED VIEW) is left to ORCA as well,
* because there it is the parent statement, not the SELECT itself, that
* decides what gets dispatched.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does Orca build a better plan for utility statement? Can you share an example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right, it is not about a better plan. The point is that such a query is not local: CREATE TABLE t AS SELECT 42 still has to distribute its result row according to the target table's policy. I reworded the comment.

Skip Orca for a SELECT with an empty range table, no sublinks and no
parent utility statement.  It also covers FROM-less SQL function bodies.
That is why some Orca fallback messages disappear even for queries that
do have a FROM clause: the message came from planning the function body,
not the query itself.

For some of these queries - the ones with parameters or with SIRV
functions - Orca does not produce a plan at all and falls back, so the
attempt is wasted twice over.  Skipping it makes such queries about 16x
faster.

Co-Authored-By: andr-sokolov <sokolov.andrey.yurevich@gmail.com>
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 4 times, most recently from 12c0258 to 6a60104 Compare September 9, 2026 14:09
@Alena0704

Copy link
Copy Markdown
Contributor Author

I tried removing the parentStmtType condition and CI shows it is not safe: with optimizer=on the distribution policy of a CTAS table changes - matview gets Distributed by: (i) instead of Distributed randomly, legacy_hashops_ctas ends up hash-distributed on cdbhash_int4_ops, and create_table_distpol starts emitting using default RANDOM distribution. So I suggest not to add it in this PR.

@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch from 6a60104 to aa50a56 Compare September 9, 2026 16:25
Comment on lines +5880 to +5881
* subquery over a distributed relation, and for CTAS the result row has to be
* distributed according to the target table's policy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is it a problem that row has to be distributed according to the target table's policy?

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