pgsentinel

active session history

Overview

PackageVersionCategoryLicenseLanguage
pgsentinel1.4.0STATPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
6410pgsentinelNoYesYesYesNoYes-
Relatedsystem_stats pgnodemx pg_stat_monitor pg_wait_sampling bgw_replstatus pg_profile pg_proctab powa

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.4.01817161514pgsentinel-
RPMPIGSTY1.4.01817161514pgsentinel_$v-
DEBPGDG1.4.01817161514postgresql-$v-pgsentinel-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

Build

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

pig build pkg pgsentinel         # build RPM / DEB packages

Install

You can install pgsentinel 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 pgsentinel;          # Install for current active PG version
pig ext install -y pgsentinel -v 18  # PG 18
pig ext install -y pgsentinel -v 17  # PG 17
pig ext install -y pgsentinel -v 16  # PG 16
pig ext install -y pgsentinel -v 15  # PG 15
pig ext install -y pgsentinel -v 14  # PG 14
dnf install -y pgsentinel_18       # PG 18
dnf install -y pgsentinel_17       # PG 17
dnf install -y pgsentinel_16       # PG 16
dnf install -y pgsentinel_15       # PG 15
dnf install -y pgsentinel_14       # PG 14
apt install -y postgresql-18-pgsentinel   # PG 18
apt install -y postgresql-17-pgsentinel   # PG 17
apt install -y postgresql-16-pgsentinel   # PG 16
apt install -y postgresql-15-pgsentinel   # PG 15
apt install -y postgresql-14-pgsentinel   # PG 14

Preload:

shared_preload_libraries = 'pgsentinel';

Create Extension:

CREATE EXTENSION pgsentinel;

Usage

pgsentinel: active session history for PostgreSQL

pgsentinel records active session history by sampling pg_stat_activity at regular intervals and linking activity with pg_stat_statements query statistics.

Active Session History

SELECT ash_time, datname, usename, pid, state,
       wait_event_type, wait_event, query, queryid
FROM pg_active_session_history
ORDER BY ash_time DESC;

Key columns beyond pg_stat_activity:

ColumnDescription
ash_timeSampling timestamp
top_level_queryTop-level statement (for PL/pgSQL)
queryStatement with actual parameter values
cmdtypeStatement type: SELECT, UPDATE, INSERT, DELETE, UTILITY, UNKNOWN, NOTHING
queryidLinks to pg_stat_statements
blockersNumber of blocking processes
blockerpidPID of a blocking process
blocker_stateState of the blocker

Query Statistics History

When enabled, pgsentinel also samples pg_stat_statements concurrently:

SELECT ash_time, queryid, calls, total_exec_time, rows,
       shared_blks_hit, shared_blks_read
FROM pg_stat_statements_history
ORDER BY ash_time DESC;

Example: Wait Analysis

-- Top wait events in the last hour
SELECT wait_event_type, wait_event, count(*)
FROM pg_active_session_history
WHERE ash_time > now() - interval '1 hour'
  AND wait_event IS NOT NULL
GROUP BY 1, 2
ORDER BY 3 DESC;

-- Blocking analysis
SELECT blockerpid, blocker_state, count(*)
FROM pg_active_session_history
WHERE blockers > 0
GROUP BY 1, 2
ORDER BY 3 DESC;

Configuration

ParameterDefaultDescription
pgsentinel_ash.sampling_period1Sampling period in seconds
pgsentinel_ash.max_entries1000Ring buffer size for ASH
pgsentinel.db_namepostgresDatabase for worker connection
pgsentinel_ash.track_idle_transfalseTrack idle-in-transaction sessions
pgsentinel_pgssh.max_entries1000Ring buffer for pg_stat_statements history
pgsentinel_pgssh.enablefalseEnable pg_stat_statements history

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