pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sebastien Flaesch <sebastien.flaesch@4js.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Concurrent INSERT statements with RETURNING clause resetting SERIAL sequence
Date: Wed, 20 Jul 2022 06:44:10 +0000
Message-ID: <DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <1156239.1658245261@sss.pgh.pa.us>
References: <DBAP191MB1289AC6BB681A44166256884B08F9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
	<1156239.1658245261@sss.pgh.pa.us>


Changing a bit the CASE/WHEN logic in the RETURNING clause solves the issue when concurrently inserting rows without specifying explicitly a value for the serial column (INSERTs specifying values for the serial column are seldom used by concurrent programs inserting many rows):

        insert into tab1 (name) VALUES ('aaa')
            returning tab1.pkey,
                      (select case
                         when tab1.pkey>(select last_value from public.tab1_pkey_seq)
                              then setval('public.tab1_pkey_seq',tab1.pkey,true)
                         else 0
                       end)


Example (assuming there is no unique constraint!):

  1.  INSERT without value for serial column => sequence=0+1 => last_value = 1
  2.  INSERT without value for serial column => sequence=1+1 => last_value = 2
  3.  INSERT with value 2 for serial column => tab1.pkey(2) > last_value(2) ? false => no sequence reset
  4.  INSERT without value for serial column => sequence=2+1 => last_value = 3
  5.  INSERT with value 4 for serial column => tab1.pkey(4) > last_value ? true => setval(seqname,4,true)
  6.  INSERT without value for serial column => sequence=4+1 => last_value = 5

This will also save setval() calls for each INSERT not specifying a value explicitly for the serial column.

Stop me if I am wrong... 🙂

Seb




Seb
________________________________
From: Tom Lane <tgl@sss.pgh.pa.us>
Sent: Tuesday, July 19, 2022 5:41 PM
To: Sebastien Flaesch <sebastien.flaesch@4js.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Concurrent INSERT statements with RETURNING clause resetting SERIAL sequence

EXTERNAL: Do not click links or open attachments if you do not recognize the sender.

Sebastien Flaesch <sebastien.flaesch@4js.com> writes:
> I try to update the underlying sequence of a SERIAL column, by using a RETURNING clause in my INSERT statement, which is checking that the column value is greater than the last_value of my sequence, and reset the sequence 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


view thread (10+ messages)  latest in thread

Message-ID: <DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
Permalink:  ../DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM/
Also on:    postgresql.org/message-id/DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: sebastien.flaesch@4js.com, tgl@sss.pgh.pa.us
  Subject: Re: Concurrent INSERT statements with RETURNING clause resetting SERIAL sequence
  In-Reply-To: <DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
  References: <DBAP191MB1289AC6BB681A44166256884B08F9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> <1156239.1658245261@sss.pgh.pa.us> <DBAP191MB1289545A8B7EC7100330FD5BB08E9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox