Full answer
Setup for the example:
CREATE TABLE orders (id int, status text);
INSERT INTO orders SELECT g, CASE WHEN g <= 20 THEN 'disputed' ELSE 'delivered' END FROM generate_series(1, 50000) g;
CREATE INDEX orders_status ON orders (status);
ANALYZE orders;PREPARE by_status(text) AS SELECT count(*) FROM orders WHERE status = $1; EXPLAIN (COSTS OFF) EXECUTE by_status('disputed');Result on PostgreSQL 17:
Aggregate
-> Index Only Scan using orders_status on orders
Index Cond: (status = 'disputed'::text)EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');Result:
Aggregate
-> Seq Scan on orders
Filter: (status = 'delivered'::text)SET plan_cache_mode = force_generic_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');Output:
Aggregate
-> Index Only Scan using orders_status on orders
Index Cond: (status = $1)SET plan_cache_mode = force_custom_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');PostgreSQL returns:
Aggregate
-> Seq Scan on orders
Filter: (status = 'delivered'::text)Custom plans use the actual value: an index-only scan for the 20 disputed orders and a sequential scan for the 49,980 delivered ones. The generic plan, forced here so the run is repeatable, is planned without knowing the value, so its condition shows the placeholder $1 and its row estimate is an average across statuses; here it picks an index-only scan, and that plan is then used for every value, including 'delivered', which matches almost the whole table and for which the custom plan prefers a sequential scan. In the default auto mode, PostgreSQL switches to the generic plan after five executions only if its estimated cost is not much worse than the custom plans seen so far, which with skewed data can lock in the wrong shape. Forcing custom plans restores value-specific planning at the cost of planning on every execution. Drivers that prepare statements on the server, such as JDBC after a threshold or psycopg with prepare_threshold, can trigger this, which makes it a common cause of a query that is fast in psql but slow in the application.
CREATE TABLE orders (id int, status text);
INSERT INTO orders SELECT g, CASE WHEN g <= 20 THEN 'disputed' ELSE 'delivered' END FROM generate_series(1, 50000) g;
CREATE INDEX orders_status ON orders (status);
ANALYZE orders;
PREPARE by_status(text) AS SELECT count(*) FROM orders WHERE status = $1; EXPLAIN (COSTS OFF) EXECUTE by_status('disputed');
EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');
SET plan_cache_mode = force_generic_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');
SET plan_cache_mode = force_custom_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- First five executions use custom plans
- Then a generic plan may be cached
- Generic plans ignore parameter values
- Skewed data makes generic plans risky
- plan_cache_mode overrides the choice
Common mistakes
- Comparing psql timings with driver behaviour without checking prepared plans
- Forcing generic plans on skewed columns
Follow-ups
- How do you see whether a plan is generic in production?
- How does pg_stat_statements help here?
Tips
- Point at $1 in the plan