14
Private and General Plans
Private Plans
May be beneficial in cases of uneven distribution, but the query is re-planned
each time it's executed.
Common Plan
Best suited for uniform distribution
Cached within the session for prepared statements
Seq Scan on tasks
Filter: status = 'done'
Index Scan on tasks
Index Cond: status = 'todo'
Index Scan on tasks
Index Cond: id = $1
is built
with values
parameters
is built
without considering the values
parameters
With the simple query protocol (refer to the "Planningand Execution"
section), each query is re-planned, taking into account the parameter values.
The extended protocol allows for the preparation of statements, which can
have parameters. Preparation always involves parsing and query rewriting,
with the parse tree stored in the backend process's local memory.
When executing a prepared statement, there are options. The query is
typically re-planned each time, taking into account the parameter values.
Such plans are called custom and (custom)
It makes sense when there's an uneven distribution, as optimal plans may
vary depending on the values. For example, index access is more effective
for highly selective conditions, while a sequential scan is preferable when
the value is common.
But the query can be planned without considering parameter values. This
allows not only saving the parse tree but also the plan itself, avoiding the
need for re-planning. Such a plan is referred to as generic (generic).
A generic plan performs well in cases of uniform distribution, where the
condition's selectivity is not dependent on the specific value.But in the case
of uneven distribution, a generic plan may perform well for some values but
poorly for others.
Let's see how the planner decides which option to use.