Within a plpgsql function I create a table and use a cursor to access its rows. While at first row, I delete a following one and surprisingly (to me at least) the cursor fetches it. When repeating within the same function, it works as I expected it so.
However, if the table pre-exists and is not created within the function, the deleted row is never fetched.
What am I missing?
DECLARE
curs1 refcursor;
rec record;
BEGIN
CREATE TABLE test as select generate_series(1,5,1) test_id;
OPEN curs1 FOR SELECT * FROM test ORDER BY test_id;
LOOP
FETCH curs1 INTO rec;
EXIT WHEN NOT FOUND;
RAISE NOTICE 'ROW:%',rec.test_id;
IF rec.test_id=1 THEN
DELETE FROM TEST WHERE test_id=3;
END IF;
END LOOP;
CLOSE curs1;
RAISE NOTICE 'AGAIN';
--just repeating without deleting
OPEN curs1 FOR SELECT * FROM test ORDER BY test_id;
LOOP
FETCH curs1 INTO rec;
EXIT WHEN NOT FOUND;
RAISE NOTICE 'ROW:%',rec.test_id;
END LOOP;
CLOSE curs1;
Output is:
NOTICE: ROW:1
NOTICE: ROW:2
NOTICE: ROW:3
NOTICE: ROW:4
NOTICE: ROW:5
NOTICE: AGAIN
NOTICE: ROW:1
NOTICE: ROW:2
NOTICE: ROW:4
NOTICE: ROW:5