%3CLINGO-SUB%20id%3D%22lingo-sub-1982966%22%20slang%3D%22en-US%22%3ERe%3A%20Don't%20let%20collation%20versions%20corrupt%20your%20PostgreSQL%20indexes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1982966%22%20slang%3D%22en-US%22%3E%3CP%3EIt's%20good%20to%20see%20work%20done%20on%20the%20collation%20issue.%20Encountering%20this%20on%20a%20production%20system%20was%20%3CEM%3Enot%3C%2FEM%3E%20the%20happiest%20experience%2C%20especially%20when%20corrupted%20indexes%20interfered%20with%20fixing%20the%20duplicate%20problem.%20Fortunately%20I%20knew%20how%20to%20disable%20their%20use%3B%20couldn't%20just%20reindex%2C%20because%20by%20then%20tables%20had%20ended%20up%20with%20uniqueness%20or%20foreign%20key%20constraint%20violations.%20Once%20string%20comparisons%20flipped%2C%20all%20bets%20were%20off!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1978394%22%20slang%3D%22en-US%22%3EDon't%20let%20collation%20versions%20corrupt%20your%20PostgreSQL%20indexes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1978394%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20part%20of%20my%20work%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fmicrosoft-azure-welcomes-postgresql-committers%2Fba-p%2F1207612%22%20target%3D%22_self%22%3Eopen%20source%20PostgreSQL%20team%20at%20Microsoft%3C%2FA%3E%2C%20I%20recently%20committed%20a%20new%20feature%20to%20track%20dependencies%20on%20collation%20versions%2C%20with%20help%20from%20co-author%20Julien%20Rouhaud%20and%20the%20many%20others%20who%20contributed%20ideas.%20It%20took%20a%20long%20time%20to%20build%20a%20consensus%20on%20how%20to%20tackle%20this%20thorny%20problem%20(work%20I%20began%20at%20EnterpriseDB%20and%20continued%20at%20Microsoft)%2C%20and%20you%20can%20read%20about%20some%20of%20the%20details%20and%20considerations%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommitdiff%3Bh%3D257836a75585934cc05ed7a80bccf8190d41e056%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecommit%20message%3C%2FA%3E%20below%20and%20the%20referenced%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2Fflat%2FCAEepm%253D0uEQCpfq_%252BLYFBdArCe4Ot98t1aR4eYiYTe%253DyavQygiQ%2540mail.gmail.com%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ediscussion%20thread%3C%2FA%3E.%20Please%20note%20that%20some%20details%20may%20change%20by%20the%20time%20PostgreSQL%2014%20is%20released.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ecommit%20257836a75585934cc05ed7a80bccf8190d41e056%0AAuthor%3A%20Thomas%20Munro%20%3CTMUNRO%3E%0ADate%3A%20%20%20Mon%20Nov%202%2019%3A50%3A45%202020%20%2B1300%0A%0A%20%20%20%20Track%20collation%20versions%20for%20indexes.%0A%20%20%20%20%0A%20%20%20%20Record%20the%20current%20version%20of%20dependent%20collations%20in%20pg_depend%20when%0A%20%20%20%20creating%20or%20rebuilding%20an%20index.%20%20When%20accessing%20the%20index%20later%2C%20warn%0A%20%20%20%20that%20the%20index%20may%20be%20corrupted%20if%20the%20current%20version%20doesn't%20match.%0A%20%20%20%20%0A%20%20%20%20Thanks%20to%20Douglas%20Doole%2C%20Peter%20Eisentraut%2C%20Christoph%20Berg%2C%20Laurenz%20Albe%2C%0A%20%20%20%20Michael%20Paquier%2C%20Robert%20Haas%2C%20Tom%20Lane%20and%20others%20for%20very%20helpful%0A%20%20%20%20discussion.%0A%20%20%20%20%0A%20%20%20%20Author%3A%20Thomas%20Munro%20%3CTHOMAS.MUNRO%3E%0A%20%20%20%20Author%3A%20Julien%20Rouhaud%20%3CRJUJU123%3E%0A%20%20%20%20Reviewed-by%3A%20Peter%20Eisentraut%20%3CPETER.EISENTRAUT%3E%20(earlier%20versions)%0A%20%20%20%20Discussion%3A%20%3CA%20href%3D%22https%3A%2F%2Fpostgr.es%2Fm%2FCAEepm%253D0uEQCpfq_%252BLYFBdArCe4Ot98t1aR4eYiYTe%253DyavQygiQ%2540mail.gmail.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpostgr.es%2Fm%2FCAEepm%253D0uEQCpfq_%252BLYFBdArCe4Ot98t1aR4eYiYTe%253DyavQygiQ%2540mail.gmail.com%3C%2FA%3E%3C%2FPETER.EISENTRAUT%3E%3C%2FRJUJU123%3E%3C%2FTHOMAS.MUNRO%3E%3C%2FTMUNRO%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'm%20pretty%20happy%20with%20the%20result%20so%20far%2C%20but%20there%20is%20more%20to%20be%20done%20(see%20further%20down)!%26nbsp%3B%3CSPAN%3ENow%20seems%20like%20a%20good%20time%20to%20walk%20you%20through%20the%20problem%20we%20needed%20to%20solve%E2%80%94that%20PostgreSQL%20indexes%20can%20get%20corrupted%20by%20changes%20in%20collations%20that%20occur%20naturally%20over%20time%E2%80%94and%20how%20the%20new%20feature%20makes%20things%20better%20in%20PostgreSQL%2014.%20Plus%2C%20you'll%20get%20a%20bit%20of%20background%20on%20collations%2C%20too%3C%2FSPAN%3E.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1180036077%22%20id%3D%22toc-hId--1180036077%22%3EWhat%20is%20a%20collation%3F%3C%2FH2%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20term%20%3CEM%3Ecollation%3C%2FEM%3E%20derives%20from%20a%20form%20of%20the%20Latin%20verb%20%3CEM%3Econferre%3C%2FEM%3E%20meaning%20to%20confer%2C%20bring%20together%2C%20or%20compare.%20Book%20binders%20use%20the%20term%20collation%20to%20mean%20putting%20pages%20into%20the%20correct%20order.%20Outside%20special%20technical%20fields%2C%20collation%20may%20be%20better%20known%20as%20an%20old%20fashioned%20and%20formal%20name%20for%20a%20cold%20meal%20(cf.%20%3CEM%3Ecolazione%3C%2FEM%3E%2C%20Italian%20for%20breakfast).%20That%20meaning%20comes%20to%20us%20from%20the%20same%20ultimate%20source%2C%20having%20developed%20a%20different%20meaning%20in%204th%20century%20monasteries%20through%20association%20with%20brown%20bag%20lunches%2C%20but%20I%20digress...%3CBR%20%2F%3E%3CBR%20%2F%3EMany%20software%20systems%20including%20%3CA%20href%3D%22https%3A%2F%2Fpubs.opengroup.org%2Fonlinepubs%2F9699919799%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPOSIX%3C%2FA%3E%20and%20Windows%20have%20%3CEM%3Elocales%3C%2FEM%3E%20that%20encapsulate%20language%20and%20cultural%20conventions.%20A%20collation%20is%20the%20component%20of%20a%20locale%20that%20controls%20the%20way%20text%20is%20sorted.%26nbsp%3B%20Even%20though%20many%20languages%20share%20common%20writing%20systems%2C%20they%20don't%20necessarily%20agree%20on%20how%20to%20sort%20words.%20Even%20within%20the%20same%20language%2C%20there%20can%20be%20differences%20in%20how%20words%20should%20be%20sorted%20in%20different%20countries%20or%20in%20specialised%20contexts.%3CBR%20%2F%3E%3CBR%20%2F%3ESimilarly%2C%20in%20the%20context%20of%20SQL%2C%20a%3CCODE%3ECOLLATION%3C%2FCODE%3Eis%20a%20database%20object%20that%20identifies%20a%20set%20of%20rules%20for%20sorting%20character%20strings.%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20example%2C%20in%20a%20Danish%20dictionary%2C%20%3CSTRONG%3E%22%C3%A5%22%3C%2FSTRONG%3E%20comes%20after%20%3CSTRONG%3E%22z%22%3C%2FSTRONG%3E%2C%20and%20%3CSTRONG%3E%22aa%22%3C%2FSTRONG%3E%20is%20considered%20to%20be%20an%20alternative%20spelling%20of%20%3CSTRONG%3E%22%C3%A5%22%3C%2FSTRONG%3E.%20PostgreSQL%20models%20the%20different%20possible%20sorting%20rules%20with%20different%20collations%2C%20and%20we%20can%20see%20this%20difference%20on%20most%20systems%20like%20so%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Epostgres%3D%23%20create%20table%20words%20(word%20text)%3B%0ACREATE%20TABLE%0Apostgres%3D%23%20insert%20into%20words%20values%20('Aarhus')%2C%20('Banana')%2C%20('Cat')%3B%0AINSERT%200%203%0Apostgres%3D%23%20select%20*%20from%20words%20order%20by%20word%20collate%20%22en_NZ%22%3B%0A%20%20word%20%20%0A--------%0A%20Aarhus%0A%20Banana%0A%20Cat%0A(3%20rows)%0A%0Apostgres%3D%23%20select%20*%20from%20words%20order%20by%20word%20collate%20%22da_DK%22%3B%0A%20%20word%20%20%0A--------%0A%20Banana%0A%20Cat%0A%20Aarhus%0A(3%20rows)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20might%20affect%20where%20a%20user%20expects%20to%20find%20a%20value%20in%20drop-down%20list%20or%20contents%20page.%3CBR%20%2F%3E%3CBR%20%2F%3ECollations%20are%20part%20of%20the%20static%20type%20system%20of%20SQL.%20If%20you're%20working%20with%20text%2C%20you%20can't%20escape%20them!%20You%20can%20mostly%20ignore%20them%20and%20just%20let%20your%20whole%20system%20use%20the%20collation%20named%20%22default%22%2C%20though.%20That's%20how%20a%20lot%20of%20you%20probably%20consume%20collations%2C%20especially%20if%20you%20only%20deal%20with%20one%20language.%20That%20means%20you%20get%20the%20default%20for%20the%20database%20you're%20connected%20to%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Epostgres%3D%23%20%5Cl%20postgres%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20List%20of%20databases%0A%20%20%20Name%20%20%20%7C%20Owner%20%20%7C%20Encoding%20%7C%20%20%20Collate%20%20%20%7C%20%20%20%20Ctype%20%20%20%20%7C%20Access%20privileges%20%0A----------%2B--------%2B----------%2B-------------%2B-------------%2B-------------------%0A%20postgres%20%7C%20tmunro%20%7C%20UTF8%20%20%20%20%20%7C%20en_NZ.UTF-8%20%7C%20en_NZ.UTF-8%20%7C%20%0A(1%20row)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOften%20the%20default%20collation%20is%20set%20by%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fapp-initdb.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Einitdb%3C%2FCODE%3E%3C%2FA%3E%20when%20the%20PostgreSQL%20cluster%20is%20first%20created.%20The%20collation%20and%20other%20locale%20components%20can%20be%20set%20using%20defaults%20from%20the%20operating%20system%20environment%2C%20and%20then%20inherited%20by%20databases%20created%20later.%20However%2C%20they%20can%20also%20be%20explicitly%20specified%2C%20and%20changed%20in%20later%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fsql-createdatabase.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3ECREATE%20DATABASE%3C%2FCODE%3E%3C%2FA%3E%20commands%20with%20certain%20restrictions.%20You%20can%20also%20opt%20for%20one%20of%20the%20special%20collations%20%22C%22%2C%20%22POSIX%22%2C%20or%20%22ucs_basic%22%20which%20sort%20in%20binary%20or%20code%20point%20order%20rather%20than%20following%20cultural%20conventions%2C%20avoiding%20the%20problems%20described%20below%E2%80%94fair%20warning%20though%2C%20the%20results%20might%20surprise%20a%20few%20humans%20if%20they%20are%20reflected%20in%20user%20interfaces.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1307476756%22%20id%3D%22toc-hId-1307476756%22%3ESo%20what's%20the%20catch%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FH2%3E%0A%3CP%3ETo%20quote%20from%20%3CA%20href%3D%22https%3A%2F%2Funicode.org%2Freports%2Ftr10%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUnicode%20Technical%20Standard%20%2310%3C%2FA%3E%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EOver%20time%2C%20collation%20order%20will%20vary%3A%20there%20may%20be%20fixes%20needed%20as%20more%20information%20becomes%20available%20about%20languages%3B%20there%20may%20be%20new%20government%20or%20industry%20standards%20for%20the%20language%20that%20require%20changes%3B%20and%20finally%2C%20new%20characters%20added%20to%20the%20Unicode%20Standard%20will%20interleave%20with%20the%20previously-defined%20ones.%20This%20means%20that%20collations%20must%20be%20carefully%20versioned.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20most%20obvious%20problem%20this%20causes%20for%20databases%20like%20PostgreSQL%20is%20that%20B-tree%20indexes%20can%20become%20corrupted%20if%20they%20are%20ordered%20by%20a%20collation%20that%20changes.%20Typical%20symptoms%20reported%20in%20the%20wild%20include%20rows%20that%20%3CEM%3Ecannot%3C%2FEM%3E%20be%20seen%20by%20one%20query%20but%20%3CEM%3Ecan%3C%2FEM%3E%20be%20seen%20by%20another%20query%20that%20is%20using%20a%20different%20plan.%20Or%20sometimes%2C%20a%20unique%20index%20fails%20to%20prevent%20a%20duplicate%20value%20from%20being%20added%20to%20a%20table.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3ETo%20see%20why%20collation%20changes%20could%20impact%20whether%20PostgreSQL%20is%20able%20to%20look%20things%20up%2C%20let's%20use%20a%20phone%20book%20analogy.%20After%20all%2C%20a%20phone%20book%20is%20an%20ordered%20index%20with%20a%20lot%20of%20similarities%20to%20indexes%20in%20databases.%20So%2C%20imagine%20trying%20to%20look%20up%20your%20own%20name%20in%20an%20old-fashioned%20paper%20phone%20book%E2%80%94then%20imagine%20the%20phone%20book%20was%20created%20by%20someone%20who%20believes%20the%20alphabet%20goes%20in%20a%20slightly%20different%20order%20than%20you.%20There's%20a%20chance%20you'll%20conclude%20your%20name%20is%20%3CEM%3Enot%3C%2FEM%3E%20in%20the%20phone%20book%2C%20because%20your%20name%20is%20ordered%20in%20some%20unexpected%20place%20in%20the%20book%2C%20effectively%20hidden%20from%20you.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EFor%20a%20simple%20real-world%20example%20of%20a%20change%20in%20sort%20order%20on%20GNU%2FLinux%20that%20involves%20only%20ASCII%20characters%2C%20the%20sort%20order%20of%20the%20strings%20%3CSTRONG%3E'a-a'%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E'a%2Ba'%3C%2FSTRONG%3E%20flipped%20in%20glibc%202.28%20(Debian%2010%2C%20RHEL%208)%3C%2Fimg%3E%20compared%20to%20earlier%20releases.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--499977707%22%20id%3D%22toc-hId--499977707%22%3EWhere%20does%20PostgreSQL's%20collation%20logic%20come%20from%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FH2%3E%0A%3CP%3EPostgreSQL%20relies%20on%20external%20libraries%20to%20order%20strings.%20There%20are%20two%20%22providers%22%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CCODE%3Elibc%3C%2FCODE%3E%2C%20meaning%20the%20operating%20system%20locale%20facility%20(POSIX%20or%20Windows)%3C%2FLI%3E%0A%3CLI%3E%3CCODE%3Eicu%3C%2FCODE%3E%2C%20meaning%20the%20%3CA%20href%3D%22http%3A%2F%2Fsite.icu-project.org%2Fhome%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EICU%20project%3C%2FA%3E%2C%20assuming%20PostgreSQL%20was%20built%20with%20ICU%20support%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EFor%20now%2C%20only%3CCODE%3Elibc%3C%2FCODE%3Ecollations%20can%20be%20used%20as%20the%20database%20default%2C%20though%20ICU%20collations%20can%20be%20used%20explicitly%2C%20for%20example%20when%20defining%20the%20columns%20of%20a%20table.%20So%20which%20should%20you%20use%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3EOne%20advantage%20of%20using%3CCODE%3Elibc%3C%2FCODE%3Ecollations%20is%20that%20the%20sorting%20behaviour%20matches%20other%20software%20on%20the%20same%20operating%20system.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EConversely%2C%20by%20using%20ICU%20it%20is%20possible%20to%20get%20software%20that%20is%20running%20on%20different%20operating%20systems%20to%20agree%20on%20sort%20order%2C%20if%20they're%20also%20using%20ICU.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EAnother%20reason%20to%20use%20ICU%3A%20some%20operating%20systems%20provide%20only%20very%20rudimentary%20collation%20logic%20in%20their%3CCODE%3Elibc%3C%2FCODE%3E%2C%20especially%20when%20using%20the%20UTF-8%20encoding.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EAnd%20a%203rd%20reason%20to%20use%20ICU%20is%20that%20ICU%20supports%20a%20rich%20system%20of%20end-user%20customisation%20(see%20some%20simple%20examples%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fcollation.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPostgreSQL%20manual%3C%2FA%3E).%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20the%20ICU%20project's%20collation%20rules%20are%20based%20strictly%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Funicode.org%2Freports%2Ftr10%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUnicode%20Collation%20Algorithm%3C%2FA%3E%20and%20the%20ICU%20library%20version%20can%20be%20matched%20up%20with%20specific%20versions%20of%20%3CA%20href%3D%22https%3A%2F%2Fhome.unicode.org%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUnicode%3C%2FA%3E%20and%20%3CA%20href%3D%22http%3A%2F%2Fcldr.unicode.org%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECLDR%3C%2FA%3E%2C%20things%20are%20a%20little%20murkier%20for%3CCODE%3Elibc%3C%2FCODE%3E%2C%20depending%20on%20your%20operating%20system.%20Many%20operating%20systems%20including%20FreeBSD%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Farchive%2Fblogs%2Fshawnste%2Flocale-data-in-windows-10-cldr%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWindows%2010%2C%26nbsp%3B%3C%2FA%3Eand%20GNU%20libc%20have%20migrated%20or%20are%20in%20the%20process%20of%20migrating%20from%20their%20own%20historical%20implementations%20of%20sorting%20rules%20to%20UCA-based%20logic%20and%20CLDR%2C%20so%20that%20eventually%20there%20will%20be%20fewer%20differences%20between%20systems%2C%20and%20between%3CCODE%3Elibc%3C%2FCODE%3Eand%20ICU%20collations.%20Ironically%20it%20is%20this%20process%20that%20has%20caused%20many%20of%20the%20changes%20that%20we've%20seen%20in%20the%20past%20few%20years%20in%3CCODE%3Elibc%3C%2FCODE%3Ecollation%20definitions%20on%20popular%20operating%20systems.%20Even%20if%20all%20implementations%20eventually%20converged%2C%20CLDR%20itself%20is%20a%20moving%20target%20and%20it's%20possible%20that%20the%20versions%20aren't%20in%20sync.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%20available%20set%20of%20collations%20on%20your%20system%20can%20be%20seen%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fcatalog-pg-collation.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Epg_collation%3C%2FCODE%3E%3C%2FA%3E%20catalog.%20You%20can%20add%20more%20with%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fsql-createcollation.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3ECREATE%20COLLATION%3C%2FCODE%3E%3C%2FA%3E%20command%2C%20but%20you%20need%20to%20know%20how%20to%20form%3CCODE%3Ecollcollate%3C%2FCODE%3Estrings%20that%20are%20meaningful%20to%20your%20operating%20system%20or%20to%20ICU%20as%20appropriate.%20Note%20that%20collations%20often%20have%20names%20like%20%22en_US%22%2C%20which%20requires%20double%20quotes%20when%20you%20reference%20it%20in%20a%3CCODE%3ECOLLATE%3C%2FCODE%3Eexpression%20due%20their%20case-sensitivity.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1987535126%22%20id%3D%22toc-hId-1987535126%22%3EWhere%20does%20collation%20version%20information%20come%20from%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20ICU%20library%20can%20report%20an%20opaque%20version%20for%20the%20collations%20it%20provides.%20In%20PostgreSQL%20releases%2010%20to%2013%2C%20versions%20of%20ICU%20collations%20were%20already%20captured%20in%20the%3CCODE%3Epg_collation%3C%2FCODE%3Ecatalog%20table%20when%20they%20were%20first%20created.%20Typically%20that's%20when%20the%20cluster%20is%20first%20set%20up.%20Then%2C%20when%20you%20use%20the%20collation%20(for%20example%2C%20by%20accessing%20an%20index%20that%20is%20ordered%20by%20it)%2C%20a%20check%20is%20performed%2C%20and%20a%20warning%20is%20produced.%20This%20would%20alert%20you%20to%20rebuild%20all%20relevant%20indexes%20and%20then%20run%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fsql-altercollation.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3EALTER%20COLLATION%20xxx%20REFRESH%20VERSION%3C%2FCODE%3E%3C%2FA%3E%20when%20done.%20This%20was%20a%20good%20start%20but%20it%20didn't%20help%20many%20people%20for%20several%20reasons%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Eit%20didn't%20work%20for%3CCODE%3Elibc%3C%2FCODE%3Ecollations%20(by%20far%20the%20more%20popular%20provider)%3C%2FLI%3E%0A%3CLI%3Eit%20was%20difficult%20to%20see%20how%20to%20extend%20this%20model%20to%20cover%20the%20%22default%22%20collation%20(by%20far%20the%20most%20popular%20way%20to%20use%20collations%2C%20even%20if%20users%20may%20be%20unaware%20they're%20doing%20it!)%3C%2FLI%3E%0A%3CLI%3Eit%20didn't%20know%20which%20indexes%20needed%20to%20be%20rebuilt%20after%20an%20upgrade%2C%20and%20relied%20on%20a%20human%20to%20figure%20that%20out%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20step%26nbsp%3B%3CSPAN%3Eto%20making%20things%20better%20and%20to%20tracking%20collation%20version%20dependencies%26nbsp%3B%3C%2FSPAN%3Ewas%20to%20figure%20out%20how%20to%20get%20version%20information%20from%20the%20operating%20system%20for%3CCODE%3Elibc%3C%2FCODE%3Ecollations.%20No%20relevant%20standard%20provides%20such%20an%20interface%2C%20but%20patches%20have%20now%20been%20committed%20to%20do%20that%20for%20GNU%2FLinux%2C%20FreeBSD%2013%2B%2C%20and%20Windows%20using%20non-standard%20interfaces%20provided%20by%20those%20systems.%20Here%20are%20some%20example%20values%2C%20as%20returned%20by%20the%3CCODE%3Epg_collation_actual_version()%3C%2FCODE%3Efunction%20in%20PostgreSQL%2014-devel%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%20width%3D%2276.0333px%22%3EOS%3C%2FTH%3E%0A%3CTH%20width%3D%2246px%22%3EProvider%3C%2FTH%3E%0A%3CTH%20width%3D%2296px%22%3ECollation%3C%2FTH%3E%0A%3CTH%20width%3D%22125px%22%3EVersion%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2276.0333px%22%3ELinux%3C%2FTD%3E%0A%3CTD%20width%3D%2246px%22%3Elibc%3C%2FTD%3E%0A%3CTD%20width%3D%2296px%22%3Een_NZ%3C%2FTD%3E%0A%3CTD%20width%3D%22125px%22%3E2.28%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2276.0333px%22%3EFreeBSD%3C%2FTD%3E%0A%3CTD%20width%3D%2246px%22%3Elibc%3C%2FTD%3E%0A%3CTD%20width%3D%2296px%22%3Een_NZ%3C%2FTD%3E%0A%3CTD%20width%3D%22125px%22%3E34.0%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2276.0333px%22%3EWindows%3C%2FTD%3E%0A%3CTD%20width%3D%2246px%22%3Elibc%3C%2FTD%3E%0A%3CTD%20width%3D%2296px%22%3Een_NZ%3C%2FTD%3E%0A%3CTD%20width%3D%22125px%22%3E1538.14%2C1538.14%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2276.0333px%22%3E(All)%3C%2FTD%3E%0A%3CTD%20width%3D%2246px%22%3Eicu%3C%2FTD%3E%0A%3CTD%20width%3D%2296px%22%3Een-NZ-x-icu%3C%2FTD%3E%0A%3CTD%20width%3D%22125px%22%3E153.88%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPatches%20for%20other%20operating%20systems%20are%20welcome!%20These%20version%20strings%20are%20not%20interpreted%20in%20any%20way%20by%20PostgreSQL%2C%20except%20to%20check%20if%20they%20have%20changed.%20Note%20that%20on%20Linux%20we%20fall%20back%20to%20the%3CCODE%3Eglibc%3C%2FCODE%3Elibrary%20version%2C%20for%20lack%20of%20anything%20better.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-180080663%22%20id%3D%22toc-hId-180080663%22%3EWhat%20do%20we%20do%20with%20these%20new%20collation%20versions%20(starting%20in%20PostgreSQL%2014)%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgreSQL%20tracks%20the%20dependencies%20between%20database%20objects%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fdevel%2Fcatalog-pg-depend.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Epg_depend%3C%2FCODE%3E%3C%2FA%3E%20catalog.%20This%20is%20used%20for%20various%20purposes%2C%20including%20stopping%20you%20from%20dropping%20things%20that%20other%20things%20reference%2C%20dropping%20dependent%20things%20automatically%2C%20and%20dumping%20objects%20in%20the%20correct%20order.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%20new%3CCODE%3Erefobjversion%3C%2FCODE%3Ecolumn%20provides%20a%20place%20for%20us%20to%20record%20the%20collation%20version%20that%20was%20in%20effect%20at%20the%20time%20an%20index%20was%20created.%20For%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Epostgres%3D%23%20create%20table%20city%20(name%20text%20primary%20key)%3B%0ACREATE%20TABLE%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20now%2C%20after%20inserting%20some%20data%2C%20suppose%20we%20upgrade%20the%20operating%20system%2C%20or%20use%20a%20streaming%20replica%20server%20which%20has%20a%20newer%20operating%20system%20version%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Epostgres%3D%23%20select%20*%20from%20city%20where%20name%20between%20'Aarhus'%20and%20'Antioch'%3B%0AWARNING%3A%20%20index%20%22city_pkey%22%20depends%20on%20collation%20%22default%22%20version%20%2234.0%22%2C%20but%20the%20current%20version%20is%20%2236.0%22%0ADETAIL%3A%20%20The%20index%20may%20be%20corrupted%20due%20to%20changes%20in%20sort%20order.%0AHINT%3A%20%20REINDEX%20to%20avoid%20the%20risk%20of%20corruption.%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20just%20a%20warning%2C%20and%20will%20only%20be%20given%20once%20per%20index%2C%20per%20session.%20The%20primary%20way%20to%20stop%20the%20warning%20is%20with%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fdevel%2Fsql-reindex.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3EREINDEX%3C%2FCODE%3E%3C%2FA%3E%20command%2C%20as%20mentioned%20in%20the%20HINT.%20It's%20also%20possible%20to%20tell%20the%20system%20that%20you%20have%20independently%20verified%20that%20the%20index%20has%20not%20been%20corrupted%20by%20the%20change%20in%20version%20with%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fdevel%2Fsql-alterindex.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3EALTER%20INDEX%20name%20ALTER%20COLLATION%20name%20REFRESH%20VERSION%3C%2FCODE%3E%3C%2FA%3E.%20One%20way%20to%20verify%20that%20is%20to%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fdevel%2Famcheck.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Eamcheck%3C%2FCODE%3E%3C%2FA%3E%20module.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1627373800%22%20id%3D%22toc-hId--1627373800%22%3EWhat%20else%20could%20we%20do%20with%20the%20new%20collation%20version%20infrastructure%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECurrently%2C%20warnings%20advising%20you%20to%20rebuild%20indexes%20are%20raised%2C%20but%20you%20need%20to%20take%20action%20one%20by%20one%20to%20clear%20the%20warnings.%20It%20would%20be%20nice%20to%20have%20a%20new%20view%20and%20a%20new%20REINDEX%20variant%20to%20see%20and%20address%20all%20problems%20at%20once%2C%20before%20PostgreSQL%2014%20is%20released%3B%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2Fflat%2F20201203093143.GA64934%2540nol%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ework%20is%20underway%3C%2FA%3E%20to%20do%20that.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3ESo%20far%20I%20talked%20about%20indexes%2C%20because%20PostgreSQL%20indexes%20are%20the%20most%20likely%20source%20of%20problems%20when%20ordering%20functions%20change.%20Equivalent%20problems%20exist%20in%20other%20places%2C%20for%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3Erange%20partitions%20might%20now%20classify%20a%20key%20as%20belonging%20to%20a%20different%20partition%20after%20a%20collation%20change%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3Echeck%20constraints%20that%20perform%20string%20comparisons%20might%20previously%20have%20passed%2C%20but%20now%20fail%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3EWhile%20proof-of-concept%20patches%20have%20been%20drafted%20to%20provide%20similar%20warnings%20for%20those%20cases%2C%20it's%20a%20little%20less%20clear%20what%20the%20user%20should%20do%20about%20these%20warnings%2C%20so%20these%20proof-of-concept%20ideas%20remain%20under%20discussion.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-860139033%22%20id%3D%22toc-hId-860139033%22%3EMutating%20IMMUTABLE%20functions%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgreSQL%20requires%20every%20function%20or%20operator%20involved%20in%20the%20definition%20of%20an%20index%20to%20be%20marked%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fxfunc-volatility.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3EIMMUTABLE%3C%2FCODE%3E%3C%2FA%3E.%20The%20case%20of%20mutating%20collations%20can%20be%20thought%20of%20as%20a%20violation%20of%20that%20requirement%2C%20because%20the%20string%20comparison%20function%20(for%20example%2C%3CCODE%3Ebttextcmp%3C%2FCODE%3E)%20fails%20to%20guarantee%20that%20it'll%20return%20the%20same%20value%20given%20the%20same%20inputs%20for%20the%20rest%20of%20eternity.%20The%20same%20applies%20to%20user-defined%20functions%20that%20are%20declared%3CCODE%3EIMMUTABLE%3C%2FCODE%3E%2C%20but%20don't%20meet%20that%20requirement%20due%20to%2C%20say%2C%20a%20dependency%20on%20the%20time%2C%20a%20GUC%20setting%2C%20or%20any%20other%20information%20that%20is%20not%20derivable%20only%20from%20the%20inputs.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThere%20is%20a%20class%20of%20functions%20that%20change%20infrequently%20and%20at%20known%20times%2C%20however.%26nbsp%3B%20Perhaps%20it%20would%20be%20possible%20to%20create%20a%20system%20of%20declarative%20versions%20for%20those%2C%20using%20the%20new%3CCODE%3Erefobjversion%3C%2FCODE%3Esystem%3A%20the%20index%20would%20depend%20on%20a%20named%20version%20of%20the%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--947315430%22%20id%3D%22toc-hId--947315430%22%3EOther%20external%20things%20that%20could%20affect%20indexes%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConsider%20the%20example%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Funaccent.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Eunaccent%3C%2FCODE%3E%3C%2FA%3E%20function.%20A%20version%20for%20the%3CCODE%3Eunaccent()%3C%2FCODE%3Efunction%20itself%20would%20be%20a%20little%20too%20simplistic%2C%20because%20there%20is%20a%20second%20object%20besides%20the%20function%20that%20affects%20its%20results%3A%20the%20rules%20file.%20The%20rules%20file%20can%20be%20modified%20or%20replaced%20by%20the%20user%2C%20either%20directly%20or%20by%20supplying%20a%20pathname%20as%20an%20extra%20argument%20to%20the%20function.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EAnother%20example%20of%20functions%20that%20are%20used%20in%20indexes%20but%20depend%20on%20objects%20that%20might%20change%20is%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Ftextsearch.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Efull%20text%20search%3C%2FA%3E%20system.%20The%20dictionaries%2C%20stopwords%2C%20and%20stemmers%20can%20all%20change%20over%20time.%20In%20theory%2C%20some%20kind%20of%20system%20could%20be%20devised%20to%20cover%20those%20objects%20too.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1540197403%22%20id%3D%22toc-hId-1540197403%22%3ETL%3BDR%20for%20collation%20versioning%20in%20PostgreSQL%2014%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgreSQL%2014%20will%20warn%20you%20about%20collation%20changes%20that%20might%20otherwise%20cause%20subtle%2C%20silent%20corruption%20such%20as%20failure%20to%20look%20up%20keys%20or%20prevent%20duplicate%20keys.%20This%20has%20been%20a%20persistent%20source%20of%20user%20complaint%20over%20the%20years%3B%26nbsp%3B%3CSPAN%3Ethe%20new%20warnings%20in%20PostgreSQL%2014%20should%20help%20you%20avoid%20the%20risks%20by%20alerting%20you%20to%20changes%20in%20collation%20versions%2C%20either%20when%20upgrading%20machines%20or%20moving%20databases%20between%20machines%3C%2FSPAN%3E.%3CBR%20%2F%3E%3CBR%20%2F%3ECollations%20and%20dependencies%20are%20nerdy%20topics%20a%20bit%20like%20leap%20seconds%20and%20time%20zones%3A%20you%20know%2C%20the%20kinds%20of%20obscure%20things%20you%20don't%20normally%20have%20to%20worry%20about%20until%20something%20is%20broken.%20If%20you%20like%20gory%20details%2C%20the%20%3CA%20href%3D%22https%3A%2F%2Funicode.org%2Freports%2Ftr10%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUCA%3C%2FA%3E%20is%20both%20fascinating%20and%20terrifying%20(my%20favourite%20part%20is%20%22backward%20accent%20ordering%22%20which%20shows%20up%20in%20some%20French%20variants%20but%20not%20others).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22application-main%20%22%20data-commit-hovercards-enabled%3D%22%22%20data-discussion-hovercards-enabled%3D%22%22%20data-issue-and-pr-hovercards-enabled%3D%22%22%3E%0A%3CDIV%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22container-xl%20clearfix%20new-discussion-timeline%20px-3%20px-md-4%20px-lg-5%22%3E%0A%3CDIV%20class%3D%22repository-content%20%22%3E%0A%3CDIV%20class%3D%22Box%20mt-3%20position-relative%0A%20%20%20%20%20%20%22%3E%0A%3CDIV%20id%3D%22readme%22%20class%3D%22Box-body%20readme%20blob%20js-code-block-container%20p-5%20p-xl-6%20gist-border-0%22%3E%0A%3CARTICLE%20class%3D%22markdown-body%20entry-content%20container-lg%22%3E%0A%3CP%3EAll%20of%20these%20things%E2%80%94time%20zones%2C%20leap%20seconds%2C%20collations%E2%80%94are%20gnarly%20details%20of%20the%20real%20world%20around%20us.%20And%20they%20can%2C%20and%20do%2C%20change.%20Collations%20matter%20to%20PostgreSQL%20because%20people%20want%20to%20be%20able%20to%20use%20natural%20language%20strings%20in%20database%20indexes%20(and%20use%20these%20indexes%20to%20drive%20user-visible%20ordering.)%20Hence%2C%20PostgreSQL%20needs%20to%20be%20able%20to%20cope%20with%20the%20fact%20that%20the%20underlying%20collation%20rules%20change%20over%20time%E2%80%94and%20that%20the%20providers%20are%20external%20libraries%20we%20don't%20control.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20for%20reading.%20I%20hope%20you%20found%20this%20deep%20dive%20into%20the%20new%20collation%20version%20tracking%20feature%20in%20PostgreSQL%2014%20interesting!%3C%2FP%3E%0A%3C%2FARTICLE%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1978394%22%20slang%3D%22en-US%22%3E%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20'Segoe%20UI'%2C%20system-ui%2C%20'Apple%20Color%20Emoji'%2C%20'Segoe%20UI%20Emoji'%2C%20sans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant-ligatures%3A%20normal%3B%20font-variant-caps%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20start%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20white-space%3A%20normal%3B%20widows%3A%202%3B%20word-spacing%3A%200px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Postgres-collation-versioning-graphic-with-Danish-alphabet-tiles.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239912i76FA78B776531B65%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Postgres-collation-versioning-graphic-with-Danish-alphabet-tiles.jpg%22%20alt%3D%22Postgres-collation-versioning-graphic-with-Danish-alphabet-tiles.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20'Segoe%20UI'%2C%20system-ui%2C%20'Apple%20Color%20Emoji'%2C%20'Segoe%20UI%20Emoji'%2C%20sans-serif%3B%20font-size%3A%2014px%3B%20font-style%3A%20normal%3B%20font-variant-ligatures%3A%20normal%3B%20font-variant-caps%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20start%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20white-space%3A%20normal%3B%20widows%3A%202%3B%20word-spacing%3A%200px%3B%22%3ECollations%20are%20wonderful%2C%20but%20we%20often%20hear%20about%20them%20only%20when%20indexes%20break!%20PostgreSQL%2014%20makes%20collations%20better.%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1978394%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDatabases%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOpen%20Source%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPostgres%20indexes%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPostgreSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

As part of my work on the open source PostgreSQL team at Microsoft, I recently committed a new feature to track dependencies on collation versions, with help from co-author Julien Rouhaud and the many others who contributed ideas. It took a long time to build a consensus on how to tackle this thorny problem (work I began at EnterpriseDB and continued at Microsoft), and you can read about some of the details and considerations in the commit message below and the referenced discussion thread. Please note that some details may change by the time PostgreSQL 14 is released.

 

commit 257836a75585934cc05ed7a80bccf8190d41e056
Author: Thomas Munro <tmunro@postgresql.org>
Date:   Mon Nov 2 19:50:45 2020 +1300

    Track collation versions for indexes.
    
    Record the current version of dependent collations in pg_depend when
    creating or rebuilding an index.  When accessing the index later, warn
    that the index may be corrupted if the current version doesn't match.
    
    Thanks to Douglas Doole, Peter Eisentraut, Christoph Berg, Laurenz Albe,
    Michael Paquier, Robert Haas, Tom Lane and others for very helpful
    discussion.
    
    Author: Thomas Munro <thomas.munro@gmail.com>
    Author: Julien Rouhaud <rjuju123@gmail.com>
    Reviewed-by: Peter Eisentraut <peter.eisentraut@2ndquadrant.com> (earlier versions)
    Discussion: https://postgr.es/m/CAEepm%3D0uEQCpfq_%2BLYFBdArCe4Ot98t1aR4eYiYTe%3DyavQygiQ%40mail.gmail.com

 

I'm pretty happy with the result so far, but there is more to be done (see further down)! Now seems like a good time to walk you through the problem we needed to solve—that PostgreSQL indexes can get corrupted by changes in collations that occur naturally over time—and how the new feature makes things better in PostgreSQL 14. Plus, you'll get a bit of background on collations, too.

What is a collation?


The term collation derives from a form of the Latin verb conferre meaning to confer, bring together, or compare. Book binders use the term collation to mean putting pages into the correct order. Outside special technical fields, collation may be better known as an old fashioned and formal name for a cold meal (cf. colazione, Italian for breakfast). That meaning comes to us from the same ultimate source, having developed a different meaning in 4th century monasteries through association with brown bag lunches, but I digress...

Many software systems including POSIX and Windows have locales that encapsulate language and cultural conventions. A collation is the component of a locale that controls the way text is sorted.  Even though many languages share common writing systems, they don't necessarily agree on how to sort words. Even within the same language, there can be differences in how words should be sorted in different countries or in specialised contexts.

Similarly, in the context of SQL, a COLLATION is a database object that identifies a set of rules for sorting character strings.

For example, in a Danish dictionary, "å" comes after "z", and "aa" is considered to be an alternative spelling of "å". PostgreSQL models the different possible sorting rules with different collations, and we can see this difference on most systems like so:

 

