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 43D92B49.8060809@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...  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
> why not just use setval(), see docs for arguments.


I think that "setval('seq_name', xx)" have the same effect than
"SEQUENCE seq_name RESTART WITH xx" (the instruction I
wanted to use in my first function).

But the problem is that in both cases, the sequence should be
locked in order to prevent problems with concurrent transactions.
For example, if I want to raise the sequence value to "1000", while
its current value is "998", I would call :
     setval('seq_name', 1000);

But because the sequence could not be locked, some concurrent
transactions could have already raised it's current value in the
meantime to, say, "1002", before the effective execution of setval().

So, instead of raising the value to 1000, my function could have
done the opposite (from 1002 to 1000) ! And the two next "INSERT"
using this sequence would then break !!

The only solution I found to prevent this is with my "loops" !!

Best regards,
Philippe Ferreira.

pgsql-general by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Arrays
Next
From: "Oisin Glynn"
Date:
Subject: VACUUM Question