by SigmundA · 4y ago · 0 comments
My understanding is the planner is so simple that it is deemed not necessary, unless of course it is then do a prepared statement which only works on one connection...
This would absolute trash performance on SQL server to not cache plans as its planner spends significant time optimizing.
If you used prepared statements, `plan_cache_mode` (since Postgres >=12) can be configured.
It defaults to auto, which will generate a generic plan after 5 (iirc) executions.
If set to `force_generic_plan`, it'll make a generic plan right away. If set to `force_custom_plan`, it'll always do planning. The latter can be useful for queries where the parameters of the prepared statement can have very different selectivities.
In MS SQL server plans are always cached unless told not to this is across connections, if it sees the same sql statement its will skip planning, this goes way back. If you go back far enough it only did that for stored procedures so there was a push to do everything through sprocs for performance, not so much in the last 20 years. Using prepared statements is for backward compatibility as is has almost no performance advantage due to modern plan caching.
I think PG finally has sprocs but still no plan caching even for them unless somethings changed.
PG has no way to share plans between worker processes AKAIK. I believe the lack of plan caching is noticed more with heavy jit optimizing performance where PG will use LLVM now which has a relatively high cost.
You can pass it a hint OPTION (RECOMPILE) that will make it act like PG and make new plan every time, if its a query with wildly varying parameters that doesn't execute as often this can make sense, most of the time it doesn't due to planning overhead.
There is also the OPTIMIZE FOR hint that can have it make a plan optimized for the more common of the parameter values if you don't want it planning every execution regardless of value passed on first execute.
I usually have the opposite problem with a good plan changing to a bad one due to statistics changing at some random time in production, which seems to be a common problem in PG as well and why everyone want hints.
I prefer hinting for critical problematic queries that tend to have unstable plans due to statistics changes, but you can also lock a plan in SQL server to force a specific plan, another useful feature to prevent production db instability.
Most apps I deal with queries are very regular and the plan cache makes it act like a prepared statement improving performance 99% of the time. Once in a while it works against you and you need to give it a hint to recompile or optimize for specific parameter values.