SQLSTATE 42804Severity mediumLab verified

Incident brief

UNION types cannot be matched

A UNION mixed two branches whose types can't be converted to each other. PostgreSQL resolves UNIONs two branches at a time, so where a mismatch appears in that chain matters.

What lands in your log

ERROR: UNION types text and integer cannot be matched

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

In 10 seconds

What triggers it
UNION an untyped NULL branch with a branch that resolves to text, then UNION the result with an integer branch.
Fix
Put a value of the type you actually want in the leftmost branch, so the pairwise resolution locks in early.
Proof
Reproduced on PostgreSQL 16.14 → Running the manual's own three-way UNION example was rejected with SQLSTATE 42804. The session was unaffected afterward.

Fix

What to do right now

Application-level steps for this error.

  • Put a value of the type you actually want in the leftmost branch, so the pairwise resolution locks in early.
  • Or explicitly cast the ambiguous NULL branch to the desired type.
  • Don't reach for a blanket cast to make the error disappear, see the premium test for why that can silently change what the query returns.
Fix SQL
-- put the integer in the leftmost branch so pairwise resolution locks in early
SELECT 1 UNION SELECT NULL UNION SELECT NULL;

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

PG 16.14
Last verified
2026-07-15 (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