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
- Declare the variable in the function’s DECLARE section.
- Correct the variable name to match its declaration.
- Ensure the reference is within the variable’s scope.
Related & next steps
Reference: PostgreSQL 18 Section 43.3 “Declarations”.
Thanks — noted. This helps keep the database accurate.