pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Matthias Apitz <guru@unixarea.de>
To: pgsql-general@postgresql.org
Subject: Re: updating sequence value for column 'serial'
Date: Tue, 24 Sep 2019 08:01:46 -0700
Message-ID: <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> (raw)
In-Reply-To: <20190924144738.GA2377@c720-r342378>
References: <20190924144738.GA2377@c720-r342378>

On 9/24/19 7:47 AM, Matthias Apitz wrote:
> 
> Hello,
> 
> We have in a database some 400 tables, 75 of them have a 'serial'
> column, like the one in the example table 'titel_daten', column 'katkey'.
> 
> I want to create a SQL script to adjust alls these sequences to the
> max+1 value in its column after loading the database from CSV file.
> I found no other way as the code below (the RAISE NOTICE is
> only for test at the moment and the output is correct for this table,
> i.e current max in 'katkey' is 330721):
> 
> sisis=# DO $$
> sisis$# DECLARE
> sisis$#    maxikatkey integer := ( select max(katkey) from titel_daten );
> sisis$#    result integer := 1;
> sisis$# BEGIN
> sisis$#    maxikatkey := maxikatkey +1;
> sisis$#    RAISE NOTICE '%', maxikatkey ;
> sisis$#    result := (SELECT SETVAL('titel_daten_katkey_seq', maxikatkey) );
> sisis$#    RAISE NOTICE '%', result ;
> sisis$# END $$;
> NOTICE:  330722
> NOTICE:  330723
> DO
> 
> Is there any better way? Thanks

I have not found a better way. I use ALTER SEQUENCE .. RESTART 330722 
though:

https://www.postgresql.org/docs/11/sql-altersequence.html


That is roughly equivalent to

SELECT SETVAL('titel_daten_katkey_seq', maxikatkey, false)

in that the next value used will be 330722 not 330723.

RESTART is also transactional whereas SETVAL() is not.

> 
> 	matthias
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (6+ messages)  latest in thread

Message-ID: <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com>
Permalink:  ../7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com/
Also on:    postgresql.org/message-id/7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.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: adrian.klaver@aklaver.com, guru@unixarea.de
  Subject: Re: updating sequence value for column 'serial'
  In-Reply-To: <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com>
  References: <20190924144738.GA2377@c720-r342378> <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.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