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 1mjKEM-0005wO-4b for pgsql-general@arkaria.postgresql.org; Sat, 06 Nov 2021 11:51:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mjKEK-0007DL-1i for pgsql-general@arkaria.postgresql.org; Sat, 06 Nov 2021 11:51:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mjKEJ-0007DC-OW for pgsql-general@lists.postgresql.org; Sat, 06 Nov 2021 11:51:27 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mjKEF-0000ZM-Og for pgsql-general@lists.postgresql.org; Sat, 06 Nov 2021 11:51:27 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id E7DEF5AAB; Sat, 6 Nov 2021 12:51:21 +0100 (CET) Date: Sat, 6 Nov 2021 12:51:21 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Question: Is it possible to get the new xlog position after query execution? Message-ID: Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="VarpQFKL087hc3yo" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --VarpQFKL087hc3yo Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2021-11-01 00:36:16 -0400, Oleg Serov wrote: > On Sun, Oct 31, 2021 at 4:29 PM Peter J. Holzer wrote: >=20 > On 2021-10-29 13:22:56 -0400, Oleg Serov wrote: > > We are using a master/slave replication system where we perform > > writes on master and use replication to offload reads. > > > > However, sometimes we have a replication lag of a few seconds > > and as a result, after the update, the change is not yet > > available on the replica.=A0 > > > > Is there a way to get XLOG position to which specific update > > query will be written? That way we can check if our replica > > caught up with changes and it is safe to read it from. Can it be > > done using SQL functions? Can I get that information from query > > protocol? >=20 > I think I would prefer a more direct approach: >=20 > If you know what you've written, can't you just check whether the > replica has the new value(s)? >=20 > The simplest answer: One thread on a single process on a server knows abo= ut it. > Now another thread on another process/other server does not know about it. So why would that other thread know the relevant XLOG position? > If not, an alternative could be a table which contains a simple count= er > or timestamp: >=20 > =A0 =A0 begin; > =A0 =A0 (lots of updates ...) > =A0 =A0 commit; > =A0 =A0 begin; > =A0 =A0 update counter set c =3D c + 1 returning c; -- save this as c= _current > =A0 =A0 commit; >=20 > =A0 =A0 Select c from counter on the replica in a loop until c >=3D c= _current. >=20 > Why invent something totally new when XLOG position is already used for > replication by postgres? What are the benefits of it? Because you had to ask. That shows that it isn't obvious. So your application relies on some non-obvious (and possibly version-dependent) implementation details of the database to ensure ordering. Using something that makes sense from the application perspective (a timestamp or a counter are just examples - your application may already have some information which can use be used for that purpose) makes it more obvious for the application programmer. (I'm generally a big fan of end-to-end checks and testing what you are really interested in. If want X but argue that X is true if Y is true and Y is true if Z is true, and then go on to test for Z, that usually makes code hard to understand. It is sometimes useful or even necessary (e.g. if X cannot be tested directly), but it should IMHO be restricted to those cases.) hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --VarpQFKL087hc3yo Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmGGbDUACgkQ8g5IURL+ KF138A//YcudOolNlnaJjcKBjhw8qJlz3744SYK39roANh2JkAF7hIvaBwdvFWrY 6XDVemC5D9aH/+9uqciNpbnCmREw6I8CkW2b5vsMvQ4dW6nziztq4CW643zBo5ib nXnDvzsIzIr/SaUGt1b5BrcvbYduVo1wa6F9qNT67B4Bc+9lnQoIFRyjN9myWY5C uI7DRd9RF6EQB9Gg24wEOHUowp2SRLGjAayYQ5LifUhZwUhWTV9YNur22oG954ic t8gQBitFUCfntPQi8ifUWrP0Zpe1v50JKHN5Qhcf08l2mgmkOKvYVYguxaUXaJel kJo47jk0Ozz1yIKCk/XhEbWH/Vqy2aYUe6CXGlMF320y4JV/unonBQnVnnkW/gcZ yWjHOtMNpBa2ZTtAMyBO9wwPh/JDOzuTvNYxSt7SUud+bA8DDymSV7w+a8qlwZyO x12NjMgJ/d0AlinbBi/IpF3xeK8h09BsjK6+b7kKYC2tkU3izkr/PrhhXLRQ9POJ fMPihchE8ZplOpVrNKlSWR+Ksnc+9waSiYOyu9g99cg3Q0BLCZAxCBJk2oInhLaA u6CESNJ0T4BoUmpuEaIm0sLcXVFNAMtBaLRVVS/rInqkfKyJIXrXS6vSNblhHM+o SJ2mvGSsNuAi4c8T+OVBXTKaZiBXMQKE5u30xLMz+X+uAOJIFYU= =0pVl -----END PGP SIGNATURE----- --VarpQFKL087hc3yo--