pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Sabino Mullane <greg@turnstep.com>
To: pgsql-admin@postgresql.org
Cc: jscalia@cityblueprinting.com
Subject: Re: getting a sequence to update properly
Date: Sat, 18 Jun 2005 13:56:26 -0000
Message-ID: <c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com> (raw)
In-Reply-To: <20050617194806.A04F0D2@bitstream.cityblueprinting.com>


-----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-----





view thread (2+ messages)

Message-ID: <c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com>
Permalink:  ../c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com/
Also on:    postgresql.org/message-id/c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: greg@turnstep.com, jscalia@cityblueprinting.com
  Subject: Re: getting a sequence to update properly
  In-Reply-To: <c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com>
  References: <20050617194806.A04F0D2@bitstream.cityblueprinting.com> <c7e8d92e65fcb10af06cc74e153e8d28@biglumber.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox