PostgreSQL Error 22026: String Data Length Mismatch
In this article
PostgreSQL error 22026 (string_data_length_mismatch) means the server received a string value whose declared length does not match the actual number of bytes or characters sent. It is most common with the binary or text wire protocol, COPY operations, or client drivers that pre-declare field lengths. The fix is almost always on the client side: send the correct length, stop truncating or padding values, and make sure the driver and server agree on encoding and protocol.
What You'll Need
- Access to the PostgreSQL server logs (to see the exact failing statement and client address).
- The connection string or client library configuration used by the failing application.
- Permission to change the client code or driver version, and to restart the application.
- A test database where you can reproduce the error safely.
Step-by-Step
- Confirm the error code and source. Search the PostgreSQL log for
22026orstring_data_length_mismatch. Note the database, user, client IP, and the statement or COPY command that triggered it. If the log only shows the code, enablelog_min_error_statement = errorand reproduce the failure to capture the full statement. - Identify the transport. Error 22026 is raised by the backend when it parses a message whose length prefix disagrees with the payload. Check whether the client uses the simple query protocol, the extended query protocol (prepared statements with binary parameters), or
COPY ... FROM STDIN. Binary parameter binding and COPY are the usual suspects. - Reproduce with a minimal case. In a test database, run the same statement with a literal value first. If the literal works and the parameterized version fails, the problem is in the driver's length handling or in the value being altered before binding (trimming, encoding conversion, or a buffer that is shorter than the declared length).
- Check the client encoding. Run
SHOW client_encoding;and compare it with the encoding your application actually sends. If the application sends UTF-8 but the session expects another encoding, multibyte characters can change the byte count and produce a mismatch. Set the correct encoding in the connection string or withSET client_encoding. - Inspect the driver and its version. Outdated or mismatched drivers can miscompute length prefixes for binary parameters, especially for
bytea,text, and arrays. Update to a current release of the official driver for your language (for example, the currentpsycopgorpgrelease) and retest. Do not use unofficial or patched builds. - Review COPY usage. For
COPY ... FROM STDIN, ensure the data stream is not truncated mid-row and that the terminator and escape characters match the options you specified. If you useCOPY ... FROM PROGRAMor a file, verify the file is complete and not being written concurrently. - Check for truncation in application code. Look for fixed-size buffers, substring operations, or ORM fields that silently cut values. A value declared as 100 bytes but containing 80 bytes will fail if the client sends a length prefix of 100. Fix the declaration or the data source so the two agree.
- Test with a different client. Connect with
psqland run the same statement. Ifpsqlsucceeds, the server and data are fine and the fault is in the application's driver or connection settings. Ifpsqlalso fails, examine the server configuration and the data itself. - Apply the fix and verify. After correcting the driver, encoding, or buffer logic, rerun the failing operation in the test database, then in production during a low-traffic window. Watch the logs for a recurrence over the next few hours.
Common Mistakes
- Assuming the error is a server bug. In practice it is nearly always a client-side length or encoding problem.
- Changing
client_encodingwithout changing what the application actually sends, which can make the mismatch worse. - Ignoring the difference between character count and byte count for multibyte encodings such as UTF-8.
- Patching the driver or using a fork instead of updating to the official release.
- Fixing only the one failing query and missing other code paths that use the same broken buffer or ORM field.
Tips
- Log the full statement and parameter values (with sensitive data masked) when reproducing; the error message alone rarely names the offending column.
- Prefer text-format parameters over binary format when debugging, then switch back once the cause is found.
- Keep the client driver and server major version within the supported compatibility range listed in the PostgreSQL documentation.
- Use a connection pooler only after confirming the error does not depend on session state such as
client_encoding.
Troubleshooting
If the error persists after updating the driver, isolate the layer. First, run the statement through psql with the same session settings. If it succeeds there, capture a network trace or enable driver-level logging to compare the bytes sent with what the server expects. Second, test with a fresh connection that has no session-level SET commands, because a stale client_encoding or standard_conforming_strings setting can change how lengths are interpreted. Third, if the failure only happens under load, check for shared buffers or connection reuse in your application that mix values from different requests. Fourth, if you recently upgraded the server, confirm the client library supports the new protocol version; downgrading the driver is not recommended, so update it instead. Finally, if none of these steps resolves it, collect the server log lines around the failure, the driver name and version, and a minimal reproduction, and consult the PostgreSQL mailing lists or your vendor's support channel.
Frequently Asked Questions
Is error 22026 caused by the PostgreSQL server?
No. The server raises it when it detects a malformed message from the client. The root cause is in the client driver, the data sent, or the connection encoding.
Can it happen with ordinary INSERT statements?
Yes, especially when parameters are sent in binary format or when the application declares a length that does not match the value after encoding conversion.
Does changing client_encoding always fix it?
No. It only helps when the application and session disagree on encoding. Changing it without matching the application's output can introduce new mismatches.
Should I downgrade the driver to make the error go away?
No. Update to the current official release of the driver for your language and retest. Downgrading may reintroduce other fixed bugs.
Where can I find the full failing statement?
In the PostgreSQL server log. Set log_min_error_statement to error or lower, reproduce the failure, and read the logged statement and client address.