I call this Postgres function from a few threads through Hiberante:
UPDATE table SET active = _active
WHERE id IN (
SELECT id FROM table
WHERE active = 0 ORDER BY id LIMIT 5);
SELECT count('PK') INTO cnt FROM table WHERE active = _active;
IF(cnt > 0)THEN
FOR ret IN
SELECT * FROM table WHERE active = _active
LOOP
RETURN NEXT ret;
END LOOP;
END IF;
I get data for few threads, handle it and after that remove from table. Is it safe? Should I use 'Select for update' or something else?