51.12. pg_collation
В каталоге pg_collation
описываются доступные правила сортировки, которые по сути представляют собой сопоставления идентификаторов SQL с категориями локалей операционной системы. За дополнительными сведениями обратитесь к Разделу 23.2.
Таблица 51.12. Столбцы pg_collation
Name | Тип | Ссылки | Описание |
---|---|---|---|
oid | oid | Идентификатор строки | |
collname | name | Имя правила сортировки (уникальное для пространства имён и кодировки) | |
collnamespace | oid |
| OID пространства имён, содержащего это правило сортировки |
collowner | oid |
| Владелец правила сортировки |
collprovider | char | Провайдер правила сортировки: d = установленный в базе по умолчанию, c = libc, i = icu | |
collisdeterministic | bool | Является ли правило сортировки детерминированным? | |
collencoding | int4 | Кодировка, для которой применимо это правило, или -1, если оно работает с любой кодировкой | |
collcollate | name | LC_COLLATE для данного объекта | |
collctype | name | LC_CTYPE для данного объекта | |
collversion | text | Определяемая провайдером версия правила сортировки. Она записывается при создании правила сортировки и проверяется при использовании для обнаружения изменений в его определении, чреватых повреждением данных. |
Заметьте, что уникальный ключ в этом каталоге определён как (collname
, collencoding
, collnamespace
), а не просто как (collname
, collnamespace
). Вообще PostgreSQL игнорирует все правила сортировки, для которых collencoding
не равняется кодировке текущей базы данных или -1, а создание новых записей с тем же именем, которое уже имеет запись с collencoding
= -1, запрещено. Таким образом, достаточно использовать полное имя SQL (схема
.имя
) для указания правила сортировки, несмотря на то, что оно может быть неуникальным согласно определению каталога. Такая организация каталога объясняется тем, что программа initdb наполняет его в момент инициализации кластера записями для всех локалей, обнаруженных в системе, так что она должна иметь возможность сохранить записи для всех кодировок, которые могут вообще когда-либо применяться в кластере.
В базе данных template0
может быть полезно создать правила сортировки, кодировки которых не соответствуют кодировке этой базы, но которые могут оказаться у баз данных, скопированных впоследствии из template0
. В настоящее время это придётся проделать вручную.
10.1. Overview #
SQL is a strongly typed language. That is, every data item has an associated data type which determines its behavior and allowed usage. Postgres Pro has an extensible type system that is more general and flexible than other SQL implementations. Hence, most type conversion behavior in Postgres Pro is governed by general rules rather than by ad hoc heuristics. This allows the use of mixed-type expressions even with user-defined types.
The Postgres Pro scanner/parser divides lexical elements into five fundamental categories: integers, non-integer numbers, strings, identifiers, and key words. Constants of most non-numeric types are first classified as strings. The SQL language definition allows specifying type names with strings, and this mechanism can be used in Postgres Pro to start the parser down the correct path. For example, the query:
SELECT text 'Origin' AS "label", point '(0,0)' AS "value"; label | value --------+------- Origin | (0,0) (1 row)
has two literal constants, of type text
and point
. If a type is not specified for a string literal, then the placeholder type unknown
is assigned initially, to be resolved in later stages as described below.
There are four fundamental SQL constructs requiring distinct type conversion rules in the Postgres Pro parser:
- Function calls
Much of the Postgres Pro type system is built around a rich set of functions. Functions can have one or more arguments. Since Postgres Pro permits function overloading, the function name alone does not uniquely identify the function to be called; the parser must select the right function based on the data types of the supplied arguments.
- Operators
Postgres Pro allows expressions with prefix (one-argument) operators, as well as infix (two-argument) operators. Like functions, operators can be overloaded, so the same problem of selecting the right operator exists.
- Value Storage
SQL
INSERT
andUPDATE
statements place the results of expressions into a table. The expressions in the statement must be matched up with, and perhaps converted to, the types of the target columns.-
UNION
,CASE
, and related constructs Since all query results from a unionized
SELECT
statement must appear in a single set of columns, the types of the results of eachSELECT
clause must be matched up and converted to a uniform set. Similarly, the result expressions of aCASE
construct must be converted to a common type so that theCASE
expression as a whole has a known output type. Some other constructs, such asARRAY[]
and theGREATEST
andLEAST
functions, likewise require determination of a common type for several subexpressions.
The system catalogs store information about which conversions, or casts, exist between which data types, and how to perform those conversions. Additional casts can be added by the user with the CREATE CAST command. (This is usually done in conjunction with defining new data types. The set of casts between built-in types has been carefully crafted and is best not altered.)
An additional heuristic provided by the parser allows improved determination of the proper casting behavior among groups of types that have implicit casts. Data types are divided into several basic type categories, including boolean
, numeric
, string
, bitstring
, datetime
, timespan
, geometric
, network
, and user-defined. (For a list see Table 51.67; but note it is also possible to create custom type categories.) Within each category there can be one or more preferred types, which are preferred when there is a choice of possible types. With careful selection of preferred types and available implicit casts, it is possible to ensure that ambiguous expressions (those with multiple candidate parsing solutions) can be resolved in a useful way.
All type conversion rules are designed with several principles in mind:
Implicit conversions should never have surprising or unpredictable outcomes.
There should be no extra overhead in the parser or executor if a query does not need implicit type conversion. That is, if a query is well-formed and the types already match, then the query should execute without spending extra time in the parser and without introducing unnecessary implicit conversion calls in the query.
Additionally, if a query usually requires an implicit conversion for a function, and if then the user defines a new function with the correct argument types, the parser should use this new function and no longer do implicit conversion to use the old function.