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 1kfpV8-0003ZP-6B for pgsql-sql@arkaria.postgresql.org; Thu, 19 Nov 2020 19:21:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfpV5-0001lP-PT for pgsql-sql@arkaria.postgresql.org; Thu, 19 Nov 2020 19:21:47 +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 1kfpV5-0001iD-Il for pgsql-sql@lists.postgresql.org; Thu, 19 Nov 2020 19:21:47 +0000 Received: from mail150.strasbourg.4js.com ([77.159.205.150]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfpV2-0006yQ-OY for pgsql-sql@lists.postgresql.org; Thu, 19 Nov 2020 19:21:46 +0000 Received: from [192.168.1.21] (lfbn-str-1-229-91.w86-243.abo.wanadoo.fr [86.243.189.91]) (authenticated bits=0) by mail150.strasbourg.4js.com (8.14.4/8.14.4/Debian-4+deb7u1) with ESMTP id 0AJJLeug019819 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 19 Nov 2020 20:21:41 +0100 To: pgsql-sql@lists.postgresql.org From: Sebastien FLAESCH Subject: Get last generated serial sequence and set it up when explicit value is used Organization: Four Js Development Tools Message-ID: Date: Thu, 19 Nov 2020 20:21:39 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Virus-Scanned: clamav-milter 0.99.4 at mail150 X-Virus-Status: Clean X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (mail150.strasbourg.4js.com [10.10.0.1]); Thu, 19 Nov 2020 20:21:41 +0100 (CET) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi all! Using SERIAL or BIGSERIAL column, I try to find a smart solution to do the following when an INSERT is done: 1) Retrieve the last generated sequence, so the program can use it. 2) Setup the underlying sequence, if an explicit value was used by the INSERT statement. So far I figured out the following by using the RETURNING clause... Is this ok / legal / without risk? (when multiple users insert rows at the same time?) test1=# create table table1 ( pkey serial not null primary key, name varchar(50) ); CREATE TABLE test1=# insert into table1 (name) values ('aaaa') returning pkey, (select last_value from table1_pkey_seq); pkey | last_value ------+------------ 1 | 1 (1 row) INSERT 0 1 test1=# insert into table1 (name) values ('aaaa') returning pkey, (select last_value from table1_pkey_seq); pkey | last_value ------+------------ 2 | 2 (1 row) INSERT 0 1 test1=# insert into table1 (pkey,name) values (100,'aaaa') returning pkey, (select last_value from table1_pkey_seq); pkey | last_value ------+------------ 100 | 2 (1 row) INSERT 0 1 I see 100 is > than 2, so reset the sequence: test1=# select setval('table1_pkey_seq',101,false); setval -------- 101 (1 row) test1=# insert into table1 (name) values ('aaaa') returning pkey, (select last_value from table1_pkey_seq); pkey | last_value ------+------------ 101 | 101 (1 row) INSERT 0 1 Any better way to do that in a single SQL statement? Is it legal to use a subquery in a RETURNING clause? Thanks! Seb