Re: autoupdate sequences after copy - Mailing list pgsql-general

From
Subject Re: autoupdate sequences after copy
Date
Msg-id 65421.216.238.112.88.1065790092.squirrel@$HOSTNAME
Whole thread Raw
In response to Re: autoupdate sequences after copy  ("Ian Harding" <ianh@tpchd.org>)
List pgsql-general
> I just run a script to update them after importing data.  Something
> like this... (not a real script...)
>
> while read tablename
> do
>     echo "select setval('${tablename}_${tablename}_seq', \
>         (select max(${tablename}id) from $tablename))" | psql database
> done
> < tablenames.txt
>
> Of course, this assumes you allowed the default sequence names to be
> created via SERIAL and that you created the primary keys as
> <tablename>id.  You might need a text file with table, key, and
> sequence names, but this is likely easier than issuing a bunch of psql
> commands by hand.
>

You can get a list of you sequences with

CREATE VIEW public.sequences AS
SELECT
 nspname,
 pg_get_userbyid(c.relowner) AS sequenceowner,
 c.relname AS sequencename
FROM (pg_class c JOIN pg_namespace t2 ON ((t2.oid = c.relnamespace)))
WHERE (c.relkind = 'S'::"char")
ORDER BY nspname, pg_get_userbyid(c.relowner), c.relname;

Maybe you use the output of this view to create a table in which you add
columns for the table name and column name, and then go through the kind
of iteration suggested above.


~Berend Tober




pgsql-general by date:

Previous
From: Martin Hart
Date:
Subject: problem with pg_dump and restoring sequences
Next
From: Gene Vital
Date:
Subject: Re: Parent Id