Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5lqf-0005ex-6b for pgsql-general@arkaria.postgresql.org; Wed, 12 Aug 2020 08:11:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5lqe-00064X-3r for pgsql-general@arkaria.postgresql.org; Wed, 12 Aug 2020 08:11:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5lqd-00064Q-QB for pgsql-general@lists.postgresql.org; Wed, 12 Aug 2020 08:10:59 +0000 Received: from mail-ej1-x641.google.com ([2a00:1450:4864:20::641]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k5lqb-0000Gj-6d for pgsql-general@postgresql.org; Wed, 12 Aug 2020 08:10:58 +0000 Received: by mail-ej1-x641.google.com with SMTP id f24so1263105ejx.6 for ; Wed, 12 Aug 2020 01:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=bgG9Nbceznng3NJ4ZZWu4l75DFos/B41B4LXmqcvBMk=; b=mfGNI1RTI+dhjkyHfEzDoLCIYdlIxcDuLvGBPNrnXP24lwsJgb295PaiSeP96ltwbV H2FBOaMOQ8YtXrbE6QKGYdsogCRDGNG1GP4iw0xWLm4zw/GnKv9Mdobq6ebnZ0FtSsHh xoVoWdkL2SeiNNjMEQXJmPTVgCidQCfiIr0NX68iLRjMQWOYq4/qRPCQK/3NbmJ/lKfC 7EJ4z9SU+pxXvAdsT8LXwrSmusXzOc+f3y0DUpPSbVG3jQe34gn5euiAh7s9ULMec9KN XF8hh/GKmZE7E8+MzkuoMCQGBkVN/wD9tEOD3I9Mtm0y6Q74yP29DrO7UBsKRt43+iLs f9UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=bgG9Nbceznng3NJ4ZZWu4l75DFos/B41B4LXmqcvBMk=; b=rmMg17p2o5ZP3/75Pw0gRK61W/tPOQhir5QUhCV/OJZAc3gtKpSf9+6YIb3UQrWAd8 v06y2p7UVJ95O4/n6AaVNXoPiURMh+6Zjzy8t0zO13QVxAO5M8dWzOlnPZrp+UjCCmei 3T1IASNkEmWiVRj3o0LiwWarA9Zpx5fXgGLje9X2ZcHj6wqWN7aR03PcFipjNP3fjYM9 eLCdw98R2iU8JBiAoQXQ0QS8C6OP0rKoPBhZUDVNjREahT9Ult0fDMI21fxRhtmipFNd UpCtQcxTBg/WDysywMY6ySppcqfhg7x6bco/tjLhsJaI6ia+KmQTtCQoUuroNUJUumUQ ZYPA== X-Gm-Message-State: AOAM532aaWP1R1jHGKPM22iWb8Vp/wszfq3Xsbzqn5qGvWk1C0agN1q0 L0aAGvM7gqBWaW+pvo7hCiE= X-Google-Smtp-Source: ABdhPJwHj6h4IGrSsXrt93LSgXIqZyl2E7x/yNp/Q/3hhnTapVIoYqkCqP8dPwj3QgCKGox3uDwC8w== X-Received: by 2002:a17:907:94ce:: with SMTP id dn14mr29294567ejc.351.1597219854850; Wed, 12 Aug 2020 01:10:54 -0700 (PDT) Received: from [192.168.1.8] (241-57-239-77.dyn.cable.fcom.ch. [77.239.57.241]) by smtp.gmail.com with ESMTPSA id a11sm996723ejk.77.2020.08.12.01.10.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Aug 2020 01:10:54 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) Subject: Re: How is PG replication typically used to create a High Availability (HA) config ? From: =?utf-8?Q?Paul_F=C3=B6rster?= In-Reply-To: Date: Wed, 12 Aug 2020 10:11:01 +0200 Cc: Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: References: To: David Gauthier X-Mailer: Apple Mail (2.3608.120.23.2.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi David, please don't top-post. > On 11. Aug, 2020, at 22:57, David Gauthier = wrote: >=20 > Thanks for the response Paul :-) >=20 > 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; =3D> p 2. select count(*) from pg_stat_wal_receiver; =3D> r if: p =3D 0 & r =3D 0 =3D> single database cluster, no replication p > 0 & r =3D 0 =3D> primary database cluster p =3D 0 & r > 0 =3D> replica database cluster p > 0 & r > 0 =3D> 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=