A Prepared Statement Switches to a Generic Plan After Five Runs
When you run the same prepared statement repeatedly, PostgreSQL plans it fresh with your actual parameter values the first five times, then compares and may switch to one generic plan reused for every set of parameters — saving planning time, but ignoring what those parameters are. If your data is skewed, the generic plan can be much worse than the plan any single value deserved. plan_cache_mode lets you force force_custom_plan or force_generic_plan when you need to.
Questions this Concept answers
Why can a generic plan be much worse than the custom plans that preceded it?
J
jeremy
Video
Partition Pruning with Prepared Statements and Generic vs Custom Query Plans in PostgreSQL
PostgreSQL prepared statements split query processing into a PREPARE step (parse analysis, with the parse tree cached per connection) and an EXECUTE step (plan selection and execution), saving parse …