1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-26 18:17:33 +03:00
postgres/doc/src/sgml/ref/create_collation.sgml
Peter Eisentraut 5e1963fb76 Collations with nondeterministic comparison
This adds a flag "deterministic" to collations.  If that is false,
such a collation disables various optimizations that assume that
strings are equal only if they are byte-wise equal.  That then allows
use cases such as case-insensitive or accent-insensitive comparisons
or handling of strings with different Unicode normal forms.

This functionality is only supported with the ICU provider.  At least
glibc doesn't appear to have any locales that work in a
nondeterministic way, so it's not worth supporting this for the libc
provider.

The term "deterministic comparison" in this context is from Unicode
Technical Standard #10
(https://unicode.org/reports/tr10/#Deterministic_Comparison).

This patch makes changes in three areas:

- CREATE COLLATION DDL changes and system catalog changes to support
  this new flag.

- Many executor nodes and auxiliary code are extended to track
  collations.  Previously, this code would just throw away collation
  information, because the eventually-called user-defined functions
  didn't use it since they only cared about equality, which didn't
  need collation information.

- String data type functions that do equality comparisons and hashing
  are changed to take the (non-)deterministic flag into account.  For
  comparison, this just means skipping various shortcuts and tie
  breakers that use byte-wise comparison.  For hashing, we first need
  to convert the input string to a canonical "sort key" using the ICU
  analogue of strxfrm().

Reviewed-by: Daniel Verite <daniel@manitou-mail.org>
Reviewed-by: Peter Geoghegan <pg@bowt.ie>
Discussion: https://www.postgresql.org/message-id/flat/1ccc668f-4cbc-0bef-af67-450b47cdfee7@2ndquadrant.com
2019-03-22 12:12:43 +01:00

261 lines
7.7 KiB
Plaintext

<!-- doc/src/sgml/ref/create_collation.sgml -->
<refentry id="sql-createcollation">
<indexterm zone="sql-createcollation">
<primary>CREATE COLLATION</primary>
</indexterm>
<refmeta>
<refentrytitle>CREATE COLLATION</refentrytitle>
<manvolnum>7</manvolnum>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
<refnamediv>
<refname>CREATE COLLATION</refname>
<refpurpose>define a new collation</refpurpose>
</refnamediv>
<refsynopsisdiv>
<synopsis>
CREATE COLLATION [ IF NOT EXISTS ] <replaceable>name</replaceable> (
[ LOCALE = <replaceable>locale</replaceable>, ]
[ LC_COLLATE = <replaceable>lc_collate</replaceable>, ]
[ LC_CTYPE = <replaceable>lc_ctype</replaceable>, ]
[ PROVIDER = <replaceable>provider</replaceable>, ]
[ DETERMINISTIC = <replaceable>boolean</replaceable>, ]
[ VERSION = <replaceable>version</replaceable> ]
)
CREATE COLLATION [ IF NOT EXISTS ] <replaceable>name</replaceable> FROM <replaceable>existing_collation</replaceable>
</synopsis>
</refsynopsisdiv>
<refsect1 id="sql-createcollation-description">
<title>Description</title>
<para>
<command>CREATE COLLATION</command> defines a new collation using
the specified operating system locale settings,
or by copying an existing collation.
</para>
<para>
To be able to create a collation, you must
have <literal>CREATE</literal> privilege on the destination schema.
</para>
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><literal>IF NOT EXISTS</literal></term>
<listitem>
<para>
Do not throw an error if a collation with the same name already exists.
A notice is issued in this case. Note that there is no guarantee that
the existing collation is anything like the one that would have been created.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>name</replaceable></term>
<listitem>
<para>
The name of the collation. The collation name can be
schema-qualified. If it is not, the collation is defined in the
current schema. The collation name must be unique within that
schema. (The system catalogs can contain collations with the
same name for other encodings, but these are ignored if the
database encoding does not match.)
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>locale</replaceable></term>
<listitem>
<para>
This is a shortcut for setting <symbol>LC_COLLATE</symbol>
and <symbol>LC_CTYPE</symbol> at once. If you specify this,
you cannot specify either of those parameters.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>lc_collate</replaceable></term>
<listitem>
<para>
Use the specified operating system locale for
the <symbol>LC_COLLATE</symbol> locale category.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>lc_ctype</replaceable></term>
<listitem>
<para>
Use the specified operating system locale for
the <symbol>LC_CTYPE</symbol> locale category.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>provider</replaceable></term>
<listitem>
<para>
Specifies the provider to use for locale services associated with this
collation. Possible values
are: <literal>icu</literal>,<indexterm><primary>ICU</primary></indexterm>
<literal>libc</literal>.
<literal>libc</literal> is the default.
The available choices depend on the operating system and build options.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>DETERMINISTIC</literal></term>
<listitem>
<para>
Specifies whether the collation should use deterministic comparisons.
The default is true. A deterministic comparison considers strings that
are not byte-wise equal to be unequal even if they are considered
logically equal by the comparison. PostgreSQL breaks ties using a
byte-wise comparison. Comparison that is not deterministic can make the
collation be, say, case- or accent-insensitive. For that, you need to
choose an appropriate <literal>LC_COLLATE</literal> setting
<emphasis>and</emphasis> set the collation to not deterministic here.
</para>
<para>
Nondeterministic collations are only supported with the ICU provider.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>version</replaceable></term>
<listitem>
<para>
Specifies the version string to store with the collation. Normally,
this should be omitted, which will cause the version to be computed
from the actual version of the collation as provided by the operating
system. This option is intended to be used
by <command>pg_upgrade</command> for copying the version from an
existing installation.
</para>
<para>
See also <xref linkend="sql-altercollation"/> for how to handle
collation version mismatches.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>existing_collation</replaceable></term>
<listitem>
<para>
The name of an existing collation to copy. The new collation
will have the same properties as the existing one, but it
will be an independent object.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 id="sql-createcollation-notes">
<title>Notes</title>
<para>
<command>CREATE COLLATION</command> takes a <literal>SHARE ROW
EXCLUSIVE</literal> lock, which is self-conflicting, on the
<structname>pg_collation</structname> system catalog, so only one
<command>CREATE COLLATION</command> command can run at a time.
</para>
<para>
Use <command>DROP COLLATION</command> to remove user-defined collations.
</para>
<para>
See <xref linkend="collation-create"/> for more information on how to create collations.
</para>
<para>
When using the <literal>libc</literal> collation provider, the locale must
be applicable to the current database encoding.
See <xref linkend="sql-createdatabase"/> for the precise rules.
</para>
</refsect1>
<refsect1 id="sql-createcollation-examples">
<title>Examples</title>
<para>
To create a collation from the operating system locale
<literal>fr_FR.utf8</literal>
(assuming the current database encoding is <literal>UTF8</literal>):
<programlisting>
CREATE COLLATION french (locale = 'fr_FR.utf8');
</programlisting>
</para>
<para>
To create a collation using the ICU provider using German phone book sort order:
<programlisting>
CREATE COLLATION german_phonebook (provider = icu, locale = 'de-u-co-phonebk');
</programlisting>
</para>
<para>
To create a collation from an existing collation:
<programlisting>
CREATE COLLATION german FROM "de_DE";
</programlisting>
This can be convenient to be able to use operating-system-independent
collation names in applications.
</para>
</refsect1>
<refsect1 id="sql-createcollation-compat">
<title>Compatibility</title>
<para>
There is a <command>CREATE COLLATION</command> statement in the SQL
standard, but it is limited to copying an existing collation. The
syntax to create a new collation is
a <productname>PostgreSQL</productname> extension.
</para>
</refsect1>
<refsect1 id="sql-createcollation-seealso">
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-altercollation"/></member>
<member><xref linkend="sql-dropcollation"/></member>
</simplelist>
</refsect1>
</refentry>