pg_show_plans

1. Overview

pg_show_plans is a PostgreSQL extension that shows the query plans of all currently running SQL statements. Query plans can be displayed in several formats, including TEXT, JSON, and YAML.

This extension creates a hash table within shared memory. The hash table is not resizable — no new plans can be added once it has been filled up.

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

Make sure pg_config is available on your PATH.

git clone https://github.com/cybertec-postgresql/pg_show_plans.git
cd pg_show_plans

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/cybertec-postgresql/pg_show_plans.git
Set-Location pg_show_plans
git checkout 92ecb21f8f23
# MSVC C2375: remove the declaration that lacks PGDLLEXPORT; PG_FUNCTION_INFO_V1 declares the function
(Get-Content pg_show_plans.c) | Where-Object { $_ -ne 'Datum pg_show_plans(PG_FUNCTION_ARGS);' } | Set-Content -Encoding ascii pg_show_plans.c
cl @CFLAGS pg_show_plans.c
link /nologo /DLL /OUT:pg_show_plans.dll pg_show_plans.obj "$IVY\lib\postgres.lib"
Copy-Item pg_show_plans.dll "$IVY\lib\postgresql\"
Copy-Item pg_show_plans.control, pg_show_plans--*.sql "$IVY\share\postgresql\extension\"

The verified version is pg_show_plans 2.1.8 (commit 92ecb21f8f23).

MSVC reports C2375 (redefinition; different linkage): the source declares pg_show_plans without PGDLLEXPORT before PG_FUNCTION_INFO_V1(pg_show_plans). The commands above delete that declaration; PG_FUNCTION_INFO_V1 already declares the function. This is a problem in the extension itself and happens with PostgreSQL and MSVC too.

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

Results: the extension’s regression tests pg_show_plans and formats pass in PG mode (2/2). In Oracle mode (with / lines added to the script) the results match the expected output exactly. 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. Configure shared_preload_libraries

Add pg_show_plans to the shared_preload_libraries parameter in ivorysql.conf:

shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_show_plans'

Restart the IvorySQL instance to apply the change:

pg_ctl restart -D data

2.4. Create the Extension

After the server has restarted, connect to the target database and create the extension:

CREATE EXTENSION pg_show_plans;

3. Usage

3.1. View Current Query Plans

Use the pg_show_plans view to inspect the execution plans of all currently running SQL statements:

SELECT * FROM pg_show_plans;
  pid  | level | userid | dbid  |                                 plan
-------+-------+--------+-------+-----------------------------------------------------------------------
 11473 |     0 |     10 | 16384 | Function Scan on pg_show_plans  (cost=0.00..10.00 rows=1000 width=56)
 11504 |     0 |     10 | 16384 | Function Scan on print_item  (cost=0.25..10.25 rows=1000 width=524)
 11504 |     1 |     10 | 16384 | Result  (cost=0.00..0.01 rows=1 width=4)
(3 rows)

3.2. View Query Plans with SQL Text

Use the pg_show_plans_q view to see query plans together with the corresponding SQL statement. This view joins pg_show_plans with pg_stat_activity:

SELECT * FROM pg_show_plans_q;
-[ RECORD 1 ]------------------------------------------------------------------------------------
pid   | 11473
level | 0
plan  | Sort  (cost=72.08..74.58 rows=1000 width=80)
      |   Sort Key: pg_show_plans.pid, pg_show_plans.level
      |   ->  Hash Left Join  (cost=2.25..22.25 rows=1000 width=80)
      |         Hash Cond: (pg_show_plans.pid = s.pid)
      |         Join Filter: (pg_show_plans.level = 0)
      |         ->  Function Scan on pg_show_plans  (cost=0.00..10.00 rows=1000 width=48)
      |         ->  Hash  (cost=1.00..1.00 rows=100 width=44)
      |               ->  Function Scan on pg_stat_get_activity s  (cost=0.00..1.00 rows=100 width=44)
query | SELECT p.pid, p.level, p.plan, a.query FROM pg_show_plans p
      |    LEFT JOIN pg_stat_activity a
      |    ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level;
-[ RECORD 2 ]------------------------------------------------------------------------------------
pid   | 11517
level | 0
plan  | Function Scan on print_item  (cost=0.25..10.25 rows=1000 width=524)
query | SELECT * FROM print_item(1,20);
-[ RECORD 3 ]------------------------------------------------------------------------------------
pid   | 11517
level | 1
plan  | Result  (cost=0.00..0.01 rows=1 width=4)
query |