pg_store_plans

track plan statistics of all SQL statements executed

Overview

PackageVersionCategoryLicenseLanguage
pg_store_plans1.9STATBSD 3-ClauseC
IDExtensionBinLibLoadCreateTrustRelocSchema
6250pg_store_plansNoYesYesYesNoYes-
Relatedpg_show_plans auto_explain pg_stat_statements pg_hint_plan pre_prepare pg_stat_monitor explain_ui plprofiler

pg18 breaks, fixed by Vonng

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.91817161514pg_store_plans-
RPMPIGSTY1.91817161514pg_store_plans_$v-
DEBPIGSTY1.91817161514postgresql-$v-pg-store-plan-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.9
PIGSTY 1.8
PIGSTY 1.8
el8.aarch64
PIGSTY 1.9
PIGSTY 1.8
PIGSTY 1.8
el9.x86_64
PIGSTY 1.9
PIGSTY 1.8
PIGSTY 1.8
el9.aarch64
PIGSTY 1.9
PIGSTY 1.8
PIGSTY 1.8
el10.x86_64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY MISS
el10.aarch64
PIGSTY 1.9
PIGSTY 1.9
PGDG 1.8
PGDG 1.8
PIGSTY MISS
d12.x86_64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
d12.aarch64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
d13.x86_64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
d13.aarch64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
u22.x86_64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
u22.aarch64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
u24.x86_64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
u24.aarch64
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9
PIGSTY 1.9

Build

You can build the RPM / DEB packages for pg_store_plans using pig build:

pig build pkg pg_store_plans         # build RPM / DEB packages

Install

You can install pg_store_plans directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_store_plans;          # Install for current active PG version
pig ext install -y pg_store_plans -v 18  # PG 18
pig ext install -y pg_store_plans -v 17  # PG 17
pig ext install -y pg_store_plans -v 16  # PG 16
pig ext install -y pg_store_plans -v 15  # PG 15
pig ext install -y pg_store_plans -v 14  # PG 14
dnf install -y pg_store_plans_18       # PG 18
dnf install -y pg_store_plans_17       # PG 17
dnf install -y pg_store_plans_16       # PG 16
dnf install -y pg_store_plans_15       # PG 15
dnf install -y pg_store_plans_14       # PG 14
apt install -y postgresql-18-pg-store-plan   # PG 18
apt install -y postgresql-17-pg-store-plan   # PG 17
apt install -y postgresql-16-pg-store-plan   # PG 16
apt install -y postgresql-15-pg-store-plan   # PG 15
apt install -y postgresql-14-pg-store-plan   # PG 14

Preload:

shared_preload_libraries = 'pg_store_plans';

Create Extension:

CREATE EXTENSION pg_store_plans;

Usage

pg_store_plans: execution plan storage and statistics

pg_store_plans tracks execution plan statistics for all SQL statements, complementing pg_stat_statements with plan-level detail. Joinable via queryid on PostgreSQL 14+.

Viewing Plan Statistics

-- View tracked plans with statistics
SELECT queryid, planid, plan, calls, total_time, rows
FROM pg_store_plans
ORDER BY total_time DESC;

-- Check module status
SELECT * FROM pg_store_plans_info;

Key View Columns

ColumnTypeDescription
queryidbigintQuery ID (joinable with pg_stat_statements)
planidbigintPlan hash code
plantextRepresentative plan text
callsbigintExecution count
total_timedouble precisionTotal execution time (ms)
rowsbigintTotal rows retrieved/affected
shared_blks_hitbigintShared buffer hits
shared_blks_readbigintShared blocks read
first_calltimestamptzFirst execution time
last_calltimestamptzLast execution time

Functions

-- Reset all statistics (superuser only)
SELECT pg_store_plans_reset();

-- Convert plan formats
SELECT pg_store_plans_textplan(plan);   -- to text
SELECT pg_store_plans_jsonplan(plan);   -- to JSON
SELECT pg_store_plans_xmlplan(plan);    -- to XML
SELECT pg_store_plans_yamlplan(plan);   -- to YAML

-- Calculate query hash
SELECT pg_store_hash_query('SELECT 1');

Configuration

ParameterDefaultDescription
pg_store_plans.max1000Maximum tracked plans (server start only)
pg_store_plans.tracktoptop, all, verbose, none
pg_store_plans.plan_formattexttext, json, xml, yaml, raw
pg_store_plans.min_duration0Minimum execution time to track (ms)
pg_store_plans.log_analyzeoffInclude EXPLAIN ANALYZE output
pg_store_plans.log_buffersoffInclude buffer statistics
pg_store_plans.log_timingtrueRecord actual timings
pg_store_plans.plan_storagefileStorage: file or shmem
pg_store_plans.max_plan_length5000Max bytes for plan text
pg_store_plans.saveonPersist stats across restarts

Last Modified 2026-03-12: add pg extension catalog (95749bf)