19.7. Query Planning (original) (raw)
19.7.4. Other Planner Options
default_statistics_target
(integer
)
Sets the default statistics target for table columns without a column-specific target set via ALTER TABLE SET STATISTICS
. Larger values increase the time needed to do ANALYZE
, but might improve the quality of the planner's estimates. The default is 100. For more information on the use of statistics by the PostgreSQL query planner, refer to Section 14.2.
constraint_exclusion
(enum
)
Controls the query planner's use of table constraints to optimize queries. The allowed values of constraint_exclusion
are on
(examine constraints for all tables), off
(never examine constraints), and partition
(examine constraints only for inheritance child tables and UNION ALL
subqueries). partition
is the default setting. It is often used with traditional inheritance trees to improve performance.
When this parameter allows it for a particular table, the planner compares query conditions with the table's CHECK
constraints, and omits scanning tables for which the conditions contradict the constraints. For example:
CREATE TABLE parent(key integer, ...); CREATE TABLE child1000(check (key between 1000 and 1999)) INHERITS(parent); CREATE TABLE child2000(check (key between 2000 and 2999)) INHERITS(parent); ... SELECT * FROM parent WHERE key = 2400;
With constraint exclusion enabled, this SELECT
will not scan child1000
at all, improving performance.
Currently, constraint exclusion is enabled by default only for cases that are often used to implement table partitioning via inheritance trees. Turning it on for all tables imposes extra planning overhead that is quite noticeable on simple queries, and most often will yield no benefit for simple queries. If you have no tables that are partitioned using traditional inheritance, you might prefer to turn it off entirely. (Note that the equivalent feature for partitioned tables is controlled by a separate parameter, enable_partition_pruning.)
Refer to Section 5.11.5 for more information on using constraint exclusion to implement partitioning.
cursor_tuple_fraction
(floating point
)
Sets the planner's estimate of the fraction of a cursor's rows that will be retrieved. The default is 0.1. Smaller values of this setting bias the planner towards using “fast start” plans for cursors, which will retrieve the first few rows quickly while perhaps taking a long time to fetch all rows. Larger values put more emphasis on the total estimated time. At the maximum setting of 1.0, cursors are planned exactly like regular queries, considering only the total estimated time and not how soon the first rows might be delivered.
from_collapse_limit
(integer
)
The planner will merge sub-queries into upper queries if the resulting FROM
list would have no more than this many items. Smaller values reduce planning time but might yield inferior query plans. The default is eight. For more information see Section 14.3.
Setting this value to geqo_threshold or more may trigger use of the GEQO planner, resulting in non-optimal plans. See Section 19.7.3.
jit
(boolean
)
Determines whether JIT compilation may be used by PostgreSQL, if available (see Chapter 31). The default is on
.
join_collapse_limit
(integer
)
The planner will rewrite explicit JOIN
constructs (except FULL JOIN
s) into lists of FROM
items whenever a list of no more than this many items would result. Smaller values reduce planning time but might yield inferior query plans.
By default, this variable is set the same as from_collapse_limit
, which is appropriate for most uses. Setting it to 1 prevents any reordering of explicit JOIN
s. Thus, the explicit join order specified in the query will be the actual order in which the relations are joined. Because the query planner does not always choose the optimal join order, advanced users can elect to temporarily set this variable to 1, and then specify the join order they desire explicitly. For more information see Section 14.3.
Setting this value to geqo_threshold or more may trigger use of the GEQO planner, resulting in non-optimal plans. See Section 19.7.3.
parallel_leader_participation
(boolean
)
Allows the leader process to execute the query plan under Gather
and Gather Merge
nodes instead of waiting for worker processes. The default is on
. Setting this value to off
reduces the likelihood that workers will become blocked because the leader is not reading tuples fast enough, but requires the leader process to wait for worker processes to start up before the first tuples can be produced. The degree to which the leader can help or hinder performance depends on the plan type, number of workers and query duration.
force_parallel_mode
(enum
)
Allows the use of parallel queries for testing purposes even in cases where no performance benefit is expected. The allowed values of force_parallel_mode
are off
(use parallel mode only when it is expected to improve performance), on
(force parallel query for all queries for which it is thought to be safe), and regress
(like on
, but with additional behavior changes as explained below).
More specifically, setting this value to on
will add a Gather
node to the top of any query plan for which this appears to be safe, so that the query runs inside of a parallel worker. Even when a parallel worker is not available or cannot be used, operations such as starting a subtransaction that would be prohibited in a parallel query context will be prohibited unless the planner believes that this will cause the query to fail. If failures or unexpected results occur when this option is set, some functions used by the query may need to be marked PARALLEL UNSAFE
(or, possibly, PARALLEL RESTRICTED
).
Setting this value to regress
has all of the same effects as setting it to on
plus some additional effects that are intended to facilitate automated regression testing. Normally, messages from a parallel worker include a context line indicating that, but a setting of regress
suppresses this line so that the output is the same as in non-parallel execution. Also, the Gather
nodes added to plans by this setting are hidden in EXPLAIN
output so that the output matches what would be obtained if this setting were turned off
.
plan_cache_mode
(enum
)
Prepared statements (either explicitly prepared or implicitly generated, for example by PL/pgSQL) can be executed using custom or generic plans. Custom plans are made afresh for each execution using its specific set of parameter values, while generic plans do not rely on the parameter values and can be re-used across executions. Thus, use of a generic plan saves planning time, but if the ideal plan depends strongly on the parameter values then a generic plan may be inefficient. The choice between these options is normally made automatically, but it can be overridden with plan_cache_mode
. The allowed values are auto
(the default), force_custom_plan
and force_generic_plan
. This setting is considered when a cached plan is to be executed, not when it is prepared. For more information see PREPARE.