pg_sqlog

Provide SQL interface to logs

Overview

PackageVersionCategoryLicenseLanguage
pg_sqlog1.6STATBSD 3-ClauseSQL
IDExtensionBinLibLoadCreateTrustRelocSchema
6500pg_sqlogNoYesNoYesNoNosqlog
Relatedfile_fdw pg_profile pg_tracing pg_show_plans pg_stat_kcache pg_stat_monitor pg_qualstats pg_store_plans

require certain params

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.61817161514pg_sqlogfile_fdw
RPMPIGSTY1.61817161514pg_sqlog_$v-
DEBPIGSTY1.61817161514postgresql-$v-pg-sqlog-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
d13.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
d13.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
u22.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
u22.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
u24.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
u24.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6

Build

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

pig build pkg pg_sqlog         # build RPM / DEB packages

Install

You can install pg_sqlog 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_sqlog;          # Install for current active PG version
pig ext install -y pg_sqlog -v 18  # PG 18
pig ext install -y pg_sqlog -v 17  # PG 17
pig ext install -y pg_sqlog -v 16  # PG 16
pig ext install -y pg_sqlog -v 15  # PG 15
pig ext install -y pg_sqlog -v 14  # PG 14
dnf install -y pg_sqlog_18       # PG 18
dnf install -y pg_sqlog_17       # PG 17
dnf install -y pg_sqlog_16       # PG 16
dnf install -y pg_sqlog_15       # PG 15
dnf install -y pg_sqlog_14       # PG 14
apt install -y postgresql-18-pg-sqlog   # PG 18
apt install -y postgresql-17-pg-sqlog   # PG 17
apt install -y postgresql-16-pg-sqlog   # PG 16
apt install -y postgresql-15-pg-sqlog   # PG 15
apt install -y postgresql-14-pg-sqlog   # PG 14

Create Extension:

CREATE EXTENSION pg_sqlog CASCADE;  -- requires: file_fdw

Usage

pg_sqlog: access PostgreSQL CSV logs via SQL

pg_sqlog provides a SQL interface for querying PostgreSQL CSV log files using a foreign table backed by file_fdw.

Querying Logs

-- Current day's log
SELECT * FROM sqlog.log();

-- Specific day's log
SELECT * FROM sqlog.log('yesterday');
SELECT * FROM sqlog.log('2024-01-15');

-- Error summary
SELECT error_severity, COUNT(*) FROM sqlog.log() GROUP BY 1;

Slow Query Analysis

-- Top 3 slowest query patterns
SELECT
  AVG(sqlog.duration(message)) AS avg_duration,
  COUNT(*) AS count,
  sqlog.preparable_query(message) AS query_pattern
FROM sqlog.log()
WHERE message ~ '^duration'
GROUP BY 3
ORDER BY 1 DESC
LIMIT 3;

-- Query summary with timing
SELECT
  log_time::time,
  sqlog.duration(message),
  sqlog.summary(message)
FROM sqlog.log('yesterday')
WHERE message ~ '^duration';

Functions

FunctionDescription
sqlog.log([timestamp])Returns log contents for a given day
sqlog.set_date([timestamp])Set the date for sqlog.log table queries
sqlog.duration(text)Extract query duration from message (ms)
sqlog.preparable_query(text)Replace arguments with ? for grouping
sqlog.summary(text, int, int)Strip metadata, show first/last N chars
sqlog.temporary_file_size(text)Extract temp file size from message
sqlog.autovacuum([timestamp])Autovacuum report for a given day
sqlog.autoanalyze([timestamp])Autoanalyze report for a given day

Autovacuum Reports

SELECT * FROM sqlog.autovacuum() LIMIT 5;
SELECT * FROM sqlog.autoanalyze() LIMIT 5;

Prerequisites

Required postgresql.conf settings:

log_destination = 'syslog,csvlog'
log_filename = 'postgresql.%F'
logging_collector = 'on'
log_rotation_age = '1d'
log_rotation_size = 0
log_truncate_on_rotation = 'on'
log_min_duration_statement = 1000

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