Add standard collation UNICODE
authorPeter Eisentraut
Fri, 10 Mar 2023 12:35:00 +0000 (13:35 +0100)
committerPeter Eisentraut
Fri, 10 Mar 2023 12:35:43 +0000 (13:35 +0100)
This adds a new predefined collation named UNICODE, which sorts by the
default Unicode collation algorithm specifications, per SQL standard.

This only works if ICU support is built.

Reviewed-by: Jeff Davis
Discussion: https://www.postgresql.org/message-id/flat/1293e382-2093-a2bf-a397-c04e8f83d3c2@enterprisedb.com

doc/src/sgml/charset.sgml
src/bin/initdb/initdb.c
src/include/catalog/catversion.h
src/test/regress/expected/collate.icu.utf8.out
src/test/regress/sql/collate.icu.utf8.sql

index 3032392b801be11ec36b199692fa354d99b5af7e..12fabb73720946e4e70722066c43f0d4ca8f360d 100644 (file)
@@ -659,9 +659,34 @@ SELECT * FROM test1 ORDER BY a || b COLLATE "fr_FR";
    
 
    
-    Additionally, the SQL standard collation name ucs_basic
-    is available for encoding UTF8.  It is equivalent
-    to C and sorts by Unicode code point.
+    Additionally, two SQL standard collation names are available:
+
+    
+     
+      unicode
+      
+       
+        This collation sorts using the Unicode Collation Algorithm with the
+        Default Unicode Collation Element Table.  It is available in all
+        encodings.  ICU support is required to use this collation.  (This
+        collation has the same behavior as the ICU root locale; see 
+        linkend="collation-managing-predefined-icu-und-x-icu"/>.)
+       
+      
+     
+
+     
+      ucs_basic
+      
+       
+        This collation sorts by Unicode code point.  It is only available for
+        encoding UTF8.  (This collation has the same
+        behavior as the libc locale specification C in
+        UTF8 encoding.)
+       
+      
+     
+    
    
   
 
index bf88cd243968e9016d45ca26b98964b0b44301ba..497f2205f0c93e8dfa321779e8b2485412ec74bc 100644 (file)
@@ -1493,10 +1493,14 @@ static void
 setup_collation(FILE *cmdfd)
 {
    /*
-    * Add an SQL-standard name.  We don't want to pin this, so it doesn't go
-    * in pg_collation.h.  But add it before reading system collations, so
-    * that it wins if libc defines a locale named ucs_basic.
+    * Add SQL-standard names.  We don't want to pin these, so they don't go
+    * in pg_collation.dat.  But add them before reading system collations, so
+    * that they win if libc defines a locale with the same name.
     */
+   PG_CMD_PRINTF("INSERT INTO pg_collation (oid, collname, collnamespace, collowner, collprovider, collisdeterministic, collencoding, colliculocale)"
+                 "VALUES (pg_nextoid('pg_catalog.pg_collation', 'oid', 'pg_catalog.pg_collation_oid_index'), 'unicode', 'pg_catalog'::regnamespace, %u, '%c', true, -1, 'und');\n\n",
+                 BOOTSTRAP_SUPERUSERID, COLLPROVIDER_ICU);
+
    PG_CMD_PRINTF("INSERT INTO pg_collation (oid, collname, collnamespace, collowner, collprovider, collisdeterministic, collencoding, collcollate, collctype)"
                  "VALUES (pg_nextoid('pg_catalog.pg_collation', 'oid', 'pg_catalog.pg_collation_oid_index'), 'ucs_basic', 'pg_catalog'::regnamespace, %u, '%c', true, %d, 'C', 'C');\n\n",
                  BOOTSTRAP_SUPERUSERID, COLLPROVIDER_LIBC, PG_UTF8);
index 24d866badf0cfbe7d0daf23c315b530d65fff2b8..5d1b957ed44bd340f7f7366060ff363f05ed9e44 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                         yyyymmddN */
-#define CATALOG_VERSION_NO 202303081
+#define CATALOG_VERSION_NO 202303101
 
 #endif
index f53857b46dddde925517d1251810955d178700f7..04c3aa14d210c56fcc40ae2f95f7f3a8e34ebbe0 100644 (file)
@@ -1151,6 +1151,15 @@ SELECT * FROM collate_test2 ORDER BY b COLLATE UCS_BASIC;
  2 | äbc
 (4 rows)
 
+SELECT * FROM collate_test2 ORDER BY b COLLATE UNICODE;
+ a |  b  
+---+-----
+ 1 | abc
+ 4 | ABC
+ 2 | äbc
+ 3 | bbc
+(4 rows)
+
 -- test ICU collation customization
 -- test the attributes handled by icu_set_collation_attributes()
 CREATE COLLATION testcoll_ignore_accents (provider = icu, locale = '@colStrength=primary;colCaseLevel=yes');
index abf17a0767aa686e97d8c1a1fb19e7eec36b30f6..4786d65a08dc2769f2cd229dce3c77d6ed59e5ae 100644 (file)
@@ -447,6 +447,7 @@ drop type textrange_en_us;
 -- standard collations
 
 SELECT * FROM collate_test2 ORDER BY b COLLATE UCS_BASIC;
+SELECT * FROM collate_test2 ORDER BY b COLLATE UNICODE;
 
 
 -- test ICU collation customization