Skip to content

0Z002 — stacked_diagnostics_accessed_without_active_handler

Source-backed reference for PostgreSQL SQLSTATE 0Z002.

0Z002

At a glance

SQLSTATE 0Z002 is emitted by PL/pgSQL when code tries to use stacked exception state without the active exception handler that owns it. PostgreSQL 18.6 has two confirmed paths: GET STACKED DIAGNOSTICS cannot be used outside an exception handler and RAISE without parameters cannot be used outside an exception handler, both reported at ERROR level.

Field Value
SQLSTATE 0Z002
Condition stacked_diagnostics_accessed_without_active_handler
Status active
Known present by 9.2.0
Locked snapshots 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3
Macros ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER
Aliases

Meaning

The first path guards GET STACKED DIAGNOSTICS; the second guards a parameterless RAISE that would re-raise the current exception. The shared boundary is the PL/pgSQL EXCEPTION handler, not a SQL BEGIN/COMMIT transaction block. The SQLSTATE member is more specific than the 0Z000 category.

Messages

The fixed messages are GET STACKED DIAGNOSTICS cannot be used outside an exception handler and RAISE without parameters cannot be used outside an exception handler.

Diagnosis

Locate the failing PL/pgSQL statement and the nearest EXCEPTION block. GET STACKED DIAGNOSTICS reads the exception being handled, while parameterless RAISE re-raises it; neither has stacked state outside a handler. Preserve the exact message and routine context because a transaction-state error or a new explicit RAISE has a different cause.

Response

Move the diagnostic read or re-raise into the active exception handler, or use an explicit RAISE condition/message when no prior exception is being handled. Keep the handler narrow and preserve RETURNED_SQLSTATE, message, detail, and hint before leaving it. A code change, rather than a transaction retry, is the repair supported by this path.

Versions

The locked catalogue places this condition at least by PostgreSQL 9.2.0. The release definitions cover the published snapshots listed in the facts block; source-path status is limited to the fixed PostgreSQL 18.6 context cited below.

0Z000, P0001

Sources

The fixed PL/pgSQL executor source reports the GET STACKED DIAGNOSTICS variant, and the RAISE path reports the parameterless variant. The PL/pgSQL diagnostics documentation describes stacked fields. See the structured evidence record for fixed source paths, scan scope, and unresolved runtime boundaries.