Skip to content

pg_plansight

Capture and analyze cumulative query plans and execution statistics

pg-plansight · Capture and analyze cumulative query plans and execution statistics

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102483pg_plansightpg-plansight0.2.0STATMITRust

PostgreSQL · 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

pg_plansight 0.2.0 captures query plans and cumulative execution statistics in PostgreSQL. The release documents PostgreSQL12–18 packages and end-to-end capture validation on16. Its Cargo manifest also has a PG19 feature; that alone does not qualify a PG19 release package.

Manual Analysis

A superuser installs the nonrelocatable extension. Manual ingestion and SQL helpers work without preload:

CREATE EXTENSION pg_plansight;
SELECT plansight_format('select count(*) from pg_class');
SELECT plansight_fingerprint('select * from pg_class where oid = 42');
SELECT * FROM plansight.statements_summary ORDER BY total_time_ms DESC;
SELECT * FROM plansight_check();

plansight_ingest(text) accepts auto_explain log text and returns the number of distinct query groups written. plansight.statements stores representative SQL, plans and JSON analysis; plansight.top_by_total_time ranks cumulative time and plansight.query_timeline exposes hourly aggregates.

Automatic Capture

Automatic capture requires preload, a background worker and restart. Install the SQL extension in the database selected by plansight.database:

shared_preload_libraries = 'pg_plansight'
plansight.database = 'postgres'
plansight.capture_mode = 'hook'
plansight.sample_rate = 0.1
plansight.min_duration_ms = 100

plansight.capture_mode defaults to off. The hook mode captures directly through executor hooks; log mode tails an auto_explain log and additionally needs auto_explain configured for analyzed text plans. These modes are mutually exclusive. Changing the worker database requires a worker or server restart.

Limits and Access

plansight_capture_stats() reports ring backlog, captured/dropped records and the last drain. Rising dropped counts mean the bounded sampler is losing records. plansight.capture_plan and plansight.track_timing control plan and timing overhead; measure overhead for the chosen sampling rate. Monitoring privileges are granted to pg_read_all_stats, while mutation/reset functions are revoked from PUBLIC. SQL and plan text can contain sensitive values.

plansight_reset() clears stored statistics; plansight_reset_stats() clears the extension’s own counters. plansight_pgss_view() creates an optional pg_stat_statements join view and must be called again after dump/restore. Back up retained tables and review version-specific upgrade instructions before replacing libraries. Captured totals are sampled statistics, not an audit log.

Table reader