ClickhouseColton ShepardExplainer
Hints and hazards: pg_plan_advice in PG19
Summary
Postgres 19 introduces pg_plan_advice and pg_stash_advice, two contrib modules that provide a query hint experience without affecting all queries or SQL syntax. pg_plan_advice works at the session level via SET commands, while pg_stash_advice applies hints to specific query identifiers. The extensions support specifying scan method, join order, join method, parallelism, partitionwise joins, and semijoin uniqueness. They work by telling the planner not to pick unselected options, but if no viable plan exists the advice is partially or fully ignored, and there is no check that the advice is good. The article demonstrates that while hints can dramatically improve performance in some scenarios, shifting data distributions can make them counterproductive, as shown by a bitmap heap scan hint that went from a 150x improvement to a 7,000x degradation.
Context
Postgres 19 introduces two new contrib modules, pg_plan_advice and pg_stash_advice, that provide a query hint experience without affecting all queries or SQL syntax. The Postgres community has historically been divided on query hints, with the general consensus being that planner failures were bugs to fix rather than work around. pg_plan_advice works at the session level via SET commands, while pg_stash_advice applies hints to specific query identifiers. The extensions support specifying scan method, join order, join method, parallelism, partitionwise joins, and semijoin uniqueness.
Approach / What changed
Use SET LOCAL pg_plan_advice.advice = ‘…’ to specify scan method, join order, join method, parallelism, partitionwise joins, or semijoin uniqueness for the next query in a session, or use pg_stash_advice to apply hints to a specific query identifier. The extensions work by telling the planner not to pick the unselected options, so if no viable plan exists the advice is partially or fully ignored.
Takeaways
pg_plan_advice and pg_stash_advice in Postgres 19 provide a hint mechanism that works by telling the planner not to pick unselected options, but cannot force a specific plan and will silently ignore advice if no viable plan exists.
A bitmap heap scan hint improved an ORDER BY…LIMIT query from 1462ms to 10ms when the planner’s index scan had to discard 10 million rows, but the same hint became 7000x worse after data distribution changed.
All hints should be regularly checked for ongoing necessity because shifting data and query patterns can make them counterproductive, and the EXPLAIN output does not flag a hint as harmful even when it degrades performance.
FAQ
How do pg_plan_advice and pg_stash_advice actually enforce hints?
The extensions work by telling the planner not to pick the options you didn't select. They cannot force Postgres to pick a specific plan, so if there is no viable way to use the hint, the advice will be partially or fully ignored. There is also no check that the advice is good, so the planner will accept input that degrades performance.
Can a hint become harmful over time?
Yes, if data distribution or query patterns shift, a previously helpful hint can become counterproductive. The article shows a scenario where a bitmap heap scan hint that improved performance by 150x became 7,000x worse after the data distribution changed, yet the EXPLAIN output still showed the hint as 'matched' with no failure flag.
How should pg_plan_advice be used in production?
Use SET LOCAL in a transaction to prevent unexpected effects on other queries. For session-level enablement, add pg_plan_advice to shared_preload_libraries or use LOAD 'pg_plan_advice'. For pg_stash_advice, note that it requires special handling for HA failover because it is not WAL-logged.
Source and freshness
- Source
- clickhouse.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.