PL/pgSQL
PL/pgSQL is a procedural programming language supported by the PostgreSQL ORDBMS. It closely resembles Oracle's PL/SQL language. Implemented by Jan Wieck, PL/pgSQL first appeared with PostgreSQL 6.4, released on October 30, 1998. Version 9 also implements some ISO SQL/PSM features, like overloading of SQL-invoked functions and procedures.
PL/pgSQL, as a fully featured programming language, allows much more procedural control than SQL, including the ability to use loops and other control structures. SQL statements and triggers can call functions created in the PL/pgSQL language.
The design of PL/pgSQL aimed to allow PostgreSQL users to perform more complex operations and computations than SQL, while providing ease of use. The language is able to be defined as trusted by the server.
PL/pgSQL is one of the programming languages included in the standard PostgreSQL distribution, the others being, PL/Perl and PL/Python. In addition, many others are available from third parties, including PL/Java, PL/pgPSM, PL/php, PL/R, PL/Ruby,
,
, Postmodern and . PostgreSQL uses Bison as its parser,
making it easy to port many open-source languages, as well as to reuse code.
Comparing with PSM
The SQL/PSM language is specified by an ISO standard, but is also inspired by Oracle's PL/SQL and PL/pgPL/SQL, so there are few differences. The PL/pgPSM contributed module implements the standard. The main features of PSM that differ from PL/pgSQL:- Exception handlers are subroutines ;
- Warnings can be handled as an exception;
- Declaration of variables should be based on SQL query results.
Example
The following example is a function that computes the sales tax of a given subtotal:CREATE FUNCTION sales_tax RETURNS real AS $$
BEGIN
RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;