From jscalia@cityblueprinting.com Fri Jun 17 20:04:19 2005 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 73C9F528C6 for ; Fri, 17 Jun 2005 17:04:19 -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 10628-07 for ; Fri, 17 Jun 2005 20:04:13 +0000 (GMT) Received: from ylpvm25.prodigy.net (ylpvm25-ext.prodigy.net [207.115.57.56]) by svr1.postgresql.org (Postfix) with ESMTP id A9EB6528D9 for ; Fri, 17 Jun 2005 17:04:12 -0300 (ADT) Received: from bitstream.cityblueprinting.com (bitstream.cityblueprinting.com [68.252.88.58]) by ylpvm25.prodigy.net (8.12.10 auth mps linux/8.12.10) with ESMTP id j5HK4feL016309 for ; Fri, 17 Jun 2005 16:04:41 -0400 Received: from John (unknown [192.168.1.16]) by bitstream.cityblueprinting.com (Postfix) with ESMTP id A04F0D2 for ; Fri, 17 Jun 2005 15:48:06 -0400 (EDT) From: "John Scalia" To: Subject: getting a sequence to update properly Date: Fri, 17 Jun 2005 16:04:12 -0400 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_005A_01C57356.34344670" X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478 Thread-Index: AcVzd7sDE2giuXSpRrCwp0N9tcDbZg== Message-Id: <20050617194806.A04F0D2@bitstream.cityblueprinting.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.057 tagged_above=0 required=5 tests=HTML_30_40, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200506/144 X-Sequence-Number: 17899 This is a multi-part message in MIME format. ------=_NextPart_000_005A_01C57356.34344670 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit I'm trying to add a sequence function to an already existing field. All the 4000 records for this table currently have a value already in this field, but I'd like new ones to just get the next value by default as opposed to users being forced to type something. What I'd like to do is to reload this table and get the field to be updated by the sequence on the reload. I thought that by dumping the table with pg_dump and reloading with the generated datafile, this would happen, but the sequence's last_value field didn't change. Apparently, the values contained in the dump file won't update the sequence. Short of thoroughly massaging the datafile generated by the dump and turning all those record lines into SQL INSERT statements, is there any way to effectively update a field for an existing record so that the field gets the value from the sequence? I would normally just set minimum value to something greater than what already in the table, 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. -- John ------=_NextPart_000_005A_01C57356.34344670 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
I'm = trying to add a=20 sequence function to an already existing field. All the 4000 records for = this=20 table currently have a value already in this field, but I'd like new = ones to=20 just get the next value by default as opposed to users being forced to = type=20 something. What I'd like to do is to reload this table and get the field = to be=20 updated by the sequence on the reload. I thought that by dumping the = table with=20 pg_dump and reloading with the generated datafile, this would happen, = but the=20 sequence's last_value field didn't change. Apparently, the values = contained in=20 the dump file won't update the sequence. Short of thoroughly massaging = the=20 datafile generated by the dump and turning all those record lines into = SQL=20 INSERT statements, is there any way to effectively update a field for an = existing record so that the field gets the value from the = sequence? I=20 would  normally just set minimum value to something greater than = what=20 already in the table, but the manually keyed values are all over the = place, and=20 I guess I'd really like to remove the gaps in the current=20 values.
--
John
------=_NextPart_000_005A_01C57356.34344670-- From greg@turnstep.com Sat Jun 18 13:56:34 2005 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-----