Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s55UL-0035DE-RS for pgsql-general@arkaria.postgresql.org; Thu, 09 May 2024 15:15:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s55UJ-008TSM-8S for pgsql-general@arkaria.postgresql.org; Thu, 09 May 2024 15:15:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s55UI-008TSE-Rf for pgsql-general@lists.postgresql.org; Thu, 09 May 2024 15:15:15 +0000 Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s55UF-000CaD-40 for pgsql-general@postgresql.org; Thu, 09 May 2024 15:15:14 +0000 Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2e3e18c24c1so10122951fa.1 for ; Thu, 09 May 2024 08:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715267711; x=1715872511; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=qTfRVC+HN/FFEN862rZs2Zts2FusmVAfz0r7Fmn5ZUQ=; b=CmeoNbUkezyaRCSPbQ0VA2B/O0TxjaMU1+5T2iY9vr8TPGktR3Pm5+b4eWycARSoYq yPdPETJa1yMlSG/rB7obN4ljaSJKyZ4azXKljgm2wgYj7FcytvXvtEmhcBt7q9143Krc iAcjQcyO9kjfvfIDG7ED6GFqZ3hGMpEq+aJURdHGBTsJQ3aJ7Brf6Zn5xBpbr815NLLG 3MKg5VN5nNS3CYrpEm8ujQv1urbRYzyH26iIkuZo+AFIVHcDeGtOCdH8dp68QBoTKHkS mb2q/b07BzA8woXGHO/VhYLNjAO3lTE3ShstkZwywoJEbyloMBjZRJS66/3ABwAa9jOm yMdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715267711; x=1715872511; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qTfRVC+HN/FFEN862rZs2Zts2FusmVAfz0r7Fmn5ZUQ=; b=uOyiDahLwmMvbBhTn66WXXa2DHi9PTRKQkq23opElb8xiPFq9AX6/RhR81ndtghZdO IE0wKz61KYMuIKIhFAB1Uhge7Mjv5HJty0GNi+OWIhrziPwUN/S1VYb9sQPmmx5RR2Ja 8kN44zM06sTZjjjLWkJFRSYC9BMlHvPwOBnsUUhf48sDoTvUB1ocPVmlwNbsqmxthD8P 1llsOS5Bak81CbXgRiwjomNBxWtDjY1pJ/GcGOJ0xRiSrOHS7ajKf04iUjv0DJjiO6/r HoBHbPaI3kNSsJISMBEVY/H3Wu3roib24cr5JG4eMPafKN0C/AfGOA3c5hFa955R5Xbi iF5w== X-Forwarded-Encrypted: i=1; AJvYcCUu58hnMvnxj1bzXp4G4psKZkBliIiQ1PhKy9CgGF+0RzvzySHOA4ZX+a352CsV5zkGfImjMJcEFA0vG4s1VaFnhVjyn7bO1w2dM605 X-Gm-Message-State: AOJu0Yxa5afjYUHU1Q06jEk1ZNZnLsFqyY9Ac29J7UGnHnmtTLILPByM eG8fpuQx+sISFGKGahpjRMU7usKjConqoqi3yKhkcBrdh568nhO7Ofq7fUssfJm9ZZxPu79P+Yg CFptaQ10qLjisJ5keh+V3xtobyw== X-Google-Smtp-Source: AGHT+IHVcMZ51aGQk2i8jmC3SUUX9pcHb9mbOAQMZ5HWkmOWBToLFt9AiNlUPlQoWsH6ksy5oBl5YR14bbF9UFQ9g8w= X-Received: by 2002:a2e:a0d5:0:b0:2e0:a574:4a13 with SMTP id 38308e7fff4ca-2e4476b9fd3mr40214661fa.32.1715267710780; Thu, 09 May 2024 08:15:10 -0700 (PDT) MIME-Version: 1.0 References: <8a534c5f-e400-4bb5-b39e-2017d259ff06@aklaver.com> <10bcc03d-fa3e-40b6-bdd2-cac0acd046f4@aklaver.com> In-Reply-To: From: =?UTF-8?Q?Torsten_F=C3=B6rtsch?= Date: Thu, 9 May 2024 17:14:59 +0200 Message-ID: Subject: Re: Unexpected data when subscribing to logical replication slot To: Daniel McKenzie Cc: Adrian Klaver , tomas.vondra@enterprisedb.com, pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="000000000000988fb3061806e2f5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000988fb3061806e2f5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I would not find this behavior surprising in particular if you have a synchronous replica. According to the documentation of synchronous_commit: The local behavior of all non-off modes is to wait for local flush of WAL to disk. This is when the logical decoder sees the item. But that does not mean the change is visible to other transactions in the MVCC sense. So, if wal2json and the rest of your stuff is fast enough, then the enrichment query may very well read old data. A transaction being committed means all the WAL has been written (and usually synced) to disk including the bit in the pg_xact directory. On Thu, May 9, 2024 at 9:32=E2=80=AFAM Daniel McKenzie < daniel.mckenzie@curvedental.com> wrote: > Asynchronous commit introduces the risk of data loss. There is a short >> time window between the report of transaction completion to the client >> and the time that the transaction is truly committed. > > > The documentation speaks about synchronous_commit changing how > transactions change behaviour for the client. So in this case, my psql > terminal is the client, and I would expect a faster commit (from its > perspective) and then a period of risk (as a process usually done as part > of the commit is now being done in the background) but it's not clear how > that affects a replication slot subscriber. > > What we're struggling to understand is: why are we seeing any updates in > the replication slot before they have been "truly committed"? > > There appears to be a state of limbo between updating data and that data > being available to query (and our subscriber is picking up changes in thi= s > period of time) but I can't pin down any documentation which describes it= . > > We've had this running in live now for years without a hiccup so we are > surprised to learn that we have this massive race condition and it just s= o > happens that the hardware is fast enough to process the transaction befor= e > the .NET application can react to replication slot changes. > > Daniel McKenzie > Software Developer > > Office: +1 403.910.5927 x 251 > Mobile: +44 7712 159045 > Website: www.curvedental.com > > *Curve Dental Confidentiality Notice* > This message is intended exclusively for the individual or entity to whic= h > it is addressed. This communication may contain information that is > proprietary, privileged, confidential, or otherwise legally exempt from > disclosure. If you are not the named addressee, you are not authorized to > read, print, retain, copy, or disseminate this message or any part of it. > If you have received this message in error, please notify the sender > immediately by replying to this e-mail and delete all copies of this > message. > > > On Wed, May 8, 2024 at 5:28=E2=80=AFPM Adrian Klaver > wrote: > >> On 5/8/24 08:24, Daniel McKenzie wrote: >> > It's running both (in docker containers) and also quite a few more >> > docker containers running various .NET applications. >> >> I think what you found is that the r7a.medium instance is not capable >> enough to do all that it is asked without introducing lag under load. >> Answering the questions posed by Tomas Vondra would help get to the >> actual cause of the lag. >> >> In meantime my suspicion is this part: >> >> "For example, when I use a psql terminal to update a user's last name >> from "Jones" to "Smith" then I would expect the enrichment query to find >> "Smith" but it will sometimes still find "Jones". It finds the old data >> perhaps 1 in 50 times." >> >> If this is being run against the Postgres server my guess is that >> synchronous_commit=3Don is causing the commit on the server to wait for >> the WAL records to be flushed to disk and this is not happening in a >> timely manner in the '... 1 in 50 times' you mention. In that case you >> see the old values not the new committed values. This seems to be >> confirmed when you set synchronous_commit=3Doff and don't see old values= . >> For completeness per: >> >> https://www.postgresql.org/docs/current/wal-async-commit.html >> >> "However, for short transactions this delay is a major component of the >> total transaction time. Selecting asynchronous commit mode means that >> the server returns success as soon as the transaction is logically >> completed, before the WAL records it generated have actually made their >> way to disk. This can provide a significant boost in throughput for >> small transactions. >> >> Asynchronous commit introduces the risk of data loss. There is a short >> time window between the report of transaction completion to the client >> and the time that the transaction is truly committed (that is, it is >> guaranteed not to be lost if the server crashes). ... >> " >> >> > >> > Daniel McKenzie >> > Software Developer >> >> -- >> Adrian Klaver >> adrian.klaver@aklaver.com >> >> --000000000000988fb3061806e2f5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I would not find this behavior surprising in particular if= you have a synchronous replica. According to the documentation of synchron= ous_commit:

