SQLSTATE P0000 ERROR Class P0: PL/pgSQL Error

plpgsql_error variable “…” is not declared — P0000

PostgreSQL error “variable … is not declared — P0000” (SQLSTATE P0000): what it means, common causes, and how to fix it.

PG 9.6, 10, 11, 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed Jun 2026 Grounded in source

Diagnostic Queries

Symptoms

A PL/pgSQL function referenced a variable that was never declared. PostgreSQL raises SQLSTATE P0000 (plpgsql_error) at compile time.

  • An undeclared variable name was used.
  • Common with typos or missing DECLARE entries.
  • Caught when the function is compiled.

What the server log shows

ERROR:  variable "totl" is not declared
CONTEXT:  compilation of PL/pgSQL function "recompute_totals" near line 6

Why PostgreSQL raises this — what the manual says

Section 41.3 Declarations:

“All variables used in a block must be declared in the declarations section of the block.”

PL/pgSQL resolves identifiers against declared variables and in-scope columns. A name that is neither declared nor a valid column reference can’t be resolved, so PostgreSQL reports P0000 during compilation.

Common causes

  • A typo in the variable name.
  • Forgetting to declare the variable in the DECLARE section.
  • Using a variable outside its block scope.

How to fix it

  1. Declare the variable in the function’s DECLARE section.
  2. Correct the variable name to match its declaration.
  3. Ensure the reference is within the variable’s scope.

Related & next steps

Reference: PostgreSQL 18 Section 43.3 “Declarations”.

Was this helpful?