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.

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

In 10 seconds

What
UNION types cannot be matched
What triggers it
UNION an untyped NULL branch with a branch that resolves to text, then UNION the result with an integer branch.
The 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.

The fix

What to do right now

The immediate, application-level response to 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;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-15 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed