# 22026 — string_data_length_mismatch

> Source-backed reference for PostgreSQL SQLSTATE 22026.
---

# 22026

## At a glance {#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.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 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 | `—` |

<!-- source facts: data/errcodes/22026.json -->
<!-- END SQLSTATE FACTS -->

## Messages {#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 {#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 {#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 {#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 {#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 {#related}
[`22001`](../22001/), [`22021`](../22021/)

## Sources {#sources}
The exact-length guard is [`src/backend/utils/adt/varbit.c#L354-357`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/varbit.c#L354); implicit versus explicit `bit(n)` and `varbit(n)` conversion is [`#L385-414`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/varbit.c#L385) and [`#L736-765`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/varbit.c#L736). The bitwise size guards are [`#L1257-1343`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/varbit.c#L1257). The structured [evidence record](../data/evidence/22026.json) retains the message roles and scope; no natural runtime was run.
