Postgresql 中文操作指南

38.3. User-Defined Functions #

PostgreSQL 提供四种函数:

PostgreSQL provides four kinds of functions:

每种函数都可以将基类型、复合类型或它们的组合作为参数(参数)获取。此外,每种函数都可以返回基类型或复合类型。还可以定义函数来返回基值或复合值集。

Every kind of function can take base types, composite types, or combinations of these as arguments (parameters). In addition, every kind of function can return a base type or a composite type. Functions can also be defined to return sets of base or composite values.

许多种函数都可以获取或返回某些伪类型(例如多态类型),但可用功能各不相同。查阅每种函数的说明以了解更多详情。

Many kinds of functions can take or return certain pseudo-types (such as polymorphic types), but the available facilities vary. Consult the description of each kind of function for more details.

最容易定义 SQL 函数,因此我们将从讨论它们开始。针对 SQL 函数提出的大多数概念都可应用到其他类型的函数。

It’s easiest to define SQL functions, so we’ll start by discussing those. Most of the concepts presented for SQL functions will carry over to the other types of functions.

在本章中,通过查看 CREATE FUNCTION 命令的参考页面来帮助理解示例。本章中的某些示例可以在 PostgreSQL 源发行版中的 src/tutorial 目录下的 funcs.sqlfuncs.c 中找到。

Throughout this chapter, it can be useful to look at the reference page of the CREATE FUNCTION command to understand the examples better. Some examples from this chapter can be found in funcs.sql and funcs.c in the src/tutorial directory in the PostgreSQL source distribution.