PL/pgSQL is a loadable procedural language for the Postgres database system.
This package was originally written by Jan Wieck.
The design goals of PL/pgSQL were to create a loadable procedural language that
can be used to create functions and trigger procedures,
adds control structures to the SQL language,
can perform complex computations,
inherits all user defined types, functions and operators,
can be defined to be trusted by the server,
is easy to use.
The PL/pgSQL call handler parses the functions source text and produces an internal binary instruction tree on the first time, the function is called by a backend. The produced bytecode is identified in the call handler by the object ID of the function. This ensures, that changing a function by a DROP/CREATE sequence will take effect without establishing a new database connection.
For all expressions and SQL statements used in the function, the PL/pgSQL bytecode interpreter creates a prepared execution plan using the SPI managers SPI_prepare() and SPI_saveplan() functions. This is done the first time, the individual statement is processed in the PL/pgSQL function. Thus, a function with conditional code that contains many statements for which execution plans would be required, will only prepare and save those plans that are really used during the entire lifetime of the database connection.
Except for input-/output-conversion and calculation functions for user defined types, anything that can be defined in C language functions can also be done with PL/pgSQL. It is possible to create complex conditional computation functions and later use them to define operators or use them in functional indices.