| 1 |
NAME |
| 2 |
pgBadger - a fast PostgreSQL log analysis report |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
Usage: pgbadger [options] logfile [...] |
| 6 |
|
| 7 |
PostgreSQL log analyzer with fully detailed reports and graphs. |
| 8 |
|
| 9 |
Arguments: |
| 10 |
|
| 11 |
logfile can be a single log file, a list of files, or a shell command |
| 12 |
returning a list of files. If you want to pass log content from stdin |
| 13 |
use - as filename. Note that input from stdin will not work with csvlog. |
| 14 |
|
| 15 |
Options: |
| 16 |
|
| 17 |
-a | --average minutes : number of minutes to build the average graphs of |
| 18 |
queries and connections. Default 5 minutes. |
| 19 |
-A | --histo-average min: number of minutes to build the histogram graphs |
| 20 |
of queries. Default 60 minutes. |
| 21 |
-b | --begin datetime : start date/time for the data to be parsed in log |
| 22 |
(either a timestamp or a time) |
| 23 |
-c | --dbclient host : only report on entries for the given client host. |
| 24 |
-C | --nocomment : remove comments like /* ... */ from queries. |
| 25 |
-d | --dbname database : only report on entries for the given database. |
| 26 |
-D | --dns-resolv : client ip addresses are replaced by their DNS name. |
| 27 |
Be warned that this can really slow down pgBadger. |
| 28 |
-e | --end datetime : end date/time for the data to be parsed in log |
| 29 |
(either a timestamp or a time) |
| 30 |
-E | --explode : explode the main report by generating one report |
| 31 |
per database. Global information not related to a |
| 32 |
database is added to the postgres database report. |
| 33 |
-f | --format logtype : possible values: syslog, syslog2, stderr, jsonlog, |
| 34 |
csv, pgbouncer, logplex, rds and redshift. Use this |
| 35 |
option when pgBadger is not able to detect the log |
| 36 |
format. |
| 37 |
-G | --nograph : disable graphs on HTML output. Enabled by default. |
| 38 |
-h | --help : show this message and exit. |
| 39 |
-H | --html-outdir path: path to directory where HTML report must be written |
| 40 |
in incremental mode, binary files stay on directory |
| 41 |
defined with -O, --outdir option. |
| 42 |
-i | --ident name : programname used as syslog ident. Default: postgres |
| 43 |
-I | --incremental : use incremental mode, reports will be generated by |
| 44 |
days in a separate directory, --outdir must be set. |
| 45 |
-j | --jobs number : number of jobs to run at same time for a single log |
| 46 |
file. Run as single by default or when working with |
| 47 |
csvlog format. |
| 48 |
-J | --Jobs number : number of log files to parse in parallel. Process |
| 49 |
one file at a time by default. |
| 50 |
-l | --last-parsed file: allow incremental log parsing by registering the |
| 51 |
last datetime and line parsed. Useful if you want |
| 52 |
to watch errors since last run or if you want one |
| 53 |
report per day with a log rotated each week. |
| 54 |
-L | --logfile-list file:file containing a list of log files to parse. |
| 55 |
-m | --maxlength size : maximum length of a query, it will be restricted to |
| 56 |
the given size. Default truncate size is 100000. |
| 57 |
-M | --no-multiline : do not collect multiline statements to avoid garbage |
| 58 |
especially on errors that generate a huge report. |
| 59 |
-N | --appname name : only report on entries for given application name |
| 60 |
-o | --outfile filename: define the filename for the output. Default depends |
| 61 |
on the output format: out.html, out.txt, out.bin, |
| 62 |
or out.json. This option can be used multiple times |
| 63 |
to output several formats. To use json output, the |
| 64 |
Perl module JSON::XS must be installed, to dump |
| 65 |
output to stdout, use - as filename. |
| 66 |
-O | --outdir path : directory where out files must be saved. |
| 67 |
-p | --prefix string : the value of your custom log_line_prefix as |
| 68 |
defined in your postgresql.conf. Only use it if you |
| 69 |
aren't using one of the standard prefixes specified |
| 70 |
in the pgBadger documentation, such as if your |
| 71 |
prefix includes additional variables like client IP |
| 72 |
or application name. MUST contain escape sequences |
| 73 |
for time (%t, %m or %n) and processes (%p or %c). |
| 74 |
See examples below. |
| 75 |
-P | --no-prettify : disable SQL queries prettify formatter. |
| 76 |
-q | --quiet : don't print anything to stdout, not even a progress |
| 77 |
bar. |
| 78 |
-Q | --query-numbering : add numbering of queries to the output when using |
| 79 |
options --dump-all-queries or --normalized-only. |
| 80 |
-r | --remote-host ip : set the host where to execute the cat command on |
| 81 |
remote log file to parse the file locally. |
| 82 |
-R | --retention N : number of weeks to keep in incremental mode. Defaults |
| 83 |
to 0, disabled. Used to set the number of weeks to |
| 84 |
keep in output directory. Older weeks and days |
| 85 |
directories are automatically removed. |
| 86 |
-s | --sample number : number of query samples to store. Default: 3. |
| 87 |
-S | --select-only : only report SELECT queries. |
| 88 |
-t | --top number : number of queries to store/display. Default: 20. |
| 89 |
-T | --title string : change title of the HTML page report. |
| 90 |
-u | --dbuser username : only report on entries for the given user. |
| 91 |
-U | --exclude-user username : exclude entries for the specified user from |
| 92 |
report. Can be used multiple time. |
| 93 |
-v | --verbose : enable verbose or debug mode. Disabled by default. |
| 94 |
-V | --version : show pgBadger version and exit. |
| 95 |
-w | --watch-mode : only report errors just like logwatch could do. |
| 96 |
-W | --wide-char : encode html output of queries into UTF8 to avoid |
| 97 |
Perl message "Wide character in print". |
| 98 |
-x | --extension : output format. Values: text, html, bin or json. |
| 99 |
Default: html |
| 100 |
-X | --extra-files : in incremental mode allow pgBadger to write CSS and |
| 101 |
JS files in the output directory as separate files. |
| 102 |
-z | --zcat exec_path : set the full path to the zcat program. Use it if |
| 103 |
zcat, bzcat or unzip is not in your path. |
| 104 |
-Z | --timezone +/-XX : Set the number of hours from GMT of the timezone. |
| 105 |
Use this to adjust date/time in JavaScript graphs. |
| 106 |
The value can be an integer, ex.: 2, or a float, |
| 107 |
ex.: 2.5. |
| 108 |
--pie-limit num : pie data lower than num% will show a sum instead. |
| 109 |
--exclude-query regex : any query matching the given regex will be excluded |
| 110 |
from the report. For example: "^(VACUUM|COMMIT)" |
| 111 |
You can use this option multiple times. |
| 112 |
--exclude-file filename: path of the file that contains each regex to use |
| 113 |
to exclude queries from the report. One regex per |
| 114 |
line. |
| 115 |
--include-query regex : any query that does not match the given regex will |
| 116 |
be excluded from the report. You can use this |
| 117 |
option multiple times. For example: "(tbl1|tbl2)". |
| 118 |
--include-file filename: path of the file that contains each regex to the |
| 119 |
queries to include from the report. One regex per |
| 120 |
line. |
| 121 |
--disable-error : do not generate error report. |
| 122 |
--disable-hourly : do not generate hourly report. |
| 123 |
--disable-type : do not generate report of queries by type, database |
| 124 |
or user. |
| 125 |
--disable-query : do not generate query reports (slowest, most |
| 126 |
frequent, queries by users, by database, ...). |
| 127 |
--disable-session : do not generate session report. |
| 128 |
--disable-connection : do not generate connection report. |
| 129 |
--disable-lock : do not generate lock report. |
| 130 |
--disable-temporary : do not generate temporary report. |
| 131 |
--disable-checkpoint : do not generate checkpoint/restartpoint report. |
| 132 |
--disable-autovacuum : do not generate autovacuum report. |
| 133 |
--charset : used to set the HTML charset to be used. |
| 134 |
Default: utf-8. |
| 135 |
--csv-separator : used to set the CSV field separator, default: , |
| 136 |
--exclude-time regex : any timestamp matching the given regex will be |
| 137 |
excluded from the report. Example: "2013-04-12 .*" |
| 138 |
You can use this option multiple times. |
| 139 |
--include-time regex : only timestamps matching the given regex will be |
| 140 |
included in the report. Example: "2013-04-12 .*" |
| 141 |
You can use this option multiple times. |
| 142 |
--exclude-db name : exclude entries for the specified database from |
| 143 |
report. Example: "pg_dump". Can be used multiple |
| 144 |
times. |
| 145 |
--exclude-appname name : exclude entries for the specified application name |
| 146 |
from report. Example: "pg_dump". Can be used |
| 147 |
multiple times. |
| 148 |
--exclude-line regex : exclude any log entry that will match the given |
| 149 |
regex. Can be used multiple times. |
| 150 |
--exclude-client name : exclude log entries for the specified client ip. |
| 151 |
Can be used multiple times. |
| 152 |
--anonymize : obscure all literals in queries, useful to hide |
| 153 |
confidential data. |
| 154 |
--noreport : no reports will be created in incremental mode. |
| 155 |
--log-duration : force pgBadger to associate log entries generated |
| 156 |
by both log_duration = on and log_statement = 'all' |
| 157 |
--enable-checksum : used to add an md5 sum under each query report. |
| 158 |
--journalctl command : command to use to replace PostgreSQL logfile by |
| 159 |
a call to journalctl. Basically it might be: |
| 160 |
journalctl -u postgresql-9.5 |
| 161 |
--pid-dir path : set the path where the pid file must be stored. |
| 162 |
Default /tmp |
| 163 |
--pid-file file : set the name of the pid file to manage concurrent |
| 164 |
execution of pgBadger. Default: pgbadger.pid |
| 165 |
--rebuild : used to rebuild all html reports in incremental |
| 166 |
output directories where there's binary data files. |
| 167 |
--pgbouncer-only : only show PgBouncer-related menus in the header. |
| 168 |
--start-monday : in incremental mode, calendar weeks start on |
| 169 |
Sunday. Use this option to start on a Monday. |
| 170 |
--iso-week-number : in incremental mode, calendar weeks start on |
| 171 |
Monday and respect the ISO 8601 week number, range |
| 172 |
01 to 53, where week 1 is the first week that has |
| 173 |
at least 4 days in the new year. |
| 174 |
--normalized-only : only dump all normalized queries to out.txt |
| 175 |
--log-timezone +/-XX : Set the number of hours from GMT of the timezone |
| 176 |
that must be used to adjust date/time read from |
| 177 |
log file before beeing parsed. Using this option |
| 178 |
makes log search with a date/time more difficult. |
| 179 |
The value can be an integer, ex.: 2, or a float, |
| 180 |
ex.: 2.5. |
| 181 |
--prettify-json : use it if you want json output to be prettified. |
| 182 |
--month-report YYYY-MM : create a cumulative HTML report over the specified |
| 183 |
month. Requires incremental output directories and |
| 184 |
the presence of all necessary binary data files |
| 185 |
--day-report YYYY-MM-DD: create an HTML report over the specified day. |
| 186 |
Requires incremental output directories and the |
| 187 |
presence of all necessary binary data files |
| 188 |
--noexplain : do not process lines generated by auto_explain. |
| 189 |
--command CMD : command to execute to retrieve log entries on |
| 190 |
stdin. pgBadger will open a pipe to the command |
| 191 |
and parse log entries generated by the command. |
| 192 |
--no-week : inform pgbadger to not build weekly reports in |
| 193 |
incremental mode. Useful if it takes too much time. |
| 194 |
--explain-url URL : use it to override the url of the graphical explain |
| 195 |
tool. Default: https://explain.depesz.com/ |
| 196 |
--tempdir DIR : set directory where temporary files will be written |
| 197 |
Default: File::Spec->tmpdir() || '/tmp' |
| 198 |
--no-process-info : disable changing process title to help identify |
| 199 |
pgbadger process, some system do not support it. |
| 200 |
--dump-all-queries : dump all queries found in the log file replacing |
| 201 |
bind parameters included in the queries at their |
| 202 |
respective placeholders positions. |
| 203 |
--keep-comments : do not remove comments from normalized queries. It |
| 204 |
can be useful if you want to distinguish between |
| 205 |
same normalized queries. |
| 206 |
--no-progressbar : disable progressbar. |
| 207 |
--dump-raw-csv : parse the log and dump the information into CSV |
| 208 |
format. No further processing is done, no report. |
| 209 |
--include-pid PID : only report events related to the session pid (%p). |
| 210 |
Can be used multiple time. |
| 211 |
--include-session ID : only report events related to the session id (%c). |
| 212 |
Can be used multiple time. |
| 213 |
--histogram-query VAL : use custom inbound for query times histogram. |
| 214 |
Default inbound in milliseconds: |
| 215 |
0,1,5,10,25,50,100,500,1000,10000 |
| 216 |
--histogram-session VAL : use custom inbound for session times histogram. |
| 217 |
Default inbound in milliseconds: |
| 218 |
0,500,1000,30000,60000,600000,1800000,3600000,28800000 |
| 219 |
--no-fork : do not fork any process, for debugging purpose. |
| 220 |
|
| 221 |
pgBadger is able to parse a remote log file using a passwordless ssh |
| 222 |
connection. Use -r or --remote-host to set the host IP address or |
| 223 |
hostname. There are also some additional options to fully control the |
| 224 |
ssh connection. |
| 225 |
|
| 226 |
--ssh-program ssh path to the ssh program to use. Default: ssh. |
| 227 |
--ssh-port port ssh port to use for the connection. Default: 22. |
| 228 |
--ssh-user username connection login name. Defaults to running user. |
| 229 |
--ssh-identity file path to the identity file to use. |
| 230 |
--ssh-timeout second timeout to ssh connection failure. Default: 10 sec. |
| 231 |
--ssh-option options list of -o options to use for the ssh connection. |
| 232 |
Options always used: |
| 233 |
-o ConnectTimeout=$ssh_timeout |
| 234 |
-o PreferredAuthentications=hostbased,publickey |
| 235 |
--ssh-sudo Runs all commands on remote host as sudo, will not work |
| 236 |
with wildcard filenames |
| 237 |
|
| 238 |
Log file to parse can also be specified using an URI, supported |
| 239 |
protocols are http[s] and [s]ftp. The curl command will be used to |
| 240 |
download the file, and the file will be parsed during download. The ssh |
| 241 |
protocol is also supported and will use the ssh command like with the |
| 242 |
remote host use. See examples bellow. |
| 243 |
|
| 244 |
Return codes: |
| 245 |
|
| 246 |
0: on success |
| 247 |
1: die on error |
| 248 |
2: if it has been interrupted using ctr+c for example |
| 249 |
3: the pid file already exists or can not be created |
| 250 |
4: no log file was given at command line |
| 251 |
|
| 252 |
Examples: |
| 253 |
|
| 254 |
pgbadger /var/log/postgresql.log |
| 255 |
pgbadger /var/log/postgres.log.2.gz /var/log/postgres.log.1.gz /var/log/postgres.log |
| 256 |
pgbadger /var/log/postgresql/postgresql-2012-05-* |
| 257 |
pgbadger --exclude-query="^(COPY|COMMIT)" /var/log/postgresql.log |
| 258 |
pgbadger -b "2012-06-25 10:56:11" -e "2012-06-25 10:59:11" /var/log/postgresql.log |
| 259 |
cat /var/log/postgres.log | pgbadger - |
| 260 |
# Log line prefix with stderr log output |
| 261 |
pgbadger --prefix '%t [%p]: user=%u,db=%d,client=%h' /pglog/postgresql-2012-08-21* |
| 262 |
pgbadger --prefix '%m %u@%d %p %r %a : ' /pglog/postgresql.log |
| 263 |
# Log line prefix with syslog log output |
| 264 |
pgbadger --prefix 'user=%u,db=%d,client=%h,appname=%a' /pglog/postgresql-2012-08-21* |
| 265 |
# Use my 8 CPUs to parse my 10GB file faster, much faster |
| 266 |
pgbadger -j 8 /pglog/postgresql-10.1-main.log |
| 267 |
|
| 268 |
Use URI notation for remote log file: |
| 269 |
|
| 270 |
pgbadger http://172.12.110.1//var/log/postgresql/postgresql-10.1-main.log |
| 271 |
pgbadger ftp://[email protected]/postgresql-10.1-main.log |
| 272 |
pgbadger ssh://[email protected]:2222//var/log/postgresql/postgresql-10.1-main.log* |
| 273 |
|
| 274 |
You can use together a local PostgreSQL log and a remote pgbouncer log |
| 275 |
file to parse: |
| 276 |
|
| 277 |
pgbadger /var/log/postgresql/postgresql-10.1-main.log ssh://[email protected]/pgbouncer.log |
| 278 |
|
| 279 |
Reporting errors every week by cron job: |
| 280 |
|
| 281 |
30 23 * * 1 /usr/bin/pgbadger -q -w /var/log/postgresql.log -o /var/reports/pg_errors.html |
| 282 |
|
| 283 |
Generate report every week using incremental behavior: |
| 284 |
|
| 285 |
0 4 * * 1 /usr/bin/pgbadger -q `find /var/log/ -mtime -7 -name "postgresql.log*"` -o /var/reports/pg_errors-`date +\%F`.html -l /var/reports/pgbadger_incremental_file.dat |
| 286 |
|
| 287 |
This supposes that your log file and HTML report are also rotated every |
| 288 |
week. |
| 289 |
|
| 290 |
Or better, use the auto-generated incremental reports: |
| 291 |
|
| 292 |
0 4 * * * /usr/bin/pgbadger -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/ |
| 293 |
|
| 294 |
will generate a report per day and per week. |
| 295 |
|
| 296 |
In incremental mode, you can also specify the number of weeks to keep in |
| 297 |
the reports: |
| 298 |
|
| 299 |
/usr/bin/pgbadger --retention 2 -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/ |
| 300 |
|
| 301 |
If you have a pg_dump at 23:00 and 13:00 each day during half an hour, |
| 302 |
you can use pgBadger as follow to exclude these periods from the report: |
| 303 |
|
| 304 |
pgbadger --exclude-time "2013-09-.* (23|13):.*" postgresql.log |
| 305 |
|
| 306 |
This will help avoid having COPY statements, as generated by pg_dump, on |
| 307 |
top of the list of slowest queries. You can also use --exclude-appname |
| 308 |
"pg_dump" to solve this problem in a simpler way. |
| 309 |
|
| 310 |
You can also parse journalctl output just as if it was a log file: |
| 311 |
|
| 312 |
pgbadger --journalctl 'journalctl -u postgresql-9.5' |
| 313 |
|
| 314 |
or worst, call it from a remote host: |
| 315 |
|
| 316 |
pgbadger -r 192.168.1.159 --journalctl 'journalctl -u postgresql-9.5' |
| 317 |
|
| 318 |
you don't need to specify any log file at command line, but if you have |
| 319 |
other PostgreSQL log files to parse, you can add them as usual. |
| 320 |
|
| 321 |
To rebuild all incremental html reports after, proceed as follow: |
| 322 |
|
| 323 |
rm /path/to/reports/*.js |
| 324 |
rm /path/to/reports/*.css |
| 325 |
pgbadger -X -I -O /path/to/reports/ --rebuild |
| 326 |
|
| 327 |
it will also update all resource files (JS and CSS). Use -E or --explode |
| 328 |
if the reports were built using this option. |
| 329 |
|
| 330 |
pgBadger also supports Heroku PostgreSQL logs using logplex format: |
| 331 |
|
| 332 |
heroku logs -p postgres | pgbadger -f logplex -o heroku.html - |
| 333 |
|
| 334 |
this will stream Heroku PostgreSQL log to pgbadger through stdin. |
| 335 |
|
| 336 |
pgBadger can auto detect RDS and cloudwatch PostgreSQL logs using rds |
| 337 |
format: |
| 338 |
|
| 339 |
pgbadger -f rds -o rds_out.html rds.log |
| 340 |
|
| 341 |
Each CloudSQL Postgresql log is a fairly normal PostgreSQL log, but |
| 342 |
encapsulated in JSON format. It is autodetected by pgBadger but in case |
| 343 |
you need to force the log format use `jsonlog`: |
| 344 |
|
| 345 |
pgbadger -f jsonlog -o cloudsql_out.html cloudsql.log |
| 346 |
|
| 347 |
This is the same as with the jsonlog extension, the json format is |
| 348 |
different but pgBadger can parse both formats. |
| 349 |
|
| 350 |
pgBadger also supports logs produced by CloudNativePG Postgres operator |
| 351 |
for Kubernetes: |
| 352 |
|
| 353 |
pgbadger -f jsonlog -o cnpg_out.html cnpg.log |
| 354 |
|
| 355 |
To create a cumulative report over a month use command: |
| 356 |
|
| 357 |
pgbadger --month-report 2919-05 /path/to/incremental/reports/ |
| 358 |
|
| 359 |
this will add a link to the month name into the calendar view in |
| 360 |
incremental reports to look at report for month 2019 May. Use -E or |
| 361 |
--explode if the reports were built using this option. |
| 362 |
|
| 363 |
DESCRIPTION |
| 364 |
pgBadger is a PostgreSQL log analyzer built for speed providing fully |
| 365 |
detailed reports based on your PostgreSQL log files. It's a small |
| 366 |
standalone Perl script that outperforms any other PostgreSQL log |
| 367 |
analyzer. |
| 368 |
|
| 369 |
It is written in pure Perl and uses a JavaScript library (flotr2) to |
| 370 |
draw graphs so that you don't need to install any additional Perl |
| 371 |
modules or other packages. Furthermore, this library gives us more |
| 372 |
features such as zooming. pgBadger also uses the Bootstrap JavaScript |
| 373 |
library and the FontAwesome webfont for better design. Everything is |
| 374 |
embedded. |
| 375 |
|
| 376 |
pgBadger is able to autodetect your log file format (syslog, stderr, |
| 377 |
csvlog or jsonlog) if the file is long enough. It is designed to parse |
| 378 |
huge log files as well as compressed files. Supported compressed formats |
| 379 |
are gzip, bzip2, lz4, xz, zip and zstd. For the xz format you must have |
| 380 |
an xz version higher than 5.05 that supports the --robot option. lz4 |
| 381 |
files must be compressed with the --content-size option for pgbadger to |
| 382 |
determine the uncompressed file size. For the complete list of features, |
| 383 |
see below. |
| 384 |
|
| 385 |
All charts are zoomable and can be saved as PNG images. |
| 386 |
|
| 387 |
You can also limit pgBadger to only report errors or remove any part of |
| 388 |
the report using command-line options. |
| 389 |
|
| 390 |
pgBadger supports any custom format set in the log_line_prefix directive |
| 391 |
of your postgresql.conf file as long as it at least specifies the %t, %m |
| 392 |
or %n and %p or %c patterns. |
| 393 |
|
| 394 |
pgBadger allows parallel processing of a single log file or multiple |
| 395 |
files through the use of the -j option specifying the number of CPUs. |
| 396 |
|
| 397 |
If you want to save system performance you can also use log_duration |
| 398 |
instead of log_min_duration_statement to have reports on duration and |
| 399 |
number of queries only. |
| 400 |
|
| 401 |
FEATURE |
| 402 |
pgBadger reports everything about your SQL queries: |
| 403 |
|
| 404 |
Overall statistics. |
| 405 |
The most frequent waiting queries. |
| 406 |
Queries that waited the most. |
| 407 |
Queries generating the most temporary files. |
| 408 |
Queries generating the largest temporary files. |
| 409 |
The slowest queries. |
| 410 |
Queries that took up the most time. |
| 411 |
The most frequent queries. |
| 412 |
The most frequent errors. |
| 413 |
Histogram of query times. |
| 414 |
Histogram of sessions times. |
| 415 |
Users involved in top queries. |
| 416 |
Applications involved in top queries. |
| 417 |
Queries generating the most cancellation. |
| 418 |
Queries most cancelled. |
| 419 |
The most time consuming prepare/bind queries |
| 420 |
|
| 421 |
The following reports are also available with hourly charts divided into |
| 422 |
periods of five minutes: |
| 423 |
|
| 424 |
SQL queries statistics. |
| 425 |
Temporary file statistics. |
| 426 |
Checkpoints statistics. |
| 427 |
Autovacuum and autoanalyze statistics. |
| 428 |
Cancelled queries. |
| 429 |
Error events (panic, fatal, error and warning). |
| 430 |
Error class distribution. |
| 431 |
|
| 432 |
There are also some pie charts about distribution of: |
| 433 |
|
| 434 |
Locks statistics. |
| 435 |
Queries by type (select/insert/update/delete). |
| 436 |
Distribution of queries type per database/application |
| 437 |
Sessions per database/user/client/application. |
| 438 |
Connections per database/user/client/application. |
| 439 |
Autovacuum and autoanalyze per table. |
| 440 |
Queries per user and total duration per user. |
| 441 |
|
| 442 |
All charts are zoomable and can be saved as PNG images. SQL queries |
| 443 |
reported are highlighted and beautified automatically. |
| 444 |
|
| 445 |
pgBadger is also able to parse PgBouncer log files and to create the |
| 446 |
following reports: |
| 447 |
|
| 448 |
Request Throughput |
| 449 |
Bytes I/O Throughput |
| 450 |
Average Query Duration |
| 451 |
Simultaneous sessions |
| 452 |
Histogram of sessions times |
| 453 |
Sessions per database |
| 454 |
Sessions per user |
| 455 |
Sessions per host |
| 456 |
Established connections |
| 457 |
Connections per database |
| 458 |
Connections per user |
| 459 |
Connections per host |
| 460 |
Most used reserved pools |
| 461 |
Most Frequent Errors/Events |
| 462 |
|
| 463 |
You can also have incremental reports with one report per day and a |
| 464 |
cumulative report per week. Two multiprocess modes are available to |
| 465 |
speed up log parsing, one using one core per log file, and the second |
| 466 |
using multiple cores to parse a single file. These modes can be |
| 467 |
combined. |
| 468 |
|
| 469 |
Histogram granularity can be adjusted using the -A command-line option. |
| 470 |
By default, they will report the mean of each top queries/errors |
| 471 |
occurring per hour, but you can specify the granularity down to the |
| 472 |
minute. |
| 473 |
|
| 474 |
pgBadger can also be used in a central place to parse remote log files |
| 475 |
using a passwordless SSH connection. This mode can be used with |
| 476 |
compressed files and in the multiprocess per file mode (-J), but cannot |
| 477 |
be used with the CSV log format. |
| 478 |
|
| 479 |
Examples of reports can be found here: |
| 480 |
https://pgbadger.darold.net/#reports |
| 481 |
|
| 482 |
REQUIREMENT |
| 483 |
pgBadger comes as a single Perl script - you do not need anything other |
| 484 |
than a modern Perl distribution. Charts are rendered using a JavaScript |
| 485 |
library, so you don't need anything other than a web browser. Your |
| 486 |
browser will do all the work. |
| 487 |
|
| 488 |
If you plan to parse PostgreSQL CSV log files, you might need some Perl |
| 489 |
Modules: |
| 490 |
|
| 491 |
Text::CSV_XS - to parse PostgreSQL CSV log files. |
| 492 |
|
| 493 |
This module is optional, if you don't have PostgreSQL log in the CSV |
| 494 |
format, you don't need to install it. |
| 495 |
|
| 496 |
If you want to export statistics as JSON file, you need an additional |
| 497 |
Perl module: |
| 498 |
|
| 499 |
JSON::XS - JSON serialising/deserialising, done correctly and fast |
| 500 |
|
| 501 |
This module is optional, if you don't select the json output format, you |
| 502 |
don't need to install it. You can install it on a Debian-like system |
| 503 |
using: |
| 504 |
|
| 505 |
sudo apt-get install libjson-xs-perl |
| 506 |
|
| 507 |
and on RPM-like system using: |
| 508 |
|
| 509 |
sudo yum install perl-JSON-XS |
| 510 |
|
| 511 |
Compressed log file format is autodetected from the file extension. If |
| 512 |
pgBadger finds a gz extension, it will use the zcat utility; with a bz2 |
| 513 |
extension, it will use bzcat; with lz4, it will use lz4cat; with zst, it |
| 514 |
will use zstdcat; if the file extension is zip or xz, then the unzip or |
| 515 |
xz utility will be used. |
| 516 |
|
| 517 |
If those utilities are not found in the PATH environment variable, then |
| 518 |
use the --zcat command-line option to change this path. For example: |
| 519 |
|
| 520 |
--zcat="/usr/local/bin/gunzip -c" or --zcat="/usr/local/bin/bzip2 -dc" |
| 521 |
--zcat="C:\tools\unzip -p" |
| 522 |
|
| 523 |
By default, pgBadger will use the zcat, bzcat, lz4cat, zstdcat and unzip |
| 524 |
utilities following the file extension. If you use the default |
| 525 |
autodetection of compression format, you can mix gz, bz2, lz4, xz, zip |
| 526 |
or zstd files. Specifying a custom value of --zcat option will remove |
| 527 |
the possibility of mixed compression format. |
| 528 |
|
| 529 |
Note that multiprocessing cannot be used with compressed files or CSV |
| 530 |
files as well as under Windows platform. |
| 531 |
|
| 532 |
INSTALLATION |
| 533 |
Download the tarball from GitHub and unpack the archive as follow: |
| 534 |
|
| 535 |
tar xzf pgbadger-11.x.tar.gz |
| 536 |
cd pgbadger-11.x/ |
| 537 |
perl Makefile.PL |
| 538 |
make && sudo make install |
| 539 |
|
| 540 |
This will copy the Perl script pgbadger to /usr/local/bin/pgbadger by |
| 541 |
default and the man page into /usr/local/share/man/man1/pgbadger.1. |
| 542 |
Those are the default installation directories for 'site' install. |
| 543 |
|
| 544 |
If you want to install all under /usr/ location, use INSTALLDIRS='perl' |
| 545 |
as an argument of Makefile.PL. The script will be installed into |
| 546 |
/usr/bin/pgbadger and the manpage into /usr/share/man/man1/pgbadger.1. |
| 547 |
|
| 548 |
For example, to install everything just like Debian does, proceed as |
| 549 |
follows: |
| 550 |
|
| 551 |
perl Makefile.PL INSTALLDIRS=vendor |
| 552 |
|
| 553 |
By default, INSTALLDIRS is set to site. |
| 554 |
|
| 555 |
POSTGRESQL CONFIGURATION |
| 556 |
You must enable and set some configuration directives in your |
| 557 |
postgresql.conf before starting. |
| 558 |
|
| 559 |
You must first enable SQL query logging to have something to parse: |
| 560 |
|
| 561 |
log_min_duration_statement = 0 |
| 562 |
|
| 563 |
Here every statement will be logged, on a busy server you may want to |
| 564 |
increase this value to only log queries with a longer duration. Note |
| 565 |
that if you have log_statement set to 'all', nothing will be logged |
| 566 |
through the log_min_duration_statement directive. See the next chapter |
| 567 |
for more information. |
| 568 |
|
| 569 |
pgBadger supports any custom format set in the log_line_prefix directive |
| 570 |
of your postgresql.conf file as long as it at least specifies a time |
| 571 |
escape sequence (%t, %m or %n) and a process-related escape sequence (%p |
| 572 |
or %c). |
| 573 |
|
| 574 |
For example, with 'stderr' log format, log_line_prefix must be at least: |
| 575 |
|
| 576 |
log_line_prefix = '%t [%p]: ' |
| 577 |
|
| 578 |
Log line prefix could add user, database name, application name and |
| 579 |
client ip address as follows: |
| 580 |
|
| 581 |
log_line_prefix = '%t [%p]: user=%u,db=%d,app=%a,client=%h ' |
| 582 |
|
| 583 |
or for syslog log file format: |
| 584 |
|
| 585 |
log_line_prefix = 'user=%u,db=%d,app=%a,client=%h ' |
| 586 |
|
| 587 |
Log line prefix for stderr output could also be: |
| 588 |
|
| 589 |
log_line_prefix = '%t [%p]: db=%d,user=%u,app=%a,client=%h ' |
| 590 |
|
| 591 |
or for syslog output: |
| 592 |
|
| 593 |
log_line_prefix = 'db=%d,user=%u,app=%a,client=%h ' |
| 594 |
|
| 595 |
You need to enable other parameters in postgresql.conf to get more |
| 596 |
information from your log files: |
| 597 |
|
| 598 |
log_checkpoints = on |
| 599 |
log_connections = on |
| 600 |
log_disconnections = on |
| 601 |
log_lock_waits = on |
| 602 |
log_temp_files = 0 |
| 603 |
log_autovacuum_min_duration = 0 |
| 604 |
log_error_verbosity = default |
| 605 |
|
| 606 |
Do not enable log_statement as its log format will not be parsed by |
| 607 |
pgBadger. |
| 608 |
|
| 609 |
Of course your log messages should be in English with or without locale |
| 610 |
support: |
| 611 |
|
| 612 |
lc_messages='en_US.UTF-8' |
| 613 |
lc_messages='C' |
| 614 |
|
| 615 |
pgBadger parser does not support other locales, like 'fr_FR.UTF-8' for |
| 616 |
example. |
| 617 |
|
| 618 |
LOG STATEMENTS |
| 619 |
Considerations about log_min_duration_statement, log_duration and |
| 620 |
log_statement configuration directives. |
| 621 |
|
| 622 |
If you want the query statistics to include the actual query strings, |
| 623 |
you must set log_min_duration_statement to 0 or more milliseconds. |
| 624 |
|
| 625 |
If you just want to report duration and number of queries and don't want |
| 626 |
all details about queries, set log_min_duration_statement to -1 to |
| 627 |
disable it and enable log_duration in your postgresql.conf file. If you |
| 628 |
want to add the most common query report, you can either choose to set |
| 629 |
log_min_duration_statement to a higher value or to enable log_statement. |
| 630 |
|
| 631 |
Enabling log_min_duration_statement will add reports about slowest |
| 632 |
queries and queries that took up the most time. Take care that if you |
| 633 |
have log_statement set to 'all', nothing will be logged with |
| 634 |
log_min_duration_statement. |
| 635 |
|
| 636 |
Warning: Do not enable both log_min_duration_statement, log_duration and |
| 637 |
log_statement all together, this will result in wrong counter values. |
| 638 |
Note that this will also increase drastically the size of your log. |
| 639 |
log_min_duration_statement should always be preferred. |
| 640 |
|
| 641 |
PARALLEL PROCESSING |
| 642 |
To enable parallel processing you just have to use the -j N option where |
| 643 |
N is the number of cores you want to use. |
| 644 |
|
| 645 |
pgBadger will then proceed as follow: |
| 646 |
|
| 647 |
for each log file |
| 648 |
chunk size = int(file size / N) |
| 649 |
look at start/end offsets of these chunks |
| 650 |
fork N processes and seek to the start offset of each chunk |
| 651 |
each process will terminate when the parser reach the end offset |
| 652 |
of its chunk |
| 653 |
each process write stats into a binary temporary file |
| 654 |
wait for all children processes to terminate |
| 655 |
All binary temporary files generated will then be read and loaded into |
| 656 |
memory to build the html output. |
| 657 |
|
| 658 |
With that method, at start/end of chunks pgBadger may truncate or omit a |
| 659 |
maximum of N queries per log file, which is an insignificant gap if you |
| 660 |
have millions of queries in your log file. The chance that the query |
| 661 |
that you were looking for is lost is near 0, this is why I think this |
| 662 |
gap is livable. Most of the time the query is counted twice but |
| 663 |
truncated. |
| 664 |
|
| 665 |
When you have many small log files and many CPUs, it is speedier to |
| 666 |
dedicate one core to one log file at a time. To enable this behavior, |
| 667 |
you have to use option -J N instead. With 200 log files of 10MB each, |
| 668 |
the use of the -J option starts being really interesting with 8 cores. |
| 669 |
Using this method you will be sure not to lose any queries in the |
| 670 |
reports. |
| 671 |
|
| 672 |
Here is a benchmark done on a server with 8 CPUs and a single file of |
| 673 |
9.5GB. |
| 674 |
|
| 675 |
Option | 1 CPU | 2 CPU | 4 CPU | 8 CPU |
| 676 |
--------+---------+-------+-------+------ |
| 677 |
-j | 1h41m18 | 50m25 | 25m39 | 15m58 |
| 678 |
-J | 1h41m18 | 54m28 | 41m16 | 34m45 |
| 679 |
|
| 680 |
With 200 log files of 10MB each, so 2GB in total, the results are |
| 681 |
slightly different: |
| 682 |
|
| 683 |
Option | 1 CPU | 2 CPU | 4 CPU | 8 CPU |
| 684 |
--------+-------+-------+-------+------ |
| 685 |
-j | 20m15 | 9m56 | 5m20 | 4m20 |
| 686 |
-J | 20m15 | 9m49 | 5m00 | 2m40 |
| 687 |
|
| 688 |
So it is recommended to use -j unless you have hundreds of small log |
| 689 |
files and can use at least 8 CPUs. |
| 690 |
|
| 691 |
IMPORTANT: when you are using parallel parsing, pgBadger will generate a |
| 692 |
lot of temporary files in the /tmp directory and will remove them at the |
| 693 |
end, so do not remove those files unless pgBadger is not running. They |
| 694 |
are all named with the following template tmp_pgbadgerXXXX.bin so they |
| 695 |
can be easily identified. |
| 696 |
|
| 697 |
INCREMENTAL REPORTS |
| 698 |
pgBadger includes an automatic incremental report mode using option -I |
| 699 |
or --incremental. When running in this mode, pgBadger will generate one |
| 700 |
report per day and a cumulative report per week. Output is first done in |
| 701 |
binary format into the mandatory output directory (see option -O or |
| 702 |
--outdir), then in HTML format for daily and weekly reports with a main |
| 703 |
index file. |
| 704 |
|
| 705 |
The main index file will show a dropdown menu per week with a link to |
| 706 |
each week report and links to daily reports of each week. |
| 707 |
|
| 708 |
For example, if you run pgBadger as follows based on a daily rotated |
| 709 |
file: |
| 710 |
|
| 711 |
0 4 * * * /usr/bin/pgbadger -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/ |
| 712 |
|
| 713 |
you will have all daily and weekly reports for the full running period. |
| 714 |
|
| 715 |
In this mode, pgBadger will create an automatic incremental file in the |
| 716 |
output directory, so you don't have to use the -l option unless you want |
| 717 |
to change the path of that file. This means that you can run pgBadger in |
| 718 |
this mode each day on a log file rotated each week, and it will not |
| 719 |
count the log entries twice. |
| 720 |
|
| 721 |
To save disk space, you may want to use the -X or --extra-files |
| 722 |
command-line option to force pgBadger to write JavaScript and CSS to |
| 723 |
separate files in the output directory. The resources will then be |
| 724 |
loaded using script and link tags. |
| 725 |
|
| 726 |
Rebuilding reports |
| 727 |
Incremental reports can be rebuilt after a pgbadger report fix or a new |
| 728 |
feature to update all HTML reports. To rebuild all reports where a |
| 729 |
binary file is still present, proceed as follow: |
| 730 |
|
| 731 |
rm /path/to/reports/*.js |
| 732 |
rm /path/to/reports/*.css |
| 733 |
pgbadger -X -I -O /path/to/reports/ --rebuild |
| 734 |
|
| 735 |
it will also update all resource files (JS and CSS). Use -E or --explode |
| 736 |
if the reports were built using this option. |
| 737 |
|
| 738 |
Monthly reports |
| 739 |
By default, pgBadger in incremental mode only computes daily and weekly |
| 740 |
reports. If you want monthly cumulative reports, you will have to use a |
| 741 |
separate command to specify the report to build. For example, to build a |
| 742 |
report for August 2019: |
| 743 |
|
| 744 |
pgbadger -X --month-report 2019-08 /var/www/pg_reports/ |
| 745 |
|
| 746 |
this will add a link to the month name into the calendar view of |
| 747 |
incremental reports to look at monthly report. The report for a current |
| 748 |
month can be run every day, it is entirely rebuilt each time. The |
| 749 |
monthly report is not built by default because it could take a lot of |
| 750 |
time following the amount of data. |
| 751 |
|
| 752 |
If reports were built with the per-database option ( -E | --explode ), |
| 753 |
it must be used too when calling pgbadger to build monthly report: |
| 754 |
|
| 755 |
pgbadger -E -X --month-report 2019-08 /var/www/pg_reports/ |
| 756 |
|
| 757 |
This is the same when using the rebuild option ( -R | --rebuild ). |
| 758 |
|
| 759 |
BINARY FORMAT |
| 760 |
Using the binary format it is possible to create custom incremental and |
| 761 |
cumulative reports. For example, if you want to refresh a pgBadger |
| 762 |
report each hour from a daily PostgreSQL log file, you can proceed by |
| 763 |
running the following commands each hour: |
| 764 |
|
| 765 |
pgbadger --last-parsed .pgbadger_last_state_file -o sunday/hourX.bin /var/log/pgsql/postgresql-Sun.log |
| 766 |
|
| 767 |
to generate the incremental data files in binary format. And to generate |
| 768 |
the fresh HTML report from that binary file: |
| 769 |
|
| 770 |
pgbadger sunday/*.bin |
| 771 |
|
| 772 |
Or as another example, if you generate one log file per hour and you |
| 773 |
want reports to be rebuilt each time the log file is rotated, proceed as |
| 774 |
follows: |
| 775 |
|
| 776 |
pgbadger -o day1/hour01.bin /var/log/pgsql/pglog/postgresql-2012-03-23_10.log |
| 777 |
pgbadger -o day1/hour02.bin /var/log/pgsql/pglog/postgresql-2012-03-23_11.log |
| 778 |
pgbadger -o day1/hour03.bin /var/log/pgsql/pglog/postgresql-2012-03-23_12.log |
| 779 |
... |
| 780 |
|
| 781 |
When you want to refresh the HTML report, for example, each time after a |
| 782 |
new binary file is generated, just do the following: |
| 783 |
|
| 784 |
pgbadger -o day1_report.html day1/*.bin |
| 785 |
|
| 786 |
Adjust the commands to suit your particular needs. |
| 787 |
|
| 788 |
JSON FORMAT |
| 789 |
JSON format is good for sharing data with other languages, which makes |
| 790 |
it easy to integrate pgBadger result into other monitoring tools, like |
| 791 |
Cacti or Graphite. |
| 792 |
|
| 793 |
AUTHORS |
| 794 |
pgBadger is an original work from Gilles Darold. |
| 795 |
|
| 796 |
The pgBadger logo is an original creation of Damien Cazeils. |
| 797 |
|
| 798 |
The pgBadger v4.x design comes from the "Art is code" company. |
| 799 |
|
| 800 |
This web site is a work of Gilles Darold. |
| 801 |
|
| 802 |
pgBadger is maintained by Gilles Darold and everyone who wants to |
| 803 |
contribute. |
| 804 |
|
| 805 |
Many people have contributed to pgBadger, they are all quoted in the |
| 806 |
Changelog file. |
| 807 |
|
| 808 |
LICENSE |
| 809 |
pgBadger is free software distributed under the PostgreSQL Licence. |
| 810 |
|
| 811 |
Copyright (c) 2012-2026, Gilles Darold |
| 812 |
|
| 813 |
A modified version of the SQL::Beautify Perl Module is embedded in |
| 814 |
pgBadger with copyright (C) 2009 by Jonas Kramer and is published under |
| 815 |
the terms of the Artistic License 2.0. |
| 816 |
|