9.1. Writing SQL Functions

Writing functions with the help of SQL is just as easy as writing SQL statements. The idea behind SQL functions is to have an easy way to add new features to a database. In addition, it is not necessary to use embedded languages such as PL/pgSQL or PL/Perl for implementing minor extensions. This is an important point because it increases the portability and flexibility of your database significantly.

9.1.1. An Example

Let's start by writing a simple function based on pure SQL code. For adding new functions to the database, you must use CREATE FUNCTION. The syntax of CREATE FUNCTION can easily be retrieved from the database by using the \h command:

phpbook=# \h CREATE FUNCTION Command: CREATE FUNCTION Description: define ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.