The error is raised when GET STACKED DIAGNOSTICS is executed outside an active EXCEPTION handler.
PostgreSQL Error 0Z002 – stacked_diagnostics_accessed_without_active_handler – means GET STACKED DIAGNOSTICS ran with no active exception handler. Move the statement inside an EXCEPTION block or use GET DIAGNOSTICS instead to resolve the issue.
PostgreSQL Error 0Z002
The error appears when a PL/pgSQL block calls GET STACKED DIAGNOSTICS
without being inside an active EXCEPTION
handler.
PostgreSQL only populates the stacked diagnostics area while handling a trapped exception. Accessing it at any other time triggers SQLSTATE 0Z002 and aborts the current function.
Calling GET STACKED DIAGNOSTICS
in the main body of a function, or in a BEGIN ...
END
block that has no active exception context, immediately raises the error.
The error also fires when you attempt to inspect stacked diagnostics after you have already re-raised or cleared the exception, leaving no handler active.
Place the GET STACKED DIAGNOSTICS
call inside the corresponding EXCEPTION
clause so an error context is active.
When you only need general information, switch to GET DIAGNOSTICS
, which is safe outside handlers and provides details on the most recent command.
Logging exceptions: Wrap risky statements in a BEGIN … EXCEPTION
block, then gather diagnostics inside the handler before re-raising.
Debugging: Replace rogue calls to GET STACKED DIAGNOSTICS
with GET DIAGNOSTICS
or conditional logic to run them only when SQLSTATE
is not null.
Always pair GET STACKED DIAGNOSTICS
with an EXCEPTION
block in the same scope.
Use static code analysis or Galaxy’s inline linting to highlight misuse before deploying functions to production.
raise_without_handler
occurs when RAISE
is executed where no exception is active.
Fix by ensuring a handler context exists or by using RETURN
.
division_by_zero
appears when dividing numeric types by zero. Prevent by validating inputs or trapping with EXCEPTION
blocks.
.
No. SQLSTATE 0Z002 is enforced by the PL/pgSQL runtime and cannot be turned off.
Use GET DIAGNOSTICS for command-level info. It does not provide exception details like ERRCODE or MESSAGE_TEXT for a trapped error.
The rule exists in all supported PostgreSQL versions, including 10 through 16. Behavior is consistent.
Galaxy’s editor flags misuse of GET STACKED DIAGNOSTICS and suggests moving it into an EXCEPTION block, preventing runtime errors.