22026 — string_data_length_mismatch
22026
At a glance
A bit-string value has the wrong declared length. The fixed varbit.c path reports bit string length %d does not match type bit(%d); bitwise operands have separate mismatch messages.
| Field | Value |
|---|---|
| SQLSTATE | 22026 |
| Condition | string_data_length_mismatch |
| Status | active |
| Known present by | 7.4 |
| Locked snapshots | 9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3 |
| Macros | ERRCODE_STRING_DATA_LENGTH_MISMATCH |
| Aliases | — |
Messages
The fixed guards use these primary texts:
| Guard | Primary |
|---|---|
bit(n) length mismatch |
bit string length %d does not match type bit(%d) |
bit_and operands |
cannot AND bit strings of different sizes |
bit_or operands |
cannot OR bit strings of different sizes |
bitxor operands |
cannot XOR bit strings of different sizes |
Meaning
22026 covers exact-length bit(n) boundaries and bitwise operations. The fixed bit() input path compares the actual bit length with the declared bit(n) length; an implicit conversion raises bit string length %d does not match type bit(%d), while an explicit bit(n) cast truncates or zero-pads. The varbit(n) path has a different contract: an implicit value that is too long uses 22001, and an explicit cast truncates to the maximum length. The AND, OR, and XOR functions separately compare both operand lengths and use 22026 when they differ.
Diagnosis
First identify whether the primary names a bit(n) typmod or a bitwise operator. For the typmod message, compare the source bit length with the target exact length and check whether the boundary is an implicit assignment/cast or an explicitly requested cast. For varbit(n) overlength, look for 22001 instead; for a bitwise message, inspect both operands. Character width and encoding failures belong to different SQLSTATE paths.
Response
Make an implicit bit(n) value exactly the declared length. Use an explicit cast only when its truncation or zero-padding is intended, and resize both operands before a bitwise operation when their lengths should agree. If an ERROR occurred in an explicit transaction, roll back or roll back to the existing savepoint before retrying; autocommit can retry the corrected action.
Versions
The locked catalogue records this condition from 7.4; the fixed source paths here are PostgreSQL 18.6. No natural runtime observation is claimed for this page.
Related
Sources
The exact-length guard is src/backend/utils/adt/varbit.c#L354-357; implicit versus explicit bit(n) and varbit(n) conversion is #L385-414 and #L736-765. The bitwise size guards are #L1257-1343. The structured evidence record retains the message roles and scope; no natural runtime was run.