postgres=# create table words (word text);
CREATE TABLE
postgres=# insert into words values ('Aarhus'), ('Banana'), ('Cat');
INSERT 0 3
postgres=# select * from words order by word collate "en_NZ";
  word  
--------
 Aarhus
 Banana
 Cat
(3 rows)

postgres=# select * from words order by word collate "da_DK";
  word  
--------
 Banana
 Cat
 Aarhus
(3 rows)

 

This might affect where a user expects to find a value in drop-down list or contents page.

Collations are part of the static type system of SQL. If you're working with text, you can't escape them! You can mostly ignore them and just let your whole system use the collation named "default", though. That's how a lot of you probably consume collations, especially if you only deal with one language. That means you get the default for the database you're connected to:

 

postgres=# \l postgres 
                              List of databases
   Name   | Owner  | Encoding |   Collate   |    Ctype    | Access privileges 
----------+--------+----------+-------------+-------------+-------------------
 postgres | tmunro | UTF8     | en_NZ.UTF-8 | en_NZ.UTF-8 | 
(1 row)

 

Often the default collation is set by initdb when the PostgreSQL cluster is first created. The collation and other locale components can be set using defaults from the operating system environment, and then inherited by databases created later. However, they can also be explicitly specified, and changed in later CREATE DATABASE commands with certain restrictions. You can also opt for one of the special collations "C", "POSIX", or "ucs_basic" which sort in binary or code point order rather than following cultural conventions, avoiding the problems described below—fair warning though, the results might surprise a few humans if they are reflected in user interfaces.

 

So what's the catch?

To quote from Unicode Technical Standard #10:

Over time, collation order will vary: there may be fixes needed as more information becomes available about languages; there may be new government or industry standards for the language that require changes; and finally, new characters added to the Unicode Standard will interleave with the previously-defined ones. This means that collations must be carefully versioned.


The most obvious problem this causes for databases like PostgreSQL is that B-tree indexes can become corrupted if they are ordered by a collation that changes. Typical symptoms reported in the wild include rows that cannot be seen by one query but can be seen by another query that is using a different plan. Or sometimes, a unique index fails to prevent a duplicate value from being added to a table.

To see why collation changes could impact whether PostgreSQL is able to look things up, let's use a phone book analogy. After all, a phone book is an ordered index with a lot of similarities to indexes in databases. So, imagine trying to look up your own name in an old-fashioned paper phone book—then imagine the phone book was created by someone who believes the alphabet goes in a slightly different order than you. There's a chance you'll conclude your name is not in the phone book, because your name is ordered in some unexpected place in the book, effectively hidden from you.

For a simple real-world example of a change in sort order on GNU/Linux that involves only ASCII characters, the sort order of the strings 'a-a' and 'a+a' flipped in glibc 2.28 (Debian 10, RHEL 8) compared to earlier releases.

 

Where does PostgreSQL's collation logic come from?

PostgreSQL relies on external libraries to order strings. There are two "providers":

 

  • libc, meaning the operating system locale facility (POSIX or Windows)
  • icu, meaning the ICU project, assuming PostgreSQL was built with ICU support

For now, only libc collations can be used as the database default, though ICU collations can be used explicitly, for example when defining the columns of a table. So which should you use?

 

  • One advantage of using libc collations is that the sorting behaviour matches other software on the same operating system.

  • Conversely, by using ICU it is possible to get software that is running on different operating systems to agree on sort order, if they're also using ICU.

  • Another reason to use ICU: some operating systems provide only very rudimentary collation logic in their libc, especially when using the UTF-8 encoding.

  • And a 3rd reason to use ICU is that ICU supports a rich system of end-user customisation (see some simple examples in the PostgreSQL manual).

 

While the ICU project's collation rules are based strictly on the Unicode Collation Algorithm and the ICU library version can be matched up with specific versions of Unicode and CLDR, things are a little murkier for libc, depending on your operating system. Many operating systems including FreeBSD, Windows 10, and GNU libc have migrated or are in the process of migrating from their own historical implementations of sorting rules to UCA-based logic and CLDR, so that eventually there will be fewer differences between systems, and between libc and ICU collations. Ironically it is this process that has caused many of the changes that we've seen in the past few years in libc collation definitions on popular operating systems. Even if all implementations eventually converged, CLDR itself is a moving target and it's possible that the versions aren't in sync.

