Chapter 12. Extending SQL: Functions

Table of Contents
12.1. Introduction
12.2. Query Language (SQL) Functions
12.2.1. Examples
12.2.2. SQL Functions on Base Types
12.2.3. SQL Functions on Composite Types
12.2.4. SQL Functions Returning Sets
12.3. Procedural Language Functions
12.4. Internal Functions
12.5. C Language Functions
12.5.1. Dynamic Loading
12.5.2. Base Types in C-Language Functions
12.5.3. Version-0 Calling Conventions for C-Language Functions
12.5.4. Version-1 Calling Conventions for C-Language Functions
12.5.5. Composite Types in C-Language Functions
12.5.6. Writing Code
12.5.7. Compiling and Linking Dynamically-Loaded Functions
12.6. Function Overloading
12.7. Procedural Language Handlers

12.1. Introduction

As it turns out, part of defining a new type is the definition of functions that describe its behavior. Consequently, while it is possible to define a new function without defining a new type, the reverse is not true. We therefore describe how to add new functions to PostgreSQL before describing how to add new types.

PostgreSQL provides four kinds of functions:

Every kind of function can take a base type, a composite type, or some combination as arguments (parameters). In addition, every kind of function can return a base type or a composite type. It's easiest to define SQL functions, so we'll start with those. Examples in this section can also be found in funcs.sql and funcs.c in the tutorial directory.

Throughout this chapter, it can be useful to look at the reference page of the CREATE FUNCTION command to understand the examples better.