darold / pgbadger

A fast PostgreSQL Log Analyzer
http://pgbadger.darold.net/
PostgreSQL License
3.51k stars 349 forks source link

PgBadger did not detect new lines in log file #726

Closed aleszeleny closed 2 years ago

aleszeleny commented 2 years ago

Hello, it looks pgbadger is unable to properly detect new log entries in the active log. Once the log is rotated, the content is parsed.

7905 /var/postgresdata/13/data/log/postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 383521 in file /var/postgresdata/pgb_reports/LAST_PARSED
DEBUG: this file has already been parsed: /var/postgresdata/13/data/log/postgresql.log, there are no new lines in this file
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: the log statistics gathering took: 0 wallclock secs ( 0.25 usr +  0.00 sys =  0.25 CPU)
DEBUG: no new entries in your log(s) since last run.
DEBUG: building reports took:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)
DEBUG: the total execution time took:  0 wallclock secs ( 0.26 usr +  0.00 sys =  0.26 CPU)

And a subsequent run a while later - as we can see from the wc output, there are new lines (logon/logoff almost every second), but pgBadger did not realize that:

-bash-4.2$ wc -l /var/postgresdata/13/data/log/postgresql.log; pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /var/postgresdata/pgb_reports /var/postgresdata/13/data/log/postgresql.log
7980 /var/postgresdata/13/data/log/postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 383521 in file /var/postgresdata/pgb_reports/LAST_PARSED
DEBUG: this file has already been parsed: /var/postgresdata/13/data/log/postgresql.log, there are no new lines in this file
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: the log statistics gathering took: 0 wallclock secs ( 0.23 usr +  0.00 sys =  0.23 CPU)
DEBUG: no new entries in your log(s) since last run.
DEBUG: building reports took:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: the total execution time took:  0 wallclock secs ( 0.23 usr +  0.00 sys =  0.23 CPU)

Behaves same way with RPM version 11.7 and with the latest commit available at time of testing:

commit 6a4750b35a49ed2f9153315a3642aea3c27db556
Author: Gilles Darold <gilles@migops.com>
Date:   Sat Mar 26 10:08:39 2022 +0100

    Fix detection of new log entries in remote files through ssh. Thanks to Luca Ferrari for the report

Sample log file entries:

2022-03-29 15:48:22.926 CEST, 21865, 62430e26.5569, 2, u_app, cfgdb, 192.168.12.93(37603), [unknown], 0, 4/6063734, 00000, authentication: LOG:  connection authorized: user=u_app database=cfgdb
2022-03-29 15:48:23.030 CEST, 21866, 62430e27.556a, 1, [unknown], [unknown], 192.168.12.93(37604), [unknown], 0, , 00000, : LOG:  connection received: host=192.168.12.93 port=37604
2022-03-29 15:48:23.032 CEST, 21866, 62430e27.556a, 2, u_app, cfgdb, 192.168.12.93(37604), [unknown], 0, 5/9448631, 00000, authentication: LOG:  connection authorized: user=u_app database=cfgdb
2022-03-29 15:48:23.066 CEST, 21869, 62430e27.556d, 1, [unknown], [unknown], 192.168.12.93(37605), [unknown], 0, , 00000, : LOG:  connection received: host=192.168.12.93 port=37605
2022-03-29 15:48:23.067 CEST, 21869, 62430e27.556d, 2, u_app, live, 192.168.12.93(37605), [unknown], 0, 6/3802407, 00000, authentication: LOG:  connection authorized: user=u_app database=live
...
2022-03-29 15:48:26.563 CEST, 21913, 62430e2a.5599, 1, , , , , 0, 6/3802498, 00000, : LOG:  automatic vacuum of table "live.pg_catalog.pg_type": index scans: 1
        pages: 0 removed, 40 remain, 0 skipped due to pins, 0 skipped frozen
        tuples: 172 removed, 1152 remain, 0 are dead but not yet removable, oldest xmin: 4895478
        buffer usage: 219 hits, 0 misses, 0 dirtied
        avg read rate: 0.000 MB/s, avg write rate: 0.000 MB/s
        system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
        WAL usage: 97 records, 0 full page images, 10235 bytes
