Skip to content

sql,hints: improve EXPLAIN output for statement hints#166330

Open
DrewKimball wants to merge 2 commits intocockroachdb:masterfrom
DrewKimball:explain-stmt-hints
Open

sql,hints: improve EXPLAIN output for statement hints#166330
DrewKimball wants to merge 2 commits intocockroachdb:masterfrom
DrewKimball:explain-stmt-hints

Conversation

@DrewKimball
Copy link
Collaborator

sql: show applied vs total count for statement hints in EXPLAIN

Track runtime hint errors (planning fallbacks, session variable
failures, validation errors) on the instrumentation helper so that
EXPLAIN output correctly reflects which hints were applied vs skipped.

The hint model is refactored from a boolean Enabled field to an
Err error field with sentinel errors for disabled and duplicate
hints. Runtime errors are stored separately in a runtimeHintErrors
map to avoid mutating shared hint state across prepared statement
executions.

Informs: #163878

Release note (sql change): EXPLAIN and EXPLAIN ANALYZE now display
statement hint counts as "applied" versus "skipped" rather than a
simple total. Hints that fail at runtime (e.g. referencing a
non-existent index or an invalid session variable) are correctly
reflected as skipped.

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

sql: add verbose EXPLAIN output for statement hint details

When EXPLAIN (VERBOSE) or EXPLAIN ANALYZE (VERBOSE) is used with
statement hints, emit a tree showing each hint's type, details (donor
SQL or variable name/value), and skip reason when applicable. Hints
are grouped into "applied" and "skipped" sections.

Fixes: #163878

Release note (sql change): EXPLAIN (VERBOSE) and EXPLAIN ANALYZE
(VERBOSE) now include a detailed tree of statement hints showing
each hint's type, configuration (donor SQL or variable name/value),
and skip reason when applicable.

DrewKimball and others added 2 commits March 20, 2026 13:49
Track runtime hint errors (planning fallbacks, session variable
failures, validation errors) on the instrumentation helper so that
EXPLAIN output correctly reflects which hints were applied vs skipped.

The hint model is refactored from a boolean `Enabled` field to an
`Err error` field with sentinel errors for disabled and duplicate
hints. Runtime errors are stored separately in a `runtimeHintErrors`
map to avoid mutating shared hint state across prepared statement
executions.

Informs: cockroachdb#163878

Release note (sql change): EXPLAIN and EXPLAIN ANALYZE now display
statement hint counts as "applied" versus "skipped" rather than a
simple total. Hints that fail at runtime (e.g. referencing a
non-existent index or an invalid session variable) are correctly
reflected as skipped.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
When EXPLAIN (VERBOSE) or EXPLAIN ANALYZE (VERBOSE) is used with
statement hints, emit a tree showing each hint's type, details (donor
SQL or variable name/value), and skip reason when applicable. Hints
are grouped into "applied" and "skipped" sections.

Fixes: cockroachdb#163878

Release note (sql change): EXPLAIN (VERBOSE) and EXPLAIN ANALYZE
(VERBOSE) now include a detailed tree of statement hints showing
each hint's type, configuration (donor SQL or variable name/value),
and skip reason when applicable.
@DrewKimball DrewKimball requested a review from a team March 20, 2026 19:00
@DrewKimball DrewKimball requested a review from a team as a code owner March 20, 2026 19:00
@DrewKimball DrewKimball requested review from yuzefovich and removed request for a team March 20, 2026 19:00
@trunk-io
Copy link
Contributor

trunk-io bot commented Mar 20, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball DrewKimball requested review from ZhouXing19 and michae2 and removed request for yuzefovich March 20, 2026 20:46
Copy link
Collaborator

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

:lgtm_strong: I only have nit comments, and defer the final stamp to Michael.

@ZhouXing19 reviewed 12 files and all commit messages, and made 4 comments.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on DrewKimball and michae2).


pkg/sql/plan_opt.go line 77 at r1 (raw file):

		// Do not return the error. If semantic analysis failed, try preparing again
		// without injected hints.
		log.Eventf(ctx, "preparing with injected hints failed with: %v", err)

Should we do p.instrumentation.recordHintError() here as well?


pkg/sql/opt/exec/explain/output.go line 350 at r1 (raw file):

	} else {
		skipped := total - applied
		ob.AddTopLevelField("statement hints", fmt.Sprintf(

Nice addition!


pkg/sql/opt/exec/execbuilder/testdata/statement_hint_builtins line 2295 at r2 (raw file):


statement ok
EXECUTE hint_retry_stmt

nit: might be more informative to run it with EXPLAIN ANALYZE (VERBOSE) EXECUTE.

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.

sql,hints: improve EXPLAIN output for statement hints

3 participants