When PostgreSQL plan advice outlives the benchmark

ProjectsCommunity Digest0 replies

Community DigestMember#1

In PostgreSQL 19's pg_plan_advice turned a 28ms query into 264ms under load, Alex Georgiev tests planner advice on PostgreSQL 19 RC2. His benchmark starts with a query that already has a sensible plan, then measures what happens when an operator overrides one decision.

A scan choice can matter more under load

Georgiev built a two-million-row orders table where just 0.5% of the rows were marked refunded. The query joined refunded orders to customers and aggregated the results by region. PostgreSQL chose an index scan for the selective orders filter and a hash join against customers. Forcing a sequential scan for orders raised the median of three single executions from 18.0 ms to 46.1 ms.

With ten pgbench clients running for ten seconds, average latency was 28.5 ms with the default plan and 264.0 ms with the forced scan. These timings describe one schema and test machine. They give a concrete reason to benchmark a proposed plan across concurrent sessions before adding it to a pooler's startup configuration.

Check what matched and when planning happened

In the follow-up, Georgiev changed the table so half the orders were refunded and refreshed statistics. The unpinned planner switched to a parallel sequential scan, while the pinned query kept the index scan. The advice still showed as matched. Its latency varied with client count and changed again after reorganizing the table. Matching feedback therefore answers whether the planner applied the request. The benchmark answers whether keeping that request is worthwhile for the current workload.

The PostgreSQL documentation describes the generated advice and the feedback that accompanies supplied advice. In Georgiev's prepared-statement test, changing the session setting left an existing generic plan in use until he ran DISCARD PLANS. When checking a change, compare the actual plan after replanning and measure it with the same data and concurrency as the baseline.

No replies yet