...
2022-03-29 15:48:26.909 CEST, 2472, 623c67ad.9a8, 7955, u_app, live, 127.0.0.1(53303), LiveDataChanger RECALC_20, 0, 38/443802, 00000, PARSE: LOG:  duration: 1.395 ms  parse py:0x7f673c02c590: SELECT * FROM api.set_recalc_is_running($1, $2, $3)
aleszeleny commented 2 years ago

I did a test case:

Several times. From the user's perspective, it looks non-deterministic, since the first round detected new lines and parse them, while the same repeated once more claims there are no new log lines to be parsed. So there must be a difference, but I haven't revealed it yet.

Create a clean output directory:

-bash-4.2$ rm -rf reports/
-bash-4.2$ mkdir reports

Copy Postgres log file: -bash-4.2$ cp /var/postgresdata/13/data/log/postgresql.log /var/postgresdata/13/data/log/postgresql-test.log

Run pgBadger in incremental mode:

-bash-4.2$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /var/postgresdata/reports /var/postgresdata/13/data/log/postgresql-test.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: pgBadger will use log format stderr to parse /var/postgresdata/13/data/log/postgresql-test.log.
DEBUG: Starting progressbar writer process
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Processing log file: /var/postgresdata/13/data/log/postgresql-test.log
DEBUG: Starting reading file "/var/postgresdata/13/data/log/postgresql-test.log"...
DEBUG: Start parsing postgresql log at offset 0 of file "/var/postgresdata/13/data/log/postgresql-test.log" to 7102089
DEBUG: setting current position in log to 7102089of 7102089 (82.80%), queries: 770, events: 61
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-03-29 16:18:03s: 7102089)nts: 69
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED

DEBUG: Saving last parsed line into /var/postgresdata/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 4 wallclock secs ( 0.00 usr  0.00 sys +  2.92 cusr  0.01 csys =  2.93 CPU)
DEBUG: Building incremental report for 2022-03-29
LOG: Ok, generating HTML daily report into /var/postgresdata/reports/2022/03/29/...
LOG: Ok, generating HTML weekly report into /var/postgresdata/reports/2022/week-14/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /var/postgresdata/reports/index.html
DEBUG: building reports took:  2 wallclock secs ( 1.85 usr +  0.01 sys =  1.86 CPU)
DEBUG: the total execution time took:  6 wallclock secs ( 1.85 usr  0.01 sys +  2.92 cusr  0.01 csys =  4.79 CPU)

It processed the log file smoothly.

Backup a copy of the parsed file and make sure that the new working file is not same as the copy:

-bash-4.2$ cp /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.1
-bash-4.2$ cp /var/postgresdata/13/data/log/postgresql.log /var/postgresdata/13/data/log/postgresql-test.log
-bash-4.2$ diff -q /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.1 
Files /var/postgresdata/13/data/log/postgresql-test.log and /var/postgresdata/13/data/log/postgresql-test.log.1 differ

Run pgBadger in incremental mode:

-bash-4.2$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /var/postgresdata/reports /var/postgresdata/13/data/log/postgresql-test.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 7102089 in file /var/postgresdata/reports/LAST_PARSED
DEBUG: pgBadger will use log format stderr to parse /var/postgresdata/13/data/log/postgresql-test.log.
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Starting progressbar writer process
DEBUG: Processing log file: /var/postgresdata/13/data/log/postgresql-test.log
DEBUG: This log might not be parsed: /var/postgresdata/13/data/log/postgresql-test.log, there are no new lines in this file
DEBUG: Starting reading file "/var/postgresdata/13/data/log/postgresql-test.log"...
DEBUG: Start parsing postgresql log at offset 7102089 of file "/var/postgresdata/13/data/log/postgresql-test.log" to 7331890
DEBUG: setting current position in log to 7331890of 7331890 (99.53%), queries: 25, events: 2
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-03-29 16:19:01s: 7331890)ts: 2
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED

