Postgresql 中文操作指南

CREATE COLLATION

CREATE COLLATION — 定义新的校对规则

CREATE COLLATION — define a new collation

Synopsis

CREATE COLLATION [ IF NOT EXISTS ] name (
    [ LOCALE = locale, ]
    [ LC_COLLATE = lc_collate, ]
    [ LC_CTYPE = lc_ctype, ]
    [ PROVIDER = provider, ]
    [ DETERMINISTIC = boolean, ]
    [ RULES = rules, ]
    [ VERSION = version ]
)
CREATE COLLATION [ IF NOT EXISTS ] name FROM existing_collation

Description

CREATE COLLATION 使用指定的 OS 区域设置或通过复制已存在的校对规则定义一个新的校对规则。

CREATE COLLATION defines a new collation using the specified operating system locale settings, or by copying an existing collation.

要创建校对规则,您必须对目标架构具有 CREATE 权限。

To be able to create a collation, you must have CREATE privilege on the destination schema.

Parameters

  • IF NOT EXISTS

    • 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.

  • name

    • 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.)

  • locale

    • The locale name for this collation. See Section 24.2.2.3.1 and Section 24.2.2.3.2 for details.

    • If provider is libc, this is a shortcut for setting LC_COLLATE and LC_CTYPE at once. If you specify locale, you cannot specify either of those parameters.

  • lc_collate

    • If provider is libc, use the specified operating system locale for the LC_COLLATE locale category.

  • lc_ctype

    • If provider is libc, use the specified operating system locale for the LC_CTYPE locale category.

  • provider

    • Specifies the provider to use for locale services associated with this collation. Possible values are icu (if the server was built with ICU support) or libc. libc is the default. See Section 24.1.4 for details.

  • DETERMINISTIC

    • 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 LOCALE setting and set the collation to not deterministic here.

    • Nondeterministic collations are only supported with the ICU provider.

  • rules

    • Specifies additional collation rules to customize the behavior of the collation. This is supported for ICU only. See Section 24.2.3.4 for details.

  • version

    • 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 pg_upgrade for copying the version from an existing installation.

    • See also ALTER COLLATION for how to handle collation version mismatches.

  • existing_collation

    • 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.

Notes

CREATE COLLATIONpg_collation 系统目录中取得 SHARE ROW EXCLUSIVE 锁,该锁会发生自冲突,因此一次只能运行一个 CREATE COLLATION 命令。

CREATE COLLATION takes a SHARE ROW EXCLUSIVE lock, which is self-conflicting, on the pg_collation system catalog, so only one CREATE COLLATION command can run at a time.

使用 DROP COLLATION 移除用户自定义排序。

Use DROP COLLATION to remove user-defined collations.

请参阅 Section 24.2.2.3 ,了解有关如何创建排序的更多信息。

See Section 24.2.2.3 for more information on how to create collations.

在使用 libc 排序提供程序时,该语言环境必须适用于当前数据库编码。请参阅 CREATE DATABASE 以了解精确的规则。

When using the libc collation provider, the locale must be applicable to the current database encoding. See CREATE DATABASE for the precise rules.

Examples

要通过操作系统语言环境 fr_FR.utf8 创建排序 (假设当前数据库编码为 UTF8 ):

To create a collation from the operating system locale fr_FR.utf8 (assuming the current database encoding is UTF8):

CREATE COLLATION french (locale = 'fr_FR.utf8');

要使用 ICU 提供程序创建排序,使用德语电话簿排序顺序:

To create a collation using the ICU provider using German phone book sort order:

CREATE COLLATION german_phonebook (provider = icu, locale = 'de-u-co-phonebk');

要使用 ICU 提供程序创建排序,基于根 ICU 语言环境,使用自定义规则:

To create a collation using the ICU provider, based on the root ICU locale, with custom rules:

CREATE COLLATION custom (provider = icu, locale = 'und', rules = '&V << w <<< W');

请参阅 Section 24.2.3.4 ,了解有关语法规则的更多详细信息和示例。

See Section 24.2.3.4 for further details and examples on the rules syntax.

要从现有排序创建排序:

To create a collation from an existing collation:

CREATE COLLATION german FROM "de_DE";

这可方便地在应用程序中使用与操作系统无关的排序名称。

This can be convenient to be able to use operating-system-independent collation names in applications.

Compatibility

SQL 标准中有 CREATE COLLATION 语句,但它仅限于复制现有排序。创建新排序的语法是 PostgreSQL 扩展。

There is a CREATE COLLATION statement in the SQL standard, but it is limited to copying an existing collation. The syntax to create a new collation is a PostgreSQL extension.