Postgresql 中文操作指南
8.21. Pseudo-Types #
PostgreSQL 类型系统包含许多专用条目,这些条目被统称为 pseudo-types。不能将伪类型用作列数据类型,但它可用于声明函数的参数或结果类型。每个可用的伪类型都可用于函数的行为不对应于简单地取用或返回特定 SQL 数据类型的值的情况。 Table 8.27 列出了现有的伪类型。
The PostgreSQL type system contains a number of special-purpose entries that are collectively called pseudo-types. A pseudo-type cannot be used as a column data type, but it can be used to declare a function’s argument or result type. Each of the available pseudo-types is useful in situations where a function’s behavior does not correspond to simply taking or returning a value of a specific SQL data type. Table 8.27 lists the existing pseudo-types.
Table 8.27. Pseudo-Types
Name |
Description |
any |
Indicates that a function accepts any input data type. |
anyelement |
Indicates that a function accepts any data type (see Section 38.2.5). |
anyarray |
Indicates that a function accepts any array data type (see Section 38.2.5). |
anynonarray |
Indicates that a function accepts any non-array data type (see Section 38.2.5). |
anyenum |
Indicates that a function accepts any enum data type (see Section 38.2.5 and Section 8.7). |
anyrange |
Indicates that a function accepts any range data type (see Section 38.2.5 and Section 8.17). |
anymultirange |
Indicates that a function accepts any multirange data type (see Section 38.2.5 and Section 8.17). |
anycompatible |
Indicates that a function accepts any data type, with automatic promotion of multiple arguments to a common data type (see Section 38.2.5). |
anycompatiblearray |
Indicates that a function accepts any array data type, with automatic promotion of multiple arguments to a common data type (see Section 38.2.5). |
anycompatiblenonarray |
Indicates that a function accepts any non-array data type, with automatic promotion of multiple arguments to a common data type (see Section 38.2.5). |
anycompatiblerange |
Indicates that a function accepts any range data type, with automatic promotion of multiple arguments to a common data type (see Section 38.2.5 and Section 8.17). |
anycompatiblemultirange |
Indicates that a function accepts any multirange data type, with automatic promotion of multiple arguments to a common data type (see Section 38.2.5 and Section 8.17). |
cstring |
Indicates that a function accepts or returns a null-terminated C string. |
internal |
Indicates that a function accepts or returns a server-internal data type. |
language_handler |
A procedural language call handler is declared to return language_handler. |
fdw_handler |
A foreign-data wrapper handler is declared to return fdw_handler. |
table_am_handler |
A table access method handler is declared to return table_am_handler. |
index_am_handler |
An index access method handler is declared to return index_am_handler. |
tsm_handler |
A tablesample method handler is declared to return tsm_handler. |
record |
Identifies a function taking or returning an unspecified row type. |
trigger |
A trigger function is declared to return trigger. |
event_trigger |
An event trigger function is declared to return event_trigger. |
pg_ddl_command |
Identifies a representation of DDL commands that is available to event triggers. |
void |
Indicates that a function returns no value. |
unknown |
Identifies a not-yet-resolved type, e.g., of an undecorated string literal. |
以 C 语言编码的函数(无论内置还是动态加载)都可以被声明为接受或返回这些伪类型。由函数作者确保当伪类型用作参数类型时,函数的行为是安全的。
Functions coded in C (whether built-in or dynamically loaded) can be declared to accept or return any of these pseudo-types. It is up to the function author to ensure that the function will behave safely when a pseudo-type is used as an argument type.
以过程语言编码的函数只能在其实现语言允许的情况下使用伪类型。目前,大多数过程语言禁止使用伪类型作为参数类型,并且只允许 void 和 record 作为结果类型(当该函数用作触发器或事件触发器时,还有 trigger 或 event_trigger)。还有一些支持使用多态伪类型的多态函数,如上所示,并在 Section 38.2.5 中进行了详细讨论。
Functions coded in procedural languages can use pseudo-types only as allowed by their implementation languages. At present most procedural languages forbid use of a pseudo-type as an argument type, and allow only void and record as a result type (plus trigger or event_trigger when the function is used as a trigger or event trigger). Some also support polymorphic functions using the polymorphic pseudo-types, which are shown above and discussed in detail in Section 38.2.5.
internal 伪类型用于声明仅供数据库系统内部调用的函数,而不是在 SQL 查询中直接调用。如果一个函数有至少一个 internal 类型的参数,那么无法从 SQL 中调用它。为保留此限制的类型安全,请务必遵循此编码规则:不要创建任何声明为返回 internal 的函数,除非它至少有一个 internal 参数。
The internal pseudo-type is used to declare functions that are meant only to be called internally by the database system, and not by direct invocation in an SQL query. If a function has at least one internal-type argument then it cannot be called from SQL. To preserve the type safety of this restriction it is important to follow this coding rule: do not create any function that is declared to return internal unless it has at least one internal argument.