DEBUG: Saving last parsed line into /var/postgresdata/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.02 usr  0.01 sys +  0.15 cusr  0.00 csys =  0.18 CPU)
DEBUG: Building incremental report for 2022-03-29
LOG: Ok, generating HTML daily report into /var/postgresdata/reports/2022/03/29/...
LOG: Ok, generating HTML weekly report into /var/postgresdata/reports/2022/week-14/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /var/postgresdata/reports/index.html
DEBUG: building reports took:  1 wallclock secs ( 1.56 usr +  0.02 sys =  1.58 CPU)
DEBUG: the total execution time took:  3 wallclock secs ( 1.58 usr  0.03 sys +  0.15 cusr  0.00 csys =  1.76 CPU)
``` This time, new lines were detected and parsed.

Repeat the test again (including the last working file backup):

-bash-4.2$ cp /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.2 -bash-4.2$ cp /var/postgresdata/13/data/log/postgresql.log /var/postgresdata/13/data/log/postgresql-test.log -bash-4.2$ diff -q /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.2 Files /var/postgresdata/13/data/log/postgresql-test.log and /var/postgresdata/13/data/log/postgresql-test.log.2 differ

And run pgBadger:
```-bash-4.2$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /var/postgresdata/reports /var/postgresdata/13/data/log/postgresql-test.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 7331890 in file /var/postgresdata/reports/LAST_PARSED
DEBUG: this file has already been parsed: /var/postgresdata/13/data/log/postgresql-test.log, there are no new lines in this file
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: the log statistics gathering took: 1 wallclock secs ( 0.78 usr +  0.00 sys =  0.78 CPU)
DEBUG: no new entries in your log(s) since last run.
DEBUG: building reports took:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: the total execution time took:  1 wallclock secs ( 0.78 usr +  0.00 sys =  0.78 CPU)

(!) This time, pgbadger did not detect the new log lines.

The last_parsed file:

-bash-4.2$ cat  /var/postgresdata/reports/LAST_PARSED
2022-03-29 16:19:01 7331890 2022-03-29 16:19:01.433 CEST, 50632, 62431555.c5c8, 2, u_app, live, 192.168.12.93(50696), [unknown], 0, 7/2827694, 00000, authentication: LOG:  connection authorized: user=u_app database=live

Double-check, there are new lines:

-bash-4.2$ wc -l /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.2
   39332 /var/postgresdata/13/data/log/postgresql-test.log
   37370 /var/postgresdata/13/data/log/postgresql-test.log.2

The postgresql-test.log.2 file (backup before copying the latest log into a test working file) has fewer lines, so there were new lines to be parsed.

and a diff:

-bash-4.2$ diff  /var/postgresdata/13/data/log/postgresql-test.log /var/postgresdata/13/data/log/postgresql-test.log.2 | head
37371,39332d37370
< 2022-03-29 16:19:01.475 CEST, 50629, 62431555.c5c5, 3, u_app, cfgdb, 192.168.12.93(50694), [unknown], 0, , 00000, idle: LOG:  disconnection: session time: 0:00:00.216 user=u_app database=cfgdb host=192.168.12.93 port=50694
< 2022-03-29 16:19:01.475 CEST, 50632, 62431555.c5c8, 3, u_app, live, 192.168.12.93(50696), [unknown], 0, , 00000, idle: LOG:  disconnection: session time: 0:00:00.044 user=u_app database=live host=192.168.12.93 port=50696
< 2022-03-29 16:19:01.540 CEST, 50633, 62431555.c5c9, 1, [unknown], [unknown], 192.168.12.93(50697), [unknown], 0, , 00000, : LOG:  connection received: host=192.168.12.93 port=50697
< 2022-03-29 16:19:01.541 CEST, 50633, 62431555.c5c9, 2, u_app, cfgdb, 192.168.12.93(50697), [unknown], 0, 5/9508614, 00000, authentication: LOG:  connection authorized: user=u_app database=cfgdb
< 2022-03-29 16:19:01.673 CEST, 50634, 62431555.c5ca, 1, [unknown], [unknown], 192.168.12.93(50698), [unknown], 0, , 00000, : LOG:  connection received: host=192.168.12.93 port=50698
< 2022-03-29 16:19:01.674 CEST, 50634, 62431555.c5ca, 2, u_app, live, 192.168.12.93(50698), [unknown], 0, 7/2827717, 00000, authentication: LOG:  connection authorized: user=u_app database=live
< 2022-03-29 16:19:01.718 CEST, 50634, 62431555.c5ca, 3, u_app, live, 192.168.12.93(50698), [unknown], 0, , 00000, idle: LOG:  disconnection: session time: 0:00:00.046 user=u_app database=live host=192.168.12.93 port=50698
< 2022-03-29 16:19:01.718 CEST, 50633, 62431555.c5c9, 3, u_app, cfgdb, 192.168.12.93(50697), [unknown], 0, , 00000, idle: LOG:  disconnection: session time: 0:00:00.179 user=u_app database=cfgdb host=192.168.12.93 port=50697
< 2022-03-29 16:19:01.809 CEST, 50635, 62431555.c5cb, 1, [unknown], [unknown], 192.168.12.93(50699), [unknown], 0, , 00000, : LOG:  connection received: host=192.168.12.93 port=50699

