update sequence conversion script

Поиск
Список
Период
Сортировка
Искать
От
Sim Zacks
Тема
update sequence conversion script
Дата
Msg-id
1381227955.20041011103903@compulab.co.il
Список
Дерево обсуждения
update sequence conversion script Sim Zacks <sim@compulab.co.il>
Re: update sequence conversion script Richard Huxton <dev@archonet.com>
Re: update sequence conversion script Terry Lee Tucker <terry@esc1.com>
Re: update sequence conversion script Peter Eisentraut <peter_e@gmx.net>
Re: update sequence conversion script Michael Fuhr <mike@fuhr.org>
I am in the process of converting an existing database to PostGreSQL
and wrote a generic script to update all of the sequences as they default at 1.
I thought it would be useful to other people who are converting their
databases.

If anyone can write this script in using plpythonu, I would love to
see how it is done.

create or replace function UpdateSequences() returns varchar(50) as
$$
declare 
        seqrecord record;
        tblname varchar(50);
        fieldname varchar(50);
        maxrecord record;
        maxvalue integer;
begin
        for seqrecord in select relname from pg_statio_user_sequences Loop
                tblname:=split_part(seqrecord.relname,'_',1);
                fieldname:=split_part(seqrecord.relname,'_',2);
                for maxrecord in execute 'select max(' || fieldname || ') as f1 from ' || tblname LOOP
                        maxvalue:=maxrecord.f1;
                end loop;
                execute 'alter sequence ' || seqrecord.relname || ' restart with ' || coalesce(maxvalue,0)+1 ;
        End LOOP;
        return 1;
end
$$
 language plpgsql

Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax

В списке pgsql-general по дате отправления
От: dom@happygiraffe.net (Dominic Mitchell)
Дата:
От: Armen Rizal
Дата:
Сообщение: Reusable pl/pgsql samples ?
FAQ