Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 25008: Held Cursor Isolation Fix

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 25008 means a cursor declared with WITH HOLD is being used in a transaction whose isolation level differs from the one that created it. The fix is to use the same isolation level for the transaction that opens the cursor and for every later transaction that fetches from it, or to avoid held cursors across isolation levels altogether. In practice, set the isolation level explicitly before declaring the cursor and keep it consistent for the life of the cursor.

What You'll Need

  • Access to a PostgreSQL database and a client that can run SQL (psql, pgAdmin, or your application's database driver).
  • Permission to start transactions and declare cursors in the relevant schema.
  • The name of the cursor and the isolation level used when it was declared.
  • A way to inspect current transaction settings, such as SHOW transaction_isolation;.

Step-by-Step

  1. Identify the failing statement. Error 25008 is raised when a FETCH, MOVE, or CLOSE references a held cursor from a transaction with a different isolation level. Look at the SQL that triggered the error and note whether it runs inside an explicit BEGIN block or in autocommit mode.
  2. Check the current isolation level. Run SHOW transaction_isolation; in the session that is failing. This shows the level for the current transaction. If you are not inside an explicit transaction, it shows the session default.
  3. Find the isolation level used when the cursor was declared. The cursor was created with DECLARE ... CURSOR WITH HOLD FOR ... in an earlier transaction. That transaction's isolation level is the one the cursor expects. If you did not set it explicitly, it was the session default at that time, usually READ COMMITTED.
  4. Set the isolation level explicitly before declaring the cursor. Start the transaction and set the level in the same transaction, for example:
    BEGIN;
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    DECLARE my_cursor CURSOR WITH HOLD FOR SELECT * FROM my_table;
    COMMIT;
    The SET TRANSACTION statement must come before the DECLARE. Labels can differ slightly by PostgreSQL version, but the SQL keywords are stable.
  5. Use the same isolation level in every later transaction that touches the cursor. For each transaction that fetches from the held cursor, set the same level before the FETCH:
    BEGIN;
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    FETCH ALL FROM my_cursor;
    COMMIT;
    If the cursor was declared at READ COMMITTED, use READ COMMITTED in the fetching transaction.
  6. If you cannot control the isolation level, avoid held cursors. Declare the cursor without WITH HOLD and keep all fetches inside the same transaction. A non-held cursor is automatically closed at the end of its transaction, so it cannot cross isolation levels.
  7. Close the cursor when done. Run CLOSE my_cursor; in a transaction with the same isolation level to release resources. Held cursors persist until closed or until the session ends.
  8. Test the change. Repeat the original workflow: declare the cursor, commit, start a new transaction, fetch. The error should no longer appear if the isolation levels match.

Common Mistakes

  • Setting the isolation level after DECLARE. The level must be set before the cursor is declared.
  • Assuming the session default is enough. If the session default changes between transactions, the held cursor still expects the level from its declaration.
  • Using SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ... only once and then changing it later. The cursor keeps the level it was declared with.
  • Forgetting that autocommit mode uses the session default. A FETCH outside an explicit transaction may run at a different level than the declaring transaction.
  • Mixing isolation levels across application connection pools. Different pooled connections may have different defaults, so a cursor declared on one connection may be fetched on another with a different level.

Tips

  • Always set the isolation level explicitly in the same transaction as DECLARE and in every transaction that fetches. This makes the requirement visible in the SQL.
  • Prefer READ COMMITTED for held cursors unless you need repeatable reads. It is the default and the least likely to conflict with other parts of the application.
  • Use SHOW transaction_isolation; before and after SET TRANSACTION to confirm the level.
  • If you use an ORM or driver, check how it sets isolation levels. Some drivers reset the level per transaction, which can break held cursors.
  • Keep held cursors short-lived. Long-lived held cursors hold resources and can surprise later transactions.

Troubleshooting

If the error persists after matching isolation levels, check the following:

  • Confirm the cursor name and session. Held cursors are session-scoped. A cursor declared in one session cannot be fetched in another. Use the same connection.
  • Check for implicit transactions. Some clients wrap statements in transactions automatically. Inspect the client's transaction behavior.
  • Look for SET TRANSACTION outside a transaction. It has no effect outside a transaction block and may be silently ignored.
  • Verify the PostgreSQL version's behavior. The error code and rules are stable, but client tools may differ. Test with psql to isolate the database from the application.
  • If you cannot change the isolation level, rewrite the query. Replace the held cursor with a single query or a temporary table that does not require cross-transaction state.

Frequently Asked Questions

What exactly triggers error 25008?

It is raised when a held cursor is used in a transaction whose isolation level is not the same as the one in effect when the cursor was declared.

Can I change the isolation level of an existing held cursor?

No. The cursor is bound to the isolation level of its declaring transaction. You must close it and declare a new cursor at the desired level.

Does this affect non-held cursors?

No. Cursors declared without WITH HOLD are closed at the end of their transaction and cannot be used across transactions, so the isolation-level mismatch does not arise.

What is the default isolation level in PostgreSQL?

READ COMMITTED. If you do not set it explicitly, both the declaring and fetching transactions use this level, and the error will not occur.

Can I use a held cursor across different sessions?

No. Held cursors are scoped to the session that declared them. Fetching from another session will fail regardless of isolation level.

Is there a way to avoid the error without changing SQL?

Yes, if your client or driver allows you to set a fixed isolation level for all transactions on a connection, and you never change it, held cursors will remain consistent.