I am desperately trying to find an easy way to regulary check if a PGSQL server is still alive from within a perl script. I intended to use pg_ping
for this, but in my tests pg_ping always returns 1, independently of the DB server status.
Here's the code I tried it with:
#!/usr/bin/perl
use strict;
use warnings;
use DBD::Pg;
my $dbh=DBI->connect("DBI:Pg:dbname=healthcheck;host=vm0484","healthcheck","areyoustillthere");
for (my $i=1;$i<100;$i++) {
print "$i. pg_ping: ".$dbh->pg_ping."\n";
sleep(1);
}
I then used kill -9
in another window to manually terminate the PGSQL-server, but the result was still 1
. Did I do something wrong?
If not, how do I best check, if the DB server is still alive?
Thanks in advance.
You can manually perform query like
select 1 as is_alive
as ping()
also claim to do something similar,
The ping method issues an empty query and checks the result status.