PostgreSQL instance is 13, logging configuration:

log_destination = 'stderr'      # Valid values are combinations of
                    # stderr, csvlog, syslog, and eventlog,
                    # depending on platform.  csvlog
                    # requires logging_collector to be on.
# This is used when logging to stderr:
logging_collector = on          # Enable capturing of stderr and csvlog
                    # into log files. Required to be on for
                    # csvlogs.
# These are only used if logging_collector is on:
log_directory = 'log'           # directory where log files are written,
log_filename = 'postgresql.log' # log file name pattern,
#log_file_mode = 0600           # creation mode for log files,
log_truncate_on_rotation = on       # If on, an existing log file with the
                    # same name as the new log file will be
#log_rotation_age = 1d          # Automatic rotation of logfiles will
log_rotation_size = 0           # Automatic rotation of logfiles will
                    # happen after that much log output.
log_min_duration_statement = 500    # -1 is disabled, 0 logs all statements
                    # and their durations, > 0 logs only
log_min_duration_sample = 1     # -1 is disabled, 0 logs a sample of statements
                    # and their durations, > 0 logs only a sample of
                    # sample fraction is determined by log_statement_sample_rate
log_statement_sample_rate = 0.01    # fraction of logged statements exceeding
                    # log_min_duration_sample to be logged;
                    # 1.0 logs all such statements, 0.0 never logs
log_checkpoints = on
log_connections = on
log_disconnections = on
log_line_prefix = '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: '        # special values:
log_lock_waits = on         # log lock waits >= deadlock_timeout
log_statement = 'ddl'           # none, ddl, mod, all
log_temp_files = 0          # log temporary files equal or larger
                    # -1 disables, 0 logs all temp files
log_timezone = 'Europe/Prague'
log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and
gilles-migops commented 2 years ago

Thanks a lot for the detailed report, I was able to reproduce the issue. Commit b88e133 might fix it.

aleszeleny commented 2 years ago

Hello, thanks for the super fast response!

To me, it still does not work, so I've extracted sample log files to have a testcase. I did a test using RPM package and 19898eb (one commit later after b88e133):

zelenya@alesnbbs:~/var/pgBadger/work/pgbadger$ git branch -v
* master 19898eb Merge pull request #727 from anayrat/typo

I prepared two log files (just copied the server log file 2 times), they have identical lines from the beginning and the second one have some more log entries.

Testcase using apt package 11.7:

zelenya@alesnbbs:~/var/pgBadger/work$ ll
total 140
drwxrwxr-x  4 zelenya zelenya  4096 bře 30 09:29 ./
drwxrwxr-x 24 zelenya zelenya  4096 bře 30 09:28 ../
-rw-r-----  1 zelenya zelenya 40584 bře 30 09:17 postgresql-test-src1.log
-rw-r-----  1 zelenya zelenya 84704 bře 30 09:20 postgresql-test-src2.log
drwxrwxr-x  2 zelenya zelenya  4096 bře 30 09:29 rep2/

