Skip to content

Commit b3f7d7b

Browse files
committed
Add support for application_name filter on long running transactions and
long idle in transaction. Good for filtering applications like pg_dump, but also others without having to run them with a specific user.
1 parent 4cea93e commit b3f7d7b

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

check_postgres.pl

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ package check_postgres;
920920

921921
## These options are multiples ('@s')
922922
for my $arr (qw/include exclude includeuser excludeuser host port
923-
dbuser dbname dbpass dbservice schema/) {
923+
dbuser dbname dbpass dbservice schema excludeapp
924+
includeapp/) {
924925
next if $name ne $arr and $name ne "${arr}2";
925926
push @{$tempopt{$name}} => $value;
926927
## Don't set below as a normal value
@@ -961,6 +962,8 @@ package check_postgres;
961962
'exclude=s@',
962963
'includeuser=s@',
963964
'excludeuser=s@',
965+
'excludeapp=s@',
966+
'includeapp=s@',
964967

965968
'host|dbhost|H|dbhost1|H1=s@',
966969
'port|dbport|p|port1|dbport1|p1=s@',
@@ -1224,6 +1227,8 @@ package check_postgres;
12241227
--exclude=name(s) items to specifically exclude (e.g. tables), depends on the action
12251228
--includeuser=include objects owned by certain users
12261229
--excludeuser=exclude objects owned by certain users
1230+
--excludeapp=exclude objects by application_name
1231+
--includeapp=include objects by application_name
12271232
12281233
Other options:
12291234
--assume-standby-mode assume that server in continious WAL recovery mode
@@ -1871,6 +1876,52 @@ sub finishup {
18711876
}
18721877
}
18731878

1879+
our $APPWHERECLAUSE = '';
1880+
if ($opt{includeapp}) {
1881+
my %applist;
1882+
for my $app (@{$opt{includeapp}}) {
1883+
for my $a2 (split /,/ => $app) {
1884+
$applist{$a2}++;
1885+
}
1886+
}
1887+
my $safeapp;
1888+
if (1 == keys %applist) {
1889+
($safeapp = each %applist) =~ s/'/''/g;
1890+
$APPWHERECLAUSE = " AND application_name = '$safeapp'";
1891+
}
1892+
else {
1893+
$APPWHERECLAUSE = ' AND application_name IN (';
1894+
for my $app (sort keys %applist) {
1895+
($safeapp = $app) =~ s/'/''/g;
1896+
$APPWHERECLAUSE .= "'$safeapp',";
1897+
}
1898+
chop $APPWHERECLAUSE;
1899+
$APPWHERECLAUSE .= ')';
1900+
}
1901+
}
1902+
elsif ($opt{excludeapp}) {
1903+
my %applist;
1904+
for my $app (@{$opt{excludeapp}}) {
1905+
for my $a2 (split /,/ => $app) {
1906+
$applist{$a2}++;
1907+
}
1908+
}
1909+
my $safeapp;
1910+
if (1 == keys %applist) {
1911+
($safeapp = each %applist) =~ s/'/''/g;
1912+
$APPWHERECLAUSE = " AND application_name <> '$safeapp'";
1913+
}
1914+
else {
1915+
$APPWHERECLAUSE = ' AND application_name NOT IN (';
1916+
for my $app (sort keys %applist) {
1917+
($safeapp = $app) =~ s/'/''/g;
1918+
$APPWHERECLAUSE .= "'$safeapp',";
1919+
}
1920+
chop $APPWHERECLAUSE;
1921+
$APPWHERECLAUSE .= ')';
1922+
}
1923+
}
1924+
18741925
## Check number of connections, compare to max_connections
18751926
check_backends() if $action eq 'backends';
18761927

@@ -7599,7 +7650,7 @@ sub check_txn_idle {
75997650
$SQL = q{SELECT datname, datid, procpid AS pid, usename, client_addr, xact_start, current_query AS current_query, '' AS state, }.
76007651
q{CASE WHEN client_port < 0 THEN 0 ELSE client_port END AS client_port, }.
76017652
qq{COALESCE(ROUND(EXTRACT(epoch FROM now()-$start)),0) AS seconds }.
7602-
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE }.
7653+
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE $APPWHERECLAUSE }.
76037654
q{ORDER BY xact_start, query_start, procpid DESC};
76047655
## Craft an alternate version for old servers that do not have the xact_start column:
76057656
($SQL2 = $SQL) =~ s/xact_start/query_start AS xact_start/;
@@ -7609,7 +7660,7 @@ sub check_txn_idle {
76097660
$SQL2 = $SQL = q{SELECT datname, datid, procpid AS pid, usename, client_addr, current_query AS current_query, '' AS state, }.
76107661
q{CASE WHEN client_port < 0 THEN 0 ELSE client_port END AS client_port, }.
76117662
qq{COALESCE(ROUND(EXTRACT(epoch FROM now()-$start)),0) AS seconds }.
7612-
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE }.
7663+
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE $APPWHERECLAUSE }.
76137664
q{ORDER BY query_start, procpid DESC};
76147665
}
76157666

0 commit comments

Comments
 (0)