From: Paul Förster <paul.foerster@gmail.com>
To: Thomas Kellerer <shammat@gmx.net>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Determine if postgresql cluster running is primary or not
Date: Fri, 20 Nov 2020 10:28:55 +0100
Message-ID: <FD9D5838-D8AB-4466-A3DA-6780D09A429D@gmail.com> (raw)
In-Reply-To: <f09b2f94-d9a5-473c-d726-7e1aa53a7e94@gmx.net>
References: <CAO_+3-AV1WV_RfZ=7sbvNtwoZpvL23kpyzaUBJWRxhP47NVdOA@mail.gmail.com>
<f09b2f94-d9a5-473c-d726-7e1aa53a7e94@gmx.net>
Hi Thomas,
> On 20. Nov, 2020, at 10:03, Thomas Kellerer <shammat@gmx.net> wrote:
>
> Raul Kaubi schrieb am 20.11.2020 um 09:53:
>> CentOS 7
>> Postgres 9 to 12
>>
>> For monitoring purpose, I would like that certain scripts are only run in primary server.
>> So I am looking ways to universally discover if postgresql cluster that is running is primary or not.
>
> As the standby will be in constant recovery, you can use
>
> select pg_is_in_recovery();
I usually don't recommend using pg_is_in_recovery() only because a database cluster can be in recovery for other reasons. This is why I always do the following:
select distinct
case
when b.sender=0 and c.receiver=0 then
'Standalone'
when b.sender>0 and c.receiver=0 then
'Primary'
when b.sender=0 and c.receiver>0 then
'Replica'
when b.sender>0 and c.receiver>0 then
'Primary+Replica'
end as pgrole
from
pg_database a,
(
select count(*) as sender
from pg_stat_replication
) b,
(
select count(*) as receiver
from pg_stat_wal_receiver
) c
where
not a.datistemplate;
Cheers,
Paul
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: paul.foerster@gmail.com, shammat@gmx.net
Subject: Re: Determine if postgresql cluster running is primary or not
In-Reply-To: <FD9D5838-D8AB-4466-A3DA-6780D09A429D@gmail.com>
References: <CAO_+3-AV1WV_RfZ=7sbvNtwoZpvL23kpyzaUBJWRxhP47NVdOA@mail.gmail.com> <f09b2f94-d9a5-473c-d726-7e1aa53a7e94@gmx.net> <FD9D5838-D8AB-4466-A3DA-6780D09A429D@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox