Unique constraint violation in Postgres due to OS upgrade


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

    

Summary

Upgrading glibc to >= 2.28  allows unique constraint violation in Postgres , as the sorting order changes for most of the collations. This may lead to database inconsistency.

Environment

  1. The Glibc library version 2.28 has been released on a majority of Linux systems:
    • Redhat 8 (released May 7th, 2019)
    • Debian 10 (July 6th, 2019)
    • Fedora 29 (October 30, 2018)
    • and others
    • Diagnosis
  2. Postgres

Diagnosis

After upgrade from Debian Stretch (PostgreSQL 9.6) to Debian Buster (PostgreSQL 11) there were duplicated data in the database table with unique constraint applied. The data looked like this: 

bitbucket=# select * from repository where project_id=146;

Output 

idslugnamestateproject_idscm_idhierarchy_idis_forkableis_publicstore_iddescription
793text-rnntext-rnn1146git695abfc80b2e075ae7adtt

812ink_dataink_data1146git2f40bb01986ce5bc3b63tf

829ink_dataink_data1146gitcb51bbad5d4d26bb9ce9tf

ids 812 and 829 have exactly the same slug  and project_id . But it shouldn’t be possible if the unique constraint (CONSTRAINT uk_slug_project_id UNIQUE (slug, project_id)) is applied to that table.

Cause

Glibc 2.28 breaks collation for PostgreSQL (and others?)

Solution

  1. If there is an upgrade of the OS, and glibc upgrade is also involved make sure the indexes are rebuilt before the application is started. The rebuilding of all the indexes will prevent the issue from happening.

  2. If the upgrade was performed by restoring the application database into upgraded operating system, the indexes should be recalculated automatically if pg_restore was used.

  3. If the collation is set to ISO C and POSIX the upgrade doesn’t break the ordering.

  4. In case the duplication is observed, follow the steps in How to fix duplicate entires in Bitbucket PostgreSQL database to resolve the duplicates.



Last modified on Aug 30, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.