Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
PL/pgSQL
Procedural programming language for PostgreSQL database systems

PL/pgSQL (Procedural Language/PostgreSQL) is a procedural programming language supported by the PostgreSQL ORDBMS, closely resembling Oracle's PL/SQL. Introduced in PostgreSQL 6.4 (1998), it offers greater procedural control than standard SQL, enabling loops, control structures, and calls from triggers. It also supports some ISO SQL/PSM features. PL/pgSQL is included in the core distribution alongside other languages like PL/Tcl, PL/Perl, and PL/Python, with many third-party languages available, such as PL/sh, PL/Lua, and PL/v8. PostgreSQL uses Bison as its parser, facilitating code reuse and open-source language portability.

Related Image Collections Add Image
We don't have any YouTube videos related to PL/pgSQL yet.
We don't have any PDF documents related to PL/pgSQL yet.
We don't have any Books related to PL/pgSQL yet.
We don't have any archived web articles related to PL/pgSQL yet.

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:1415

  • Exception handlers are subroutines (continue handlers);
  • Warnings can be handled as an exception;
  • Declaration of variables should be based on SQL query results.

All three languages (Oracle PL/SQL,16 PostgreSQL PL/pgSQL and ISO SQL/PSM17) were heavily influenced by the Ada programming language.

Example

The following example is a function that computes the sales tax of a given subtotal:

CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$ BEGIN RETURN subtotal * 0.06; END; $$ LANGUAGE plpgsql;

Inline documentation external support

Formal pseudo-language for documentation can be embedded in SQL and PL/pgSQL scripts. This documentation is then processed by a documentation generator—an external tool which extracts data and generates hypertext. Since PL/SQL: supports some of these tools, PL/pgSQL is expected to provide full or partial support as well.

ToolFull PL/pgSQLJavadoc styleOther stylePL/pgSQL projects using it
Document! X?YesYes?
Natural Docs?NoYes?
ROBODoc?YesNo?

Other documentation tools: Doxygen, DBScribe, HyperSQL, Universal Report.

References

  1. "PostgreSQL Documentation, Appendix E: Release Notes, Release 6.4". PostgreSQL Global Development Group. January 2012. https://www.postgresql.org/docs/current/static/release-6-4.html

  2. "feature T322", SQL standard features (9 ed.), PostgreSQL. https://www.postgresql.org/docs/9.0/static/features-sql-standard.html

  3. "PL/pgSQL – SQL Procedural Language". PostgreSQL. Retrieved 2007-11-15. https://www.postgresql.org/docs/current/static/plpgsql.html

  4. "Procedural Languages". 9 May 2024. https://www.postgresql.org/docs/current/static/xplang.html

  5. "PL/Perl", Docs (current ed.), PostgreSQL. https://www.postgresql.org/docs/current/static/plperl.html

  6. "PL/Python", Docs (current ed.), PostgreSQL. https://www.postgresql.org/docs/current/static/plpython.html

  7. "PL/Java", Gborg (project), PostgreSQL. https://tada.github.io/pljava/

  8. SQL/PSM (manual), PostgreSQL, 20 May 2008. https://postgres.cz/wiki/SQL/PSM_Manual

  9. "PL/PHP", Community, Command prompt. https://public.commandprompt.com/projects/plphp/

  10. Conway, Joe, PL/R, archived from the original on 2014-06-19, retrieved 2005-02-25. https://web.archive.org/web/20140619175406/http://www.joeconway.com/plr/

  11. PL/Ruby (project), Ruby lang, 2018-11-23. https://github.com/knu/postgresql-plruby

  12. Haverbeke, Marijn (2024-05-27), marijnh/Postmodern, retrieved 2024-06-01 https://github.com/marijnh/Postmodern

  13. "Parser stage", Docs (9 ed.), PostgreSQL https://www.postgresql.org/docs/9.0/static/parser-stage.html

  14. SQL/PSM (manual), PostgreSQL, 20 May 2008. https://postgres.cz/wiki/SQL/PSM_Manual

  15. Stehule, P, "Proposal: PL/pgPSM for pg9.3", Hackers (mailing list), PostgreSQL. https://www.postgresql.org/message-id/CAFj8pRDWFdcjNSnwQB_3j1-rMO6b8=TmLTNBvDCSpRrOW2Dfeg@mail.gmail.com

  16. "C. PL/SQL Program Limits", PL/SQL Language Reference, Database Online Documentation (11g Release 1 (11.1) ed.), Oracle, p. 87, PL/SQL is based on the programming language Ada. http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/limits.htm#LNPLS018

  17. "1", Stored procedures (ODP), O’Reilly. http://cdn.oreillystatic.com/en/assets/1/event/36/Stored%2520Procedures%2520-%2520Part%25201%2520Presentation.odp