---
title: "Hints and hazards: pg_plan_advice in PG19"
description: "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."
---

# Hints and hazards: pg\_plan\_advice in PG19

[Clickhouse](https://yomu.fyi/company/clickhouse) · Colton Shepard · Nov 25, 2026

**Type:** Explainer

## 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.

**Tags:** [pg\_plan\_advice](https://yomu.fyi/topic/pg-plan-advice), [pg\_stash\_advice](https://yomu.fyi/topic/pg-stash-advice), [Postgres](https://yomu.fyi/topic/postgres)

- Source: [Clickhouse](https://clickhouse.com/blog/pg-plan-advice-postgresql-19)
- Source URL: https://clickhouse.com/blog/pg-plan-advice-postgresql-19
- Ingested by Yomu: 2026-11-25T21:00:16.388Z

[Read original post](https://clickhouse.com/blog/pg-plan-advice-postgresql-19)
