X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5DB385282C for ; Sat, 18 Jun 2005 10:56:34 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 67143-02 for ; Sat, 18 Jun 2005 13:56:24 +0000 (GMT) Received: from biglumber.com (biglumber.com [207.228.252.42]) by svr1.postgresql.org (Postfix) with SMTP id B186652861 for ; Sat, 18 Jun 2005 10:56:22 -0300 (ADT) Received: (qmail 20158 invoked from network); 18 Jun 2005 13:56:26 -0000 Received: from unknown (HELO localhost) (207.228.252.42) by 0 with SMTP; 18 Jun 2005 13:56:26 -0000 From: "Greg Sabino Mullane" To: pgsql-admin@postgresql.org Cc: jscalia@cityblueprinting.com Cc: jscalia@cityblueprinting.com Subject: Re: getting a sequence to update properly X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 X-Request-PGP: http://www.biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 In-Reply-To: <20050617194806.A04F0D2@bitstream.cityblueprinting.com> Date: Sat, 18 Jun 2005 13:56:26 -0000 X-Mailer: JoyMail 1.48 Message-ID: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.228 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200506/147 X-Sequence-Number: 17902 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 > I would normally just set minimum value to something greater than what > already in the table, That's the way to go. > but the manually keyed values are all over the place, and I guess I'd > really like to remove the gaps in the current values. You'll have to do that yourself, perhaps with a plpgsql function or an perl script. Here's a quick little function that should do the job: give it a tablename and a column. The column must contain unique, positive ids. After running, just slap a default nextval on the column with a sequence starting at one above the number returned and you're done. CREATE OR REPLACE FUNCTION removeholes(text,text) RETURNS INT LANGUAGE plpgsql AS $$ DECLARE T ALIAS FOR $1; C ALIAS FOR $2; myst TEXT; mycount INT := 0; myrec RECORD; BEGIN myst := 'UPDATE '||T||' SET '||C||' = -'||C; EXECUTE myst; myst := 'SELECT '||C||' FROM '||T||' ORDER BY '||C||' DESC '; FOR myrec IN EXECUTE myst LOOP mycount := mycount + 1; myst := 'UPDATE '||T||' SET '||C||' = '||mycount||' WHERE '||C||' = '||myrec.id; EXECUTE myst; END LOOP; RETURN mycount; END; $$; - -- Greg Sabino Mullane greg@turnstep.com PGP Key: 0x14964AC8 200506180923 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -----BEGIN PGP SIGNATURE----- iD8DBQFCtCfbvJuQZxSWSsgRAuBJAJ4q9STr6u6fhssye0l8FitirLZwzQCgwR8g G2+MqdQquDJz8O3G6zBsVS4= =MfLV -----END PGP SIGNATURE-----