PostgreSQL Error 22010 Invalid Indicator Parameter Value
In this article
PostgreSQL error 22010 occurs when a database driver or application passes an invalid value for a "null indicator" parameter during a parameterized query. To fix this, ensure that the indicator variable used to signal a NULL value is set to -1 for NULLs and 0 for non-NULL values, as any other integer is considered invalid by the PostgreSQL interface.
This error typically does not happen within raw SQL queries executed in a tool like pgAdmin or psql. Instead, it arises in the communication layer between your application code (C, C++, Python, Java, etc.) and the PostgreSQL server, specifically when using the Extended Query protocol or specific database drivers (like ODBC or libpq) that allow the application to explicitly define whether a parameter is null using a separate indicator variable.
What You'll Need
- Access to the application source code triggering the error.
- The specific database driver documentation (e.g., ODBC, JDBC, or a language-specific wrapper) being used.
- A development environment where you can log the values of parameters being sent to the database.
Step-by-Step
- Identify the Failing Query: Check your application logs to find the exact SQL statement that triggers error 22010. Look for queries that use placeholders (like
?or$1, $2) rather than hardcoded values. - Locate the Parameter Binding Logic: Find the section of your code where variables are bound to the SQL placeholders. Look for functions like
SQLBindParameter(in ODBC) or similar binding methods in your chosen language. - Inspect the Indicator Variable: In many low-level drivers, parameters are passed as a pair: the actual value and an "indicator" (often a pointer to an integer). Examine the value being passed as the indicator for each parameter.
- Correct the Indicator Value: Ensure the indicator follows these strict rules:
- Set to 0: If the data is present and should be treated as a normal value.
- Set to -1: If the data is missing and should be treated as a
NULL. - Remove other values: If the indicator is being set to a positive number (which some other databases use to indicate string length) or any other integer, change it to 0 or -1.
- Verify Data Types: Ensure that the data type of the variable being bound matches the column type in the database. While a type mismatch usually triggers a different error, an incorrect type can sometimes lead to the driver miscalculating the indicator value.
- Recompile and Test: Deploy the change to a staging environment and execute the specific action that previously caused the crash to confirm the error is resolved.
Common Mistakes
- Using Length as Indicator: A common mistake when migrating from other database systems (like Oracle or SQL Server) is passing the length of a string as the indicator value. PostgreSQL expects 0 for valid data, regardless of length.
- Uninitialized Variables: Declaring an indicator variable but failing to assign it a value before binding. This results in "garbage" memory values being sent to the server, which will almost certainly trigger error 22010.
- Confusing NULL with Empty Strings: Passing an empty string
""while setting the indicator to -1. If you want a NULL, the indicator must be -1; if you want an empty string, the indicator must be 0. - Incorrect Pointer Usage: In C/C++ applications, passing the value of the indicator instead of the address (pointer) to the indicator variable in the binding function.
Tips
- Use High-Level ORMs: If you are not constrained by performance requirements that necessitate low-level driver calls, use an Object-Relational Mapper (ORM) or a high-level library. These libraries handle null indicators automatically.
- Log Parameter Values: Implement a debug mode in your application that prints the exact values of all parameters and their corresponding indicators immediately before they are sent to the database.
- Standardize NULL Handling: Create a helper function in your code to handle parameter binding. Instead of manually setting indicators, use a function like
BindParam(value)that internally handles the 0/-1 logic.
Troubleshooting
If you have corrected the indicator values to 0 and -1 but the error persists, try the following steps:
- Update the Driver: Check if you are using an outdated version of the ODBC or JDBC driver. Some older drivers had bugs in how they translated indicator values for specific PostgreSQL versions.
- Check for Trigger Interference: While rare for this specific error, check if there are
BEFORE INSERTorBEFORE UPDATEtriggers on the table that might be manipulating the incoming data in a way that confuses the driver. - Simplify the Query: Remove parameters one by one from the query. If the error disappears after removing a specific column, you have narrowed down exactly which variable is causing the invalid indicator.
- Test with a Different Driver: If possible, switch from an ODBC driver to a native driver (like
psycopg2for Python orpgfor Node.js) to determine if the issue is with the application logic or the driver's implementation of the protocol.
Frequently Asked Questions
Is this a database corruption issue?
No. Error 22010 is a protocol/communication error. It means the server received a value it didn't understand for a parameter flag; it does not indicate any corruption of the data on the disk.
Can I fix this by changing the table schema?
No. Changing a column from NOT NULL to NULL (or vice versa) will not fix this error. The problem is not whether the database allows NULLs, but how the application is telling the database that a value is NULL.
Does this happen with standard SQL INSERT statements?
No. If you write INSERT INTO table (col) VALUES (NULL), you will not see this error. It only occurs when using parameterized queries where the driver manages the "nullness" of the value via a separate indicator parameter.
What is the difference between a NULL value and an invalid indicator?
A NULL value is a valid state for a database cell. An invalid indicator is a "broken" signal sent by the application. For example, telling the database "this value is NULL" (indicator -1) is valid; telling the database "this value is status 5" (indicator 5) is invalid and triggers error 22010.
Which programming languages are most susceptible to this?
This is most common in languages that interface with PostgreSQL via C-based APIs or ODBC, such as C, C++, Delphi, or older versions of VB, where the developer is responsible for managing memory pointers and indicator variables manually.