variable “…” is not declared

SQLSTATE P0000 condition plpgsql_error class P0 — PL/pgSQL Error severity ERROR
Reproduced & verified on PostgreSQL 14.23, 15.18, 16.14, 17.10 and 18.4 — identical message on every version.
Last reviewed 11 Jun 2026 · Reproduced live with the SQL on this page.

Symptoms

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

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

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”.