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