What does pgBadger do for a PostgreSQL server, what input does it read, and what does its report show?
answer
- offline, after the fact
- server log files, not views
- one self-contained HTML page
- queries, errors, locks, checkpoints, vacuum
basics
~20 spgBadger is a standalone Perl script that parses PostgreSQL server log files offline and builds a self-contained HTML report of slowest and most frequent queries, errors, lock waits, temporary files, checkpoints, autovacuum activity, connections and sessions.
solid answer
~40 spgBadger is a log analyser: a single Perl script you point at PostgreSQL's log files (stderr, syslog, `csvlog` or `jsonlog`, plain or compressed), and it writes one report, by default `out.html`, with the charts embedded. The report covers the slowest queries, the queries that took the most total time, the most frequent queries, errors and events, lock waits, temporary files, checkpoints, autovacuum and autoanalyze, and connections and sessions per database, user, client and application. It never connects to the database: everything comes from what the server logged, so the report is only as good as the logging settings. It can also read PgBouncer logs.
go deeper
Remember the shape: a Perl script that reads PostgreSQL log files after the fact and writes one HTML report. Name three report sections, such as slowest queries, errors and checkpoints.
Explain that the report is only as good as the logging: durations need log_min_duration_statement, and each other section needs its own log setting and a parseable line prefix.
Show where it fits in operations: daily and weekly incremental reports from cron, run off the database host, kept as an archive next to live monitoring and cumulative statistics.
Frame the trade: log-based analysis costs logging I/O on the server but gives timestamped samples and history that cumulative counters cannot; decide which questions need which source.
## What pgBadger is **pgBadger** is an open-source **PostgreSQL log analyser**. It is one **Perl script** with no database connection, no agent and no daemon. You run it, usually from cron, against log files the PostgreSQL server has already written, and it produces a report. The HTML report embeds its JavaScript charting and CSS, so a single file can be copied to a web server or attached to a ticket. The key mental model is **offline and after the fact**. pgBadger answers questions such as "which statements took the most time between Monday and Sunday?" or "when did the lock waits start?" It does not show what is running right now; that is the job of live views and consoles. ## What it reads pgBadger reads the PostgreSQL **server log**. It auto-detects the common formats if the file is long enough, and `-f`/`--format` forces one when detection fails. | Log format | How the server writes it | Notes for pgBadger | |---|---|---| | `stderr` | `log_destination = 'stderr'` with a `log_line_prefix` | the prefix must carry a time escape and a process escape | | `syslog` | sent to the system logger | the prefix carries user and database fields | | `csvlog` | `log_destination = 'csvlog'`, needs `logging_collector` | needs the Perl module `Text::CSV_XS`; cannot be read from stdin | | `jsonlog` | `log_destination = 'jsonlog'`, needs `logging_collector` | also used for several managed and operator-produced logs | It also understands compressed files (gzip, bzip2, lz4, xz, zip, zstd), files fetched over `ssh://` or `http(s)://`, `journalctl` output, and **PgBouncer** logs, for which it builds separate throughput and session reports. ## What the report shows The report is grouped into sections you can switch off individually with `--disable-*` options: - **Queries**: overall statistics, the **slowest queries**, the queries that **took up the most time** in total, the **most frequent** queries, queries by type (SELECT, INSERT, UPDATE, DELETE), and histograms of query and session times. - **Normalised queries**: literals are replaced so that `WHERE id = 7` and `WHERE id = 9` count as one query, with a few samples kept (`-s`, default 3) and the top list sized by `-t` (default 20). - **Waits and locks**: queries that waited the most, and lock statistics. - **Temporary files**: queries generating the most and the largest temporary files. - **Checkpoints and autovacuum**: checkpoint statistics and autovacuum and autoanalyze activity per table. - **Connections and sessions**: per database, user, client host and application. - **Errors and events**: the most frequent errors, cancellations and server events. Each section has hourly charts, and where `auto_explain` plans were logged, the slow-query entries carry the plan. ## What it needs from the server Because pgBadger only sees the log, it can only report what PostgreSQL wrote there. A default PostgreSQL 18 install logs almost nothing about statements: `log_min_duration_statement` defaults to `-1`, which disables statement duration logging. A useful report needs, at least: 1. a **parseable log line**: a `log_line_prefix` with a timestamp and a process ID (the 18 default `'%m [%p] '` qualifies), or `csvlog`/`jsonlog`; 2. **statement durations**, via `log_min_duration_statement`; 3. the event settings for the other sections: checkpoints, connections, disconnections, lock waits, temporary files and autovacuum; 4. log messages in **English** (`lc_messages` of `'C'` or an English locale), because the parser matches message text. ## How it is run and what it outputs A one-off run is just the script and a file name: ```bash pgbadger /var/log/postgresql/postgresql-18-main.log -o /tmp/report.html ``` The output format follows the file extension or `-x`: `html` (the default), `text`, `json` (needs the `JSON::XS` module) or `bin`, a binary data file that a later run can merge. The binary format is what makes **incremental mode** (`-I`) possible: one report per day plus a cumulative weekly report, built night after night from a cron job. ## What it is not - It is **not a live monitor**: nothing in it is current; it describes a past window of logs. - It is **not a cumulative statistics view**: unlike `pg_stat_statements`, it sees individual logged executions with timestamps, but only the ones that were logged. - It does **not change the server**: enabling the logging it needs is a separate configuration task with its own overhead.
- Why would a team keep pgBadger reports when they already have a live monitoring console?A console shows the present and short history; pgBadger shows a past window with timestamps and query samples, so it answers "what happened last Tuesday at 14:00" or "which statement grew slower this week". Its HTML files are also an archive you can keep per day and week and compare, without any service running.
- Does pgBadger need to connect to the database it reports on?No. It reads log files only: local, compressed, piped on stdin, or fetched over ssh or http. That is why it can run on a separate reporting host, and also why it reports nothing the server did not log.
- pgBadger queries pg_stat_activity to build its report
- pgBadger shows what is running on the server right now
- A default PostgreSQL install already logs every query for pgBadger
- pgBadger needs an agent installed on each database host
- pgBadger reads the WAL to reconstruct the queries that ran