From: SerTitan (@SerTitan) <noreply+SerTitan@github.com>
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Subject: [pgaudit/pgaudit] issue #298: Add exception for fetch statement to stack check.
Date: Wed, 24 Dec 2025 00:25:35 +0000
Message-ID: <gh-pgaudit-pgaudit-298@github.com> (raw)
This issue is closely related to the previous fix introduced in [PR #249](https://github.com/pgaudit/pgaudit/pull/249), which addressed the `pgaudit stack is not empty` error by allowing additional statement types to remain on the audit stack.
During execution of a transaction that fetches data from a refcursor. After investigation, we found that a FETCH statement remains on the audit stack at the moment of stack validation.
We were able to reproduce the issue using JDBC with cursor-based execution enabled. For reproduction, the following JDBC settings were used:
- setAutoCommit(false)
- PreparedStatement.setFetchSize(n), n > 0
The transaction was executed as a single JDBC statement and contains a stored procedure call followed by cursor fetch and close:
```
BEGIN;
DO $$
DECLARE
var_ImplicitCallerRetVal int;
var_ImplicitResultSet refcursor := 'var_implicitresultset';
BEGIN
CALL test_outer(var_ImplicitCallerRetVal, var_ImplicitResultSet);
IF NOT EXISTS (
SELECT 1 FROM pg_cursors WHERE name = 'var_implicitresultset'
) THEN
OPEN var_ImplicitResultSet FOR
SELECT 0 AS ResultSetNotFound WHERE 0 = 1;
END IF;
END;
$$;
FETCH ALL FROM var_implicitresultset;
CLOSE var_implicitresultset;
COMMIT;
```
During execution, the **T_FetchStmt** is pushed onto the pgaudit stack. As a result, the `pgaudit stack is not empty` at **CLOSE** statement. To align this behavior with the fix introduced in [PR #249](https://github.com/pgaudit/pgaudit/pull/249), **FETCH** can be treated as an additional allowed statement type during audit stack validation.