The PL/pgSQL Language
Rdbhost now supports using the language PL/pgSQL in queries. The language is used to define functions (aka procedures) that can be called from SQL, just as a native procedure can.
General Merits
PL/pgsql programming allows control flow more sophisticated than that allowed by plain SQL. Later statements can depend on the outcome of earlier statements, coding with conditional blocks. The set of queries and conditional blocks can be requested of the server in one request, reducing the time required for the sequence to execute.
From the Postgresql.org website, the design goals of PL/pgSQL include:
- 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.
Usage Example
Programmers with experience in MySQL might want an equivalent to the REPLACE INTO statement. In PostgreSQL, you can use the sequence of i) UPDATE, ii) catch exception, iii)INSERT, like:
This code segment taken from the PostgreSQL developer web site.
