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

2.2. Building from Source on Windows

This section was verified on Windows 11 with Visual Studio 2022 Build Tools (MSVC 19.44). The Windows port of IvorySQL is still under review, so the verification used commit 947b50e1 of the Windows test baseline: master plus the open Windows port PRs (IvorySQL #2321, #2332, #2333, #2485, #2530, #2811, #3418, #3419, #3420 and #3421).

Build and install IvorySQL in "Developer PowerShell for VS 2022". Git, Strawberry Perl, winflexbison and Meson (with Ninja) must be on PATH. The verification build also used -Dcassert=true -Dtap_tests=enabled to run the tests.

git clone --branch win/reference https://github.com/jiabin777on-hue/IvorySQL.git
Set-Location IvorySQL
git checkout 947b50e1
meson setup build --prefix=C:\ivorysql -Dauto_features=disabled -Duuid=none
ninja -C build
meson install -C build
Set-Location ..

Windows has no PGXS, so compile the extension directly with MSVC’s cl and link, linking against postgres.lib from the IvorySQL installation. Clone with core.autocrlf=false so that test data files are not converted to CRLF.

$IVY = 'C:\ivorysql'
$INC = "$IVY\include\postgresql\server"
$CFLAGS = @("/nologo", "/c", "/O2", "/MD", "/utf-8", "/std:c11", "/DWIN32", "/DWINDOWS",
    "/D__WINDOWS__", "/D__WIN32__", "/D_CRT_SECURE_NO_DEPRECATE", "/D_CRT_NONSTDC_NO_DEPRECATE",
    "/I.", "/I$INC\port\win32_msvc", "/I$INC\port\win32", "/I$INC", "/I$IVY\include")
git clone -c core.autocrlf=false https://github.com/ossc-db/pg_hint_plan.git
Set-Location pg_hint_plan
git checkout 52af48fb1ab4
win_flex --wincompat -o query_scan.c query_scan.l
cl @CFLAGS pg_hint_plan.c query_scan.c
link /nologo /DLL /OUT:pg_hint_plan.dll pg_hint_plan.obj query_scan.obj "$IVY\lib\postgres.lib"
Copy-Item pg_hint_plan.dll "$IVY\lib\postgresql\"
Copy-Item pg_hint_plan.control, pg_hint_plan--*.sql "$IVY\share\postgresql\extension\"

IvorySQL master is based on PostgreSQL 19, so the verification used the master branch of pg_hint_plan (2.0.0, commit 52af48fb1ab4) rather than the PG18 branch used for IvorySQL 5 above. The source needs no changes. query_scan.c is generated from query_scan.l; on Windows use win_flex from winflexbison.

pg_hint_plan must be preloaded. IvorySQL sets shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora' in ivorysql.conf by default; append pg_hint_plan to that list, for example shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_hint_plan'. Setting it to 'pg_hint_plan' alone replaces the default list, and connections in Oracle mode then fail with liboracle_parser not found.

Results: in PG mode, 17 of the extension’s 19 regression tests pass (hypopg was left out because it needs the hypopg extension). The other 2 (pg_hint_plan, ut-S) differ only in the automatically generated names of expression indexes: for example p2_c1_val_120_idx expected, p2_c1_expr_idx produced. This is a PostgreSQL version difference and is unrelated to Windows.

In Oracle mode, the tests of scan, join and row hints (ut-J, ut-L, ut-G, ut-R, ut-fdw, hint_table, query_parser and others) pass. The other differences come from Oracle-mode syntax and semantics: nested block comments are not supported, an empty string is NULL, and built-in types are shown as pg_catalog.int4 and so on. Also, CREATE EXTENSION btree_gist, which ut-init uses, fails in Oracle mode because of the bare INTERVAL type name (IvorySQL#1784). The extension’s regression scripts are written for PostgreSQL. In Oracle mode, psql sends a CREATE FUNCTION only after a line containing just /, even when the body is dollar-quoted, so to run these scripts in Oracle mode, add a / line after each CREATE FUNCTION statement.

2.3. Modify Database Configuration File

Modify the ivorysql.conf file to add pg_hint_plan to shared_preload_libraries:

shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_hint_plan'

Restart the database for the configuration to take effect.

2.4. Load Extension

postgres=# LOAD 'pg_hint_plan';
LOAD

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

3.4. Parallel Execution Hints

  • Parallel(table count[ workers]) - Set number of parallel worker processes

  • count - Whether to use parallel (0 means no, 1 means yes)

  • workers - Number of parallel worker processes (optional, defaults to planner-calculated value)

3.5. Other Hints

  • Set(enable_*) - Set GUC parameters

  • Rows(table table[ table…​] correction) - Correct rows estimate

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;

4.3. Parallel Execution Example

Setting parallel degree and join methods for multiple tables:

postgres=# /*+
   Parallel(t1 3)
   Parallel(t2 5)
   HashJoin(t1 t2)
*/
EXPLAIN SELECT * FROM large_table1 t1
JOIN large_table2 t2 ON t1.id = t2.id;

4.4. Specify Index Scan Example

Force scanning using a specific index:

postgres=# /*+
   IndexScan(employees emp_name_idx)
*/
EXPLAIN SELECT * FROM employees
WHERE last_name = 'SMITH';

5. Hint Table (Optional Feature)

pg_hint_plan provides an optional hint table feature for persistently storing hints.

5.1. Create Hint Table

postgres=# CREATE TABLE hint_plan.hints (
    id serial PRIMARY KEY,
    norm_query_string text NOT NULL,
    application_name text,
    hints text NOT NULL,
    CONSTRAINT hint_plan_hints_norm_query_string_key UNIQUE (norm_query_string, application_name)
);

5.2. Insert Hints

postgres=# INSERT INTO hint_plan.hints (norm_query_string, application_name, hints)
VALUES (
    'SELECT * FROM table1 WHERE id = ?;',
    'psql',
    'SeqScan(table1)'
);

Through the hint table, hints can be automatically applied to specific queries without modifying SQL statements.

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.