pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Förster <paul.foerster@gmail.com>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How is PG replication typically used to create a High Availability (HA) config ?
Date: Wed, 12 Aug 2020 10:11:01 +0200
Message-ID: <CA943E1A-24BC-4208-9EBA-1528055D7BE0@gmail.com> (raw)
In-Reply-To: <CAMBRECCgm_J--LM6PFYJo1tuE--t2Ke=-7FBFhxzL7-oKwAiaQ@mail.gmail.com>
References: <CAMBRECD3EYs1AvkXPZ10gR0q4eiF-oZTqzikyw4xv6d4nrCJsA@mail.gmail.com>
	<D93B5DB0-83BB-4327-BB1F-BACB2237381D@gmail.com>
	<CAMBRECCgm_J--LM6PFYJo1tuE--t2Ke=-7FBFhxzL7-oKwAiaQ@mail.gmail.com>

Hi David,

please don't top-post.

> On 11. Aug, 2020, at 22:57, David Gauthier <davegauthierpg@gmail.com> wrote:
> 
> Thanks for the response Paul :-)
> 
> Our code is actually perl which uses DBI which has functions to ping a DB on a specific server and connect to it.
> But my question was more along the lines  of whether or not the onus to do this sort of thing typically lies with the app or something outside which is orchestrating the HA cfg.

it should be handled outside the app, im my opinion. But then, many installations don't use pg-bouncer, HA-proxy, virtual IP addresses or something like that. That's why I suggested using libpq. libpq can handle it. I'm not sure if and how it can in done in Perl, though.

I played around a little with perl-DBI, perl-DBI-Pg, perl-URI and perl-URI-db and, though I managed to get connected, I did not manage to specifically select a connect to the primary or replica database cluster.

Also, your initial steps should be done differently:

1. select count(*) from pg_stat_replication; => p
2. select count(*) from pg_stat_wal_receiver; => r

if:

p = 0 & r = 0 => single database cluster, no replication
p > 0 & r = 0 => primary database cluster
p = 0 & r > 0 => replica database cluster
p > 0 & r > 0 => primary and replica database cluster

The last case can for example happen, if you have database cluster A replicate to B, and B replicate to C, and then connect to B.

Also, the test that many people do to select pg_is_in_recovery(); is not a good idea because B and C of the above example are probably in recovery mode, so you still don't know which end you're on.

Also, pg_is_in_recovery() will probably not work with logical but only streaming replication (both async and sync) because I expect B and C to not be in recovery mode when using logical replication. I didn't try logical replication, so someone please correct me if I'm wrong here.

If you just want to know, whether your connection is read-write or read-only, you can simply:

show transaction_read_only;

Cheers,
Paul




view thread (6+ messages)  latest in thread

Message-ID: <CA943E1A-24BC-4208-9EBA-1528055D7BE0@gmail.com>
Permalink:  ../CA943E1A-24BC-4208-9EBA-1528055D7BE0@gmail.com/
Also on:    postgresql.org/message-id/CA943E1A-24BC-4208-9EBA-1528055D7BE0@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: paul.foerster@gmail.com, davegauthierpg@gmail.com
  Subject: Re: How is PG replication typically used to create a High Availability (HA) config ?
  In-Reply-To: <CA943E1A-24BC-4208-9EBA-1528055D7BE0@gmail.com>
  References: <CAMBRECD3EYs1AvkXPZ10gR0q4eiF-oZTqzikyw4xv6d4nrCJsA@mail.gmail.com> <D93B5DB0-83BB-4327-BB1F-BACB2237381D@gmail.com> <CAMBRECCgm_J--LM6PFYJo1tuE--t2Ke=-7FBFhxzL7-oKwAiaQ@mail.gmail.com> <CA943E1A-24BC-4208-9EBA-1528055D7BE0@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