<?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><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 xmlns="http://www.w3.org/1999/xhtml" class="application">PL/pgSQL</span> - <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.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></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 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></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql-declarations.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpgsql.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpgsql-statements.html">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">Home</a></td><td width="40%" align="right" valign="top"> 43.5. Basic Statements</td></tr></table></div></body></html>