PostgreSQL "database has a collation version mismatch" After an OS Upgrade: How to Fix It Safely
After an OS upgrade changes glibc, PostgreSQL warns of a collation version mismatch. How to rebuild affected indexes and refresh the recorded version.
After an operating system upgrade that changes the C library, PostgreSQL logs a warning on every connection to each database. A typical case is Debian 12 to Debian 13, which moves glibc from 2.36 to 2.41:
WARNING: database "<dbname>" has a collation version mismatch
DETAIL: The database was created using collation version 2.36, but the operating system provides version 2.41.
HINT: Rebuild all objects in this database that use the default collation and run ALTER DATABASE <dbname> REFRESH COLLATION VERSION, or build PostgreSQL with the right library version.
Cause
Text indexes are stored in collation (sort) order. With libc collations, PostgreSQL uses the glibc version as a stand-in for the collation version. It records that version when the database is created and compares it on connection.
When glibcβs sort rules change, indexes built under the old rules can return wrong results or miss rows. Unique indexes may even let duplicates in.
PostgreSQL cannot tell whether the sort order actually changed between two glibc versions. It only sees that the version number differs. The PostgreSQL wiki records glibc 2.28 as the well-known release with major locale changes. Rebuilding is still the only way to be certain for any upgrade.
Database-level version tracking, and the REFRESH COLLATION VERSION command for databases, exist in PostgreSQL 15 and later.
Fix
Do this for every database in the cluster, including postgres and template1.
1. See which databases are affected
SELECT datname,
datcollversion,
pg_database_collation_actual_version(oid) AS actual_version
FROM pg_database
WHERE datcollversion IS DISTINCT FROM pg_database_collation_actual_version(oid);
2. Find objects that depend on non-default collations
This query is from the PostgreSQL documentation. Run it in each database:
SELECT pg_describe_object(refclassid, refobjid, refobjsubid) AS "Collation",
pg_describe_object(classid, objid, objsubid) AS "Object"
FROM pg_depend d JOIN pg_collation c
ON refclassid = 'pg_collation'::regclass AND refobjid = c.oid
WHERE c.collversion <> pg_collation_actual_version(c.oid)
ORDER BY 1, 2;
Indexes that use the database default collation do not appear in pg_depend. Treat every index on text, varchar or char columns as affected.
3. Rebuild the indexes
The simplest safe option is to rebuild every index in the database. CONCURRENTLY avoids long write locks on user tables but takes longer:
REINDEX DATABASE CONCURRENTLY <dbname>;
On PostgreSQL 15 you must be connected to the database being reindexed. To limit the work on very large databases, reindex only the indexes on text-type columns, plus any objects returned by step 2.
4. Record the new version
Only after rebuilding:
ALTER DATABASE <dbname> REFRESH COLLATION VERSION;
For any non-default collations reported in step 2:
ALTER COLLATION <schema>.<collation_name> REFRESH VERSION;
The refresh only updates the catalogue and silences the warning. It does not check whether anything was rebuilt.
5. Repeat for the remaining databases
Template and maintenance databases can be refreshed from any session:
ALTER DATABASE template1 REFRESH COLLATION VERSION;
ALTER DATABASE postgres REFRESH COLLATION VERSION;
Reindex them first if they contain your own objects.
Verify
Re-run the step 1 query. It should return no rows. New connections should no longer log the warning.
Notes
- Do not run
REFRESH COLLATION VERSIONon its own just to stop the warning. If the sort order did change, that leaves possibly corrupt indexes in place with no further warning. - Replicas: a physical (streaming) replica must use the same glibc collation behaviour as its primary. Upgrade the OS on the whole replication set together, or use logical replication or dump and restore across the change.
pg_upgradeand OS upgrades: doing both at once combines two risks. Upgrade one, rebuild and verify, then do the other.- ICU collations carry their own version numbers and are handled the same way with
ALTER COLLATION ... REFRESH VERSION. - PostgreSQL 14 and older do not track the database default collation version, so no warning appears. Rebuild text indexes after any glibc upgrade anyway.
- The
amcheckextension (bt_index_check) can help confirm index integrity, but a clean result does not replace rebuilding.