Hints PostgreSQL to change its intended execution plan via special comment statements using pg_hint_plan extension.
Background information
Unlike adjusting GUC variables to affect execution plans, which impacts all SQL in the current session, pg_hint_plan only modifies the specified single SQL execution plan to optimize a specific SQL execution plan.
Prerequisites
Before using this extension, include pg_stat_statements and pg_hint_plan in the shared_preload_libraries parameter. For more information about how to modify parameters, refer to Modifying parameters. Create and delete extension
tip
A super account is required to create the extension.
Usage
Usage prompt phrases
pg_hint_plan reads special format prompt phrases in the target sql statement. Prompt phrases start with characters /*+ and end with */. Prompt phrases consist of prompt names and parameters, enclosed in parentheses, with space separating two parameters. To improve readability, each prompt phrase occupies one line.
The default plan in the following example is:
Specifying HashJoin as the chosen join method starts with /*+ and ends with */, plan is:
Usage prompt table
Using prompt phrases causes many inconveniences when the query statement cannot be edited. At this time, the prompt can be placed in the special table hint_plan.hints, with the table structure as follows:
Example
Prompt type
According to how the prompt phrases affect the execution plan, they can be divided into the following six categories: scan method, join method, join order, row number correction, parallel query, and GUC settings. The list of prompt phrases for each type can be viewed in the prompt list. Scan method prompt
- The scan method prompt forces the target table to use a specific scan method. pg_hint_plan identifies the target table by its table alias (if any). Scan methods include SeqScan, IndexScan, etc.
- Scan prompts are effective for ordinary tables, inherited tables, UNLOGGED tables, temporary tables, and system tables, but ineffective for external tables, table functions, VALUES clauses, common expressions, views, and subqueries.
Connection method prompt
- Connection method prompt forces specifying related tables to aggregate together.
- Connection method prompt is valid for ordinary tables, inherited tables, UNLOGGED tables, temporary tables, external tables, system tables, table functions, VALUES clauses, and common expressions, but invalid for views and subqueries.
Join order prompt
- This prompt "Leading" indicates the join order of two or more tables. There are two methods to force specify here:
- Force a specific join order without restricting the direction at each join level.
Line number correction prompt
- Line number correction prompt corrects line number errors caused by scheduler limitations.
Parallel execution prompt
- Parallel query prompt forces parallel execution configuration during scanning.
- The third parameter represents intensity. soft means pg_hint_plan only changes max_parallel_worker_per_gather; hard changes all parameters to make prompt numbers effective.
- Parallel query prompts apply to ordinary tables, inheritance tables, UNLOGGED tables, and system tables; they do not apply to external tables, table functions, VALUES clauses, common table expressions (CTEs), views, or subqueries. The target object can be specified by the real name or alias of the internal table.
- The following two examples illustrate different ways to execute queries on each table:
Setting temporary GUC Parameter
- Temporarily modify the GUC Parameter during planning. The set GUC is effective if the planner's configuration Parameter does not conflict with any other Prompt. The same GUC Parameter setting is subject to the latest one.
Prompt list
All supported formats for Prompt are shown in the following table:
Others