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.
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.
-- put the integer in the leftmost branch so pairwise resolution locks in early
SELECT 1 UNION SELECT NULL UNION SELECT NULL;Diagnose
See it live on the server
Run these against the affected instance to confirm the diagnosis before you act.
Standard triage — not specific to this error
These are canonical PostgreSQL system-catalog queries, shown as SQL to run. No sample output is attached because this is general triage, not a captured lab transcript.This SQLSTATE does not have an error-specific live snapshot yet. These are the canonical system-catalog queries you run against the affected server to see the problem in real time — standard triage, not a reproduced transcript.
What is running right now
Active backends, how long each has been running, and what it is waiting on.
SELECT pid,
state,
wait_event_type,
wait_event,
now() - query_start AS running_for,
left(query, 80) AS query
FROM pg_stat_activity
WHERE state <> 'idle'
AND pid <> pg_backend_pid()
ORDER BY running_for DESC NULLS LAST;Who is blocking whom
Turn raw blocking PIDs into the actual queries on both sides of the wait.
SELECT blocked.pid AS blocked_pid,
blocked.query AS blocked_query,
blocking.pid AS blocking_pid,
blocking.query AS blocking_query
FROM pg_stat_activity AS blocked
JOIN LATERAL unnest(pg_blocking_pids(blocked.pid)) AS b(pid) ON true
JOIN pg_stat_activity AS blocking ON blocking.pid = b.pid
WHERE cardinality(pg_blocking_pids(blocked.pid)) > 0;Locks that are still waiting
Every lock a backend has requested but not yet been granted.
SELECT l.pid,
l.locktype,
l.mode,
l.granted,
COALESCE(c.relname, l.transactionid::text) AS object
FROM pg_locks l
LEFT JOIN pg_class c ON c.oid = l.relation
WHERE NOT l.granted
ORDER BY l.pid;Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual — not paraphrased.
PostgreSQL 16 Documentation — §10.5 UNION, CASE, and Related Constructs
The problem can be fixed by ensuring that the leftmost UNION has at least one input of the desired result type.Read the full section on postgresql.org →
The mismatched UNION
PostgreSQL resolves multiple UNIONs as a nest of pairwise operations. The first pair (NULL UNION NULL) resolved to text — PostgreSQL's fallback type for an all-NULL union — and that text result then met an integer in the outer UNION, with no implicit conversion between them.Confirming the session still works
The failed UNION didn't affect the session — the next statement ran normally.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab — the commands below are exactly what was executed.
- 1UNION an untyped NULL branch with a branch that resolves to text, then UNION the result with an integer branch.
- 2PostgreSQL resolves multiple UNIONs pairwise, left to right — the inner pair locks in a type before the outer pair is checked.
- 3SQLSTATE 42804 is reported once the two sides of a pair can't be matched, naming both types.
One client: UNION three SELECTs where the pairwise-resolved type becomes text before it meets an integer.
-- No table needed: this failure happens purely at SQL type-resolution time.
SELECT 'no schema required' AS setup_note;SELECT NULL UNION SELECT NULL UNION SELECT 1;SELECT 1 AS session_still_alive;What PostgreSQL actually returned
setup_note
--------------------
no schema required
(1 row)ERROR: UNION types text and integer cannot be matched
LINE 1: SELECT NULL UNION SELECT NULL UNION SELECT 1;
^ session_still_alive
----------------------
1
(1 row)The manual's own suggested fix — put a value of the desired type in the leftmost branch — was tested directly against the same three-way UNION shape.
Reorder the exact same three SELECTs so the integer comes first.
Without this
Above: NULL first — pairwise resolution starts as text and then collides with integer.
With this, tested
Below: the same three SELECTs, integer first — pairwise resolution starts as integer and both NULLs convert to it.
What Pro unlocks here
- The exact prevention SQL — copy-paste ready
- Raw psql output captured from the Docker lab
- A senior-DBA action list to take it further
- Live monitoring queries to catch it in production
- The deeper audit: fix-that-fails counterexample, GUC before/after, server-log evidence
Related & next steps
Follow the thread
Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.
Related errors
Verification
- 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