The available set of collations on your system can be seen in the pg_collation catalog. You can add more with the CREATE COLLATION command, but you need to know how to form collcollate strings that are meaningful to your operating system or to ICU as appropriate. Note that collations often have names like "en_US", which requires double quotes when you reference it in a COLLATE expression due their case-sensitivity.

Where does collation version information come from?

 

The ICU library can report an opaque version for the collations it provides. In PostgreSQL releases 10 to 13, versions of ICU collations were already captured in the pg_collation catalog table when they were first created. Typically that's when the cluster is first set up. Then, when you use the collation (for example, by accessing an index that is ordered by it), a check is performed, and a warning is produced. This would alert you to rebuild all relevant indexes and then run ALTER COLLATION xxx REFRESH VERSION when done. This was a good start but it didn't help many people for several reasons:

 

  • it didn't work for libc collations (by far the more popular provider)
  • it was difficult to see how to extend this model to cover the "default" collation (by far the most popular way to use collations, even if users may be unaware they're doing it!)
  • it didn't know which indexes needed to be rebuilt after an upgrade, and relied on a human to figure that out

 

The first step to making things better and to tracking collation version dependencies was to figure out how to get version information from the operating system for libc collations. No relevant standard provides such an interface, but patches have now been committed to do that for GNU/Linux, FreeBSD 13+, and Windows using non-standard interfaces provided by those systems. Here are some example values, as returned by the pg_collation_actual_version() function in PostgreSQL 14-devel:

 

OS Provider Collation Version
Linux libc en_NZ 2.28
FreeBSD libc en_NZ 34.0
Windows libc en_NZ 1538.14,1538.14
(All) icu en-NZ-x-icu 153.88

 

Patches for other operating systems are welcome! These version strings are not interpreted in any way by PostgreSQL, except to check if they have changed. Note that on Linux we fall back to the glibc library version, for lack of anything better.

 

What do we do with these new collation versions (starting in PostgreSQL 14)?

 

PostgreSQL tracks the dependencies between database objects in the pg_depend catalog. This is used for various purposes, including stopping you from dropping things that other things reference, dropping dependent things automatically, and dumping objects in the correct order.

The new refobjversion column provides a place for us to record the collation version that was in effect at the time an index was created. For example:

 

postgres=# create table city (name text primary key);
CREATE TABLE

 

And now, after inserting some data, suppose we upgrade the operating system, or use a streaming replica server which has a newer operating system version:

 

postgres=# select * from city where name between 'Aarhus' and 'Antioch';
WARNING:  index "city_pkey" depends on collation "default" version "34.0", but the current version is "36.0"
DETAIL:  The index may be corrupted due to changes in sort order.
HINT:  REINDEX to avoid the risk of corruption.

 

This is just a warning, and will only be given once per index, per session. The primary way to stop the warning is with the REINDEX command, as mentioned in the HINT. It's also possible to tell the system that you have independently verified that the index has not been corrupted by the change in version with ALTER INDEX name ALTER COLLATION name REFRESH VERSION. One way to verify that is to use the amcheck module.

 

What else could we do with the new collation version infrastructure?

 

Currently, warnings advising you to rebuild indexes are raised, but you need to take action one by one to clear the warnings. It would be nice to have a new view and a new REINDEX variant to see and address all problems at once, before PostgreSQL 14 is released; work is underway to do that.

So far I talked about indexes, because PostgreSQL indexes are the most likely source of problems when ordering functions change. Equivalent problems exist in other places, for example:

 

  • range partitions might now classify a key as belonging to a different partition after a collation change

  • check constraints that perform string comparisons might previously have passed, but now fail


While proof-of-concept patches have been drafted to provide similar warnings for those cases, it's a little less clear what the user should do about these warnings, so these proof-of-concept ideas remain under discussion.

 

Mutating IMMUTABLE functions

 

PostgreSQL requires every function or operator involved in the definition of an index to be marked IMMUTABLE. The case of mutating collations can be thought of as a violation of that requirement, because the string comparison function (for example, bttextcmp) fails to guarantee that it'll return the same value given the same inputs for the rest of eternity. The same applies to user-defined functions that are declared IMMUTABLE, but don't meet that requirement due to, say, a dependency on the time, a GUC setting, or any other information that is not derivable only from the inputs.

There is a class of functions that change infrequently and at known times, however.  Perhaps it would be possible to create a system of declarative versions for those, using the new refobjversion system: the index would depend on a named version of the function.

 

Other external things that could affect indexes

 

Consider the example of the unaccent function. A version for the unaccent() function itself would be a little too simplistic, because there is a second object besides the function that affects its results: the rules file. The rules file can be modified or replaced by the user, either directly or by supplying a pathname as an extra argument to the function.

Another example of functions that are used in indexes but depend on objects that might change is the full text search system. The dictionaries, stopwords, and stemmers can all change over time. In theory, some kind of system could be devised to cover those objects too.

 

TL;DR for collation versioning in PostgreSQL 14

 

PostgreSQL 14 will warn you about collation changes that might otherwise cause subtle, silent corruption such as failure to look up keys or prevent duplicate keys. This has been a persistent source of user complaint over the years; the new warnings in PostgreSQL 14 should help you avoid the risks by alerting you to changes in collation versions, either when upgrading machines or moving databases between machines.

Collations and dependencies are nerdy topics a bit like leap seconds and time zones: you know, the kinds of obscure things you don't normally have to worry about until something is broken. If you like gory details, the UCA is both fascinating and terrifying (my favourite part is "backward accent ordering" which shows up in some French variants but not others).

 

All of these things—time zones, leap seconds, collations—are gnarly details of the real world around us. And they can, and do, change. Collations matter to PostgreSQL because people want to be able to use natural language strings in database indexes (and use these indexes to drive user-visible ordering.) Hence, PostgreSQL needs to be able to cope with the fact that the underlying collation rules change over time—and that the providers are external libraries we don't control.

 

Thanks for reading. I hope you found this deep dive into the new collation version tracking feature in PostgreSQL 14 interesting!

1 Comment
Visitor

It's good to see work done on the collation issue. Encountering this on a production system was not the happiest experience, especially when corrupted indexes interfered with fixing the duplicate problem. Fortunately I knew how to disable their use; couldn't just reindex, because by then tables had ended up with uniqueness or foreign key constraint violations. Once string comparisons flipped, all bets were off!