SQLSTATE 22026Severity lowLab verified

Incident brief

String data length mismatch

A bit string value was stored into a bit(n) column whose length didn't exactly match n. PostgreSQL rejected the write instead of guessing how to fit it.

What lands in your log

ERROR: bit string length 2 does not match type bit(3)

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What triggers it
Create a table with a fixed-length bit(3) column.
Fix
Supply a bit string whose length exactly matches the column's declared length.
Proof
Reproduced on PostgreSQL 16.14 → Inserting a 2-bit value into a bit(3) column was rejected with SQLSTATE 22026, naming both the supplied length and the declared length. The session was unaffected afterward.

Fix

What to do right now

Application-level steps for this error.

  • Supply a bit string whose length exactly matches the column's declared length.
  • If you intentionally want truncation/padding instead of an error, that's a different, explicit operation, see the counterexample for what it actually does and why it's risky.
  • Prefer bit varying(n) over bit(n) if the true length legitimately varies.
Fix SQL
-- Use equal-length bit strings for bitwise operations.
SELECT B'101' & B'010' AS matched_length;

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Verification scope
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed
ShareLinkedInX

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops