How to Fix Postgres Error 22002 Null Value No Indicator Parameter

Published
Length
4 min read

Luis Felipe Dias

All posts

The postgres error 22002 null value no indicator parameter happens when a query returns a null value into a host variable that lacks a null indicator. You fix it by adding an indicator variable in your client code or replacing nulls in the query result. Database drivers use indicator variables to track when a column holds a null value. Without this extra variable, the client app cannot tell a real value apart from an empty one. The database stops the query to prevent your app from reading bad d

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

The postgres error 22002 null value no indicator parameter happens when a query returns a null value into a host variable that lacks a null indicator. You fix it by adding an indicator variable in your client code or replacing nulls in the query result.

Database drivers use indicator variables to track when a column holds a null value. Without this extra variable, the client app cannot tell a real value apart from an empty one. The database stops the query to prevent your app from reading bad data.

What causes postgres error 22002 null value no indicator parameter

In PostgreSQL, SQLSTATE 22002 maps to the official condition name null_value_no_indicator_parameter. The engine also labels this fault with the internal macro ERRCODE_NULL_VALUE_NO_INDICATOR_PARAMETER. It belongs to the data exception class of errors, which all start with the prefix 22.

This failure occurs most often in embedded SQL programs or older C client libraries. A developer binds a local variable to a database column to store fetched data. When the database sends back a row, one of the selected columns contains a null value.

Typed languages like C cannot store a database null inside a standard integer or string buffer. They need a second variable, called an indicator parameter, to flag when the main variable is null. If the developer forgets to bind that indicator variable, the client interface raises SQLSTATE 22002 right away.

Stored procedures and dynamic SQL calls can also trigger this error during output binding. If an output parameter receives a null value without a matching indicator, the execution fails. Schema changes often bring this bug to light when a previously required column begins allowing nulls.

How to diagnose and fix the error in production

First, find the exact query and column that returned the unexpected null value. Check your database logs and client error traces to see which statement failed. Look at the target table schema to see which columns permit null entries.

When you inspect production data to find the null rows, keep your database permissions tight. Do not log in with a superuser account just to check table contents. In PostgreSQL, the SELECT privilege lets a role read specific columns of a table or view.

End-to-End Encryption + Indicator of Compromise (IoC): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Grant your debugging role read rights only on the columns involved in the failing query. If your team has many engineers, set up group roles to manage these read privileges cleanly. You should also check that the PUBLIC role does not hold unwanted rights on sensitive tables.

Once you find the null column, you have two main ways to fix the problem. The best fix in client code is to define and bind an indicator variable for every nullable column. Your app checks this indicator variable before reading the main data buffer.

If you cannot change the compiled client app right away, you can adjust the SQL query instead. Use a standard null replacement function in your query to return a safe default value. You can also create a database view that replaces nulls with default values for legacy apps.

Preventing null binding errors across your team

Many 22002 errors happen after a team alters a table and drops a not-null rule. Before you relax column rules in production, check all client apps that read from that table. Make sure every client driver handles nullable fields properly.

When you test query fixes against live data, watch out for personal details in your results. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This step keeps private user records safe while developers test their fixes.

Also review who can change schemas on your database server. In PostgreSQL 15 and later, the default setup stops every user from creating objects in the public schema. On older databases, an admin should revoke create rights on the public schema from the PUBLIC role.

Check every Postgres query on the wire

Database roles and network rules decide who can open a session. They do not read what each SQL statement does after login. With hoop.dev, you run an open-source proxy, a sidecar for your Postgres, inline in the Postgres wire protocol. On every query an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review. Your apps and agents keep their login details and change only the host and port in the connection string their tool uses.

Before a query runs, hoop.dev guardrails refuse bad deletes or drops with a real Postgres error, or hold risky queries for approval. On the way back, hoop.dev data masking rewrites sensitive values in returned rows in memory, and every verdict goes to the audit trail. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for HIPAA, GDPR, and PCI DSS. See the code on GitHub.

Frequently asked questions

What does SQLSTATE 22002 mean in PostgreSQL?

SQLSTATE 22002 is the standard PostgreSQL error code for null_value_no_indicator_parameter. It means a query fetched a null value into a client variable that has no indicator variable attached.

Can I fix error 22002 without changing my C code?

Yes, you can update the SQL query or point the app at a database view. Have the query or view replace null values with a safe default number or empty string.

Why did error 22002 start happening without a code deploy?

New rows with null values may have entered the table, or someone dropped a not-null constraint. The client code always lacked an indicator variable, but it never hit a null value until now.

The query runs. The emails stay hidden.