← Back to PostgreSQL, page 4

postgresql-0071 · Hard senior

Why can a prepared statement get slower after five executions? Explain custom versus generic plans.

For a prepared statement, PostgreSQL plans the first five executions with the actual parameter values (custom plans), then considers a generic plan that ignores the values and switches if its estimated cost is not worse on average. With skewed data, a generic plan that suits rare values can be terrible for common ones, or the reverse. plan_cache_mode = force_custom_plan or force_generic_plan overrides the choice.

Full answer

Setup for the example:

sql
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;
sql
PREPARE by_status(text) AS SELECT count(*) FROM orders WHERE status = $1; EXPLAIN (COSTS OFF) EXECUTE by_status('disputed');

Result on PostgreSQL 17:

text
Aggregate
  ->  Index Only Scan using orders_status on orders
        Index Cond: (status = 'disputed'::text)
sql
EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');

Result:

text
Aggregate
  ->  Seq Scan on orders
        Filter: (status = 'delivered'::text)
sql
SET plan_cache_mode = force_generic_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');

Output:

text
Aggregate
  ->  Index Only Scan using orders_status on orders
        Index Cond: (status = $1)
sql
SET plan_cache_mode = force_custom_plan; EXPLAIN (COSTS OFF) EXECUTE by_status('delivered');

PostgreSQL returns:

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

sql
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