First log:

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/rep2 postgresql-test-src1.log 
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: pgBadger will use log format stderr to parse postgresql-test-src1.log.
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Starting progressbar writer process
DEBUG: Processing log file: postgresql-test-src1.log
DEBUG: Starting reading file "postgresql-test-src1.log"...
DEBUG: Start parsing postgresql log at offset 0 of file "postgresql-test-src1.log" to 40584
DEBUG: setting current position in log to 40584of 40584 (37.20%), queries: 1, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-03-30 09:08:53, e40584) 0
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED

DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/rep2/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: Building incremental report for 2022-03-30
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/rep2/2022/03/30/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/rep2/2022/week-14/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/rep2/index.html
DEBUG: building reports took:  0 wallclock secs ( 0.13 usr +  0.02 sys =  0.15 CPU)
DEBUG: the total execution time took:  2 wallclock secs ( 0.13 usr +  0.02 sys =  0.15 CPU)

and subsequent run on "longer" log file:

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/rep2 postgresql-test-src2.log 
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 40584 in file /home/zelenya/var/pgBadger/work/rep2/LAST_PARSED
DEBUG: this file has already been parsed: postgresql-test-src2.log, there no new lines in this file
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: the log statistics gathering took: 0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)
DEBUG: no new entries in your log(s) since last run.
DEBUG: building reports took:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: the total execution time took:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)

Testcase using freshly cloned repository:

zelenya@alesnbbs:~/var/pgBadger/work$ git clone https://github.com/darold/pgbadger.git
Cloning into 'pgbadger'...
remote: Enumerating objects: 4633, done.
remote: Counting objects: 100% (321/321), done.
remote: Compressing objects: 100% (216/216), done.
remote: Total 4633 (delta 195), reused 197 (delta 96), pack-reused 4312
Receiving objects: 100% (4633/4633), 11.23 MiB | 8.23 MiB/s, done.
Resolving deltas: 100% (2859/2859), done.

Parse the log file:

zelenya@alesnbbs:~/var/pgBadger/work$ mkdir rep3
zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/rep3 postgresql-test-src1.log 
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: pgBadger will use log format stderr to parse postgresql-test-src1.log.
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Starting progressbar writer process
DEBUG: Processing log file: postgresql-test-src1.log
DEBUG: Starting reading file "postgresql-test-src1.log"...
DEBUG: Start parsing postgresql log at offset 0 of file "postgresql-test-src1.log" to 40584
DEBUG: setting current position in log to 40584of 40584 (37.20%), queries: 1, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-03-30 09:08:53, e40584) 0

DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/rep3/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.01 usr  0.00 sys +  0.01 cusr  0.00 csys =  0.02 CPU)
DEBUG: Building incremental report for 2022-03-30
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/rep3/2022/03/30/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/rep3/2022/week-14/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/rep3/index.html
DEBUG: building reports took:  0 wallclock secs ( 0.12 usr +  0.02 sys =  0.14 CPU)
DEBUG: the total execution time took:  2 wallclock secs ( 0.13 usr  0.02 sys +  0.01 cusr  0.00 csys =  0.16 CPU)

And subsequent incremental run:

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/rep3 postgresql-test-src2.log 
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 40584 in file /home/zelenya/var/pgBadger/work/rep3/LAST_PARSED
DEBUG: this file has already been parsed: postgresql-test-src2.log, there are no new lines in this file
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: the log statistics gathering took: 0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)
DEBUG: no new entries in your log(s) since last run.
DEBUG: building reports took:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: the total execution time took:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)

Testcase log files are attached: postgresql-test-src1.zip postgresql-test-src2.zip

I hope it is enough for a reproducible test case.

P.S.: While reviewing the generated report, I've realized that some application names like "LiveAPI RECALC_13" were not parsed - I haven't found them on queries by application. P.P.S: I don't have a test case (yet), but once backend type is added to logline prefix, it seems not to be able to parse stderr formated log file, but it might be side-effect of the incremental issue, so postponing this test for a later time.

darold commented 2 years ago

Very helpful, this was an other problem occurring only with timestamp with milliseconds or epoch. Commit 61c8b18 might fix the issue now.

aleszeleny commented 2 years ago

Superfast fix!!! I can confirm it works, thanks a lot.

