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.4. Expressions</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-declarations.html" title="41.3. Declarations" /><link rel="next" href="plpgsql-statements.html" title="41.5. Basic Statements" /></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.4. Expressions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql-declarations.html" title="41.3. Declarations">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-statements.html" title="41.5. Basic Statements">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPGSQL-EXPRESSIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">41.4. Expressions <a href="#PLPGSQL-EXPRESSIONS" class="id_link">#</a></h2></div></div></div><p>
3 All expressions used in <span class="application">PL/pgSQL</span>
4 statements are processed using the server's main
5 <acronym class="acronym">SQL</acronym> executor. For example, when you write
6 a <span class="application">PL/pgSQL</span> statement like
7 </p><pre class="synopsis">
8 IF <em class="replaceable"><code>expression</code></em> THEN ...
10 <span class="application">PL/pgSQL</span> will evaluate the expression by
12 </p><pre class="synopsis">
13 SELECT <em class="replaceable"><code>expression</code></em>
15 to the main SQL engine. While forming the <code class="command">SELECT</code> command,
16 any occurrences of <span class="application">PL/pgSQL</span> variable names
17 are replaced by query parameters, as discussed in detail in
18 <a class="xref" href="plpgsql-implementation.html#PLPGSQL-VAR-SUBST" title="41.11.1. Variable Substitution">Section 41.11.1</a>.
19 This allows the query plan for the <code class="command">SELECT</code> to
20 be prepared just once and then reused for subsequent
21 evaluations with different values of the variables. Thus, what
22 really happens on first use of an expression is essentially a
23 <code class="command">PREPARE</code> command. For example, if we have declared
24 two integer variables <code class="literal">x</code> and <code class="literal">y</code>, and we write
25 </p><pre class="programlisting">
28 what happens behind the scenes is equivalent to
29 </p><pre class="programlisting">
30 PREPARE <em class="replaceable"><code>statement_name</code></em>(integer, integer) AS SELECT $1 < $2;
32 and then this prepared statement is <code class="command">EXECUTE</code>d for each
33 execution of the <code class="command">IF</code> statement, with the current values
34 of the <span class="application">PL/pgSQL</span> variables supplied as
35 parameter values. Normally these details are
36 not important to a <span class="application">PL/pgSQL</span> user, but
37 they are useful to know when trying to diagnose a problem.
38 More information appears in <a class="xref" href="plpgsql-implementation.html#PLPGSQL-PLAN-CACHING" title="41.11.2. Plan Caching">Section 41.11.2</a>.
40 Since an <em class="replaceable"><code>expression</code></em> is converted to a
41 <code class="literal">SELECT</code> command, it can contain the same clauses
42 that an ordinary <code class="literal">SELECT</code> would, except that it
43 cannot include a top-level <code class="literal">UNION</code>,
44 <code class="literal">INTERSECT</code>, or <code class="literal">EXCEPT</code> clause.
45 Thus for example one could test whether a table is non-empty with
46 </p><pre class="programlisting">
47 IF count(*) > 0 FROM my_table THEN ...
49 since the <em class="replaceable"><code>expression</code></em>
50 between <code class="literal">IF</code> and <code class="literal">THEN</code> is parsed as
51 though it were <code class="literal">SELECT count(*) > 0 FROM my_table</code>.
52 The <code class="literal">SELECT</code> must produce a single column, and not
53 more than one row. (If it produces no rows, the result is taken as
55 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql-declarations.html" title="41.3. Declarations">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-statements.html" title="41.5. Basic Statements">Next</a></td></tr><tr><td width="40%" align="left" valign="top">41.3. Declarations </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.5. Basic Statements</td></tr></table></div></body></html>