Chapter 11. PL/pgSQL

Table of Contents
Introduction to PL/pgSQL
Adding PL/pgSQL to your Database
Language Structure
Using Variables
Controlling Program Flow
PL/pgSQL and Triggers

Introduction to PL/pgSQL

PL/pgSQL is a loadable procedural language similar to the Oracle procedural language, PL/SQL. A procedural language is a programming language used to specify a a sequence of steps that are followed to produce an intended result.

You can use PL/pgSQL to group sequences of SQL and programmatic statements together within a database server, reducing network and communications overhead incurred by client applications having to constantly request data from the database and perform logic operations upon that data from a remote location.

You have access to all PostgreSQL data types, operators, and functions within PL/pgSQL code. The "SQL" in PL/pgSQL is indicative of the fact that are allowed to directly use the SQL language from within PL/pgSQL code. The use of SQL within PL/pgSQL code can increase the power, flexibility, and performance of your programs. If multiple statements are sent from a PL/pgSQL code block, the statements are processed at one time, instead of the normal behavior of processing a single statement at a time.

Another important aspect of using PL/pgSQL is its portability; its functions are compatible with all platforms that can operate the PostgreSQL database system.