codekingpro/portable-devtools
115k
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>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 16.3 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 <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 main5 <acronym class="acronym">SQL</acronym> executor. For example, when you write6 a <span class="application">PL/pgSQL</span> statement like7</p><pre class="synopsis">8IF <em class="replaceable"><code>expression</code></em> THEN ...9</pre><p>10 <span class="application">PL/pgSQL</span> will evaluate the expression by11 feeding a query like12</p><pre class="synopsis">13SELECT <em class="replaceable"><code>expression</code></em>14</pre><p>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 names17 are replaced by query parameters, as discussed in detail in18 <a class="xref" href="plpgsql-implementation.html#PLPGSQL-VAR-SUBST" title="43.11.1. Variable Substitution">Section 43.11.1</a>.19 This allows the query plan for the <code class="command">SELECT</code> to20 be prepared just once and then reused for subsequent21 evaluations with different values of the variables. Thus, what22 really happens on first use of an expression is essentially a23 <code class="command">PREPARE</code> command. For example, if we have declared24 two integer variables <code class="literal">x</code> and <code class="literal">y</code>, and we write25</p><pre class="programlisting">26IF x < y THEN ...27</pre><p>28 what happens behind the scenes is equivalent to29</p><pre class="programlisting">30PREPARE <em class="replaceable"><code>statement_name</code></em>(integer, integer) AS SELECT $1 < $2;31</pre><p>32 and then this prepared statement is <code class="command">EXECUTE</code>d for each33 execution of the <code class="command">IF</code> statement, with the current values34 of the <span class="application">PL/pgSQL</span> variables supplied as35 parameter values. Normally these details are36 not important to a <span class="application">PL/pgSQL</span> user, but37 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="43.11.2. Plan Caching">Section 43.11.2</a>.39 </p><p>40 Since an <em class="replaceable"><code>expression</code></em> is converted to a41 <code class="literal">SELECT</code> command, it can contain the same clauses42 that an ordinary <code class="literal">SELECT</code> would, except that it43 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 with46</p><pre class="programlisting">47IF count(*) > 0 FROM my_table THEN ...48</pre><p>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 as51 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 not53 more than one row. (If it produces no rows, the result is taken as54 NULL.)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="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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 43.5. Basic Statements</td></tr></table></div></body></html>