=C2=A0 =C2=A0The local behavior of all non-o= ff modes is to wait for local flush of WAL to disk.

This is when the logical decoder sees the item. But that does not mean th= e change is visible to other transactions in the MVCC sense. So, if wal2jso= n and the rest of your stuff is fast enough, then the enrichment query may = very well read old data.

A transaction being commi= tted means all the WAL has been written (and usually synced) to disk includ= ing the bit in the pg_xact directory.

On Thu, May 9, 2024 at 9:32=E2= =80=AFAM Daniel McKenzie <daniel.mckenzie@curvedental.com> wrote:
Asynchronous commit introduces the risk of data= loss. There is a short
time window between the report of transaction co= mpletion to the client
and the time that the transaction is truly commit= ted.

The documentation speaks about=C2= =A0synchronous_commit changing how transactions change behaviour for the cl= ient. So in this case, my psql terminal is the client, and I would expect a= faster commit (from its perspective) and then a period of risk (as a proce= ss usually done as part of the commit is now being done in the background) = but it's not clear how that affects a replication slot subscriber.

What we're struggling to understand is:= why are we seeing any updates in the replication slot before they have bee= n "truly committed"?

There appears t= o be a state of limbo between updating data and that data being available t= o query (and our subscriber is picking up changes in this period of time) b= ut I can't pin down any documentation which describes it.

