pgBadger Documentation
repository·master·Indexed 26 days ago
https://github.com/darold/pgbadgerA high-performance PostgreSQL log analyzer that generates detailed reports and visual graphs to diagnose database performance issues. It supports various log formats (syslog, stderr, jsonlog, csv, pgbouncer, logplex, rds, redshift), parallel processing for large files, and an incremental mode for continuous log parsing and historical data management.
What's inside pgBadger
- pgBadger is a fast PostgreSQL log analyzer that generates fully detailed reports and graphs from PostgreSQL log files. It helps developers and DBAs understand database performance by analyzing log data.
Use incremental reporting for periodic analysis
masterIncremental mode (-I) allows you to generate daily reports and cumulative weekly reports. This is ideal for cron jobs to monitor logs over time without re-parsing everything every time.Optimize parsing speed with parallel processing
masterUse the-jflag to specify the number of CPUs to use for parallel processing. This can speed up the parsing of a single large log file or multiple files.Create custom reports using binary format
masterYou can manually manage incremental and cumulative reports by generating and then reading binary files.
- Generate binary data from a log file using
-o:pgbadger --last-parsed <state_file> -o <output_bin> <log_file> - Generate HTML reports from the accumulated binary files:
pgbadger <output_dir>/*.bin
- Generate binary data from a log file using
Generate incremental reports with -I
masterThe
-Ior--incrementaloption enables automatic incremental report mode. pgBadger will generate one report per day and a cumulative report per week.- Output: Data is first written in binary format to the directory specified by
-Oor--outdir, then converted to HTML. - Automation: This mode is ideal for cron jobs. It creates an automatic incremental file in the output directory so it won't count log entries twice across runs.
- Extra Files: Use
-Xor--extra-filesto force JavaScript and CSS into separate files to save disk space.
Example Cron Job:
0 4 * * * /usr/bin/pgbadger -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/pgbadger -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/- Output: Data is first written in binary format to the directory specified by
Use the pgBadger CLI command
masterThe basic syntax for running pgBadger is to provide the command followed by optional flags and the target log file(s).
Arguments:
logfile: Can be a single log file, a list of files, or a shell command that returns a list of files.- To pass log content from
stdin, use-as the filename. Note thatstdinis not compatible withcsvlogformat.
Install pgBadger from source
masterTo install pgBadger, download the tarball from GitHub and run the following commands to build and install it using Perl's
Makefile.PL.By default, it installs to
/usr/local/bin/pgbadger(site install). To install into/usr/(similar to Debian), useINSTALLDIRS='perl'orINSTALLDIRS=vendorduring theMakefile.PLstep.tar xzf pgbadger-11.x.tar.gz cd pgbadger-11.x/ perl Makefile.PL make && sudo make install # To install like Debian (vendor directory): perl Makefile.PL INSTALLDIRS=vendor make && sudo make installFilter queries and time ranges
masterYou can refine your reports by excluding specific queries, applications, or time periods to remove noise (likepg_dumpactivity).Configure PostgreSQL for pgBadger parsing
masterTo ensure pgBadger can parse your logs, you must configure specific directives in
postgresql.conf.Required Settings
- Enable Query Logging: Set
log_min_duration_statement = 0(or a higher value to log only slow queries).- Warning: Do not enable
log_statement = 'all', as it will preventlog_min_duration_statementfrom working and pgBadger will not parse it correctly.
- Warning: Do not enable
- Log Line Prefix: The
log_line_prefixmust include a time escape sequence (%t,%m, or%n) and a process-related sequence (%por%c).- Example for
stderr:log_line_prefix = '%t [%p]: ' - Example with extra metadata:
log_line_prefix = '%t [%p]: user=%u,db=%d,app=%a,client=%h '
- Example for
- Locale: Ensure logs are in English (e.g.,
lc_messages='en_US.UTF-8'orlc_messages='C').
Recommended Settings for Detailed Reports
Enable these to get more information in your reports:
log_checkpoints = onlog_connections = onlog_disconnections = onlog_lock_waits = onlog_temp_files = 0log_autovacuum_min_duration = 0log_error_verbosity = default
- Enable Query Logging: Set
Parse local and remote PostgreSQL log files
masterpgBadger can parse local log files, compressed files (gzip, bzip2, lz4, xz, zip, zstd), or remote files via SSH or URI. For remote hosts, use the-ror--remote-hostflag. You can also use URI notation forhttp[s]and[s]ftpprotocols, orssh://for remote access.Use incremental mode for continuous log parsing
masterIncremental mode allows you to generate reports by days and manage historical data efficiently.
-I | --incremental: Enables incremental mode. Requires--outdirto be set.-l | --last-parsed file: Registers the last datetime and line parsed to allow incremental parsing. Useful for watching errors since the last run or handling rotated logs.-R | --retention N: Sets the number of weeks to keep in incremental mode (default is 0, which is disabled). Older weeks/days are automatically removed.--day-report YYYY-MM-DD: Creates an HTML report for a specific day. Requires incremental output directories and necessary binary data files.--iso-week-number: In incremental mode, ensures calendar weeks start on Monday and follow ISO 8601.-X | --extra-files: (See output configuration) Allows writing CSS/JS as separate files in the output directory.
Configure log format and custom prefixes
masterIf pgBadger cannot automatically detect your log format, use the following:
-f | --format logtype: Manually specify the format. Supported values:syslog,syslog2,stderr,jsonlog,csv,pgbouncer,logplex,rds, andredshift.-p | --prefix string: Specify your customlog_line_prefixfrompostgresql.conf. This is required if your prefix includes non-standard variables like client IP or application name. The prefix MUST contain escape sequences for time (%t,%m, or%n) and processes (%por%c).