PostgreSQL Error 22002: Null Value No Indicator Fix
In this article
Error 22002 in PostgreSQL means you tried to fetch a NULL value into a host variable (or output parameter) that has no null indicator, so the client library has no way to represent “no value.” The fix is to either supply a null indicator for that variable, make the column non-nullable, or change the query so it never returns NULL into that position. This guide walks through the cause, the exact steps to resolve it in common client environments, and how to prevent it from recurring.
What You'll Need
- Access to the database with permission to inspect table definitions and run test queries.
- The client code or application layer that executes the failing query (e.g., a C program using libpq, a stored procedure, or an ETL job).
- A PostgreSQL client such as psql for verifying nullability and testing queries.
- Basic familiarity with the programming language used to call PostgreSQL.
Step-by-Step
- Reproduce the error and capture the exact statement. Run the failing operation and note the full SQL text and the host variables involved. In psql, set
\set VERBOSITY verbosebefore running the statement to get the error code and context. The message usually names the column or parameter that received NULL without an indicator. - Identify which result column can be NULL. Use psql to inspect the table definition:
\d+ table_nameshows the “not null” constraint per column. Any column without it can return NULL. Also check views and function return types, since a view can expose a nullable expression even if the base column is NOT NULL. - Confirm the NULL is actually present. Run a targeted query such as
SELECT column_name FROM table_name WHERE column_name IS NULL LIMIT 5;If rows come back, the data contains NULLs and your client must handle them. - Add a null indicator in your client code. In C with libpq, every parameter or result variable that may be NULL needs a companion
intindicator variable passed toPQexecParamsor read viaPQgetisnull. For example, declareint ind_col;and pass&ind_colin the parameter array; after fetching, checkPQgetisnull(result, row, col)before reading the value. In other languages, use the native nullable type (e.g.,Option,Nullable, or a boxed type) rather than a primitive. - Or make the column non-nullable. If business rules forbid NULL, alter the table:
ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;This requires that no existing rows contain NULL; backfill them first withUPDATE table_name SET column_name = default_value WHERE column_name IS NULL;Then re-run the failing statement. - Or change the query to avoid NULL. Use
COALESCE(column_name, fallback_value)in the SELECT list so the client always receives a concrete value. This is the quickest fix when you cannot change client code or table schema. - Re-test the operation. Run the original statement or application path again. If the error persists, check for NULLs in other columns or parameters that you have not yet covered.
Common Mistakes
- Assuming the error is about the database when it is actually a client-side binding issue: the server sent NULL correctly, but the host program had no indicator to receive it.
- Adding
NOT NULLto a column without first cleaning existing NULL rows, which fails with a different error. - Using
COALESCEonly in the SELECT list while the same column is used in a WHERE or JOIN clause that still returns NULL into a parameter. - Forgetting that outer joins produce NULLs for non-matching rows even when both base columns are NOT NULL.
- Mixing up error 22002 with 23502 (not-null violation on insert/update); 22002 is about reading NULL into a host variable, not about writing it.
Tips
- Enable verbose error reporting in psql (
\set VERBOSITY verbose) to see the exact column or parameter name in the message. - In libpq, always pair every output variable with an indicator and check
PQgetisnullbefore dereferencing. - Prefer explicit nullable types in application code over sentinel values like -1 or empty string, which hide the real NULL semantics.
- Review views and function signatures: a view column can be nullable even if the underlying table column is NOT NULL.
- Add a regression test that inserts a NULL into every nullable column and exercises the read path, so this error is caught before production.
Troubleshooting
If the error persists after adding an indicator, verify that the indicator variable is actually passed to the correct parameter position; off-by-one mistakes in parameter arrays are common. If you changed the schema, confirm the change with \d+ table_name and reconnect the client, since some drivers cache metadata. If the error appears only under certain data conditions, run the query with EXPLAIN (VERBOSE) to see which join or subquery produces the NULL. When using stored procedures, check that OUT parameters are declared with the correct nullability and that the caller supplies indicators for each. If the client library is old, consult its documentation for the exact indicator API; do not assume it matches a newer version. Finally, if the problem is in a third-party ETL tool, look for a setting that controls null handling and enable it rather than modifying the database schema.
Frequently Asked Questions
What does error 22002 actually mean?
It is the SQLSTATE for “null value no indicator parameter”: a NULL was returned or passed into a host variable that lacks a null indicator, so the client cannot represent the absence of a value.
Is 22002 the same as a NOT NULL constraint violation?
No. A NOT NULL violation is 23502 and occurs on insert or update. 22002 occurs when reading or binding a NULL into a variable that has no indicator.
Can I fix it without changing code?
Yes, if you can change the query or schema. Use COALESCE to replace NULLs with a default, or add a NOT NULL constraint after backfilling existing rows.
Why does it happen only with outer joins?
Outer joins return NULL for columns from the non-matching side even when those columns are NOT NULL in their base table, so the result set can contain NULLs your client did not expect.
Does this error affect all PostgreSQL client libraries?
It is most visible in low-level APIs such as libpq where indicators are explicit. Higher-level drivers often handle nullability automatically, but they can still surface 22002 if configured to use non-nullable types.
How do I find which column caused it?
Run the statement with verbose error reporting in psql, or add logging around the fetch call to print the column index and name before reading the value.