Received: from ece.rice.edu (ece.rice.edu [128.42.4.34]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f9TJIIP48080 for ; Mon, 29 Oct 2001 14:18:18 -0500 (EST) (envelope-from reedstrm@rice.edu) Received: from localhost (localhost [127.0.0.1]) by ece.rice.edu (Postfix) with ESMTP id 7BEA568A5A; Mon, 29 Oct 2001 13:18:16 -0600 (CST) Received: from wallace.ece.rice.edu (wallace.ece.rice.edu [128.42.12.154]) by ece.rice.edu (Postfix) with ESMTP id B4E0868A17; Mon, 29 Oct 2001 13:18:15 -0600 (CST) Received: from reedstrm by wallace.ece.rice.edu with local (Exim 3.22 #1 (Debian)) id 15yHvL-0006AT-00; Mon, 29 Oct 2001 13:18:15 -0600 Date: Mon, 29 Oct 2001 13:18:15 -0600 From: "Ross J. Reedstrom" To: steve boyle Cc: pgsql-admin@postgresql.org Subject: Re: Code to automatically refresh sequences after loading data? Message-ID: <20011029131815.B23138@rice.edu> Mail-Followup-To: "Ross J. Reedstrom" , steve boyle , pgsql-admin@postgresql.org References: <9rerbg$25uq$1@news.tht.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9rerbg$25uq$1@news.tht.net> User-Agent: Mutt/1.3.18i X-Virus-Scanned: by AMaViS snapshot-20010714 X-Archive-Number: 200110/250 X-Sequence-Number: 2494 On Sat, Oct 27, 2001 at 06:36:16PM +0100, steve boyle wrote: > 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. You need to go grovelling through pg_attrdef, looking for the sequence name in the adsrc field. Then the adrelid field gives you the oid from pg_class of the table, and the adnum gives you the ordinal for the column that has this default, which is in pg_attribute.attnum. Be careful of other tricky uses of sequences: recent discussion on one of these lists has been about isung one sequence to generate unique ids across _multiple_ tables. I usually keep a hand edited file around with my (also hand-edited) defining schema, in which I have a bunch of: SELECT setval('sequence_name_here',max(column_name)) from tablename; Generated by a little awk/sed/grep of the schema, or from some SQL on the db. Ah, this should be useful: select 'SELECT setval(''"'||cs.relname||'"'', max("'||attname||'") FROM "'||c.relname||'";' from pg_class c, pg_class cs, pg_attribute a, pg_attrdef d where cs.relkind = 'S' and d.adsrc ~ cs.relname and c.oid = a.attrelid and c.oid = d.adrelid and d.adnum = a.attnum; Here's what it does on a simple db of mine (excuse the wrap) all those quotes are to make it MixEdCase proof. ?column? --------------------------------------------------------------------------------------- SELECT setval('"people_peid_seq"', max("peid") FROM "people"; SELECT setval('"other_programs_prog_id_seq"', max("prog_id") FROM "other_programs"; SELECT setval('"other_courses_course_id_seq"', max("course_id") FROM "other_courses"; Ross > > 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 > > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly -- Ross Reedstrom, Ph.D. reedstrm@rice.edu Executive Director phone: 713-348-6166 Gulf Coast Consortium for Bioinformatics fax: 713-348-6182 Rice University MS-39 Houston, TX 77005