pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@postgresql.org
Subject: Re: Reset Sequence number
Date: Wed, 22 Nov 2017 12:04:45 +0100
Message-ID: <ov3lk5$eke$1@blaine.gmane.org> (raw)
In-Reply-To: <CA+wLFo3kRQh+qbY5stqipzGC1EPryd8aTRH3pY0=NxcXw_xo-A@mail.gmail.com>
References: <CA+wLFo3kRQh+qbY5stqipzGC1EPryd8aTRH3pY0=NxcXw_xo-A@mail.gmail.com>

Brahmam Eswar schrieb am 22.11.2017 um 10:36:
> 
> we are in process of migrating to postgres and need to reset the
> sequence number with highest value of table key. I want to make it
> procedural to do that as mentioned below, But it's throwing an error

> DO $$
>  DECLARE
>  SEQ BIGINT;
>  BEGIN 
>  
>  SEQ:=(SELECT MAX(ID) FROM TABLE_1);
>  ALTER SEQUENCE TABLE_1_SEQ  RESTART WITH SEQ;
>   
>  END$$; 
>  
> Error : syntax error at or near "SEQ"

No need for a PL/pgSQL block.

You can do that with a plain SQL statement using setval():

   select setval('table_1_seq', (select max(id) from table1));






view thread (5+ messages)

Message-ID: <ov3lk5$eke$1@blaine.gmane.org>
Permalink:  ../ov3lk5$eke$1@blaine.gmane.org/
Also on:    postgresql.org/message-id/ov3lk5$eke$1@blaine.gmane.org

 · 

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: spam_eater@gmx.net
  Subject: Re: Reset Sequence number
  In-Reply-To: <ov3lk5$eke$1@blaine.gmane.org>
  References: <CA+wLFo3kRQh+qbY5stqipzGC1EPryd8aTRH3pY0=NxcXw_xo-A@mail.gmail.com> <ov3lk5$eke$1@blaine.gmane.org>

* 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