Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
42P21 is raised while the query is parsed, before any row is read. A UNION, a || concatenation or a comparison combines two text values whose collations differ (here C and POSIX), and PostgreSQL refuses to pick one. The message says implicit collations when the collations come from the columns, and explicit collations when both sides have a COLLATE clause.
Below, customers.code is C and legacy_customers.code is POSIX. Each case was fixed by giving the result one collation: a COLLATE "C" on one side, or changing the column.
01UNION of two columns with different collations
SELECT code FROM customers UNION SELECT code FROM legacy_customers ORDER BY code failed; the caret points at the second SELECT's code. UNION has to merge the two columns into one result column, and that column needs one collation.
With SELECT code COLLATE "C" FROM legacy_customers both sides were "C" (checked with collation for) and the query returned A01, B02, C03.
Setup
CREATE TABLE customers (code text COLLATE "C", name text);
CREATE TABLE legacy_customers (code text COLLATE "POSIX", name text);
INSERT INTO customers VALUES ('A01', 'Kim'), ('B02', 'Lee');
INSERT INTO legacy_customers VALUES ('A01', 'Kim'), ('C03', 'Park');CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2SELECT code FROM customers
UNION
SELECT code FROM legacy_customers
ORDER BY code;ERROR: 42P21: collation mismatch between implicit collations "C" and "POSIX"
LINE 3: SELECT code FROM legacy_customers
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.SELECT table_name, column_name, collation_name
FROM information_schema.columns
WHERE column_name = 'code' AND table_schema = 'lab_a'
ORDER BY table_name; table_name | column_name | collation_name
------------------+-------------+----------------
customers | code | C
legacy_customers | code | POSIX
(2 rows)The same code column is C in customers and POSIX in legacy_customers.
SELECT (SELECT collation for (code) FROM customers LIMIT 1) AS customers_code,
(SELECT collation for (code COLLATE "C") FROM legacy_customers LIMIT 1) AS legacy_code_with_collate; customers_code | legacy_code_with_collate
----------------+--------------------------
"C" | "C"
(1 row)SELECT code FROM customers
UNION
SELECT code COLLATE "C" FROM legacy_customers
ORDER BY code; code
------
A01
B02
C03
(3 rows)02CREATE VIEW with a UNION over both tables
CREATE VIEW all_customers AS ... UNION ... failed with the same message, so the view was never created.
Instead of adding COLLATE inside the view, we changed the column: after ALTER TABLE legacy_customers ALTER COLUMN code TYPE text COLLATE "C", the same CREATE VIEW succeeded and the view returned three rows.
Setup
CREATE TABLE customers (code text COLLATE "C", name text);
CREATE TABLE legacy_customers (code text COLLATE "POSIX", name text);
INSERT INTO customers VALUES ('A01', 'Kim'), ('B02', 'Lee');
INSERT INTO legacy_customers VALUES ('A01', 'Kim'), ('C03', 'Park');CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2CREATE VIEW all_customers AS
SELECT code, name FROM customers
UNION
SELECT code, name FROM legacy_customers;ERROR: 42P21: collation mismatch between implicit collations "C" and "POSIX"
LINE 4: SELECT code, name FROM legacy_customers;
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.ALTER TABLE legacy_customers ALTER COLUMN code TYPE text COLLATE "C";ALTER TABLEChanging the column collation affects every query on that column, not only this view.
CREATE VIEW all_customers AS
SELECT code, name FROM customers
UNION
SELECT code, name FROM legacy_customers;
SELECT * FROM all_customers ORDER BY code;CREATE VIEW
code | name
------+------
A01 | Kim
B02 | Lee
C03 | Park
(3 rows)03Concatenating (||) the two columns and sorting by the result
c.code || '-' || l.code AS pair ... ORDER BY pair failed at l.code. Concatenation also produces a text value that needs one collation, and here one part was "C" and the other "POSIX".
Adding COLLATE "C" to l.code made the result "C", and the query returned A01-A01.
Setup
CREATE TABLE customers (code text COLLATE "C", name text);
CREATE TABLE legacy_customers (code text COLLATE "POSIX", name text);
INSERT INTO customers VALUES ('A01', 'Kim'), ('B02', 'Lee');
INSERT INTO legacy_customers VALUES ('A01', 'Kim'), ('C03', 'Park');CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2SELECT c.code || '-' || l.code AS pair
FROM customers c JOIN legacy_customers l ON l.name = c.name
ORDER BY pair;ERROR: 42P21: collation mismatch between implicit collations "C" and "POSIX"
LINE 1: SELECT c.code || '-' || l.code AS pair
^
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.SELECT collation for (c.code) AS c_code, collation for (l.code) AS l_code
FROM customers c JOIN legacy_customers l ON l.name = c.name; c_code | l_code
--------+---------
"C" | "POSIX"
(1 row)collation for shows "C" for c.code and "POSIX" for l.code in the same row.
SELECT collation for (c.code || '-' || l.code COLLATE "C") AS pair_collation
FROM customers c JOIN legacy_customers l ON l.name = c.name; pair_collation
----------------
"C"
(1 row)SELECT c.code || '-' || l.code COLLATE "C" AS pair
FROM customers c JOIN legacy_customers l ON l.name = c.name
ORDER BY pair; pair
---------
A01-A01
(1 row)04Two different COLLATE clauses in one comparison
ON c.code COLLATE "C" = l.code COLLATE "POSIX" failed with collation mismatch between explicit collations "C" and "POSIX". Explicit COLLATE clauses always win over column collations, so two different ones cannot be resolved. This error has no HINT line.
Keeping only one COLLATE (ON c.code = l.code COLLATE "C") worked: the plan is a normal Hash Join and the query returned A01 | Kim.
Setup
CREATE TABLE customers (code text COLLATE "C", name text);
CREATE TABLE legacy_customers (code text COLLATE "POSIX", name text);
INSERT INTO customers VALUES ('A01', 'Kim'), ('B02', 'Lee');
INSERT INTO legacy_customers VALUES ('A01', 'Kim'), ('C03', 'Park');CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2SELECT c.code, c.name
FROM customers c JOIN legacy_customers l
ON c.code COLLATE "C" = l.code COLLATE "POSIX";ERROR: 42P21: collation mismatch between explicit collations "C" and "POSIX"
LINE 3: ON c.code COLLATE "C" = l.code COLLATE "POSIX";
^EXPLAIN (COSTS OFF)
SELECT c.code, c.name
FROM customers c JOIN legacy_customers l
ON c.code = l.code COLLATE "C"; QUERY PLAN
--------------------------------------------
Hash Join
Hash Cond: (c.code = (l.code)::text)
-> Seq Scan on customers c
-> Hash
-> Seq Scan on legacy_customers l
(5 rows)SELECT c.code, c.name
FROM customers c JOIN legacy_customers l
ON c.code = l.code COLLATE "C"; code | name
------+------
A01 | Kim
(1 row)FAQ
What is the difference between 42P21 and 42P22?
Here 42P21 came at parse time from UNION, || and two COLLATE clauses. On the 42P22 page, a JOIN, IN subquery or > between the same columns failed later with could not determine which collation to use. Both were fixed with COLLATE.
How do I check a column's collation?
information_schema.columns.collation_name shows it per column (C and POSIX here). For an expression, SELECT collation for (expr) shows the collation of the result.
Do I put COLLATE on one side or both?
One side was enough in every case here. Putting two different COLLATE clauses is exactly what caused the explicit-collations error.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12