Re: Resetting SEQUENCEs

From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: "Laurent ROCHE" <laurent_roche(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Resetting SEQUENCEs
Date: 2007-10-18 17:33:50
Message-ID: 92869e660710181033hf1c3467u4f98ec9fdad5aca1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2007/10/18, Laurent ROCHE <laurent_roche(at)yahoo(dot)com>:
>
> Hi,
>
> I am quite surprised I could not find a way to automatically reset the value
> of a sequence for all my tables.
>
> Of course, I can write:
> SELECT setval('serial', max(id)) FROM distributors
> But if I reload data into all my tables, it's a real pain to have to write
> something like this for every single table with a sequence.
>
> I would expect PostgreSQL to provide some command like:
> resynchAllSequences my_schema;

try something like

CREATE FUNCTION execute(in_sql TEXT) RETURNS void as $$
BEGIN
EXECUTE in_sql;
RETURN;
END;
$BODY$ language plpgsql;

select execute(
$$select setval( '$$
|| table_name ||
$$_id_seq', coalesce((select max(id) from $$
|| table_name ||
$$),1), false ) $$
) from information_schema.tables where you want;

--
Filip Rembiałkowski

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laurent ROCHE 2007-10-18 17:38:39 pg_dump SERIAL and SEQUENCE
Previous Message Alan Hodgson 2007-10-18 17:33:48 Re: Resetting SEQUENCEs