9.2. PL/pgSQL

In this section you will learn to implement simple functions based on PL/pgSQL. PL/pgSQL is an embedded language provided by PostgreSQL for writing extensions to PostgreSQL. Before you can start working with PL/pgSQL and PostgreSQL, you must add the language to the database explicitly by using a command-line tool:

bash-2.04$ createlang plpgsql phpbook
					

If no error occurred, the database called phpbook now supports PL/pgSQL.

9.2.1. The Structure of PL/pgSQL Functions

Usually a PL/pgSQL function consists of three components as shown in the next listing:

[<<label>>]
[DECLARE
    declarations]
BEGIN
    statements
END;

First, a label is defined and after that a section for declaring variables can be added. Finally, the block containing the ...

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.