pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthias Apitz <guru@unixarea.de>
To: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-general@postgresql.org
Subject: Re: updating sequence value for column 'serial'
Date: Thu, 26 Sep 2019 07:12:46 +0200
Message-ID: <20190926051246.GA2939@c720-r342378> (raw)
In-Reply-To: <3d189c7f-2e45-a1a4-fc53-5dff1d364540@aklaver.com>
References: <20190924144738.GA2377@c720-r342378>
	<7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com>
	<20190925054011.GA2887@c720-r342378>
	<3d189c7f-2e45-a1a4-fc53-5dff1d364540@aklaver.com>

El día miércoles, septiembre 25, 2019 a las 07:42:11a. m. -0700, Adrian Klaver escribió:

> >>> 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:
> > 
> > Yes, but I found no way to use a variable like 'maxikatkey' in the ALTER SEQUENCE ...
> > it only excepts digits like 330722.
> 
> DO $$
> DECLARE
>      max_id int;
> BEGIN
>      SELECT INTO max_id max(id) + 1 FROM seq_test;
>      RAISE NOTICE 'Max id is %', max_id;
>      EXECUTE 'ALTER SEQUENCE seq_test_id_seq RESTART ' || max_id::text;
> END;
> $$ LANGUAGE plpgsql;

Hi Adrian,

I adopted your code to the name of my table 'ig_target_ipfilter' and its
SERIAL column 'id'; it does not work (and I don't know how it could
works because in the 'ALTER SEQUENCE ...' stmt is somehow missing '...  WITH value ...')
or do I understand something wrong?):

cat -n /home/apitzm/postgreSQL/test.sql
     1  DO $$
     2  DECLARE
     3      max_id int;
     4  BEGIN
     5      SELECT INTO max_id max(id) + 1 FROM ig_target_ipfilter ;
     6      RAISE NOTICE 'Max id in % is %', 'ig_target_ipfilter', max_id;
     7      EXECUTE 'ALTER SEQUENCE ig_target_ipfilter_id_seq RESTART ' || max_id::text;
     8  END;
     9  $$ LANGUAGE plpgsql;

psql -Usisis -dsisis < /home/apitzm/postgreSQL/test.sql
NOTICE:  Max id in ig_target_ipfilter is <NULL>
ERROR:  query string argument of EXECUTE is null
KONTEXT:  PL/pgSQL function inline_code_block line 7 at EXECUTE

Please clarify. Thanks

	matthias


-- 
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub

Mientras haya voluntad de lucha habrá esperanza de vencer.

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (6+ messages)  latest in thread

Message-ID: <20190926051246.GA2939@c720-r342378>
Permalink:  ../20190926051246.GA2939@c720-r342378/
Also on:    postgresql.org/message-id/20190926051246.GA2939@c720-r342378

 · 

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: guru@unixarea.de, adrian.klaver@aklaver.com
  Subject: Re: updating sequence value for column 'serial'
  In-Reply-To: <20190926051246.GA2939@c720-r342378>
  References: <20190924144738.GA2377@c720-r342378> <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> <20190925054011.GA2887@c720-r342378> <3d189c7f-2e45-a1a4-fc53-5dff1d364540@aklaver.com> <20190926051246.GA2939@c720-r342378>

* 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