The application name is not parsed (except [unknown]). Shall I attach the same sample log file to a new issue focused on this?

darold commented 2 years ago

Ah yes I wanted to have a look to this, thanks for the remainder. Just keeping this issue open is enough.

darold commented 2 years ago

Hi, actually the problem is not only related to application name but you may have a global wrong report of queries. Commit 71748e4 fixes this issue. The problem was related to the name of the prepared statements, for example: execute py:0x7f6754010ad0:, the presence of the first : in this name was not expected and was breaking the parsing. The log lines was declared as an unknown format and skipped.

aleszeleny commented 2 years ago

Thanks a lot, I can confirm that paring works and yes global report looks now way more reliable.

darold commented 2 years ago

@aleszeleny please give a try to latest commit 44f0be5. I have done a full review of the log file detection so please confirm that is not break your reports. I have tested with your sample log file and it is working well but just to be sure.

aleszeleny commented 2 years ago

Sure, I've pulled the commit just now, so subsequent pgb runs will use that new version. Sometime in the past, I thought having a problem with "backend type" in log line prefix, but it could be related to the timestamp issue (testing this will take a bit more time - I have to prepare an environment for that).

aleszeleny commented 2 years ago

Hello, I've run test and it seem to work almost correctly, except for sessions by application wehere application (graph nor table) are not properly detected.

Testcase file: postgresql.log.src.zip

Tests:

sed -n '1,100p' postgresql.log.src > postgresql.log

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/reports postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: pgBadger will use log format stderr to parse postgresql.log.
DEBUG: Starting progressbar writer process
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Processing log file: postgresql.log
DEBUG: Starting reading file "postgresql.log"...
DEBUG: Start parsing postgresql log at offset 0 of file "postgresql.log" to 19834
DEBUG: setting current position in log to 19834of 19834 (9.80%), queries: 0, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-04-08 03:36:18, e19834) 0

DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED
DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.00 usr  0.01 sys +  0.01 cusr  0.00 csys =  0.02 CPU)
DEBUG: Building incremental report for 2022-04-08
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/reports/2022/04/08/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/reports/2022/week-15/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/reports/index.html
DEBUG: building reports took:  0 wallclock secs ( 0.04 usr +  0.00 sys =  0.04 CPU)
DEBUG: the total execution time took:  2 wallclock secs ( 0.04 usr  0.01 sys +  0.01 cusr  0.00 csys =  0.06 CPU)

sed -n '101,200p' postgresql.log.src >> postgresql.log

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/reports postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 19834 in file /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: pgBadger will use log format stderr to parse postgresql.log.
DEBUG: Starting progressbar writer process
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Processing log file: postgresql.log
DEBUG: Starting reading file "postgresql.log"...f 39962 (0.00%), queries: 0, events: 0
DEBUG: Start parsing postgresql log at offset 19834 of file "postgresql.log" to 39962
DEBUG: setting current position in log to 39962of 39962 (54.49%), queries: 0, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-04-08 03:36:29, e39962) 0
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSEDqueries: 3, events: 0

DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
DEBUG: Building incremental report for 2022-04-08
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/reports/2022/04/08/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/reports/2022/week-15/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/reports/index.html
DEBUG: building reports took:  0 wallclock secs ( 0.12 usr +  0.00 sys =  0.12 CPU)
DEBUG: the total execution time took:  2 wallclock secs ( 0.12 usr +  0.00 sys =  0.12 CPU)

sed -n '201,500p' postgresql.log.src >> postgresql.log

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/reports postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 39962 in file /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: pgBadger will use log format stderr to parse postgresql.log.
DEBUG: Starting progressbar writer process
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Processing log file: postgresql.log
DEBUG: Starting reading file "postgresql.log"...f 95265 (0.00%), queries: 0, events: 0
DEBUG: Start parsing postgresql log at offset 39962 of file "postgresql.log" to 95265
DEBUG: setting current position in log to 95265of 95265 (63.09%), queries: 7, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-04-08 03:37:096, 95265): 0
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED

DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.00 usr  0.00 sys +  0.03 cusr  0.01 csys =  0.04 CPU)
DEBUG: Building incremental report for 2022-04-08
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/reports/2022/04/08/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/reports/2022/week-15/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/reports/index.html
DEBUG: building reports took:  1 wallclock secs ( 0.25 usr +  0.01 sys =  0.26 CPU)
DEBUG: the total execution time took:  3 wallclock secs ( 0.25 usr  0.01 sys +  0.03 cusr  0.01 csys =  0.30 CPU)

sed -n '501,1000p' postgresql.log.src >> postgresql.log

zelenya@alesnbbs:~/var/pgBadger/work$ pgbadger/pgbadger -v --start-monday -I -p '%m, %p, %c, %l, %u, %d, %r, %a, %x, %v, %e, %i: ' -f stderr -O /home/zelenya/var/pgBadger/work/reports postgresql.log
DEBUG: pgBadger version 11.7.
DEBUG: Output 'html' reports will be written to out.html
DEBUG: Found log offset 95265 in file /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: pgBadger will use log format stderr to parse postgresql.log.
DEBUG: timezone not specified, using -7200 seconds
DEBUG: Starting progressbar writer process
DEBUG: Processing log file: postgresql.log
DEBUG: Starting reading file "postgresql.log"...of 188982 (0.00%), queries: 0, events: 0
DEBUG: Start parsing postgresql log at offset 95265 of file "postgresql.log" to 188982
DEBUG: setting current position in log to 188982of 188982 (61.07%), queries: 5, events: 0
DEBUG: Saving last parsed line into /tmp/tmp_LAST_PARSED (2022-04-08 03:38:09 22188982)s: 0
DEBUG: Reading temporary last parsed line from /tmp/tmp_LAST_PARSED

DEBUG: Saving last parsed line into /home/zelenya/var/pgBadger/work/reports/LAST_PARSED
DEBUG: the log statistics gathering took: 2 wallclock secs ( 0.00 usr  0.00 sys +  0.06 cusr  0.00 csys =  0.06 CPU)
DEBUG: Building incremental report for 2022-04-08
LOG: Ok, generating HTML daily report into /home/zelenya/var/pgBadger/work/reports/2022/04/08/...
LOG: Ok, generating HTML weekly report into /home/zelenya/var/pgBadger/work/reports/2022/week-15/...
LOG: Ok, generating global index to access incremental reports...
DEBUG: writing global index into /home/zelenya/var/pgBadger/work/reports/index.html
DEBUG: building reports took:  1 wallclock secs ( 0.27 usr +  0.02 sys =  0.29 CPU)
DEBUG: the total execution time took:  3 wallclock secs ( 0.27 usr  0.02 sys +  0.06 cusr  0.00 csys =  0.35 CPU)

zelenya@alesnbbs:~/var/pgBadger/work$ ll
total 392
drwxrwxr-x  4 zelenya zelenya   4096 dub  8 11:47 ./
drwxrwxr-x 24 zelenya zelenya   4096 dub  8 11:28 ../
drwxrwxr-x  7 zelenya zelenya   4096 dub  8 11:23 pgbadger/
-rw-rw-r--  1 zelenya zelenya 188982 dub  8 11:51 postgresql.log
-rw-rw-r--  1 zelenya zelenya 188982 dub  8 11:22 postgresql.log.src
drwxrwxr-x  3 zelenya zelenya   4096 dub  8 11:51 reports/
zelenya@alesnbbs:~/var/pgBadger/work$ cd pgbadger/
zelenya@alesnbbs:~/var/pgBadger/work/pgbadger$ git branch -v
* master 44f0be5 Full review and simplification of the log file change detection. Method check_file_changed() return code:

image

Kind regards Ales

darold commented 2 years ago

I don't see any problem here, the application name for session is retrieved from the disconnect message and in this log the sessions that are disconnecting have never set the application_name:

$ zgrep "disconnection:" postgresql-session.log.src.zip | awk -F, '{print $8}' | grep -v unknown | wc -l
0

This report is expected for me.

aleszeleny commented 2 years ago

Mine fault, I've checked the application names in queries by application, and expected to see the same on apps in the sessions tab. Checked logs where some other that "unknown" applications disconnected and yes, it wokred like a charm.