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 1ssNKJ-0081mL-7L for pgsql-general@arkaria.postgresql.org; Sun, 22 Sep 2024 14:12:40 +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 1ssNKI-006cPr-GK for pgsql-general@arkaria.postgresql.org; Sun, 22 Sep 2024 14:12:38 +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 1ssNKI-006cPj-56 for pgsql-general@lists.postgresql.org; Sun, 22 Sep 2024 14:12:38 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ssNKF-000UlC-Nt for pgsql-general@lists.postgresql.org; Sun, 22 Sep 2024 14:12:36 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-53654dbde59so4349115e87.1 for ; Sun, 22 Sep 2024 07:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727014354; x=1727619154; darn=lists.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=wUWMn9MpSLRyjwsSzylyrUva7ZFtBRfkRLT41sHBtTw=; b=AjBjrnmb6tG1uylzs1mNLVl/FenNTbktxx1ULFQBUTiGz4j9wFnNS7KZ06dYsWO+6g xTZPKi4A+xaIzRofl6nDK6wTd8+QPV/3ttq5N3pOk2Git85/IXUDdLtZm/Cl2nOA2cXK ymRpiGL0xiIK+CeAq87IKe+9K04L4h+QRXsCXYg/l+AqcqOnKSbtwAyrznOGkTN47uxV JaNxaqhtBuYgqANMovbe+YWfbpksPfowMApPQVNvf3G3L1TGo5ZTVpQmjUwjqxr1akeg uYmTtj+XIkc/2UIc1DFfVRBb7++3m33I01IQ+pPX5HXrJ4YTbRSe1GL7OxbtZ7KX/OVH vs0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727014354; x=1727619154; 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=wUWMn9MpSLRyjwsSzylyrUva7ZFtBRfkRLT41sHBtTw=; b=Fdq/NdzdieG1FA8/lFaMEOhJycWW6GXq4kKBOSR6Li7mdjIiUDYg4HU+lURIBnuyL0 dT1vACPI9e5WTkzAEMuep1WsDqTLbwXYLcKU7dlw07SFfOcb6wlMIGKdiSSMFtjIULL4 a98w64bqsAi5Ozfeu4p3umx7UQyomdxQZhBLVGSDMzEjQYW5bZA0DCU1dAMEyThgfF6f NYrhes3QtspLWTACeD/vxMYMvu/reNJGZ6wIgsIHIxR8HP7LjksxJzPG+qYb2oW80at6 BzFpoI/xteTlKY46NzKcm32F5u7wKC1/emyX6+ItFBW67HfDZCtAmqcPRYfNJ8/WWa6y S1dw== X-Forwarded-Encrypted: i=1; AJvYcCUYSvOshv4Vwg8JutE3vR164fJpYIVt7O0u2TgkN5PzV16tBeYnzeDH/A8UYSNqccAYczIMFxfhgRCypFzF@lists.postgresql.org X-Gm-Message-State: AOJu0YxW4qytreU8uyy8xWcdtdvEhUZfxYU5y0BHj2Ry323D99krGpLZ HVFw5mQYyquTCCstqeLZxzYpyRO28gpEHD93s8Qil6OQ1iZmEdf+6tBU+Z5x0wPuCOCBRURi69V j3YYicpY2JfuUDGRdQtFAcGWEOpI= X-Google-Smtp-Source: AGHT+IFy04RUXqIuG1HrB1ptg8PpW4h3qC4em8HbmdrA8nWCJTgtnRJJ2m8RdLMsgzNbPT5/jfGOwoB1YtVhjvoHWcQ= X-Received: by 2002:a05:6512:12d6:b0:52f:260:c459 with SMTP id 2adb3069b0e04-536abb54a41mr3851748e87.22.1727014353696; Sun, 22 Sep 2024 07:12:33 -0700 (PDT) MIME-Version: 1.0 References: <3dddea5e-52ab-4075-970d-a87b0c921ae7@aklaver.com> <225d1bc1-5117-4c72-85a1-bac6355fb659@aklaver.com> <586d988d-978b-4b08-84b4-ef3ebb8736a8@aklaver.com> In-Reply-To: From: Greg Sabino Mullane Date: Sun, 22 Sep 2024 10:11:57 -0400 Message-ID: Subject: Re: IO related waits To: veem v Cc: Adrian Klaver , Christophe Pettus , pgsql-general , Tom Lane , Ron Johnson , "Peter J. Holzer" Content-Type: multipart/alternative; boundary="00000000000013038c0622b5dde1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000013038c0622b5dde1 Content-Type: text/plain; charset="UTF-8" You may be able to solve this with advisory locks. In particular, transaction-level advisory locks with the "try-pass/fail" variant. Here, "123" is a unique number used by your app, related to this particular table. You also need to force read committed mode, as the advisory locks go away after the transaction ends, so at that point, we switch from advisory locks to the ON CONFLICT clause, which can only work smoothly if other processes can see the new row right away. drop table if exists t1; create table t1(id int primary key); -- Session 1: begin transaction isolation level read committed; -- inserts one row: insert into t1 select 1 where pg_try_advisory_xact_lock(123,1) on conflict(id) do nothing; -- Session 2: begin transaction isolation level read committed; -- inserts one row: insert into t1 select 2 where pg_try_advisory_xact_lock(123,2) on conflict(id) do nothing; -- Session 1: -- silently 'fails' because no lock is granted, so inserts zero rows: insert into t1 select 2 where pg_try_advisory_xact_lock(123,2) on conflict(id) do nothing; -- Session 2: -- silently 'fails' because no lock is granted, so inserts zero rows: insert into t1 select 1 where pg_try_advisory_xact_lock(123,1) on conflict(id) do nothing; -- inserts one row: insert into t1 select 3 where pg_try_advisory_xact_lock(123,3) on conflict(id) do nothing; commit; -- lock on 2 and 3 goes away -- Session 1: -- silently fails because of the on conflict clause insert into t1 select 3 where pg_try_advisory_xact_lock(123,3) on conflict(id) do nothing; Cheers, Greg --00000000000013038c0622b5dde1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
You may be able to solve this with advisory locks. In part= icular, transaction-level advisory locks with the "try-pass/fail"= variant. Here, "123" is a unique number used by your app, relate= d to this particular table. You also need to force read committed mode, as = the advisory=C2=A0locks go away after the transaction=C2=A0ends, so at that= point, we switch from advisory locks to the ON CONFLICT clause, which can = only work smoothly if other processes can see the new row right away.
drop table if exists t1;
create table t1(id int primary key);

-= - Session 1:
begin transaction isolation level read committed;

--= inserts one row:
insert into t1 select 1 where pg_try_advisory_xact_loc= k(123,1)
=C2=A0 on conflict(id) do nothing;

-- Session 2:
begi= n transaction isolation level read committed;

-- inserts one row:insert into t1 select 2 where pg_try_advisory_xact_lock(123,2)
=C2=A0 o= n conflict(id) do nothing;

-- Session 1:

-- silently 'fai= ls' because no lock is granted, so inserts zero rows:
insert into t1= select 2 where pg_try_advisory_xact_lock(123,2)
=C2=A0 on conflict(id) = do nothing;

-- Session 2:

-- silently 'fails' because= no lock is granted, so inserts zero rows:
insert into t1 select 1 where= pg_try_advisory_xact_lock(123,1)
=C2=A0 on conflict(id) do nothing;
=
-- inserts one row:
insert into t1 select 3 where pg_try_advisory_xa= ct_lock(123,3)
=C2=A0 on conflict(id) do nothing;

commit; -- lock on 2 and 3 goes away

-- Session 1:

-- silently fails because of the on conflict clause
insert into t1 sel= ect 3 where pg_try_advisory_xact_lock(123,3)
=C2=A0 on conflict(id) do n= othing;


Cheers,
Greg<= /div>

--00000000000013038c0622b5dde1--