
A few years ago, I wrote about an innocent-looking microservices pattern that could make PostgreSQL performance unexpectedly dramatic. Not because the SQL changed. Not because someone dropped an index. The data changed, the statistics told an outdated story, and the planner made a perfectly rational decision based on fiction.
Back then, pg_hint_plan was one possible escape hatch. PostgreSQL 19 introduces two supplied tools, pg_plan_advice and pg_stash_advice. Time to revisit our old friend: the transactional outbox.
Transactional outbox pattern :
The transactional outbox solves the “database committed, message publish failed” problem.


Set up the lab :
Prerequisites (restart the PostgreSQL service after applying).
ALTER SYSTEM SET shared_preload_libraries = pg_plan_advice, pg_stash_advice;
ALTER SYSTEM SET compute_query_id = on;

For simpler plans to read, disabled parallel query and jit is off (hurrah!) by default in pg19.
Schema :
“LAB ONLY” autovacuum off so nothing refreshes the statistics behind our back.

-- Classic Outbox pattern: order and event are committed in one transaction.
BEGIN;
INSERT INTO demo_orders (order_id, customer_id, order_total) VALUES (1, 42, 129.00);
INSERT INTO demo_outbox (aggregate_id, event_type, event_payload, created_at, delivery_status)
VALUES (1, 'OrderPlaced', '{"orderId":1,"total":129.00}', clock_timestamp(), 'READY');
COMMIT;
Lets generate dummy data mimicking a production outbox.

We check plan of classical polling query when stats are accurate.

Generated Plan Advice is the advice string that would reproduce the plan you got. In the healthy run that’s INDEX_SCAN(e public.demo_outbox_poll_idx), so it’s a ready-made string to copy into a stash.
Let’s do retention cleanup, then ANALYZE at the worst possible moment.

What the planner now believes about the table :

Suddenly traffic burst, NO ANALYZE afterwards (autovacuum busy in real life) ==> Suboptimal plan selected
-- 900k events arrive during a message-broker outage: the relay cannot publish,so every one of them is still READY. Nobody re-analyzes.
INSERT INTO demo_outbox (aggregate_id, event_type, event_payload, created_at, delivery_status)
SELECT g, 'OrderPlaced',
'{"orderId":' || g || ',"details":"' || repeat('x', 250) || '"}',
now() - interval '2 days' + g * interval '0.1 second',
'READY'
FROM generate_series(300002, 1200001) AS g;

Planner still says 21 rows / 14286 pages; real file is ~57000 pages.
EXPLAIN (ANALYZE,BUFFERS)
SELECT e.event_id, e.event_payload
FROM demo_outbox AS e
WHERE e.delivery_status = 'READY'
ORDER BY e.created_at, e.event_id
LIMIT 50 FOR UPDATE OF e SKIP LOCKED;

And there is our plot twist. In my captured run, the bad plan is a Bitmap Heap Scan followed by an external merge Sort, not a plain sequential scan. The engine processes a huge number of READY rows, sorts them, and returns 50. The sort spills to temporary disk files.
pg_plan_advice — fix the plan for this session
We already know how plan looked when stats were correct so lets try to use previously seen advice.
-- Advice names the query ALIAS (e), not the table.
-- SET statement could be put inside application code.
SET pg_plan_advice.advice = 'INDEX_SCAN(e demo_outbox_poll_idx)';
EXPLAIN (ANALYZE, BUFFERS)
SELECT e.event_id, e.event_payload
FROM demo_outbox AS e
WHERE e.delivery_status = 'READY'
ORDER BY e.created_at, e.event_id
LIMIT 50 FOR UPDATE OF e SKIP LOCKED;
-- Index Scan again: ~0.4 ms, ~100 buffers, no temp I/O.

pg_stash_advice — fix it for every session and persist, no app change
A stash is a named mapping of query ID to advice string. One stash can hold MULTIPLE query entries. Query ID can be seen on explain with advice option as shown below :

A stash maps query ID -> advice. Use the Query Identifier you wrote down.

-- turn above stash on for every NEW connection to this database
ALTER DATABASE findb SET pg_stash_advice.stash_name = 'outbox_incident';
Open a new connection and check plan.

-- Stash could also be assigned to a specific role
ALTER ROLE outbox_app
SET pg_stash_advice.stash_name = 'outbox_production';
The takeaway
PostgreSQL 19 finally lets the DBA tell the planner: “I value your opinion. I am declining it for this query.” pg_plan_advice makes the intervention testable; pg_stash_advice makes it deployable by query ID, role or database, without changing the SQL text in application code. That is useful power. Please don’t turn every query into a custody battle.

