Received: from news.tht.net (news.hub.org [216.126.91.242] (may be forged)) by postgresql.org (8.11.3/8.11.4) with ESMTP id f9RI7AP78013 for ; Sat, 27 Oct 2001 14:07:10 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f9RHana71645 for pgsql-admin@postgresql.org; Sat, 27 Oct 2001 13:36:49 -0400 (EDT) (envelope-from news) From: "steve boyle" X-Newsgroups: comp.databases.postgresql.admin Subject: Code to automatically refresh sequences after loading data? Date: Sat, 27 Oct 2001 18:36:16 +0100 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 37 Message-ID: <9rerbg$25uq$1@news.tht.net> X-Complaints-To: scrappy@hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 To: pgsql-admin@postgresql.org X-Archive-Number: 200110/244 X-Sequence-Number: 2488 Has anyone got any code that will automatically carry out setvals for serial fields after loading data. I'm trying to write a function that will identify and go through all sequences within the current database and 'fix' the sequences to be set as the correct next value. I cannot find any way of consistently identifiying the table/field that the sequence has been defined over by interrogating the system catalogues. The function so far is: create function sys_refresh_sequences () returns integer as ' DECLARE myfield RECORD; tblname text; BEGIN FOR myseq IN SELECT * FROM pg_class WHERE relkind=''S'' ORDER BY relname LOOP myfield := substring(myseq.relname, 1, char_length(myseq.relname)-4); .... .... RAISE NOTICE ''REFRESHING SEQUENCE % ON %'', myseq.relname, myfield; END LOOP; return 1; END; ' language 'plpgsql'; Any pointers would be appreciated Many thanks Steve Boyle boylesa@dial.pipex.com