Re: My very first PL/pgSQL procedure... - Mailing list pgsql-general

From Philippe Ferreira
Subject Re: My very first PL/pgSQL procedure...
Date
Msg-id 43D7E96F.1000709@worldonline.fr
Whole thread Raw
In response to Re: My very first PL/pgSQL procedure...  ("Jim Buttafuoco" <jim@contactbda.com>)
Responses Re: My very first PL/pgSQL procedure...  ("Jim Buttafuoco" <jim@contactbda.com>)
List pgsql-general
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.

pgsql-general by date:

Previous
From: Patrick Hatcher
Date:
Subject: Re: Trigger question: ROW or STATEMENT?
Next
From: Tom Lane
Date:
Subject: Re: Postgresql Segfault in 8.1