pg_sqlog
Provide SQL interface to logs
Repository
kouber/pg_sqlog
https://github.com/kouber/pg_sqlog
Source
pg_sqlog-1.6.tar.gz
pg_sqlog-1.6.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_sqlog | 1.6 | STAT | BSD 3-Clause | SQL |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 6500 | pg_sqlog | No | Yes | No | Yes | No | No | sqlog |
| Related | file_fdw pg_profile pg_tracing pg_show_plans pg_stat_kcache pg_stat_monitor pg_qualstats pg_store_plans |
|---|
require certain params
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.6 | 1817161514 | pg_sqlog | file_fdw |
| RPM | PIGSTY | 1.6 | 1817161514 | pg_sqlog_$v | - |
| DEB | PIGSTY | 1.6 | 1817161514 | postgresql-$v-pg-sqlog | - |
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 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
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.