Skip to content

Commit

Permalink
Add new option, --csv-separator CHAR to be able to change the CSV
Browse files Browse the repository at this point in the history
separator character. Default separator is the semicolon (;). Thanks
to Henrietta Dombrovskaya.
  • Loading branch information
darold committed Jan 19, 2024
1 parent dc108b9 commit d192b36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ SYNOPSIS
--no-progressbar : disable progressbar.
--dump-raw-csv : parse the log and dump the information into CSV
format. No further processing is done, no report.
--csv-separator CHAR : change CSV separator character. Default: semicolon.
--include-pid PID : only report events related to the session pid (%p).
Can be used multiple time.
--include-session ID : only report events related to the session id (%c).
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ Options:
defined in your postgresql.conf. Only use it if you
aren't using one of the standard prefixes specified
in the pgBadger documentation, such as if your
prefix includes additional variables like client ip
or application name. See examples below.
prefix includes additional variables like client IP
or application name. MUST contain escape sequences
for time (%t, %m or %n) and processes (%p or %c).
See examples below.
-P | --no-prettify : disable SQL queries prettify formatter.
-q | --quiet : don't print anything to stdout, not even a progress
bar.
Expand Down Expand Up @@ -221,6 +223,13 @@ Options:
can be useful if you want to distinguish between
same normalized queries.
--no-progressbar : disable progressbar.
--dump-raw-csv : parse the log and dump the information into CSV
format. No further processing is done, no report.
--csv-separator CHAR : change CSV separator character. Default: semicolon.
--include-pid PID : only report events related to the session pid (%p).
Can be used multiple time.
--include-session ID : only report events related to the session id (%c).
Can be used multiple time.

pgBadger is able to parse a remote log file using a passwordless ssh connection.
Use -r or --remote-host to set the host IP address or hostname. There are also
Expand Down
1 change: 1 addition & 0 deletions doc/pgBadger.pod
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Options:
--no-progressbar : disable progressbar.
--dump-raw-csv : parse the log and dump the information into CSV
format. No further processing is done, no report.
--csv-separator CHAR : change CSV separator character. Default: semicolon.
--include-pid PID : only report events related to the session pid (%p).
Can be used multiple time.
--include-session ID : only report events related to the session id (%c).
Expand Down
19 changes: 12 additions & 7 deletions pgbadger
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ my %last_execute_stmt = ();
my $disable_process_title = 0;
my $dump_all_queries = 0;
my $dump_raw_csv = 0;
my $csv_separator = 0;
my $header_done = 0;
my @include_pid = ();
my @include_session = ();
Expand Down Expand Up @@ -570,6 +571,7 @@ my $result = GetOptions(
'dump-raw-csv!' => \$dump_raw_csv,
'include-pid=i' => \@include_pid,
'include-session=s' => \@include_session,
'cvs-separator=s' => \$csv_separator,
);
die "FATAL: use pgbadger --help\n" if (not $result);

Expand Down Expand Up @@ -2264,6 +2266,7 @@ Options:
--no-progressbar : disable progressbar.
--dump-raw-csv : parse the log and dump the information into CSV
format. No further processing is done, no report.
--csv-separator CHAR : change CSV separator character. Default: semicolon.
--include-pid PID : only report events related to the session pid (\%p).
Can be used multiple time.
--include-session ID : only report events related to the session id (\%c).
Expand Down Expand Up @@ -19662,6 +19665,8 @@ sub dump_raw_csv

my $t_pid = shift;

my $sep = $csv_separator || ';';

# CSV columns information:
# ------------------------
# timestamp without milliseconds
Expand All @@ -19679,11 +19684,11 @@ sub dump_raw_csv
# backend type
# query id

print "timestamp;username;dbname;pid;client;sessionid;loglevel;sqlstate;duration;query/error;parameters;appname;backendtype;queryid\n" if (!$header_done);
print "timestamp${sep}username${sep}dbname${sep}pid${sep}client${sep}sessionid${sep}loglevel${sep}sqlstate${sep}duration${sep}query/error${sep}parameters${sep}appname${sep}backendtype${sep}queryid\n" if (!$header_done);
$header_done = 1;
print "$cur_info{$t_pid}{timestamp};$cur_info{$t_pid}{dbuser};$cur_info{$t_pid}{dbname};";
print "$cur_info{$t_pid}{pid};$cur_info{$t_pid}{dbclient};$cur_info{$t_pid}{session};";
print "$cur_info{$t_pid}{loglevel};$cur_info{$t_pid}{sqlstate};$cur_info{$t_pid}{duration};";
print "$cur_info{$t_pid}{timestamp}${sep}$cur_info{$t_pid}{dbuser}${sep}$cur_info{$t_pid}{dbname}${sep}";
print "$cur_info{$t_pid}{pid}${sep}$cur_info{$t_pid}{dbclient}${sep}$cur_info{$t_pid}{session}${sep}";
print "$cur_info{$t_pid}{loglevel}${sep}$cur_info{$t_pid}{sqlstate}${sep}$cur_info{$t_pid}{duration}${sep}";
my $query = ($cur_info{$t_pid}{query} || $cur_lock_info{$t_pid}{query} || $cur_temp_info{$t_pid}{query}
|| $cur_cancel_info{$t_pid}{query} || "plan:\n" .$cur_plan_info{$t_pid}{plan});
$query =~ s/[\r\n]/\\n/gs;
Expand All @@ -19692,9 +19697,9 @@ sub dump_raw_csv
$query = '"' . $query . '"';
}
print $query . ";";
($cur_info{$t_pid}{parameters}) ? print "\"$cur_info{$t_pid}{parameters}\";" : print ';';
($cur_info{$t_pid}{dbappname}) ? print "\"$cur_info{$t_pid}{dbappname}\";" : print ';';
print "$cur_info{$t_pid}{backendtype};$cur_info{$t_pid}{queryid}\n";
($cur_info{$t_pid}{parameters}) ? print "\"$cur_info{$t_pid}{parameters}\"${sep}" : print '${sep}';
($cur_info{$t_pid}{dbappname}) ? print "\"$cur_info{$t_pid}{dbappname}\"${sep}" : print '${sep}';
print "$cur_info{$t_pid}{backendtype}${sep}$cur_info{$t_pid}{queryid}\n";
}


Expand Down

0 comments on commit d192b36

Please sign in to comment.