Skip to content
pg_stat_plans

pg_stat_plans

pg_stat_plans : Track per-plan call counts, execution times, and example EXPLAIN texts.

Overview

ID Extension Package Version Category License Language
6050
pg_stat_plans
pg_stat_plans
2.1.0
STAT
PostgreSQL
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd-r
No
Yes
Yes
Yes
yes
no
Relationships
See Also
pg_stat_statements
pg_store_plans
pg_show_plans
pg_stat_kcache

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PGDG
2.1.0
18
17
16
15
14
pg_stat_plans -
RPM
PGDG
2.1.0
18
17
16
15
14
pg_stat_plans_$v -
DEB
PGDG
2.1.0
18
17
16
15
14
postgresql-$v-pg-stat-plans -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
el8.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
el9.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
el9.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
el10.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
el10.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
d12.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
d12.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
d13.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
d13.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u22.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u22.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u24.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u24.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u26.x86_64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
u26.aarch64
PGDG 2.1.0
PGDG 2.1.0
PGDG 2.1.0
MISS
MISS
Package Version OS ORG SIZE File URL
pg_stat_plans_18 2.1.0 el8.x86_64 pgdg 44.7 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel8.10.x86_64.rpm
pg_stat_plans_18 2.1.0 el8.aarch64 pgdg 42.1 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel8.10.aarch64.rpm
pg_stat_plans_18 2.1.0 el9.x86_64 pgdg 42.3 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.8.x86_64.rpm
pg_stat_plans_18 2.1.0 el9.x86_64 pgdg 42.3 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.7.x86_64.rpm
pg_stat_plans_18 2.1.0 el9.x86_64 pgdg 42.4 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.6.x86_64.rpm
pg_stat_plans_18 2.1.0 el9.aarch64 pgdg 41.5 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.8.aarch64.rpm
pg_stat_plans_18 2.1.0 el9.aarch64 pgdg 41.5 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.7.aarch64.rpm
pg_stat_plans_18 2.1.0 el9.aarch64 pgdg 41.6 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel9.6.aarch64.rpm
pg_stat_plans_18 2.1.0 el10.x86_64 pgdg 42.8 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.2.x86_64.rpm
pg_stat_plans_18 2.1.0 el10.x86_64 pgdg 42.9 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.1.x86_64.rpm
pg_stat_plans_18 2.1.0 el10.x86_64 pgdg 43.2 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.0.x86_64.rpm
pg_stat_plans_18 2.1.0 el10.aarch64 pgdg 42.0 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.2.aarch64.rpm
pg_stat_plans_18 2.1.0 el10.aarch64 pgdg 42.0 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.1.aarch64.rpm
pg_stat_plans_18 2.1.0 el10.aarch64 pgdg 42.0 KiB pg_stat_plans_18-2.1.0-1PGDG.rhel10.0.aarch64.rpm
postgresql-18-pg-stat-plans 2.1.0 d12.x86_64 pgdg 85.8 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg12+1_amd64.deb
postgresql-18-pg-stat-plans 2.0.0 d12.x86_64 pgdg 81.6 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg12+1_amd64.deb
postgresql-18-pg-stat-plans 2.1.0 d12.aarch64 pgdg 83.4 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg12+1_arm64.deb
postgresql-18-pg-stat-plans 2.0.0 d12.aarch64 pgdg 79.2 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg12+1_arm64.deb
postgresql-18-pg-stat-plans 2.1.0 d13.x86_64 pgdg 85.8 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg13+1_amd64.deb
postgresql-18-pg-stat-plans 2.0.0 d13.x86_64 pgdg 81.5 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg13+1_amd64.deb
postgresql-18-pg-stat-plans 2.1.0 d13.aarch64 pgdg 83.3 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg13+1_arm64.deb
postgresql-18-pg-stat-plans 2.0.0 d13.aarch64 pgdg 79.2 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg13+1_arm64.deb
postgresql-18-pg-stat-plans 2.1.0 u22.x86_64 pgdg 89.2 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg22.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.0.0 u22.x86_64 pgdg 84.8 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg22.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.1.0 u22.aarch64 pgdg 86.7 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg22.04+1_arm64.deb
postgresql-18-pg-stat-plans 2.0.0 u22.aarch64 pgdg 82.9 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg22.04+1_arm64.deb
postgresql-18-pg-stat-plans 2.1.0 u24.x86_64 pgdg 83.5 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg24.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.0.0 u24.x86_64 pgdg 80.3 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg24.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.1.0 u24.aarch64 pgdg 81.2 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg24.04+1_arm64.deb
postgresql-18-pg-stat-plans 2.0.0 u24.aarch64 pgdg 78.0 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg24.04+1_arm64.deb
postgresql-18-pg-stat-plans 2.1.0 u26.x86_64 pgdg 82.8 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg26.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.0.0 u26.x86_64 pgdg 80.3 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg26.04+1_amd64.deb
postgresql-18-pg-stat-plans 2.1.0 u26.aarch64 pgdg 80.6 KiB postgresql-18-pg-stat-plans_2.1.0-1.pgdg26.04+1_arm64.deb
postgresql-18-pg-stat-plans 2.0.0 u26.aarch64 pgdg 77.3 KiB postgresql-18-pg-stat-plans_2.0.0-1.pgdg26.04+1_arm64.deb

Source

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pg_stat_plans;		# install via package name, for the active PG version

pig install pg_stat_plans -v 18;   # install for PG 18
pig install pg_stat_plans -v 17;   # install for PG 17
pig install pg_stat_plans -v 16;   # install for PG 16

Config this extension to shared_preload_libraries:

shared_preload_libraries = '$libdir/pg_stat_plans';

Create this extension with:

CREATE EXTENSION pg_stat_plans;

Usage

Sources: README, v2.1.0 release, SQL objects

pg_stat_plans tracks aggregate statistics for PostgreSQL plan shapes. It hashes planned query trees into plan IDs, stores example EXPLAIN text in shared memory, and helps identify when the same query ID is executed with different plans.

Enable

pg_stat_plans requires PostgreSQL 16 or newer and must be loaded at server start:

shared_preload_libraries = 'pg_stat_plans'
pg_stat_plans.compress = 'zstd'
CREATE EXTENSION pg_stat_plans;

Using pg_stat_statements alongside it is recommended so plan IDs can be correlated with query text.

Query Plans

SELECT *
FROM pg_stat_plans;

The view exposes userid, dbid, toplevel, queryid, planid, calls, total_exec_time, and plan. To omit stored plan text for lighter queries:

SELECT *
FROM pg_stat_plans(false);

Group by queryid to see multiple plan shapes chosen for one normalized query:

SELECT queryid, planid, calls, total_exec_time / NULLIF(calls, 0) AS avg_exec_time
FROM pg_stat_plans(false)
ORDER BY queryid, avg_exec_time DESC;

Running Queries

On PostgreSQL 18 and newer, pg_stat_plans_activity can show plan IDs and example plans for currently running queries:

SELECT *
FROM pg_stat_plans_activity;

Reset And Configure

SELECT pg_stat_plans_reset();

Important settings include pg_stat_plans.max, pg_stat_plans.max_size, pg_stat_plans.max_plan_memory, pg_stat_plans.track, pg_stat_plans.compress, and pg_stat_plans.plan_advice.

Notes

Statistics use PostgreSQL’s cumulative statistics system, so counters are flushed at transaction end and may be delayed. Plan IDs describe plan shape and can change when partitions, casts, or expression details change.

Last updated on