Locale data changes

From PostgreSQL wiki

Jump to: navigation, search

The following information focuses on operating systems using the GNU C library (glibc), which includes most popular Linux distributions. All versions of PostgreSQL are affected. Other operating systems could in principle have the same issues, but we have not gathered any information about that.

PostgreSQL uses locale data provided by the operating system’s C library for sorting text. Sorting happens in a variety of contexts, including for user output, merge joins, B-tree indexes, and range partitions. In the latter two cases, sorted data is persisted to disk. If the locale data in the C library changes during the lifetime of a database, the persisted data may become inconsistent with the expected sort order, which could lead to erroneous query results and other incorrect behavior. For example, if an index is not sorted in a way that an index scan is expecting it, a query could fail to find data that is actually there, and an update could insert duplicate data that should be disallowed. Similarly, in a partitioned table, a query could look in the wrong partition and an update could write to the wrong partition. Therefore, it is essential to the correct operation of a database that the locale definitions do not change incompatibly during the lifetime of a database.

Operating system vendors, and in particular the authors of the GNU C library, change the locale data from time to time in minor ways to correct mistakes or add support for more languages. While this in theory violates the above rule, it has historically affected few users and has not received wide attention. However, in glibc version 2.28, released 2018-08-01, a major update to the locale data has been included, which can potentially affect the data of many users. It should be noted that the update itself is legitimate, as it brings the locale data in line with current international standards. But problems are bound to happen if these updates are applied to an existing PostgreSQL system.

The integration of glibc updates into Linux distributions is the domain of the operating system vendor. We expect that vendors of long-term support Linux distributions will not apply incompatible locale updates to their distribution within a given release, but this is only an expectation, as we cannot predict or influence future actions. Moreover, PostgreSQL currently has no way to detect an incompatible glibc update. Therefore, some manual care is required in planning any updates or upgrades.

Contents

What is affected

Situations that are potentially affected involve changed locale data being applied to an existing instance or binary-equivalent instance, in particular:

  • Changing locale data on a running instance (even if restarted).
    • This includes in particular upgrading the Linux distribution to a new major release while keeping the PostgreSQL data directory around.
    • Using pg_upgrade (e.g. when upgrading both the operating system and the PostgreSQL major at the same time) does not avoid the problem.
  • Using streaming replication to a standby instance with different locale data. (The standby is then potentially corrupted, but the primary is fine.)
  • Restoring a binary backup (e.g., pg_basebackup) on a system with different locale data.

Not affected are situations where the data is transported in a logical (not binary) way, including:

  • Backups/upgrades using pg_dump
  • Logical replication

What to do

When an instance needs to be upgraded to a new glibc release, for example to upgrade the operating system, then after the upgrade

  • All indexes involving columns of type text, varchar, char, and citext should be reindexed before the instance is put into production.
  • Range-partitioned tables using those types in the partition key should be checked to verify that all rows are still in the correct partitions. (This is quite unlikely to be a problem, only with particularly obscure partitioning bounds.)
  • To avoid downtime due to reindexing or repartitioning, consider upgrading using logical replication.
  • Databases or table columns using the “C” or “POSIX” locales are not affected. All other locales are potentially affected.
  • Table columns using collations with the ICU provider are not affected.

What indexes are affected

Use this SQL query in each database to find out which indexes are affected:

SELECT indrelid::regclass::text, indexrelid::regclass::text, collname, pg_get_indexdef(indexrelid) 
FROM (SELECT indexrelid, indrelid, indcollation[i] coll FROM pg_index, generate_subscripts(indcollation, 1) g(i)) s 
  JOIN pg_collation c ON coll=c.oid
WHERE collprovider IN ('d', 'c') AND collname NOT IN ('C', 'POSIX');

What Linux distributions are affected

To help users assess the situation with their current operating system, we have assembled the following information. Note again that this is only a report of the current situation and that we cannot influence what these vendors do or might do in the future.

Debian

Version 8 (jessie) and 9 (stretch) use the old locale data. We don’t expect any incompatible changes inside those releases. Upgrades from version 8 to 9 are safe.

Version 10 (buster) uses the new locale data. Therefore, caution will be necessary when upgrading.

See also:

Ubuntu

Ubuntu up to version 18.04 (bionic) uses the old locale data.

The new glibc 2.28 locale data is introduced in version 18.10 (cosmic) (not LTS). Upgrading from bionic-or-older to cosmic-or-newer needs the mitigation steps outlined above.

RHEL/CentOS

Versions 6 and 7 use the old locale data. Upgrades from version 6 to 7 are safe.

Version 8 uses the new locale data. Therefore, caution will be necessary when upgrading.

add yours here

References

Personal tools