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.1 |
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_show_plans pg_store_plans auto_explain explain_ui pg_overexplain pg_stat_statements pg_hint_plan hypopg pg_qualstats online_advisor |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG | 2.1.1 |
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.1 |
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 | N/A | N/A |
| el8.aarch64 | PGDG 2.1.0 | PGDG 2.1.0 | PGDG 2.1.0 | N/A | N/A |
| el9.x86_64 | PGDG 2.1.0 | PGDG 2.1.0 | PGDG 2.1.0 | N/A | N/A |
| el9.aarch64 | PGDG 2.1.0 | PGDG 2.1.0 | PGDG 2.1.0 | N/A | N/A |
| el10.x86_64 | PGDG 2.1.0 | PGDG 2.1.0 | PGDG 2.1.0 | N/A | N/A |
| el10.aarch64 | PGDG 2.1.0 | PGDG 2.1.0 | PGDG 2.1.0 | N/A | N/A |
| d12.x86_64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| d12.aarch64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| d13.x86_64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| d13.aarch64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u22.x86_64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u22.aarch64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u24.x86_64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u24.aarch64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u26.x86_64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
| u26.aarch64 | PGDG 2.1.1 | PGDG 2.1.1 | PGDG 2.1.1 | N/A | N/A |
Source
github.com/pganalyze/pg_stat_plans
Install
Make sure PGDG repo available:
Install this extension with pig:
Config this extension to shared_preload_libraries:
Create this extension with:
Usage
Sources:
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:
Restart after changing the preload list. Using pg_stat_statements alongside it is recommended so plan IDs can be correlated with query text. Optional zstd compression requires a build with zstd support.
Query Plans
The view exposes userid, dbid, toplevel, queryid, planid, calls, total_exec_time, plan, and plan_advice. 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.
Version, Memory and Visibility
Release 2.1.1 uses SQL extension version 2.1. After installing new files and reloading the library at restart, update existing SQL objects with ALTER EXTENSION pg_stat_plans UPDATE. The 2.1 upgrade drops and recreates the function and views; dependent user objects can prevent the update.
pg_stat_plans.max_plan_memory limits total plan-text storage. A full memory budget or oversized plan text can leave the text empty while counters remain tracked. pg_stat_plans.plan_advice requires PostgreSQL 19 or later and pg_plan_advice in the preload list.
Plan text and query IDs for other users require superuser access or pg_read_all_stats; otherwise only the caller’s own query details are shown. The reset function is not granted to PUBLIC. Collected plan text can contain sensitive constants; restrict monitoring privileges accordingly.