pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mladen Gogala <gogala.mladen@gmail.com>
To: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: How to determine whether I'm running on a standby?
Date: Thu, 10 Feb 2022 14:54:05 -0500
Message-ID: <45425911-b002-4c2d-256c-49877eca7457@gmail.com> (raw)

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

view thread (3+ messages)  latest in thread

Message-ID: <45425911-b002-4c2d-256c-49877eca7457@gmail.com>
Permalink:  ../45425911-b002-4c2d-256c-49877eca7457@gmail.com/
Also on:    postgresql.org/message-id/45425911-b002-4c2d-256c-49877eca7457@gmail.com

 · 

reply

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: gogala.mladen@gmail.com
  Subject: Re: How to determine whether I'm running on a standby?
  In-Reply-To: <45425911-b002-4c2d-256c-49877eca7457@gmail.com>
  References: <45425911-b002-4c2d-256c-49877eca7457@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