pg_stat_monitor

The pg_stat_monitor is a PostgreSQL Query Performance Monitoring tool, based on PostgreSQL contrib module pg_stat_statements. pg_stat_monitor provides aggregated statistics, client information, plan details including plan, and histogram information.

Overview

PackageVersionCategoryLicenseLanguage
pg_stat_monitor2.3.2STATBSD 3-ClauseC
IDExtensionBinLibLoadCreateTrustRelocSchema
6230pg_stat_monitorNoYesYesYesNoYes-
Relatedpg_show_plans pg_stat_kcache pg_stat_statements pg_qualstats pg_store_plans pgsentinel auto_explain logerrors

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY2.3.21817161514pg_stat_monitor-
RPMPIGSTY2.3.21817161514pg_stat_monitor_$v-
DEBPIGSTY2.3.21817161514postgresql-$v-pg-stat-monitor-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
d12.aarch64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
d13.x86_64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
d13.aarch64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
u22.x86_64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
u22.aarch64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
u24.x86_64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
u24.aarch64
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2
PIGSTY 2.3.2

Build

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

pig build pkg pg_stat_monitor         # build RPM / DEB packages

Install

You can install pg_stat_monitor 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_stat_monitor;          # Install for current active PG version
pig ext install -y pg_stat_monitor -v 18  # PG 18
pig ext install -y pg_stat_monitor -v 17  # PG 17
pig ext install -y pg_stat_monitor -v 16  # PG 16
pig ext install -y pg_stat_monitor -v 15  # PG 15
pig ext install -y pg_stat_monitor -v 14  # PG 14
dnf install -y pg_stat_monitor_18       # PG 18
dnf install -y pg_stat_monitor_17       # PG 17
dnf install -y pg_stat_monitor_16       # PG 16
dnf install -y pg_stat_monitor_15       # PG 15
dnf install -y pg_stat_monitor_14       # PG 14
apt install -y postgresql-18-pg-stat-monitor   # PG 18
apt install -y postgresql-17-pg-stat-monitor   # PG 17
apt install -y postgresql-16-pg-stat-monitor   # PG 16
apt install -y postgresql-15-pg-stat-monitor   # PG 15
apt install -y postgresql-14-pg-stat-monitor   # PG 14

Preload:

shared_preload_libraries = 'pg_stat_monitor';

Create Extension:

CREATE EXTENSION pg_stat_monitor;

Usage

pg_stat_monitor: query performance monitoring tool for PostgreSQL

pg_stat_monitor is an advanced replacement for pg_stat_statements that aggregates statistics into configurable time-based buckets, provides query origin information, actual parameter capture, and query plan details.

Querying Statistics

-- Basic query monitoring
SELECT application_name, userid::regrole AS user_name,
       datname AS database_name, substr(query, 0, 50) AS query,
       calls, client_ip
FROM pg_stat_monitor;

-- Bucket-based time analysis
SELECT bucket, bucket_start_time, query, calls,
       mean_exec_time, total_exec_time
FROM pg_stat_monitor
ORDER BY total_exec_time DESC;

-- Show query plans
SELECT query, query_plan FROM pg_stat_monitor
WHERE query_plan IS NOT NULL;

Key Features

  • Time-based buckets: Statistics are grouped into configurable intervals for more accurate analysis
  • Client IP tracking: Each query records the originating client IP address
  • Actual parameters: Optionally capture real query parameter values instead of placeholders
  • Query plans: Each query is accompanied by its actual execution plan
  • Top query tracking: Identify the most resource-intensive queries per bucket
  • Histogram support: Visual timing distribution via histogram function

Functions

-- Reset all statistics
SELECT pg_stat_monitor_reset();

-- View internal info
SELECT * FROM pg_stat_monitor_info;

Configuration

Key parameters (set in postgresql.conf):

ParameterDescription
pg_stat_monitor.pgsm_maxMaximum number of statements tracked
pg_stat_monitor.pgsm_query_max_lenMaximum query length
pg_stat_monitor.pgsm_bucket_timeBucket duration in seconds
pg_stat_monitor.pgsm_max_bucketsMaximum number of buckets
pg_stat_monitor.pgsm_enable_query_planEnable query plan capture
pg_stat_monitor.pgsm_trackTrack: top, all, or none

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