1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>41.1. Overview</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plpgsql.html" title="Chapter 41. PL/pgSQL — SQL Procedural Language" /><link rel="next" href="plpgsql-structure.html" title="41.2. Structure of PL/pgSQL" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">41.1. Overview</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql.html" title="Chapter 41. PL/pgSQL — SQL Procedural Language">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpgsql.html" title="Chapter 41. PL/pgSQL — SQL Procedural Language">Up</a></td><th width="60%" align="center">Chapter 41. <span class="application">PL/pgSQL</span> — <acronym class="acronym">SQL</acronym> Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpgsql-structure.html" title="41.2. Structure of PL/pgSQL">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPGSQL-OVERVIEW"><div class="titlepage"><div><div><h2 class="title" style="clear: both">41.1. Overview <a href="#PLPGSQL-OVERVIEW" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="plpgsql-overview.html#PLPGSQL-ADVANTAGES">41.1.1. Advantages of Using <span class="application">PL/pgSQL</span></a></span></dt><dt><span class="sect2"><a href="plpgsql-overview.html#PLPGSQL-ARGS-RESULTS">41.1.2. Supported Argument and Result Data Types</a></span></dt></dl></div><p>
3 <span class="application">PL/pgSQL</span> is a loadable procedural
4 language for the <span class="productname">PostgreSQL</span> database
5 system. The design goals of <span class="application">PL/pgSQL</span> were to create
6 a loadable procedural language that
8 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
9 can be used to create functions, procedures, and triggers,
10 </p></li><li class="listitem"><p>
11 adds control structures to the <acronym class="acronym">SQL</acronym> language,
12 </p></li><li class="listitem"><p>
13 can perform complex computations,
14 </p></li><li class="listitem"><p>
15 inherits all user-defined types, functions, procedures, and operators,
16 </p></li><li class="listitem"><p>
17 can be defined to be trusted by the server,
18 </p></li><li class="listitem"><p>
20 </p></li></ul></div><p>
22 Functions created with <span class="application">PL/pgSQL</span> can be
23 used anywhere that built-in functions could be used.
24 For example, it is possible to
25 create complex conditional computation functions and later use
26 them to define operators or use them in index expressions.
28 In <span class="productname">PostgreSQL</span> 9.0 and later,
29 <span class="application">PL/pgSQL</span> is installed by default.
30 However it is still a loadable module, so especially security-conscious
31 administrators could choose to remove it.
32 </p><div class="sect2" id="PLPGSQL-ADVANTAGES"><div class="titlepage"><div><div><h3 class="title">41.1.1. Advantages of Using <span class="application">PL/pgSQL</span> <a href="#PLPGSQL-ADVANTAGES" class="id_link">#</a></h3></div></div></div><p>
33 <acronym class="acronym">SQL</acronym> is the language <span class="productname">PostgreSQL</span>
34 and most other relational databases use as query language. It's
35 portable and easy to learn. But every <acronym class="acronym">SQL</acronym>
36 statement must be executed individually by the database server.
38 That means that your client application must send each query to
39 the database server, wait for it to be processed, receive and
40 process the results, do some computation, then send further
41 queries to the server. All this incurs interprocess
42 communication and will also incur network overhead if your client
43 is on a different machine than the database server.
45 With <span class="application">PL/pgSQL</span> you can group a block of
46 computation and a series of queries <span class="emphasis"><em>inside</em></span>
47 the database server, thus having the power of a procedural
48 language and the ease of use of SQL, but with considerable
49 savings of client/server communication overhead.
50 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p> Extra round trips between
51 client and server are eliminated </p></li><li class="listitem"><p> Intermediate results that the client does not
52 need do not have to be marshaled or transferred between server
53 and client </p></li><li class="listitem"><p> Multiple rounds of query
54 parsing can be avoided </p></li></ul></div><p> This can result in a considerable performance increase as
55 compared to an application that does not use stored functions.
57 Also, with <span class="application">PL/pgSQL</span> you can use all
58 the data types, operators and functions of SQL.
59 </p></div><div class="sect2" id="PLPGSQL-ARGS-RESULTS"><div class="titlepage"><div><div><h3 class="title">41.1.2. Supported Argument and Result Data Types <a href="#PLPGSQL-ARGS-RESULTS" class="id_link">#</a></h3></div></div></div><p>
60 Functions written in <span class="application">PL/pgSQL</span> can accept
61 as arguments any scalar or array data type supported by the server,
62 and they can return a result of any of these types. They can also
63 accept or return any composite type (row type) specified by name.
64 It is also possible to declare a <span class="application">PL/pgSQL</span>
65 function as accepting <code class="type">record</code>, which means that any
66 composite type will do as input, or
67 as returning <code class="type">record</code>, which means that the result
68 is a row type whose columns are determined by specification in the
69 calling query, as discussed in <a class="xref" href="queries-table-expressions.html#QUERIES-TABLEFUNCTIONS" title="7.2.1.4. Table Functions">Section 7.2.1.4</a>.
71 <span class="application">PL/pgSQL</span> functions can be declared to accept a variable
72 number of arguments by using the <code class="literal">VARIADIC</code> marker. This
73 works exactly the same way as for SQL functions, as discussed in
74 <a class="xref" href="xfunc-sql.html#XFUNC-SQL-VARIADIC-FUNCTIONS" title="36.5.6. SQL Functions with Variable Numbers of Arguments">Section 36.5.6</a>.
76 <span class="application">PL/pgSQL</span> functions can also be declared to
77 accept and return the polymorphic types described in
78 <a class="xref" href="extend-type-system.html#EXTEND-TYPES-POLYMORPHIC" title="36.2.5. Polymorphic Types">Section 36.2.5</a>, thus allowing the actual data
79 types handled by the function to vary from call to call.
80 Examples appear in <a class="xref" href="plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS" title="41.3.1. Declaring Function Parameters">Section 41.3.1</a>.
82 <span class="application">PL/pgSQL</span> functions can also be declared to return
83 a <span class="quote">“<span class="quote">set</span>”</span> (or table) of any data type that can be returned as
84 a single instance. Such a function generates its output by executing
85 <code class="command">RETURN NEXT</code> for each desired element of the result
86 set, or by using <code class="command">RETURN QUERY</code> to output the result of
89 Finally, a <span class="application">PL/pgSQL</span> function can be declared to return
90 <code class="type">void</code> if it has no useful return value. (Alternatively, it
91 could be written as a procedure in that case.)
93 <span class="application">PL/pgSQL</span> functions can also be declared with output
94 parameters in place of an explicit specification of the return type.
95 This does not add any fundamental capability to the language, but
96 it is often convenient, especially for returning multiple values.
97 The <code class="literal">RETURNS TABLE</code> notation can also be used in place
98 of <code class="literal">RETURNS SETOF</code>.
100 Specific examples appear in
101 <a class="xref" href="plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS" title="41.3.1. Declaring Function Parameters">Section 41.3.1</a> and
102 <a class="xref" href="plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING" title="41.6.1. Returning from a Function">Section 41.6.1</a>.
103 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql.html" title="Chapter 41. PL/pgSQL — SQL Procedural Language">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpgsql.html" title="Chapter 41. PL/pgSQL — SQL Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpgsql-structure.html" title="41.2. Structure of PL/pgSQL">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 41. <span class="application">PL/pgSQL</span> — <acronym class="acronym">SQL</acronym> Procedural Language </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 41.2. Structure of <span class="application">PL/pgSQL</span></td></tr></table></div></body></html>