pg_stat_plans
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_stat_plans | 2.1.0 | STAT | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 6050 | pg_stat_plans | No | Yes | Yes | Yes | No | Yes | - |
| Related | pg_show_plans pg_store_plans auto_explain explain_ui pg_overexplain pg_stat_statements pg_hint_plan hypopg pg_qualstats online_advisor |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 2.1.0 | 1817161514 | pg_stat_plans | - |
| RPM | PGDG | 2.1.0 | 1817161514 | pg_stat_plans_$v | - |
| DEB | PGDG | 2.1.0 | 1817161514 | postgresql-$v-pg-stat-plans | - |
Install
You can install pg_stat_plans directly. First, make sure the PGDG repository is added and enabled:
Install the extension using pig or apt/yum/dnf:
Preload:
Create Extension:
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:
Using pg_stat_statements alongside it is recommended so plan IDs can be correlated with query text.
Query Plans
The view exposes userid, dbid, toplevel, queryid, planid, calls, total_exec_time, and plan. To omit stored plan text for lighter queries:
Group by queryid to see multiple plan shapes chosen for one normalized query:
Running Queries
On PostgreSQL 18 and newer, pg_stat_plans_activity can show plan IDs and example plans for currently running queries:
Reset And Configure
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.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)