pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to determine whether I'm running on a standby?
3+ messages / 3 participants
[nested] [flat]

* How to determine whether I'm running on a standby?
@ 2022-02-10 19:54  Mladen Gogala <gogala.mladen@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Mladen Gogala @ 2022-02-10 19:54 UTC (permalink / raw)
  To: pgsql-general

Hi!

I am running PostgreSQL 13.5, one primary cluster and two standby 
clusters, managed by PgBouncer which will automatically promote one of 
the  standbys to the primary in case of failure. I have a job that is 
cleaning "old data",  with "old data" being whatever business side of 
the company deems as "old data". The cleanup is a PgPL/SQL procedure 
which uses a bunch of DELETE commands. The procedure works fine on the 
primary but reports a bunch of errors on the read-only standbys. In 
another database system, I have the table called V$DATABASE and the 
column named DATABASE_ROLE which can be either primary or some kind of 
standby (physical or logical). Is there anything like that in the world 
of Postgres? I would like to do something like this:

DECLARE

DB_ROLE VARCHAR2(64);

BEGIN

SELECT DATABASE_ROLE INTO DB_ROLE FROM V$DATABASE;

IF DB_ROLE = 'PRIMARY' THEN

    <do a bunch of deletions>

ELSE

     exit;

END IF;

END;

What would a similar idiom in the PostgreSQL world look like? I have the 
job in crontab on the primary and I don't really know which of the 3 
clusters will be my primary at any given moment. So far, I am using 
manual labor to do that, which is silly. Knowing Postgres idioms, there 
will probably be a function for this but I was unable to figure out 
which one.

Regards

-- 
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com

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

* Re: How to determine whether I'm running on a standby?
@ 2022-02-10 19:56  David G. Johnston <david.g.johnston@gmail.com>
  parent: Mladen Gogala <gogala.mladen@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2022-02-10 19:56 UTC (permalink / raw)
  To: Mladen Gogala <gogala.mladen@gmail.com>; +Cc: pgsql-general

On Thu, Feb 10, 2022 at 12:54 PM Mladen Gogala <gogala.mladen@gmail.com>
wrote:

> The procedure works fine on the primary but reports a bunch of errors on
> the read-only standbys.
>
A standby is in perpetual recovery mode. There is a function that returns a
boolean true if you are in recovery.

https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-RECOVERY-CONTROL

David J.

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

* Re: How to determine whether I'm running on a standby?
@ 2022-02-11 01:55  Ron <ronljohnsonjr@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Ron @ 2022-02-11 01:55 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2/10/22 1:56 PM, David G. Johnston wrote:
> On Thu, Feb 10, 2022 at 12:54 PM Mladen Gogala <gogala.mladen@gmail.com 
> <mailto:gogala.mladen@gmail.com>> wrote:
>
>     The procedure works fine on the primary but reports a bunch of errors
>     on the read-only standbys.
>
> A standby is in perpetual recovery mode.

(Just like in SQL Server.)

-- 
Angular momentum makes the world go 'round.

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


end of thread, other threads:[~2022-02-11 01:55 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-02-10 19:54 How to determine whether I'm running on a standby? Mladen Gogala <gogala.mladen@gmail.com>
2022-02-10 19:56 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-11 01:55   ` Ron <ronljohnsonjr@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