↑↓ select↵ open⌫ change scopeOpen full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

Wiki / SQLSTATE

0Z002 — stacked_diagnostics_accessed_without_active_handler

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.

English manual coverage begins with PostgreSQL 8.1. Source history and runtime observations have their own recorded version boundaries.

Reading PG 18·Select a recorded version

aliases
Unknown
class name
Diagnostics Exception
condition name
stacked_diagnostics_accessed_without_active_handler
lang
en
sqlstate
0Z002

Definition PG 18

0Z002

At a glance {#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.

Meaning {#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 {#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 {#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 {#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 {#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.

Related {#related}

0Z000, P0001

Sources {#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.

Compare versions

SQLSTATE evidence and recorded runtime versions

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed errcodes definition row, severity, and macro.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "0Z002 is stacked_diagnostics_accessed_without_active_handler in SQLSTATE Class 0Z."
}
claims · resolved-path
{
  "claim_id": "resolved-path",
  "limits": "This is source confirmation, not a runtime observation in this batch.",
  "method": "Read the complete source context around the resolved report group.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.pl_exec.18.6"
  ],
  "statement": "The fixed PostgreSQL 18.6 source contains the report path and message context described on the page."
}
claims · history-boundary
{
  "claim_id": "history-boundary",
  "limits": "Definition presence is not an emitting-path observation.",
  "method": "Compare canonical release-definition facts with the available history boundary.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked release definitions establish catalogue presence from the known 9.2.0 boundary; they do not establish a precise introducing release or runtime use."
}
messages · message.get-stacked-outside-handler
{
  "limits": "",
  "message_id": "message.get-stacked-outside-handler",
  "path": "",
  "position": 0,
  "raw": {
    "id": "message.get-stacked-outside-handler",
    "primary_template": "GET STACKED DIAGNOSTICS cannot be used outside an exception handler",
    "severity_source": "ERROR",
    "sources": [
      "src.pl_exec.18.6"
    ],
    "sqlstate": "0Z002"
  },
  "severity": "ERROR",
  "sources": [
    "src.pl_exec.18.6"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "GET STACKED DIAGNOSTICS cannot be used outside an exception handler",
      "position": 0,
      "role": "",
      "template": "GET STACKED DIAGNOSTICS cannot be used outside an exception handler"
    }
  ]
}
messages · message.raise-without-parameters-outside-handler
{
  "limits": "",
  "message_id": "message.raise-without-parameters-outside-handler",
  "path": "",
  "position": 1,
  "raw": {
    "id": "message.raise-without-parameters-outside-handler",
    "primary_template": "RAISE without parameters cannot be used outside an exception handler",
    "severity_source": "ERROR",
    "sources": [
      "src.pl_exec.18.6"
    ],
    "sqlstate": "0Z002"
  },
  "severity": "ERROR",
  "sources": [
    "src.pl_exec.18.6"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "RAISE without parameters cannot be used outside an exception handler",
      "position": 0,
      "role": "",
      "template": "RAISE without parameters cannot be used outside an exception handler"
    }
  ]
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L142"
}
sources · src.pl_exec.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/pl/plpgsql/src/pl_exec.c",
  "position": 1,
  "sha256": "0df3c70a6bccf6dddf443fb99e152a16a67009827c44a820597e98e585cb8b20",
  "source_id": "src.pl_exec.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/pl/plpgsql/src/pl_exec.c"
}
sources · src.calls.REL_18_6
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 2,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6",
  "tag": "",
  "url": ""
}

Source references

Full definition and evidence JSON

Definition sources

center · PostgreSQL 18 · english-manuals:4279fcb65c55c3c763ccff3bc8f63fd08daa08dfad9ff6ab28936823e54a9d33

Text language: en · english-manuals:4279fcb65c55c3c763ccff3bc8f63fd08daa08dfad9ff6ab28936823e54a9d33