pg-logstats
pg-logstats is a PostgreSQL log investigation CLI. It reads supported
PostgreSQL stderr logs, groups related statements into query families, ranks the
most useful findings, and prints follow-up SQL for live PostgreSQL inspection.
Supported Workflows
top query-families: rank query families in one log window by total runtime.slow-queries diff: compare a target log window against a baseline window.suggest-sql: printpg_stat_statementsandpg_stat_activityfollow-up SQL for a finding from JSON output.
Supported input is PostgreSQL stderr logs using this prefix shape:
2024-01-15 10:00:00.000 UTC [2001] app@appdb api: LOG: statement: SELECT * FROM users WHERE id = 1;
2024-01-15 10:00:00.020 UTC [2001] app@appdb api: LOG: duration: 20.000 ms
That corresponds to a PostgreSQL log_line_prefix similar to:
%m [%p] %u@%d %a:
Quick Start
Global flags such as --output-format, --outfile, --outdir, and --quiet
can be placed before or after the workflow command.
Installation
From crates.io:
From a local checkout:
From source without installing:
Commands
Top Query Families
Rank normalized query families in one log window:
Analyze every .log or .txt file in a directory:
Limit the number of emitted findings:
Write JSON findings for shell or agent workflows:
Slow Query Diff
Compare a target log window with a baseline log window:
Apply eligibility thresholds:
Suggested SQL
Generate follow-up SQL for a finding selected by rank:
Or select by exact finding id:
JSON Output
JSON output is centered on findings:
|
Useful fields include:
schema_versionmetadata.analysis_timestampmetadata.tool_versionmetadata.total_log_entriesfindings[].finding_idfindings[].kindfindings[].rankfindings[].titlefindings[].reasonfindings[].reason_codesfindings[].scorefindings[].confidencefindings[].query_family.normalized_sqlfindings[].query_family.databasefindings[].query_family.userfindings[].query_family.application_namefindings[].metrics.execution_countfindings[].metrics.total_duration_msfindings[].metrics.max_duration_msfindings[].next_sql
For diff findings, each finding also includes baseline, target, and delta
duration summaries.
Fixture Logs
tests/fixtures/cli contains the checked-in fixture logs used by the commands above.
Development
Run a smoke command during local development:
Troubleshooting
If no findings are emitted, first check the log prefix. The current parser expects the supported stderr prefix shown above and statement/duration lines that can be correlated by process id and order.
Use --sample-size <N> with top query-families or slow-queries diff when you
want a quick pass over the first N lines of each file.
License
This project is licensed under the MIT License. See LICENSE.