pg_hint_plan
1. Overview
pg_hint_plan is an extension that allows controlling PostgreSQL/IvorySQL execution plans using SQL comment hints. It optimizes query performance without modifying SQL logic and works in both PostgreSQL and Oracle compatibility modes.
2. Installation
| The source installation environment is Ubuntu 24.04(x86_64), with IvorySQL 5 or above installed at /usr/ivory-5 |
2.1. Install from Source
# Clone PG18 branch source code git clone --branch PG18 https://github.com/ossc-db/pg_hint_plan.git cd pg_hint_plan # Compile and install the extension make PG_CONFIG=/usr/ivory-5/bin/pg_config make PG_CONFIG=/usr/ivory-5/bin/pg_config install
3. Hint Types
pg_hint_plan provides various types of hints to control different aspects of query execution plans.
3.1. Scan Method Hints
Hints specifying table scan methods:
-
SeqScan(table)- Sequential scan -
IndexScan(table[ index])- Index scan, optionally specifying index name -
BitmapScan(table[ index])- Bitmap scan, optionally specifying index name -
TidScan(table)- TID scan
Negative form hints (prohibit using a specific scan method):
-
NoSeqScan(table) -
NoIndexScan(table) -
NoBitmapScan(table) -
NoTidScan(table)
3.2. Join Method Hints
Hints specifying table join methods:
-
HashJoin(table table[ table…])- Hash join -
NestedLoop(table table[ table…])- Nested loop join -
MergeJoin(table table[ table…])- Merge join
Negative form hints:
-
NoHashJoin(table table) -
NoNestedLoop(table table) -
NoMergeJoin(table table)
3.3. Join Order Hints
-
Leading(table table[ table…])- Specify join order, tables are joined in sequence
4. Usage Examples
4.1. Basic Usage Example
Using HashJoin and SeqScan hints:
postgres=# /*+ HashJoin(a b) SeqScan(a) */ EXPLAIN SELECT * FROM pgbench_branches b JOIN pgbench_accounts a ON b.bid = a.bid ORDER BY a.aid;
4.2. Complex Multi-Hint Example
Combining multiple hints to control execution plan for complex queries:
postgres=# /*+ NestedLoop(t1 t2) IndexScan(t2 t2_pkey) MergeJoin(t1 t2 t3) Leading(t1 t2 t3) */ EXPLAIN SELECT * FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id JOIN table3 t3 ON t2.id = t3.id;
5. Hint Table (Optional Feature)
pg_hint_plan provides an optional hint table feature for persistently storing hints.
6. IvorySQL Compatibility
6.1. Oracle Compatibility Mode
pg_hint_plan works in both PostgreSQL and Oracle compatibility modes. The hint syntax remains consistent and is compatible with IvorySQL-specific data types (VARCHAR2, NUMBER, etc.).
6.2. Oracle Mode Example
-- Connect to port 1521 (Oracle mode) postgres=# /*+ IndexScan(employees emp_name_idx) */ SELECT * FROM employees WHERE last_name = 'SMITH';
In Oracle compatibility mode, hint usage is identical to PostgreSQL mode and can normally control query execution plans containing IvorySQL-specific data types and syntax.