For my Hi/Lo implementation, I need a function which will acquire exclusive table lock, update value and select one row. I came up with the following code:
CREATE OR REPLACE FUNCTION bb_next_hi(tbl varchar(35))
RETURNS setof record AS
$$
LOCK TABLE hilo IN ACCESS EXCLUSIVE MODE;
OPEN ref FOR SELECT hi as "Hi", lo as "Lo", "table" as "Table" FROM hilo WHERE "table" = $1;
UPDATE hilo SET hi = hi + 1 WHERE "table" = $1
RETURN ref;
$$ LANGUAGE plpgsql;
However, when invoking the function, it returns not a row, but one column with a content similar to "unnamed portal 3". I guess I should iterate over returned ref (but how)?
Another approach that I could use is to use UPDATE RETURNING statement, but I'm not sure if race conditions can occur in this case. Any help would be appreciated. Thanks