> I lock just that particular row, which is no good. I need to have all
> the codes for the service 1 locked, so if it happens that two users send
> the very same code, one has to fail. Therefore, from within plpgsql I
> first do:
I'm a bit tired tonight so I'll simplify your example :
CREATE TABLE stuff ( a INT, b INT );
Basically you want to lock ALL rows with a certain value of a, in order
to perform an operation on only one of them.You could do this :
CREATE TABLE all_as ( a INT PRIMARY KEY )
CREATE TABLE stuff ( a INT REFERENCES all_as(a), b INT );
Now all the rows in "stuff" that have the same value of "a" reference the
same row in "all_as".All you have to do is
SELECT * FROM all_as WHERE a=the value FOR UPDATE
and you lock all rows having that particular value of a in the big table.