Skip to content

PostgreSQL: Parse optimizer hints in leading comments#2320

Merged
iffyio merged 1 commit intoapache:mainfrom
readysettech:query_hints_pg_upstream
Apr 24, 2026
Merged

PostgreSQL: Parse optimizer hints in leading comments#2320
iffyio merged 1 commit intoapache:mainfrom
readysettech:query_hints_pg_upstream

Conversation

@altmannmarcelo
Copy link
Copy Markdown
Contributor

Enable comment-based optimizer hint parsing for the PostgreSQL dialect by overriding supports_comment_optimizer_hint to return true.

PostgreSQL itself does not ship a built-in hint system, but the widely used pg_hint_plan extension (https://github.com/ossc-db/pg_hint_plan) introduces hints using the exact same /*+ ... */ block-comment and --+ ... single-line-comment syntax already supported for MySQL and Oracle (#2162). Without this flag, parsing a pg_hint_plan statement such as:

SELECT /*+ SeqScan(t) */ * FROM t;

discards the hint as an ordinary comment, making it impossible for downstream tools (query rewriters, proxies, planners) built on sqlparser-rs to preserve or act on hints when targeting PostgreSQL.

The parser side already handles this via maybe_parse_optimizer_hint across SELECT / INSERT / UPDATE / DELETE / MERGE, so no parser or AST changes are required — this is purely a dialect opt-in. The default remains false, so no other dialect is affected.

Enable comment-based optimizer hint parsing for the PostgreSQL dialect
by overriding `supports_comment_optimizer_hint` to return `true`.

PostgreSQL itself does not ship a built-in hint system, but the widely
used `pg_hint_plan` extension (https://github.com/ossc-db/pg_hint_plan)
introduces hints using the exact same `/*+ ... */` block-comment and
`--+ ...` single-line-comment syntax already supported for MySQL and
Oracle (apache#2162). Without this flag, parsing a `pg_hint_plan` statement
such as:

    SELECT /*+ SeqScan(t) */ * FROM t;

discards the hint as an ordinary comment, making it impossible for
downstream tools (query rewriters, proxies, planners) built on
`sqlparser-rs` to preserve or act on hints when targeting PostgreSQL.

The parser side already handles this via `maybe_parse_optimizer_hint`
across SELECT / INSERT / UPDATE / DELETE / MERGE, so no parser or AST
changes are required — this is purely a dialect opt-in. The default
remains `false`, so no other dialect is affected.
@altmannmarcelo altmannmarcelo force-pushed the query_hints_pg_upstream branch from bd958be to 4114ff0 Compare April 23, 2026 18:12
Copy link
Copy Markdown
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @altmannmarcelo!

@iffyio iffyio added this pull request to the merge queue Apr 24, 2026
Merged via the queue into apache:main with commit 7eba3ca Apr 24, 2026
10 checks passed
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