pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedchecking existence of a table before updating its SERIAL
3+ messages / 3 participants
[nested] [flat]
* checking existence of a table before updating its SERIAL
@ 2020-06-08 07:53 Matthias Apitz <guru@unixarea.de>
2020-06-08 08:05 ` Re: checking existence of a table before updating its SERIAL David G. Johnston <david.g.johnston@gmail.com>
2020-06-08 08:18 ` Re: checking existence of a table before updating its SERIAL Thomas Kellerer <shammat@gmx.net>
0 siblings, 2 replies; 3+ messages in thread
From: Matthias Apitz @ 2020-06-08 07:53 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello,
We're updating the SERIAL of a bunch of tables with a SQL script which
does for any table:
/* table: idm_tasktab */
DO $$
DECLARE
max_id int;
BEGIN
SELECT INTO max_id GREATEST(COALESCE(max(taskid), 0),0) + 1 FROM idm_tasktab;
RAISE NOTICE '% % %', 'idm_tasktab', 'taskid', max_id ;
EXECUTE 'ALTER SEQUENCE idm_tasktab_taskid_seq RESTART ' || max_id::text;
END $$ LANGUAGE plpgsql;
Can some kind soul help me with doing a test for the existence of the
table to avoid the error message about non existing relation?
Thanks in advance.
matthias
--
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: checking existence of a table before updating its SERIAL
2020-06-08 07:53 checking existence of a table before updating its SERIAL Matthias Apitz <guru@unixarea.de>
@ 2020-06-08 08:05 ` David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: David G. Johnston @ 2020-06-08 08:05 UTC (permalink / raw)
To: Matthias Apitz <guru@unixarea.de>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Monday, June 8, 2020, Matthias Apitz <guru@unixarea.de> wrote:
>
> Can some kind soul help me with doing a test for the existence of the
> table to avoid the error message about non existing relation?
>
https://www.postgresql.org/docs/12/catalogs-overview.html
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: checking existence of a table before updating its SERIAL
2020-06-08 07:53 checking existence of a table before updating its SERIAL Matthias Apitz <guru@unixarea.de>
@ 2020-06-08 08:18 ` Thomas Kellerer <shammat@gmx.net>
1 sibling, 0 replies; 3+ messages in thread
From: Thomas Kellerer @ 2020-06-08 08:18 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Matthias Apitz schrieb am 08.06.2020 um 09:53:
> We're updating the SERIAL of a bunch of tables with a SQL script which
> does for any table:
>
> /* table: idm_tasktab */
> DO $$
> DECLARE
> max_id int;
> BEGIN
> SELECT INTO max_id GREATEST(COALESCE(max(taskid), 0),0) + 1 FROM idm_tasktab;
> RAISE NOTICE '% % %', 'idm_tasktab', 'taskid', max_id ;
> EXECUTE 'ALTER SEQUENCE idm_tasktab_taskid_seq RESTART ' || max_id::text;
> END $$ LANGUAGE plpgsql;
>
> Can some kind soul help me with doing a test for the existence of the
> table to avoid the error message about non existing relation?
I think the easiest way is to use to_regclass():
DO $$
DECLARE
max_id int;
BEGIN
if to_regclass('idm_tasktab') is not null then
SELECT INTO max_id GREATEST(COALESCE(max(taskid), 0),0) + 1 FROM idm_tasktab;
RAISE NOTICE '% % %', 'idm_tasktab', 'taskid', max_id ;
EXECUTE 'ALTER SEQUENCE idm_tasktab_taskid_seq RESTART ' || max_id::text;
end if;
END $$ LANGUAGE plpgsql;
Note that you don't really need dynamic SQL for this, you can simplify this to:
select setval('idm_tasktab_taskid_seq', GREATEST(COALESCE(max(taskid), 0),0))
from idm_tasktab;
I also don't think greatest() is necessary.
Thomas
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2020-06-08 08:18 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-06-08 07:53 checking existence of a table before updating its SERIAL Matthias Apitz <guru@unixarea.de>
2020-06-08 08:05 ` David G. Johnston <david.g.johnston@gmail.com>
2020-06-08 08:18 ` Thomas Kellerer <shammat@gmx.net>
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