pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Determine if postgresql cluster running is primary or not
11+ messages / 4 participants
[nested] [flat]

* Determine if postgresql cluster running is primary or not
@ 2020-11-20 08:53  Raul Kaubi <raulkaubi@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Raul Kaubi @ 2020-11-20 08:53 UTC (permalink / raw)
  To: pgsql-general

Hi

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.

What would be the best way to achieve this?

Regards
Raul

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 09:03  Thomas Kellerer <shammat@gmx.net>
  parent: Raul Kaubi <raulkaubi@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Thomas Kellerer @ 2020-11-20 09:03 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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();






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 09:28  Paul Förster <paul.foerster@gmail.com>
  parent: Thomas Kellerer <shammat@gmx.net>
  0 siblings, 2 replies; 11+ messages in thread

From: Paul Förster @ 2020-11-20 09:28 UTC (permalink / raw)
  To: Thomas Kellerer <shammat@gmx.net>; +Cc: pgsql-general@lists.postgresql.org

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




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 09:34  David G. Johnston <david.g.johnston@gmail.com>
  parent: Paul Förster <paul.foerster@gmail.com>
  1 sibling, 1 reply; 11+ messages in thread

From: David G. Johnston @ 2020-11-20 09:34 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Friday, November 20, 2020, Paul Förster <paul.foerster@gmail.com> wrote:
>
>
> > On 20. Nov, 2020, at 10:03, Thomas Kellerer <shammat@gmx.net> wrote:
>
> >
> >   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:
>

Do any of those other reasons allow connections that could execute that
function to exist?

David J.

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 09:41  Raul Kaubi <raulkaubi@gmail.com>
  parent: Paul Förster <paul.foerster@gmail.com>
  1 sibling, 1 reply; 11+ messages in thread

From: Raul Kaubi @ 2020-11-20 09:41 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Hi

Thanks.
Seems like 9.5 does not work.

ERROR:  relation "pg_stat_wal_receiver" does not exist
> LINE 20:         from pg_stat_wal_receiver


Any ide how to achieve this in 9.5 ?

Raul

Kontakt Paul Förster (<paul.foerster@gmail.com>) kirjutas kuupäeval R, 20.
november 2020 kell 11:29:

> 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
>
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 10:02  Paul Förster <paul.foerster@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Paul Förster @ 2020-11-20 10:02 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi David,

> On 20. Nov, 2020, at 10:34, David G. Johnston <david.g.johnston@gmail.com> wrote:
> 
> 
> On Friday, November 20, 2020, Paul Förster <paul.foerster@gmail.com> wrote:
> 
> > On 20. Nov, 2020, at 10:03, Thomas Kellerer <shammat@gmx.net> wrote:
> 
> > 
> >   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:
> 
> Do any of those other reasons allow connections that could execute that function to exist?

that always depends on what your application does. An application could still select a lot of things, maybe even wrongly so, even if the cluster is in recovery mode.

That was my idea when writing this query and it's been working fine for years now.

Cheers,
Paul




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 10:04  Paul Förster <paul.foerster@gmail.com>
  parent: Raul Kaubi <raulkaubi@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Paul Förster @ 2020-11-20 10:04 UTC (permalink / raw)
  To: Raul Kaubi <raulkaubi@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Hi Raul,

> On 20. Nov, 2020, at 10:41, Raul Kaubi <raulkaubi@gmail.com> wrote:
> 
> Hi
> 
> Thanks.
> Seems like 9.5 does not work.
> 
> ERROR:  relation "pg_stat_wal_receiver" does not exist
> LINE 20:         from pg_stat_wal_receiver
> 
> Any ide how to achieve this in 9.5 ?
> 
> Raul

this query is tested to work on 10.x and newer, not 9.x. I don't have 9.x, so I can't say, sorry.

Cheers,
Paul




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 10:45  Raul Kaubi <raulkaubi@gmail.com>
  parent: Paul Förster <paul.foerster@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Raul Kaubi @ 2020-11-20 10:45 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Hmm, ok.

But how is this possible..?

 when b.sender>0 and c.receiver>0 then
>             'Primary+Replica'


Raul

Kontakt Paul Förster (<paul.foerster@gmail.com>) kirjutas kuupäeval R, 20.
november 2020 kell 12:04:

