Skip to content

Don't use Orca to plan trivial constant queries#400

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

Don't use Orca to plan trivial constant queries#400
Alena0704 wants to merge 1 commit into
OPENGPDB_STABLEfrom
orca-skip-constant-select

Conversation

@Alena0704

Copy link
Copy Markdown
Contributor

Don't use Orca to plan trivial constant queries

Queries such as "SELECT 42" have no range table and nothing to distribute across segments. Sending them to Orca brings no benefit and only adds optimization overhead before falling back to the Postgres planner anyway.

@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
Comment on lines +5879 to +5882
return parse->commandType == CMD_SELECT &&
parse->rtable == NIL &&
!parse->hasSubLinks &&
parse->parentStmtType == PARENTSTMTTYPE_NONE;

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.

I think we should somehow change this condition to don't affect this test

 select aggfns(a,b,c)
   from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
-INFO:  GPORCA failed to produce a plan, falling back to planner
-DETAIL:  Feature not supported: ROW EXPRESSION
-CONTEXT:  SQL function "aggfns_trans" during startup

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.

I had to update the expected .out files because we now never invoke Orca for trivial constant queries, so there is nothing to fail and the notice is gone.

@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 4 times, most recently from f0ece2e to 66d4c4a Compare July 9, 2026 07:26
Queries such as "SELECT 42" have no range table and nothing
to distribute across segments. Sending them to Orca brings
no benefit and only adds optimization overhead before
falling back to the Postgres planner anyway.
@Alena0704
Alena0704 force-pushed the orca-skip-constant-select branch 2 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
Optimizer: Postgres query optimizer
(2 rows)

explain select pg_lock_status();

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.

This query is not a trivial constant query, but Orca doesn't plan it now. Fix please the PR description

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