pg_sqlog

pg_sqlog

pg_sqlog : Provide SQL interface to logs

Overview

ID Extension Package Version Category License Language
6500
pg_sqlog
pg_sqlog
1.6
STAT
BSD 3-Clause
SQL
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
Schemas sqlog
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

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
1.6
18
17
16
15
14
pg_sqlog file_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 / PG PG18 PG17 PG16 PG15 PG14
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
Package Version OS ORG SIZE File URL
pg_sqlog_18 1.6 el8.x86_64 pigsty 15.1 KiB pg_sqlog_18-1.6-1PIGSTY.el8.x86_64.rpm
pg_sqlog_18 1.6 el8.aarch64 pigsty 15.0 KiB pg_sqlog_18-1.6-1PIGSTY.el8.aarch64.rpm
pg_sqlog_18 1.6 el9.x86_64 pigsty 15.0 KiB pg_sqlog_18-1.6-1PIGSTY.el9.x86_64.rpm
pg_sqlog_18 1.6 el9.aarch64 pigsty 15.0 KiB pg_sqlog_18-1.6-1PIGSTY.el9.aarch64.rpm
pg_sqlog_18 1.6 el10.x86_64 pigsty 15.2 KiB pg_sqlog_18-1.6-1PIGSTY.el10.x86_64.rpm
pg_sqlog_18 1.6 el10.aarch64 pigsty 15.2 KiB pg_sqlog_18-1.6-1PIGSTY.el10.aarch64.rpm
postgresql-18-pg-sqlog 1.6 d12.x86_64 pigsty 9.9 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-sqlog 1.6 d12.aarch64 pigsty 9.9 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-sqlog 1.6 d13.x86_64 pigsty 9.9 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-sqlog 1.6 d13.aarch64 pigsty 9.9 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-sqlog 1.6 u22.x86_64 pigsty 9.5 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-sqlog 1.6 u22.aarch64 pigsty 9.5 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-sqlog 1.6 u24.x86_64 pigsty 9.5 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~noble_amd64.deb
postgresql-18-pg-sqlog 1.6 u24.aarch64 pigsty 9.5 KiB postgresql-18-pg-sqlog_1.6-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_sqlog_17 1.6 el8.x86_64 pigsty 15.1 KiB pg_sqlog_17-1.6-1PIGSTY.el8.x86_64.rpm
pg_sqlog_17 1.6 el8.aarch64 pigsty 15.0 KiB pg_sqlog_17-1.6-1PIGSTY.el8.aarch64.rpm
pg_sqlog_17 1.6 el9.x86_64 pigsty 15.0 KiB pg_sqlog_17-1.6-1PIGSTY.el9.x86_64.rpm
pg_sqlog_17 1.6 el9.aarch64 pigsty 15.0 KiB pg_sqlog_17-1.6-1PIGSTY.el9.aarch64.rpm
pg_sqlog_17 1.6 el10.x86_64 pigsty 15.2 KiB pg_sqlog_17-1.6-1PIGSTY.el10.x86_64.rpm
pg_sqlog_17 1.6 el10.aarch64 pigsty 15.2 KiB pg_sqlog_17-1.6-1PIGSTY.el10.aarch64.rpm
postgresql-17-pg-sqlog 1.6 d12.x86_64 pigsty 9.9 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~bookworm_amd64.deb
postgresql-17-pg-sqlog 1.6 d12.aarch64 pigsty 9.9 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~bookworm_arm64.deb
postgresql-17-pg-sqlog 1.6 d13.x86_64 pigsty 9.9 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~trixie_amd64.deb
postgresql-17-pg-sqlog 1.6 d13.aarch64 pigsty 9.9 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~trixie_arm64.deb
postgresql-17-pg-sqlog 1.6 u22.x86_64 pigsty 9.5 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~jammy_amd64.deb
postgresql-17-pg-sqlog 1.6 u22.aarch64 pigsty 9.5 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~jammy_arm64.deb
postgresql-17-pg-sqlog 1.6 u24.x86_64 pigsty 9.5 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~noble_amd64.deb
postgresql-17-pg-sqlog 1.6 u24.aarch64 pigsty 9.5 KiB postgresql-17-pg-sqlog_1.6-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_sqlog_16 1.6 el8.x86_64 pigsty 15.1 KiB pg_sqlog_16-1.6-1PIGSTY.el8.x86_64.rpm
pg_sqlog_16 1.6 el8.aarch64 pigsty 15.0 KiB pg_sqlog_16-1.6-1PIGSTY.el8.aarch64.rpm
pg_sqlog_16 1.6 el9.x86_64 pigsty 15.0 KiB pg_sqlog_16-1.6-1PIGSTY.el9.x86_64.rpm
pg_sqlog_16 1.6 el9.aarch64 pigsty 15.0 KiB pg_sqlog_16-1.6-1PIGSTY.el9.aarch64.rpm
pg_sqlog_16 1.6 el10.x86_64 pigsty 15.2 KiB pg_sqlog_16-1.6-1PIGSTY.el10.x86_64.rpm
pg_sqlog_16 1.6 el10.aarch64 pigsty 15.2 KiB pg_sqlog_16-1.6-1PIGSTY.el10.aarch64.rpm
postgresql-16-pg-sqlog 1.6 d12.x86_64 pigsty 9.9 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~bookworm_amd64.deb
postgresql-16-pg-sqlog 1.6 d12.aarch64 pigsty 9.9 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~bookworm_arm64.deb
postgresql-16-pg-sqlog 1.6 d13.x86_64 pigsty 9.9 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~trixie_amd64.deb
postgresql-16-pg-sqlog 1.6 d13.aarch64 pigsty 9.9 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~trixie_arm64.deb
postgresql-16-pg-sqlog 1.6 u22.x86_64 pigsty 9.5 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~jammy_amd64.deb
postgresql-16-pg-sqlog 1.6 u22.aarch64 pigsty 9.5 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~jammy_arm64.deb
postgresql-16-pg-sqlog 1.6 u24.x86_64 pigsty 9.5 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~noble_amd64.deb
postgresql-16-pg-sqlog 1.6 u24.aarch64 pigsty 9.5 KiB postgresql-16-pg-sqlog_1.6-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_sqlog_15 1.6 el8.x86_64 pigsty 15.1 KiB pg_sqlog_15-1.6-1PIGSTY.el8.x86_64.rpm
pg_sqlog_15 1.6 el8.aarch64 pigsty 15.0 KiB pg_sqlog_15-1.6-1PIGSTY.el8.aarch64.rpm
pg_sqlog_15 1.6 el9.x86_64 pigsty 15.0 KiB pg_sqlog_15-1.6-1PIGSTY.el9.x86_64.rpm
pg_sqlog_15 1.6 el9.aarch64 pigsty 15.0 KiB pg_sqlog_15-1.6-1PIGSTY.el9.aarch64.rpm
pg_sqlog_15 1.6 el10.x86_64 pigsty 15.2 KiB pg_sqlog_15-1.6-1PIGSTY.el10.x86_64.rpm
pg_sqlog_15 1.6 el10.aarch64 pigsty 15.2 KiB pg_sqlog_15-1.6-1PIGSTY.el10.aarch64.rpm
postgresql-15-pg-sqlog 1.6 d12.x86_64 pigsty 9.9 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~bookworm_amd64.deb
postgresql-15-pg-sqlog 1.6 d12.aarch64 pigsty 9.9 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~bookworm_arm64.deb
postgresql-15-pg-sqlog 1.6 d13.x86_64 pigsty 9.9 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~trixie_amd64.deb
postgresql-15-pg-sqlog 1.6 d13.aarch64 pigsty 9.9 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~trixie_arm64.deb
postgresql-15-pg-sqlog 1.6 u22.x86_64 pigsty 9.5 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~jammy_amd64.deb
postgresql-15-pg-sqlog 1.6 u22.aarch64 pigsty 9.5 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~jammy_arm64.deb
postgresql-15-pg-sqlog 1.6 u24.x86_64 pigsty 9.5 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~noble_amd64.deb
postgresql-15-pg-sqlog 1.6 u24.aarch64 pigsty 9.5 KiB postgresql-15-pg-sqlog_1.6-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_sqlog_14 1.6 el8.x86_64 pigsty 15.1 KiB pg_sqlog_14-1.6-1PIGSTY.el8.x86_64.rpm
pg_sqlog_14 1.6 el8.aarch64 pigsty 15.0 KiB pg_sqlog_14-1.6-1PIGSTY.el8.aarch64.rpm
pg_sqlog_14 1.6 el9.x86_64 pigsty 15.0 KiB pg_sqlog_14-1.6-1PIGSTY.el9.x86_64.rpm
pg_sqlog_14 1.6 el9.aarch64 pigsty 15.0 KiB pg_sqlog_14-1.6-1PIGSTY.el9.aarch64.rpm
pg_sqlog_14 1.6 el10.x86_64 pigsty 15.2 KiB pg_sqlog_14-1.6-1PIGSTY.el10.x86_64.rpm
pg_sqlog_14 1.6 el10.aarch64 pigsty 15.2 KiB pg_sqlog_14-1.6-1PIGSTY.el10.aarch64.rpm
postgresql-14-pg-sqlog 1.6 d12.x86_64 pigsty 9.9 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~bookworm_amd64.deb
postgresql-14-pg-sqlog 1.6 d12.aarch64 pigsty 9.9 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~bookworm_arm64.deb
postgresql-14-pg-sqlog 1.6 d13.x86_64 pigsty 9.9 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~trixie_amd64.deb
postgresql-14-pg-sqlog 1.6 d13.aarch64 pigsty 9.9 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~trixie_arm64.deb
postgresql-14-pg-sqlog 1.6 u22.x86_64 pigsty 9.5 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~jammy_amd64.deb
postgresql-14-pg-sqlog 1.6 u22.aarch64 pigsty 9.5 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~jammy_arm64.deb
postgresql-14-pg-sqlog 1.6 u24.x86_64 pigsty 9.5 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~noble_amd64.deb
postgresql-14-pg-sqlog 1.6 u24.aarch64 pigsty 9.5 KiB postgresql-14-pg-sqlog_1.6-1PIGSTY~noble_arm64.deb

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

Function Description
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