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 1srwZ3-005bN8-RF for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 09:38:06 +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 1srwY2-00Et5x-3F for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 09:37:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1srwY1-00Et5a-I6 for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 09:37:03 +0000 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1srwY0-000JXx-1c for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 09:37:01 +0000 Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-a8d60e23b33so334734666b.0 for ; Sat, 21 Sep 2024 02:36:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726911418; x=1727516218; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=5CRu3AM0YPX9+llFuIWlLuZIVgeXq/qINaAliofbf1I=; b=l/odnJS7WI0ndWeOxMrQ69ioTQSxkGIQo/UhAhrSEY/zkCtH3pPK5N1eEwadzIQXJ1 5eOCxob2Rs4iQFN6QvfpsNYtWkZ1HDOkXLMPQCHDON5pgmECzev08ly+eACAbtgciQ45 Rzn2k3IJ01iKf/UKIAqa1V38x2SfQUgoOhLqeRLedASscyTUxZNDWaRn58WROolDooO4 0HwFzN8RbAOsE74PKcEXPs+wHsuakPZfeXgwWVdCqLuQ4Xwz8tNolIoTbk5LfWDBv1kJ v8MZgz/w7pAdErWrrZb2EpCQ7ox2XMNC9dgjYQGPRh9bH5srLT1X+bhR1+tEa7b4CXhi SZ+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726911418; x=1727516218; h=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=5CRu3AM0YPX9+llFuIWlLuZIVgeXq/qINaAliofbf1I=; b=rEgE29681eZkj+b8d4sbCv6h7WK12UDPxuFR7cvSw2jEbR+ZtI5MMwpLigxpyYcfKE cjtH8kz14iPSNJzoIWtw9l5KW4uYjtiZ3klOx10FixggHG+DyQA4zw0KP25zrid9WiwK Ctz0Gwjiti2NEvQnCTdDmZCGnpS7ViJe21HdoIIti7xZv/7IMYZBdmKsyj1vqgq94Dbb 22tMNO6Z9q4V5kZ4GyKfbdVzli+1uEePBZU5D9WkqdM/Bg/d5Elh+mwybram/b3SuXes kK/sZ3BhEdsbEAPnvvfxisgWAzg1p3c2Mfx/dyk/4HWbJWJ4xHSqVHcSlBy8vwsqmyFa ri+g== X-Gm-Message-State: AOJu0YxD00KDa/3Xh5U0akC5bxr+P968LVwPedTq5IMwiaaBisToH334 0AshnoEbUQvgh02m01oZ/1/ikd/8Otv7YlexC4A2gUKFbIuDCh+Hm2YYnipXBmDw9xwATYceY/G nYuAROfc6YXf1RXEFhk9zEzJHHWEAlQ== X-Google-Smtp-Source: AGHT+IGUU/ePHQMFuyk2kKnGiqry4RevnDesGFoYB19+TJec2kFSHL6iFD5LSZEXt+VhoLHSANhliHtrZxPKXoWgV0M= X-Received: by 2002:a17:907:f725:b0:a8b:154b:7649 with SMTP id a640c23a62f3a-a90d55dd8b2mr483152366b.15.1726911418113; Sat, 21 Sep 2024 02:36:58 -0700 (PDT) MIME-Version: 1.0 References: <586d988d-978b-4b08-84b4-ef3ebb8736a8@aklaver.com> <4cfe720e-4283-43fc-8d72-8fa11e2f741a@aklaver.com> <20240920221718.ctq5whqfyqlrjwr2@hjp.at> In-Reply-To: <20240920221718.ctq5whqfyqlrjwr2@hjp.at> From: veem v Date: Sat, 21 Sep 2024 15:06:45 +0530 Message-ID: Subject: Re: IO related waits To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000a2a62406229de53a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a2a62406229de53a Content-Type: text/plain; charset="UTF-8" On Sat, 21 Sept 2024 at 03:47, Peter J. Holzer wrote: > On 2024-09-20 14:11:38 -0700, Adrian Klaver wrote: > > On 9/20/24 1:01 PM, veem v wrote: > > > Able to reproduce this deadlock graph as below. Now my question is , > > > this is a legitimate scenario in which the same ID can get inserted > from > > > multiple sessions and in such cases it's expected to skip that (thus > "On > > > conflict Do nothing" is used) row. But as we see it's breaking the code > > > > Yeah, as I see it that would not work with concurrent uncommitted > sessions > > as it would be unresolved whether a conflict actually exists until at > least > > one of the sessions completes. > > > > > with deadlock error during race conditions where a lot of parallel > > > threads are operating. So how should we handle this scenario? Will > > > setting the "lock_timeout" parameter at session level will help us > > > anyway here? > > > > Serializable transaction?: > > > > > https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE > > Doesn't help here, at least not directly. It would help indirectly > because isolation level serializable makes it very proable that > serialization errors occur. So an application designed for serializable > would have some kind of retry logic already in place. > > SO that leads as to another solution: > > Retry each batch (possibly after reducing the batch size) until it > succeeds. > > Actually here the application is using kafka and flink stream and is using one of existing code in which it was doing row by row commit which is now changed to Batch commit i.e. the commit point is shifted from row by row to batch now. There are multiple sessions spawning at the same time to process the incoming messages 24/7. And also as described in another ongoing thread and also we saw in the past we did not see much difference between "batch commit" and "Batch insert" performance. We only realized the row by row commit is having worse performance. Now, in such a scenario when the messages are coming from a streaming framework using kafka +flink and also the insert is happening using row by row only (but just the commit is shifted to batch), i don't see any way to sort the ID columns in this streaming process, so that they won't overlap across session. In such a situation , the only way seems to have the messages replayed for which the deadlock error happens , as I think during a deadlock error, one session gets terminated by postgres and that messages perhap we can save in some exception table and then replay? --000000000000a2a62406229de53a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Sat, 21 Sept 2024 at 03:47, Peter = J. Holzer <hjp-pgsql@hjp.at> = wrote:
On 2024-0= 9-20 14:11:38 -0700, Adrian Klaver wrote:
> On 9/20/24 1:01 PM, veem v wrote:
> > Able to reproduce this deadlock graph as below.=C2=A0 Now my ques= tion is ,
> > this is a legitimate=C2=A0scenario in which the same ID can get i= nserted from
> > multiple sessions and in such cases it's expected to skip tha= t (thus "On
> > conflict Do=C2=A0nothing" is used) row. But as we see it'= ;s breaking the code
>
> Yeah, as I see it that would not work with concurrent uncommitted sess= ions
> as it would be unresolved whether a conflict actually exists until at = least
> one of the sessions completes.
>
> > with deadlock error during race=C2=A0conditions where a lot of pa= rallel
> > threads are operating. So how should we handle this scenario? Wil= l
> > setting the "lock_timeout" parameter at session level w= ill help us
> > anyway here?
>
> Serializable transaction?:
>
> https://www.postg= resql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE

Doesn't help here, at least not directly. It would help indirectly
because isolation level serializable makes it very proable that
serialization errors occur. So an application designed for serializable
would have some kind of retry logic already in place.

SO that leads as to another solution:

Retry each batch (possibly after reducing the batch size) until it
succeeds.


Actually here the applica= tion is using kafka and=C2=A0 flink stream and is using one of existing cod= e in=C2=A0which it was doing row by row=C2=A0commit which is now changed to= Batch commit i.e. the commit point is shifted from row by row to batch now= . There are multiple sessions spawning=C2=A0at the same time to process the= incoming messages 24/7. And also as described in another ongoing thread an= d also we saw in the past we did not see much difference between "batc= h commit" and "Batch insert" performance. We only realized t= he row by row commit is having worse=C2=A0performance.

=
Now, in such a scenario when the messages=C2=A0are coming from a strea= ming framework using kafka=C2=A0+flink and also the insert is happening usi= ng row by row only (but just the commit is shifted to batch), i don't s= ee any way to sort the ID columns in this streaming process, so that they w= on't=C2=A0overlap across session.

In such a si= tuation , the only way seems to have the messages replayed for which the de= adlock error=C2=A0happens ,=C2=A0as I think during a deadlock error, one se= ssion gets terminated by postgres and that messages perhap we can save in s= ome exception table and then replay?=C2=A0

--000000000000a2a62406229de53a--