Re: My very first PL/pgSQL procedure...

From: "Jim Buttafuoco" <jim(at)contactbda(dot)com>
To: Philippe Ferreira <phil(dot)f(at)worldonline(dot)fr>, pgsql-general(at)postgresql(dot)org
Subject: Re: My very first PL/pgSQL procedure...
Date: 2006-01-25 22:08:48
Message-ID: 20060125220829.M51676@contactbda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

why not just use setval(), see docs for arguments.

---------- Original Message -----------
From: Philippe Ferreira <phil(dot)f(at)worldonline(dot)fr>
To: pgsql-general(at)postgresql(dot)org
Sent: Wed, 25 Jan 2006 22:11:11 +0100
Subject: Re: [GENERAL] My very first PL/pgSQL procedure...

> Hi,
>
> The only solution I've found to get the same reliable result, but
> without locking, is the dirty way (loops) :
> ---------------------------------------------------------------------------------------------------------------------
> CREATE OR REPLACE FUNCTION seq_min(sequence_name varchar, minval
> integer) RETURNS VOID AS $$
> DECLARE
> sequence_record RECORD;
> BEGIN
> -- Get the current sequence value :
> FOR sequence_record IN EXECUTE 'SELECT last_value FROM ' ||
> sequence_name LOOP
> NULL;
> END LOOP;
> -- Loop to bring the sequence to (at least) minval :
> WHILE sequence_record.last_value < minval LOOP
> -- Increment by 1 the sequence (and get the new value) :
> FOR sequence_record IN EXECUTE 'SELECT nextval(''' || sequence_name
> || ''') AS last_value' LOOP
> NULL;
> END LOOP;
> END LOOP;
> RETURN;
> END;
> $$ LANGUAGE plpgsql;
> ---------------------------------------------------------------------------------------------------------------------
>
> It gives the result I expect (and it doesn't interfere with concurrent
> uses of the sequence), but it is not very optimized !
> So, if someone have a better idea, I'm still open !
>
> Thank you,
> Philippe Ferreira.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
------- End of Original Message -------

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick Hatcher 2006-01-25 22:47:45 Re: Trigger question: ROW or STATEMENT?
Previous Message Doug McNaught 2006-01-25 21:36:07 Re: Trigger question: ROW or STATEMENT?