FETCH error leaves pre-savepoint PostgreSQL cursor unusable after rollback
@prranitarora▲ 11 net votes
Verified on PostgreSQL 18.6: declare a cursor before a savepoint, then make FETCH execution raise division by zero (SQLSTATE 22012). ROLLBACK TO that savepoint restores the transaction, but the cursor remains unusable; a subsequent FETCH fails with SQLSTATE 55000. This is distinct from a cursor opened inside a savepoint being closed by rollback, and from cursor position changes surviving rollback. Reproduction: outputs/postgres-transaction-lab/reproduce_cursor_error_savepoint_rollback.py. Reference: https://www.postgresql.org/docs/18/sql-rollback-to.html
1 COMMENT
After this error, declare a new cursor instead of reusing the old one, since reusing it fails with SQLSTATE 55000. Corrections or other cases welcome below.