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 1oDpLP-0004jP-9f for pgsql-general@arkaria.postgresql.org; Tue, 19 Jul 2022 15:41:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oDpLO-0002Tu-6b for pgsql-general@arkaria.postgresql.org; Tue, 19 Jul 2022 15:41:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDpLN-0002MS-Sb for pgsql-general@lists.postgresql.org; Tue, 19 Jul 2022 15:41:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDpLL-0004OF-ON for pgsql-general@lists.postgresql.org; Tue, 19 Jul 2022 15:41:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 26JFf1vo1156240; Tue, 19 Jul 2022 11:41:01 -0400 From: Tom Lane To: Sebastien Flaesch cc: pgsql-general Subject: Re: Concurrent INSERT statements with RETURNING clause resetting SERIAL sequence In-reply-to: References: Comments: In-reply-to Sebastien Flaesch message dated "Tue, 19 Jul 2022 15:33:52 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1156238.1658245261.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 19 Jul 2022 11:41:01 -0400 Message-ID: <1156239.1658245261@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sebastien Flaesch writes: > I try to update the underlying sequence of a SERIAL column, by using a R= ETURNING clause in my INSERT statement, which is checking that the column = value is greater than the last_value of my sequence, and reset the sequenc= e with setval() if needed. It's not too surprising that that doesn't work, if you're coding it based on this assumption: > The whole INSERT statement (including the code in the RETURNING clause),= should execute in a ATOMIC manner. Sequence-related actions are always carried out immediately, they do not participate in any atomicity guarantees about the calling transaction. Without this, any sequence update would have to block all concurrent uses of that sequence until they see whether the first update commits. If that's the behavior you want, you can build it out of standard SQL facilities (e.g. update a one-row table). The point of sequence objects is exactly to provide a feature with better concurrent performance, at the cost of no rollback guarantees. So, there's no bug here, and calling it one isn't going to change anybody's mind about that. regards, tom lane