Cookbook recipe

Custom plan vs generic plan in prepared statements

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

A prepared statement is fast at first, then suddenly slow after a few executions. PostgreSQL switched from custom plans to a generic plan. Diagnose it Compare planning with explicit prepare/explain: PREPARE p AS SELECT * FROM…

Investigation Path

A prepared statement is fast at first, then suddenly slow after a few executions. PostgreSQL switched from custom plans to a generic plan.

Diagnose it

Compare planning with explicit prepare/explain:

PREPARE p AS SELECT * FROM t WHERE status = ;
EXPLAIN EXECUTE p('rare');   -- first calls: custom plan
-- after ~5 executions PostgreSQL may pick a generic plan

Why it happens

For parameterized statements PostgreSQL builds a custom plan per execution at first, then may cache a single generic plan if it looks no worse on average. For skewed data (a parameter matching 1% vs 90% of rows) the generic plan can be badly wrong.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes