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 1tyGhK-004Tfn-Fv for pgsql-general@arkaria.postgresql.org; Fri, 28 Mar 2025 20:53:02 +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 1tyGhJ-00GWQ6-03 for pgsql-general@arkaria.postgresql.org; Fri, 28 Mar 2025 20:53:01 +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 1tyGhI-00GWPx-Gv for pgsql-general@lists.postgresql.org; Fri, 28 Mar 2025 20:53:00 +0000 Received: from mail-ua1-x92b.google.com ([2607:f8b0:4864:20::92b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tyGhF-001lpm-23 for pgsql-general@postgresql.org; Fri, 28 Mar 2025 20:53:00 +0000 Received: by mail-ua1-x92b.google.com with SMTP id a1e0cc1a2514c-86d5e3ddb66so1223341241.2 for ; Fri, 28 Mar 2025 13:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=singh-im.20230601.gappssmtp.com; s=20230601; t=1743195175; x=1743799975; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=B5S1cPZ+j6BtGK5KQVLm5ENljFR+TDIWkr/H7waMoN4=; b=M1mons9cV6PJWLwRO1+nl4xm6zpgdoDlc31vndDgNPIQRSWM9lpzntZEdNrrevftya /2uM9pZDtMYobEQXDhkzijiGMRSLda++rvF5EmNl+E7UX7dKEqkuCSoMiNh+YOy2IQyM BwdwvOLfZeGbUYT456RPVZJcs0XXUJUr5t562fcTCcVw7RX8lxJ9DraHfuYFFUBUZeTC sW9P8K8Jo65tbytzNOE2afES2cVo0JFxWT1Z0VaaJ+ixn77NFlZop7PZvzreF5GToD3Q jNXFsQl7FKDSEGyujynGaAhrINxPu2JJLcVKyqfm3A2h0I6BohqtWNBBbSln4pbWdOac CyjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743195175; x=1743799975; h=content-transfer-encoding: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=B5S1cPZ+j6BtGK5KQVLm5ENljFR+TDIWkr/H7waMoN4=; b=eSqEkSebpbBg0xh/bUAQYMp4AIcLuhP/FZUgYjx+0jDxFA2DTJZ8I3Gkkl5a8XKFV1 FBEOZSa5fGjh9T9gRY2Ds9vy3GQfMeH8UBe+3AwelRgJ4nEG7XyvrmkR8L4d+Gx0z1Db rzBvj0a5tz/h24tuppi9r+JmPTBkqYy069dEMbpiDErzWdD0AvRbLdYEVR/jeg5zlXWc DxzO6AwWyF81A/w7agFnQKSyhtB4+8SY4ajj5Gt+hkAS6dRY5YRzhgRd+4mSn6WZc8nI skm5Za6TW78gSU2gdCw++FbBF0KdqWzzqRLZNL2xhTJJLo/IhVH0z1TXF8s1ODWUWXcq hVow== X-Gm-Message-State: AOJu0YxnXydOfrOOQRKNY340ZKWJW/bmv1ozwIAofSxrh+5LxmOvHBYR TSu6OPXlPo5uB8rnEhOa/4G/6M6b3B8tCOoZgSvzaD0U79DQ7HbckJcZeSlBDDqjqoO95Alj937 tnSis2DFQRX+/CeNPAdxcPVg8f+c= X-Gm-Gg: ASbGncv944v9E6b2sKPo3adRv8k81g+lyOGIBaz8M+XHBmNvjvqEbutVX9u8bKNg8kG KMMIaYuNdYhJ3s9pjmxvgEz3Qc/QzWW71GFRBv0TPUUahsKTHvkGK2nVCrC6NEH2tUL1bP1kz81 8XCkqksIR9000b3SROY0LL1gltMtr7dDqnDoXBNaKa8OeQbd8xj5yoH5Tgog== X-Google-Smtp-Source: AGHT+IHoNpM1lv+/he008fjOvoMZeb4dOagF0x+LMozZqjqya+GB7iNdLEYfqC7A71z9bKcVM13cQH7UI48Bbl8byDU= X-Received: by 2002:a67:e7c7:0:b0:4c3:c9:c667 with SMTP id ada2fe7eead31-4c6d39a490emr1176595137.24.1743195175489; Fri, 28 Mar 2025 13:52:55 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Gurjeet Singh Date: Fri, 28 Mar 2025 13:52:18 -0700 X-Gm-Features: AQ5f1JoxlxRmuYSJNhhPQ2LLPw28UN9_e7A3tfoxYUPXdme8U3GjNwomUDCevzI Message-ID: Subject: Re: Concurrent delete and insert on same key, potentially a bug To: Sasa Vilic Cc: pgsql-general@postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Mar 28, 2025 at 12:39=E2=80=AFPM Sasa Vilic w= rote: > > Hello, > > Please excuse my ignorance, because I might be missing something obvious,= but how is this not a bug? > > I simply open 2 terminals and connect to the same database. > > **TERMINAL 1:** > > sasa=3D# create table tst1(id int primary key); > CREATE TABLE > sasa=3D# insert into tst1(id) values(1); > INSERT 0 1 > sasa=3D# begin; > BEGIN > sasa=3D# select * from tst1 where id =3D 1; > id > ---- > 1 > (1 row) > > **TERMINAL 2:** > > sasa=3D# begin; > BEGIN > sasa=3D# select * from tst1 where id =3D 1; > id > ---- > 1 > (1 row) > > **TERMINAL 1:** > > sasa=3D# delete from tst1 where id =3D 1; > DELETE 1 > sasa=3D# insert into tst1(id) values(1); > INSERT 0 1 > > **TERMINAL 2:** > > sasa=3D# delete from tst1 where id =3D 1; > // client is blocked due to row lock/index lock > > **TERMINAL 1:** > > sasa=3D# commit; > COMMIT > > **TERMINAL 2:** > // resumes because terminal 1 committed transaction > DELETE 0 > sasa=3D# insert into tst1(id) values(1); > ERROR: duplicate key value violates unique constraint "tst1_pkey" > DETAIL: Key (id)=3D(1) already exists. > > To my understanding, the second client (terminal 2) should already see ch= anges from the first client, because the transaction isolation level is REA= D COMMITTED and the first client did commit its transaction. > The session/transaction interactions you show are exactly how I'd expect the to interact. I don't see any buggy behaviour here. It would help others if you pointed at specific lines in the text above and share what you would expect instead to happen at that step. I think your concern is about the point in time where the second session prints DELETE 0, but can't say for sure. Best regards, Gurjeet http://Gurje.et