Skip to content

Commit f069af5

Browse files
committed
Query all sequences per DB in parallel for action=sequence
action=sequence used to open a new session for every sequence checked, which could be very slow. Fix by creating a single SQL statement using UNION ALL to query all sequences at once.
1 parent 5a5cf9f commit f069af5

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

check_postgres.pl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7274,23 +7274,26 @@ sub check_sequence {
72747274
my %seqinfo;
72757275
my %seqperf;
72767276
my $multidb = @{$info->{db}} > 1 ? "$db->{dbname}." : '';
7277-
for my $r (@{$db->{slurp}}) {
7277+
my @seq_sql;
7278+
for my $r (@{$db->{slurp}}) { # for each sequence, create SQL command to inspect it
72787279
my ($schema, $seq, $seqname, $typename) = @$r{qw/ nspname seqname safename typname /};
72797280
next if skip_item($seq);
72807281
my $maxValue = $typename eq 'int2' ? $MAXINT2 : $typename eq 'int4' ? $MAXINT4 : $MAXINT8;
7281-
$SQL = qq{
7282-
SELECT last_value, slots, used, ROUND(used/slots*100) AS percent,
7282+
my $seqname_l = $seqname;
7283+
$seqname_l =~ s/'/''/g; # SQL literal quoting (name is already identifier-quoted)
7284+
push @seq_sql, qq{
7285+
SELECT '$seqname_l' AS seqname, last_value, slots, used, ROUND(used/slots*100) AS percent,
72837286
CASE WHEN slots < used THEN 0 ELSE slots - used END AS numleft
72847287
FROM (
72857288
SELECT last_value,
72867289
CEIL((LEAST(max_value, $maxValue)-min_value::numeric+1)/increment_by::NUMERIC) AS slots,
72877290
CEIL((last_value-min_value::numeric+1)/increment_by::NUMERIC) AS used
72887291
FROM $seqname) foo
72897292
};
7290-
7291-
my $seqinfo = run_command($SQL, { target => $db });
7292-
my $r2 = $seqinfo->{db}[0]{slurp}[0];
7293-
my ($last, $slots, $used, $percent, $left) = @$r2{qw/ last_value slots used percent numleft / };
7293+
}
7294+
my $seqinfo = run_command(join("\nUNION ALL\n", @seq_sql), { target => $db }); # execute all SQL commands at once
7295+
for my $r2 (@{$seqinfo->{db}[0]{slurp}}) { # now look at all results
7296+
my ($seqname, $last, $slots, $used, $percent, $left) = @$r2{qw/ seqname last_value slots used percent numleft / };
72947297
if (! defined $last) {
72957298
ndie msg('seq-die', $seqname);
72967299
}
@@ -9825,6 +9828,8 @@ =head1 HISTORY
98259828
98269829
Declare POD encoding to be utf8. (Christoph Berg)
98279830
9831+
Query all sequences per DB in parallel for action=sequence. (Christoph Berg)
9832+
98289833
=item B<Version 2.21.0> September 24, 2013
98299834
98309835
Fix issue with SQL steps in check_pgagent_jobs for sql steps which perform deletes

t/02_sequence.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use 5.006;
66
use strict;
77
use warnings;
88
use Data::Dumper;
9-
use Test::More tests => 10;
9+
use Test::More tests => 12;
1010
use lib 't','.';
1111
use CP_Testing;
1212

@@ -45,6 +45,7 @@ if ($ver < 80100) {
4545
my $seqname = 'cp_test_sequence';
4646
$cp->drop_sequence_if_exists($seqname);
4747
$cp->drop_sequence_if_exists("${seqname}2");
48+
$cp->drop_sequence_if_exists("${seqname}'\"evil");
4849

4950
$t=qq{$S works when no sequences exist};
5051
like ($cp->run(''), qr{OK:.+No sequences found}, $t);
@@ -83,4 +84,16 @@ like ($cp->run('--critical=22%'), qr{CRITICAL:.+public.cp_test_sequence=33% \(ca
8384
$t=qq{$S returns correct information with MRTG output};
8485
is ($cp->run('--critical=22% --output=mrtg'), "33\n0\n\npublic.cp_test_sequence\n", $t);
8586

87+
# create second sequence
88+
$dbh->do("CREATE SEQUENCE ${seqname}2");
89+
$dbh->commit();
90+
$t=qq{$S returns correct information for two sequences};
91+
like ($cp->run(''), qr{OK:.+public.cp_test_sequence=33% .* \| .*${seqname}=33%.*${seqname}2=0%}, $t);
92+
93+
# test SQL quoting
94+
$dbh->do(qq{CREATE SEQUENCE "${seqname}'""evil"});
95+
$dbh->commit();
96+
$t=qq{$S handles SQL quoting};
97+
like ($cp->run(''), qr{OK:.+'public."${seqname}''""evil"'}, $t); # extra " and ' because name is both identifier+literal quoted
98+
8699
exit;

t/CP_Testing.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,8 @@ sub drop_sequence_if_exists {
700700
$SQL = q{SELECT count(*) FROM pg_class WHERE relkind = 'S' AND relname = } . $dbh->quote($name);
701701
my $count = $dbh->selectall_arrayref($SQL)->[0][0];
702702
if ($count) {
703-
$dbh->do("DROP SEQUENCE $name");
703+
$name =~ s/"/""/g;
704+
$dbh->do("DROP SEQUENCE \"$name\"");
704705
$dbh->commit();
705706
}
706707
return;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy