pg_sqlog

pg_sqlog

pg_sqlog : Provide SQL interface to logs

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
6500
pg_sqlog
pg_sqlog
1.6
STAT
BSD 3-Clause
SQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
Schemassqlog
Requires
file_fdw
See Also
pg_profile
pg_tracing
pg_show_plans
pg_stat_kcache
pg_stat_monitor
pg_qualstats
pg_store_plans

require certain params

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
1.6
18
17
16
15
14
pg_sqlogfile_fdw
RPM
PIGSTY
1.6
18
17
16
15
14
pg_sqlog_$v-
DEB
PIGSTY
1.6
18
17
16
15
14
postgresql-$v-pg-sqlog-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
el8.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
el9.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
el9.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
el10.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
el10.aarch64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
d12.x86_64
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
PIGSTY 1.6
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

Source

pig build pkg pg_sqlog;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_sqlog;		# install via package name, for the active PG version

pig install pg_sqlog -v 18;   # install for PG 18
pig install pg_sqlog -v 17;   # install for PG 17
pig install pg_sqlog -v 16;   # install for PG 16
pig install pg_sqlog -v 15;   # install for PG 15
pig install pg_sqlog -v 14;   # install for PG 14

Create this extension with:

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 updated on