We've had this running in live now for years without = a hiccup so we are surprised to learn that we have this massive race condit= ion and it just so happens that the hardware is fast enough to process the = transaction before the .NET application can react to replication slot chang= es.

Daniel McKenz= ie
Software Developer

Office: +1 403.910.5927 x 251
Mobi= le: +44 7712 159045
Website:=C2=A0<= a href=3D"http://www.curvedental.com/" target=3D"_blank">www.curvedental.co= m

Curve Dental Confid= entiality Notice
This message is = intended exclusively for the individual or entity to which it is addressed.= This communication may=C2=A0conta= in information that is proprietary, privileged, confidential, or otherwise = legally exempt from disclosure. If you are=C2=A0not the named addressee, you are not authorized to read, prin= t, retain, copy, or disseminate this message or any=C2=A0part of it. If you have received this message in err= or, please notify the sender immediately by replying to this e-mail=C2=A0and delete all copies of this messag= e.


On Wed, May 8, 2024 at 5:2= 8=E2=80=AFPM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 5/8/24 08:24, Daniel McKenzi= e wrote:
> It's running both (in docker containers) and also quite a few more=
> docker containers running various .NET applications.

I think what you found is that the r7a.medium instance is not capable
enough to do all that it is asked without introducing lag under load.
Answering the questions posed by Tomas Vondra would help get to the
actual cause of the lag.

In meantime my suspicion is this part:

"For example, when I use a psql terminal to update a user's last n= ame
from "Jones" to "Smith" then I would expect the enrichm= ent query to find
"Smith" but it will sometimes still find "Jones". It fi= nds the old data
perhaps 1 in 50 times."

If this is being run against the Postgres server my guess is that
synchronous_commit=3Don is causing the commit on the server to wait for the WAL records to be flushed to disk and this is not happening in a
timely manner in the '... 1 in 50 times' you mention. In that case = you
see the old values not the new committed values. This seems to be
confirmed when you set synchronous_commit=3Doff and don't see old value= s.
For completeness per:

https://www.postgresql.org/docs/current= /wal-async-commit.html

"However, for short transactions this delay is a major component of th= e
total transaction time. Selecting asynchronous commit mode means that
the server returns success as soon as the transaction is logically
completed, before the WAL records it generated have actually made their way to disk. This can provide a significant boost in throughput for
small transactions.

Asynchronous commit introduces the risk of data loss. There is a short
time window between the report of transaction completion to the client
and the time that the transaction is truly committed (that is, it is
guaranteed not to be lost if the server crashes).=C2=A0 ...
"

>
> Daniel McKenzie
> Software Developer

--
Adrian Klaver
adrian.klave= r@aklaver.com

--000000000000988fb3061806e2f5--