> Hi Raul,
>
> > On 20. Nov, 2020, at 10:41, Raul Kaubi <raulkaubi@gmail.com> wrote:
> >
> > Hi
> >
> > Thanks.
> > Seems like 9.5 does not work.
> >
> > ERROR:  relation "pg_stat_wal_receiver" does not exist
> > LINE 20:         from pg_stat_wal_receiver
> >
> > Any ide how to achieve this in 9.5 ?
> >
> > Raul
>
> this query is tested to work on 10.x and newer, not 9.x. I don't have 9.x,
> so I can't say, sorry.
>
> Cheers,
> Paul

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 10:54  Paul Förster <paul.foerster@gmail.com>
  parent: Raul Kaubi <raulkaubi@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Paul Förster @ 2020-11-20 10:54 UTC (permalink / raw)
  To: Raul Kaubi <raulkaubi@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Hi Raul,

> On 20. Nov, 2020, at 11:45, Raul Kaubi <raulkaubi@gmail.com> wrote:
> 
> Hmm, ok.
> 
> But how is this possible..?
> 
>  when b.sender>0 and c.receiver>0 then
>             'Primary+Replica'
> 
> Raul

this happens for example if you have a primary a and replica b running as a normal cluster (we use Patroni for automatic failover) and then add another replica c to the existing replica b, effectively replicating: a => b => c In this case, b would be the replica of a, but also be the primary for c.

It's called cascading replication.

Cheers,
Paul




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 11:00  Raul Kaubi <raulkaubi@gmail.com>
  parent: Paul Förster <paul.foerster@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Raul Kaubi @ 2020-11-20 11:00 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Ok, Thanks!

Raul

Kontakt Paul Förster (<paul.foerster@gmail.com>) kirjutas kuupäeval R, 20.
november 2020 kell 12:54:

> Hi Raul,
>
> > On 20. Nov, 2020, at 11:45, Raul Kaubi <raulkaubi@gmail.com> wrote:
> >
> > Hmm, ok.
> >
> > But how is this possible..?
> >
> >  when b.sender>0 and c.receiver>0 then
> >             'Primary+Replica'
> >
> > Raul
>
> this happens for example if you have a primary a and replica b running as
> a normal cluster (we use Patroni for automatic failover) and then add
> another replica c to the existing replica b, effectively replicating: a =>
> b => c In this case, b would be the replica of a, but also be the primary
> for c.
>
> It's called cascading replication.
>
> Cheers,
> Paul

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Determine if postgresql cluster running is primary or not
@ 2020-11-20 17:53  David G. Johnston <david.g.johnston@gmail.com>
  parent: Paul Förster <paul.foerster@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: David G. Johnston @ 2020-11-20 17:53 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Friday, November 20, 2020, Paul Förster <paul.foerster@gmail.com> wrote:

> Hi David,
>
> > On 20. Nov, 2020, at 10:34, David G. Johnston <
> david.g.johnston@gmail.com> wrote:
> >
> >
> > On Friday, November 20, 2020, Paul Förster <paul.foerster@gmail.com>
> wrote:
> >
> > > On 20. Nov, 2020, at 10:03, Thomas Kellerer <shammat@gmx.net> wrote:
> >
> > >
> > >   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:
> >
> > Do any of those other reasons allow connections that could execute that
> function to exist?
>
> that always depends on what your application does. An application could
> still select a lot of things, maybe even wrongly so, even if the cluster is
> in recovery mode.


I don’t follow - i posit that if psql successfully connects to a server
that reports it is is recovery that server is a secondary to some other
server, period.  Can you provide a counter-example for when that isn’t true
(given the whole psql connects successfully bit).

David J.

^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2020-11-20 17:53 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20 08:53 Determine if postgresql cluster running is primary or not Raul Kaubi <raulkaubi@gmail.com>
2020-11-20 09:03 ` Thomas Kellerer <shammat@gmx.net>
2020-11-20 09:28   ` Paul Förster <paul.foerster@gmail.com>
2020-11-20 09:34     ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-20 10:02       ` Paul Förster <paul.foerster@gmail.com>
2020-11-20 17:53         ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-20 09:41     ` Raul Kaubi <raulkaubi@gmail.com>
2020-11-20 10:04       ` Paul Förster <paul.foerster@gmail.com>
2020-11-20 10:45         ` Raul Kaubi <raulkaubi@gmail.com>
2020-11-20 10:54           ` Paul Förster <paul.foerster@gmail.com>
2020-11-20 11:00             ` Raul Kaubi <raulkaubi@gmail.com>

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