Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / doc / src / sgml / plpgsql.sgml
blob07078e6aea58f048108fcd8a51f745d7981976c4
1 <!-- $PostgreSQL$ -->
3 <chapter id="plpgsql">
4 <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
6 <indexterm zone="plpgsql">
7 <primary>PL/pgSQL</primary>
8 </indexterm>
10 <sect1 id="plpgsql-overview">
11 <title>Overview</title>
13 <para>
14 <application>PL/pgSQL</application> is a loadable procedural
15 language for the <productname>PostgreSQL</productname> database
16 system. The design goals of <application>PL/pgSQL</> were to create
17 a loadable procedural language that
19 <itemizedlist>
20 <listitem>
21 <para>
22 can be used to create functions and trigger procedures,
23 </para>
24 </listitem>
25 <listitem>
26 <para>
27 adds control structures to the <acronym>SQL</acronym> language,
28 </para>
29 </listitem>
30 <listitem>
31 <para>
32 can perform complex computations,
33 </para>
34 </listitem>
35 <listitem>
36 <para>
37 inherits all user-defined types, functions, and operators,
38 </para>
39 </listitem>
40 <listitem>
41 <para>
42 can be defined to be trusted by the server,
43 </para>
44 </listitem>
45 <listitem>
46 <para>
47 is easy to use.
48 </para>
49 </listitem>
50 </itemizedlist>
51 </para>
53 <para>
54 Functions created with <application>PL/pgSQL</application> can be
55 used anywhere that built-in functions could be used.
56 For example, it is possible to
57 create complex conditional computation functions and later use
58 them to define operators or use them in index expressions.
59 </para>
61 <sect2 id="plpgsql-advantages">
62 <title>Advantages of Using <application>PL/pgSQL</application></title>
64 <para>
65 <acronym>SQL</acronym> is the language <productname>PostgreSQL</>
66 and most other relational databases use as query language. It's
67 portable and easy to learn. But every <acronym>SQL</acronym>
68 statement must be executed individually by the database server.
69 </para>
71 <para>
72 That means that your client application must send each query to
73 the database server, wait for it to be processed, receive and
74 process the results, do some computation, then send further
75 queries to the server. All this incurs interprocess
76 communication and will also incur network overhead if your client
77 is on a different machine than the database server.
78 </para>
80 <para>
81 With <application>PL/pgSQL</application> you can group a block of
82 computation and a series of queries <emphasis>inside</emphasis>
83 the database server, thus having the power of a procedural
84 language and the ease of use of SQL, but with considerable
85 savings of client/server communication overhead.
86 </para>
87 <itemizedlist>
89 <listitem><para> Extra round trips between
90 client and server are eliminated </para></listitem>
92 <listitem><para> Intermediate results that the client does not
93 need do not have to be marshaled or transferred between server
94 and client </para></listitem>
96 <listitem><para> Multiple rounds of query
97 parsing can be avoided </para></listitem>
99 </itemizedlist>
100 <para> This can result in a considerable performance increase as
101 compared to an application that does not use stored functions.
102 </para>
104 <para>
105 Also, with <application>PL/pgSQL</application> you can use all
106 the data types, operators and functions of SQL.
107 </para>
108 </sect2>
110 <sect2 id="plpgsql-args-results">
111 <title>Supported Argument and Result Data Types</title>
113 <para>
114 Functions written in <application>PL/pgSQL</application> can accept
115 as arguments any scalar or array data type supported by the server,
116 and they can return a result of any of these types. They can also
117 accept or return any composite type (row type) specified by name.
118 It is also possible to declare a <application>PL/pgSQL</application>
119 function as returning <type>record</>, which means that the result
120 is a row type whose columns are determined by specification in the
121 calling query, as discussed in <xref linkend="queries-tablefunctions">.
122 </para>
124 <para>
125 <application>PL/pgSQL</> functions can be declared to accept a variable
126 number of arguments by using the <literal>VARIADIC</> marker. This
127 works exactly the same way as for SQL functions, as discussed in
128 <xref linkend="xfunc-sql-variadic-functions">.
129 </para>
131 <para>
132 <application>PL/pgSQL</> functions can also be declared to accept
133 and return the polymorphic types
134 <type>anyelement</type>, <type>anyarray</type>, <type>anynonarray</type>,
135 and <type>anyenum</>. The actual
136 data types handled by a polymorphic function can vary from call to
137 call, as discussed in <xref linkend="extend-types-polymorphic">.
138 An example is shown in <xref linkend="plpgsql-declaration-aliases">.
139 </para>
141 <para>
142 <application>PL/pgSQL</> functions can also be declared to return
143 a <quote>set</> (or table) of any data type that can be returned as
144 a single instance. Such a function generates its output by executing
145 <command>RETURN NEXT</> for each desired element of the result
146 set, or by using <command>RETURN QUERY</> to output the result of
147 evaluating a query.
148 </para>
150 <para>
151 Finally, a <application>PL/pgSQL</> function can be declared to return
152 <type>void</> if it has no useful return value.
153 </para>
155 <para>
156 <application>PL/pgSQL</> functions can also be declared with output
157 parameters in place of an explicit specification of the return type.
158 This does not add any fundamental capability to the language, but
159 it is often convenient, especially for returning multiple values.
160 The <literal>RETURNS TABLE</> notation can also be used in place
161 of <literal>RETURNS SETOF</>.
162 </para>
164 <para>
165 Specific examples appear in
166 <xref linkend="plpgsql-declaration-aliases"> and
167 <xref linkend="plpgsql-statements-returning">.
168 </para>
169 </sect2>
170 </sect1>
172 <sect1 id="plpgsql-structure">
173 <title>Structure of <application>PL/pgSQL</application></title>
175 <para>
176 <application>PL/pgSQL</application> is a block-structured language.
177 The complete text of a function definition must be a
178 <firstterm>block</>. A block is defined as:
180 <synopsis>
181 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
182 <optional> DECLARE
183 <replaceable>declarations</replaceable> </optional>
184 BEGIN
185 <replaceable>statements</replaceable>
186 END <optional> <replaceable>label</replaceable> </optional>;
187 </synopsis>
188 </para>
190 <para>
191 Each declaration and each statement within a block is terminated
192 by a semicolon. A block that appears within another block must
193 have a semicolon after <literal>END</literal>, as shown above;
194 however the final <literal>END</literal> that
195 concludes a function body does not require a semicolon.
196 </para>
198 <tip>
199 <para>
200 A common mistake is to write a semicolon immediately after
201 <literal>BEGIN</>. This is incorrect and will result in a syntax error.
202 </para>
203 </tip>
205 <para>
206 A <replaceable>label</replaceable> is only needed if you want to
207 identify the block for use
208 in an <literal>EXIT</> statement, or to qualify the names of the
209 variables declared in the block. If a label is given after
210 <literal>END</>, it must match the label at the block's beginning.
211 </para>
213 <para>
214 All key words are case-insensitive.
215 Identifiers are implicitly converted to lowercase
216 unless double-quoted, just as they are in ordinary SQL commands.
217 </para>
219 <para>
220 There are two types of comments in <application>PL/pgSQL</>. A double
221 dash (<literal>--</literal>) starts a comment that extends to the end of
222 the line. A <literal>/*</literal> starts a block comment that extends to
223 the next occurrence of <literal>*/</literal>. Block comments nest,
224 just as in ordinary SQL.
225 </para>
227 <para>
228 Any statement in the statement section of a block
229 can be a <firstterm>subblock</>. Subblocks can be used for
230 logical grouping or to localize variables to a small group
231 of statements. Variables declared in a subblock mask any
232 similarly-named variables of outer blocks for the duration
233 of the subblock; but you can access the outer variables anyway
234 if you qualify their names with their block's label. For example:
235 <programlisting>
236 CREATE FUNCTION somefunc() RETURNS integer AS $$
237 &lt;&lt; outerblock &gt;&gt;
238 DECLARE
239 quantity integer := 30;
240 BEGIN
241 RAISE NOTICE 'Quantity here is %', quantity; -- Prints 30
242 quantity := 50;
244 -- Create a subblock
246 DECLARE
247 quantity integer := 80;
248 BEGIN
249 RAISE NOTICE 'Quantity here is %', quantity; -- Prints 80
250 RAISE NOTICE 'Outer quantity here is %', outerblock.quantity; -- Prints 50
251 END;
253 RAISE NOTICE 'Quantity here is %', quantity; -- Prints 50
255 RETURN quantity;
256 END;
257 $$ LANGUAGE plpgsql;
258 </programlisting>
259 </para>
261 <note>
262 <para>
263 There is actually a hidden <quote>outer block</> surrounding the body
264 of any <application>PL/pgSQL</> function. This block provides the
265 declarations of the function's parameters (if any), as well as some
266 special variables such as <literal>FOUND</literal> (see
267 <xref linkend="plpgsql-statements-diagnostics">). The outer block is
268 labeled with the function's name, meaning that parameters and special
269 variables can be qualified with the function's name.
270 </para>
271 </note>
273 <para>
274 It is important not to confuse the use of
275 <command>BEGIN</>/<command>END</> for grouping statements in
276 <application>PL/pgSQL</> with the similarly-named SQL commands
277 for transaction
278 control. <application>PL/pgSQL</>'s <command>BEGIN</>/<command>END</>
279 are only for grouping; they do not start or end a transaction.
280 Functions and trigger procedures are always executed within a transaction
281 established by an outer query &mdash; they cannot start or commit that
282 transaction, since there would be no context for them to execute in.
283 However, a block containing an <literal>EXCEPTION</> clause effectively
284 forms a subtransaction that can be rolled back without affecting the
285 outer transaction. For more about that see <xref
286 linkend="plpgsql-error-trapping">.
287 </para>
288 </sect1>
290 <sect1 id="plpgsql-declarations">
291 <title>Declarations</title>
293 <para>
294 All variables used in a block must be declared in the
295 declarations section of the block.
296 (The only exceptions are that the loop variable of a <literal>FOR</> loop
297 iterating over a range of integer values is automatically declared as an
298 integer variable, and likewise the loop variable of a <literal>FOR</> loop
299 iterating over a cursor's result is automatically declared as a
300 record variable.)
301 </para>
303 <para>
304 <application>PL/pgSQL</> variables can have any SQL data type, such as
305 <type>integer</type>, <type>varchar</type>, and
306 <type>char</type>.
307 </para>
309 <para>
310 Here are some examples of variable declarations:
311 <programlisting>
312 user_id integer;
313 quantity numeric(5);
314 url varchar;
315 myrow tablename%ROWTYPE;
316 myfield tablename.columnname%TYPE;
317 arow RECORD;
318 </programlisting>
319 </para>
321 <para>
322 The general syntax of a variable declaration is:
323 <synopsis>
324 <replaceable>name</replaceable> <optional> CONSTANT </optional> <replaceable>type</replaceable> <optional> NOT NULL </optional> <optional> { DEFAULT | := } <replaceable>expression</replaceable> </optional>;
325 </synopsis>
326 The <literal>DEFAULT</> clause, if given, specifies the initial value assigned
327 to the variable when the block is entered. If the <literal>DEFAULT</> clause
328 is not given then the variable is initialized to the
329 <acronym>SQL</acronym> null value.
330 The <literal>CONSTANT</> option prevents the variable from being assigned to,
331 so that its value remains constant for the duration of the block.
332 If <literal>NOT NULL</>
333 is specified, an assignment of a null value results in a run-time
334 error. All variables declared as <literal>NOT NULL</>
335 must have a nonnull default value specified.
336 </para>
338 <para>
339 A variable's default value is evaluated and assigned to the variable
340 each time the block is entered (not just once per function call).
341 So, for example, assigning <literal>now()</literal> to a variable of type
342 <type>timestamp</type> causes the variable to have the
343 time of the current function call, not the time when the function was
344 precompiled.
345 </para>
347 <para>
348 Examples:
349 <programlisting>
350 quantity integer DEFAULT 32;
351 url varchar := 'http://mysite.com';
352 user_id CONSTANT integer := 10;
353 </programlisting>
354 </para>
356 <sect2 id="plpgsql-declaration-aliases">
357 <title>Aliases for Function Parameters</title>
359 <para>
360 Parameters passed to functions are named with the identifiers
361 <literal>$1</literal>, <literal>$2</literal>,
362 etc. Optionally, aliases can be declared for
363 <literal>$<replaceable>n</replaceable></literal>
364 parameter names for increased readability. Either the alias or the
365 numeric identifier can then be used to refer to the parameter value.
366 </para>
368 <para>
369 There are two ways to create an alias. The preferred way is to give a
370 name to the parameter in the <command>CREATE FUNCTION</command> command,
371 for example:
372 <programlisting>
373 CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
374 BEGIN
375 RETURN subtotal * 0.06;
376 END;
377 $$ LANGUAGE plpgsql;
378 </programlisting>
379 The other way, which was the only way available before
380 <productname>PostgreSQL</productname> 8.0, is to explicitly
381 declare an alias, using the declaration syntax
383 <synopsis>
384 <replaceable>name</replaceable> ALIAS FOR $<replaceable>n</replaceable>;
385 </synopsis>
387 The same example in this style looks like:
388 <programlisting>
389 CREATE FUNCTION sales_tax(real) RETURNS real AS $$
390 DECLARE
391 subtotal ALIAS FOR $1;
392 BEGIN
393 RETURN subtotal * 0.06;
394 END;
395 $$ LANGUAGE plpgsql;
396 </programlisting>
397 </para>
399 <note>
400 <para>
401 These two examples are not perfectly equivalent. In the first case,
402 <literal>subtotal</> could be referenced as
403 <literal>sales_tax.subtotal</>, but in the second case it could not.
404 (Had we attached a label to the block, <literal>subtotal</> could
405 be qualified with that label, instead.)
406 </para>
407 </note>
409 <para>
410 Some more examples:
411 <programlisting>
412 CREATE FUNCTION instr(varchar, integer) RETURNS integer AS $$
413 DECLARE
414 v_string ALIAS FOR $1;
415 index ALIAS FOR $2;
416 BEGIN
417 -- some computations using v_string and index here
418 END;
419 $$ LANGUAGE plpgsql;
422 CREATE FUNCTION concat_selected_fields(in_t sometablename) RETURNS text AS $$
423 BEGIN
424 RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
425 END;
426 $$ LANGUAGE plpgsql;
427 </programlisting>
428 </para>
430 <para>
431 When a <application>PL/pgSQL</application> function is declared
432 with output parameters, the output parameters are given
433 <literal>$<replaceable>n</replaceable></literal> names and optional
434 aliases in just the same way as the normal input parameters. An
435 output parameter is effectively a variable that starts out NULL;
436 it should be assigned to during the execution of the function.
437 The final value of the parameter is what is returned. For instance,
438 the sales-tax example could also be done this way:
440 <programlisting>
441 CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
442 BEGIN
443 tax := subtotal * 0.06;
444 END;
445 $$ LANGUAGE plpgsql;
446 </programlisting>
448 Notice that we omitted <literal>RETURNS real</> &mdash; we could have
449 included it, but it would be redundant.
450 </para>
452 <para>
453 Output parameters are most useful when returning multiple values.
454 A trivial example is:
456 <programlisting>
457 CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS $$
458 BEGIN
459 sum := x + y;
460 prod := x * y;
461 END;
462 $$ LANGUAGE plpgsql;
463 </programlisting>
465 As discussed in <xref linkend="xfunc-output-parameters">, this
466 effectively creates an anonymous record type for the function's
467 results. If a <literal>RETURNS</> clause is given, it must say
468 <literal>RETURNS record</>.
469 </para>
471 <para>
472 Another way to declare a <application>PL/pgSQL</application> function
473 is with <literal>RETURNS TABLE</>, for example:
475 <programlisting>
476 CREATE FUNCTION extended_sales(p_itemno int) RETURNS TABLE(quantity int, total numeric) AS $$
477 BEGIN
478 RETURN QUERY SELECT quantity, quantity * price FROM sales WHERE itemno = p_itemno;
479 END;
480 $$ LANGUAGE plpgsql;
481 </programlisting>
483 This is exactly equivalent to declaring one or more <literal>OUT</>
484 parameters and specifying <literal>RETURNS SETOF
485 <replaceable>sometype</></literal>.
486 </para>
488 <para>
489 When the return type of a <application>PL/pgSQL</application>
490 function is declared as a polymorphic type (<type>anyelement</type>,
491 <type>anyarray</type>, <type>anynonarray</type>, or <type>anyenum</>),
492 a special parameter <literal>$0</literal>
493 is created. Its data type is the actual return type of the function,
494 as deduced from the actual input types (see <xref
495 linkend="extend-types-polymorphic">).
496 This allows the function to access its actual return type
497 as shown in <xref linkend="plpgsql-declaration-type">.
498 <literal>$0</literal> is initialized to null and can be modified by
499 the function, so it can be used to hold the return value if desired,
500 though that is not required. <literal>$0</literal> can also be
501 given an alias. For example, this function works on any data type
502 that has a <literal>+</> operator:
504 <programlisting>
505 CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement)
506 RETURNS anyelement AS $$
507 DECLARE
508 result ALIAS FOR $0;
509 BEGIN
510 result := v1 + v2 + v3;
511 RETURN result;
512 END;
513 $$ LANGUAGE plpgsql;
514 </programlisting>
515 </para>
517 <para>
518 The same effect can be had by declaring one or more output parameters as
519 polymorphic types. In this case the
520 special <literal>$0</literal> parameter is not used; the output
521 parameters themselves serve the same purpose. For example:
523 <programlisting>
524 CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement,
525 OUT sum anyelement)
526 AS $$
527 BEGIN
528 sum := v1 + v2 + v3;
529 END;
530 $$ LANGUAGE plpgsql;
531 </programlisting>
532 </para>
533 </sect2>
535 <sect2 id="plpgsql-declaration-type">
536 <title>Copying Types</title>
538 <synopsis>
539 <replaceable>variable</replaceable>%TYPE
540 </synopsis>
542 <para>
543 <literal>%TYPE</literal> provides the data type of a variable or
544 table column. You can use this to declare variables that will hold
545 database values. For example, let's say you have a column named
546 <literal>user_id</literal> in your <literal>users</literal>
547 table. To declare a variable with the same data type as
548 <literal>users.user_id</> you write:
549 <programlisting>
550 user_id users.user_id%TYPE;
551 </programlisting>
552 </para>
554 <para>
555 By using <literal>%TYPE</literal> you don't need to know the data
556 type of the structure you are referencing, and most importantly,
557 if the data type of the referenced item changes in the future (for
558 instance: you change the type of <literal>user_id</>
559 from <type>integer</type> to <type>real</type>), you might not need
560 to change your function definition.
561 </para>
563 <para>
564 <literal>%TYPE</literal> is particularly valuable in polymorphic
565 functions, since the data types needed for internal variables can
566 change from one call to the next. Appropriate variables can be
567 created by applying <literal>%TYPE</literal> to the function's
568 arguments or result placeholders.
569 </para>
571 </sect2>
573 <sect2 id="plpgsql-declaration-rowtypes">
574 <title>Row Types</title>
576 <synopsis>
577 <replaceable>name</replaceable> <replaceable>table_name</replaceable><literal>%ROWTYPE</literal>;
578 <replaceable>name</replaceable> <replaceable>composite_type_name</replaceable>;
579 </synopsis>
581 <para>
582 A variable of a composite type is called a <firstterm>row</>
583 variable (or <firstterm>row-type</> variable). Such a variable
584 can hold a whole row of a <command>SELECT</> or <command>FOR</>
585 query result, so long as that query's column set matches the
586 declared type of the variable.
587 The individual fields of the row value
588 are accessed using the usual dot notation, for example
589 <literal>rowvar.field</literal>.
590 </para>
592 <para>
593 A row variable can be declared to have the same type as the rows of
594 an existing table or view, by using the
595 <replaceable>table_name</replaceable><literal>%ROWTYPE</literal>
596 notation; or it can be declared by giving a composite type's name.
597 (Since every table has an associated composite type of the same name,
598 it actually does not matter in <productname>PostgreSQL</> whether you
599 write <literal>%ROWTYPE</literal> or not. But the form with
600 <literal>%ROWTYPE</literal> is more portable.)
601 </para>
603 <para>
604 Parameters to a function can be
605 composite types (complete table rows). In that case, the
606 corresponding identifier <literal>$<replaceable>n</replaceable></> will be a row variable, and fields can
607 be selected from it, for example <literal>$1.user_id</literal>.
608 </para>
610 <para>
611 Only the user-defined columns of a table row are accessible in a
612 row-type variable, not the OID or other system columns (because the
613 row could be from a view). The fields of the row type inherit the
614 table's field size or precision for data types such as
615 <type>char(<replaceable>n</>)</type>.
616 </para>
618 <para>
619 Here is an example of using composite types. <structname>table1</>
620 and <structname>table2</> are existing tables having at least the
621 mentioned fields:
623 <programlisting>
624 CREATE FUNCTION merge_fields(t_row table1) RETURNS text AS $$
625 DECLARE
626 t2_row table2%ROWTYPE;
627 BEGIN
628 SELECT * INTO t2_row FROM table2 WHERE ... ;
629 RETURN t_row.f1 || t2_row.f3 || t_row.f5 || t2_row.f7;
630 END;
631 $$ LANGUAGE plpgsql;
633 SELECT merge_fields(t.*) FROM table1 t WHERE ... ;
634 </programlisting>
635 </para>
636 </sect2>
638 <sect2 id="plpgsql-declaration-records">
639 <title>Record Types</title>
641 <synopsis>
642 <replaceable>name</replaceable> RECORD;
643 </synopsis>
645 <para>
646 Record variables are similar to row-type variables, but they have no
647 predefined structure. They take on the actual row structure of the
648 row they are assigned during a <command>SELECT</> or <command>FOR</> command. The substructure
649 of a record variable can change each time it is assigned to.
650 A consequence of this is that until a record variable is first assigned
651 to, it has no substructure, and any attempt to access a
652 field in it will draw a run-time error.
653 </para>
655 <para>
656 Note that <literal>RECORD</> is not a true data type, only a placeholder.
657 One should also realize that when a <application>PL/pgSQL</application>
658 function is declared to return type <type>record</>, this is not quite the
659 same concept as a record variable, even though such a function might
660 use a record variable to hold its result. In both cases the actual row
661 structure is unknown when the function is written, but for a function
662 returning <type>record</> the actual structure is determined when the
663 calling query is parsed, whereas a record variable can change its row
664 structure on-the-fly.
665 </para>
666 </sect2>
668 <sect2 id="plpgsql-declaration-renaming-vars">
669 <title><literal>RENAME</></title>
671 <synopsis>
672 RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
673 </synopsis>
675 <para>
676 Using the <literal>RENAME</literal> declaration you can change the
677 name of a variable, record or row. This is primarily useful if
678 <varname>NEW</varname> or <varname>OLD</varname> should be
679 referenced by another name inside a trigger procedure. See also
680 <literal>ALIAS</literal>.
681 </para>
683 <para>
684 Examples:
685 <programlisting>
686 RENAME id TO user_id;
687 RENAME this_var TO that_var;
688 </programlisting>
689 </para>
691 <note>
692 <para>
693 <literal>RENAME</literal> appears to be broken as of
694 <productname>PostgreSQL</> 7.3. Fixing this is of low priority,
695 since <literal>ALIAS</literal> covers most of the practical uses
696 of <literal>RENAME</literal>.
697 </para>
698 </note>
699 </sect2>
700 </sect1>
702 <sect1 id="plpgsql-expressions">
703 <title>Expressions</title>
705 <para>
706 All expressions used in <application>PL/pgSQL</application>
707 statements are processed using the server's main
708 <acronym>SQL</acronym> executor. For example, when you write
709 a <application>PL/pgSQL</application> statement like
710 <synopsis>
711 IF <replaceable>expression</replaceable> THEN ...
712 </synopsis>
713 <application>PL/pgSQL</application> will evaluate the expression by
714 feeding a query like
715 <synopsis>
716 SELECT <replaceable>expression</replaceable>
717 </synopsis>
718 to the main SQL engine. While forming the <command>SELECT</> command,
719 any occurrences of <application>PL/pgSQL</application> variable names
720 are replaced by parameters, as discussed in detail in
721 <xref linkend="plpgsql-var-subst">.
722 This allows the query plan for the <command>SELECT</command> to
723 be prepared just once and then reused for subsequent
724 evaluations with different values of the variables. Thus, what
725 really happens on first use of an expression is essentially a
726 <command>PREPARE</> command. For example, if we have declared
727 two integer variables <literal>x</> and <literal>y</>, and we write
728 <programlisting>
729 IF x &lt; y THEN ...
730 </programlisting>
731 what happens behind the scenes is
732 <programlisting>
733 PREPARE <replaceable>statement_name</>(integer, integer) AS SELECT $1 &lt; $2;
734 </programlisting>
735 and then this prepared statement is <command>EXECUTE</>d for each
736 execution of the <command>IF</> statement, with the current values
737 of the <application>PL/pgSQL</application> variables supplied as
738 parameter values.
739 The query plan prepared in this way is saved for the life of the database
740 connection, as described in
741 <xref linkend="plpgsql-plan-caching">. Normally these details are
742 not important to a <application>PL/pgSQL</application> user, but
743 they are useful to know when trying to diagnose a problem.
744 </para>
745 </sect1>
747 <sect1 id="plpgsql-statements">
748 <title>Basic Statements</title>
750 <para>
751 In this section and the following ones, we describe all the statement
752 types that are explicitly understood by
753 <application>PL/pgSQL</application>.
754 Anything not recognized as one of these statement types is presumed
755 to be an SQL command and is sent to the main database engine to execute,
756 as described in <xref linkend="plpgsql-statements-sql-noresult">
757 and <xref linkend="plpgsql-statements-sql-onerow">.
758 </para>
760 <sect2 id="plpgsql-statements-assignment">
761 <title>Assignment</title>
763 <para>
764 An assignment of a value to a <application>PL/pgSQL</application>
765 variable or row/record field is written as:
766 <synopsis>
767 <replaceable>variable</replaceable> := <replaceable>expression</replaceable>;
768 </synopsis>
769 As explained above, the expression in such a statement is evaluated
770 by means of an SQL <command>SELECT</> command sent to the main
771 database engine. The expression must yield a single value.
772 </para>
774 <para>
775 If the expression's result data type doesn't match the variable's
776 data type, or the variable has a specific size/precision
777 (like <type>char(20)</type>), the result value will be implicitly
778 converted by the <application>PL/pgSQL</application> interpreter using
779 the result type's output-function and
780 the variable type's input-function. Note that this could potentially
781 result in run-time errors generated by the input function, if the
782 string form of the result value is not acceptable to the input function.
783 </para>
785 <para>
786 Examples:
787 <programlisting>
788 tax := subtotal * 0.06;
789 my_record.user_id := 20;
790 </programlisting>
791 </para>
792 </sect2>
794 <sect2 id="plpgsql-statements-sql-noresult">
795 <title>Executing a Command With No Result</title>
797 <para>
798 For any SQL command that does not return rows, for example
799 <command>INSERT</> without a <literal>RETURNING</> clause, you can
800 execute the command within a <application>PL/pgSQL</application> function
801 just by writing the command.
802 </para>
804 <para>
805 Any <application>PL/pgSQL</application> variable name appearing
806 in the command text is replaced by a parameter symbol, and then the
807 current value of the variable is provided as the parameter value
808 at run time. This is exactly like the processing described earlier
809 for expressions; for details see <xref linkend="plpgsql-var-subst">.
810 As an example, if you write:
811 <programlisting>
812 DECLARE
813 key TEXT;
814 delta INTEGER;
815 BEGIN
817 UPDATE mytab SET val = val + delta WHERE id = key;
818 </programlisting>
819 the command text seen by the main SQL engine will look like:
820 <programlisting>
821 UPDATE mytab SET val = val + $1 WHERE id = $2;
822 </programlisting>
823 Although you don't normally have to think about this, it's helpful
824 to know it when you need to make sense of syntax-error messages.
825 </para>
827 <caution>
828 <para>
829 <application>PL/pgSQL</application> will substitute for any identifier
830 matching one of the function's declared variables; it is not bright
831 enough to know whether that's what you meant! Thus, it is a bad idea
832 to use a variable name that is the same as any table, column, or
833 function name that you need to reference in commands within the
834 function. For more discussion see <xref linkend="plpgsql-var-subst">.
835 </para>
836 </caution>
838 <para>
839 When executing a SQL command in this way,
840 <application>PL/pgSQL</application> plans the command just once
841 and re-uses the plan on subsequent executions, for the life of
842 the database connection. The implications of this are discussed
843 in detail in <xref linkend="plpgsql-plan-caching">.
844 </para>
846 <para>
847 Sometimes it is useful to evaluate an expression or <command>SELECT</>
848 query but discard the result, for example when calling a function
849 that has side-effects but no useful result value. To do
850 this in <application>PL/pgSQL</application>, use the
851 <command>PERFORM</command> statement:
853 <synopsis>
854 PERFORM <replaceable>query</replaceable>;
855 </synopsis>
857 This executes <replaceable>query</replaceable> and discards the
858 result. Write the <replaceable>query</replaceable> the same
859 way you would write an SQL <command>SELECT</> command, but replace the
860 initial keyword <command>SELECT</> with <command>PERFORM</command>.
861 <application>PL/pgSQL</application> variables will be
862 substituted into the query just as for commands that return no result,
863 and the plan is cached in the same way. Also, the special variable
864 <literal>FOUND</literal> is set to true if the query produced at
865 least one row, or false if it produced no rows (see
866 <xref linkend="plpgsql-statements-diagnostics">).
867 </para>
869 <note>
870 <para>
871 One might expect that writing <command>SELECT</command> directly
872 would accomplish this result, but at
873 present the only accepted way to do it is
874 <command>PERFORM</command>. A SQL command that can return rows,
875 such as <command>SELECT</command>, will be rejected as an error
876 unless it has an <literal>INTO</> clause as discussed in the
877 next section.
878 </para>
879 </note>
881 <para>
882 An example:
883 <programlisting>
884 PERFORM create_mv('cs_session_page_requests_mv', my_query);
885 </programlisting>
886 </para>
887 </sect2>
889 <sect2 id="plpgsql-statements-sql-onerow">
890 <title>Executing a Query with a Single-Row Result</title>
892 <indexterm zone="plpgsql-statements-sql-onerow">
893 <primary>SELECT INTO</primary>
894 <secondary>in PL/pgSQL</secondary>
895 </indexterm>
897 <indexterm zone="plpgsql-statements-sql-onerow">
898 <primary>RETURNING INTO</primary>
899 <secondary>in PL/pgSQL</secondary>
900 </indexterm>
902 <para>
903 The result of a SQL command yielding a single row (possibly of multiple
904 columns) can be assigned to a record variable, row-type variable, or list
905 of scalar variables. This is done by writing the base SQL command and
906 adding an <literal>INTO</> clause. For example,
908 <synopsis>
909 SELECT <replaceable>select_expressions</replaceable> INTO <optional>STRICT</optional> <replaceable>target</replaceable> FROM ...;
910 INSERT ... RETURNING <replaceable>expressions</replaceable> INTO <optional>STRICT</optional> <replaceable>target</replaceable>;
911 UPDATE ... RETURNING <replaceable>expressions</replaceable> INTO <optional>STRICT</optional> <replaceable>target</replaceable>;
912 DELETE ... RETURNING <replaceable>expressions</replaceable> INTO <optional>STRICT</optional> <replaceable>target</replaceable>;
913 </synopsis>
915 where <replaceable>target</replaceable> can be a record variable, a row
916 variable, or a comma-separated list of simple variables and
917 record/row fields.
918 <application>PL/pgSQL</application> variables will be
919 substituted into the rest of the query, and the plan is cached,
920 just as described above for commands that do not return rows.
921 This works for <command>SELECT</>,
922 <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> with
923 <literal>RETURNING</>, and utility commands that return row-set
924 results (such as <command>EXPLAIN</>).
925 Except for the <literal>INTO</> clause, the SQL command is the same
926 as it would be written outside <application>PL/pgSQL</application>.
927 </para>
929 <tip>
930 <para>
931 Note that this interpretation of <command>SELECT</> with <literal>INTO</>
932 is quite different from <productname>PostgreSQL</>'s regular
933 <command>SELECT INTO</command> command, wherein the <literal>INTO</>
934 target is a newly created table. If you want to create a table from a
935 <command>SELECT</> result inside a
936 <application>PL/pgSQL</application> function, use the syntax
937 <command>CREATE TABLE ... AS SELECT</command>.
938 </para>
939 </tip>
941 <para>
942 If a row or a variable list is used as target, the query's result columns
943 must exactly match the structure of the target as to number and data
944 types, or a run-time error
945 occurs. When a record variable is the target, it automatically
946 configures itself to the row type of the query result columns.
947 </para>
949 <para>
950 The <literal>INTO</> clause can appear almost anywhere in the SQL
951 command. Customarily it is written either just before or just after
952 the list of <replaceable>select_expressions</replaceable> in a
953 <command>SELECT</> command, or at the end of the command for other
954 command types. It is recommended that you follow this convention
955 in case the <application>PL/pgSQL</application> parser becomes
956 stricter in future versions.
957 </para>
959 <para>
960 If <literal>STRICT</literal> is not specified in the <literal>INTO</>
961 clause, then <replaceable>target</replaceable> will be set to the first
962 row returned by the query, or to nulls if the query returned no rows.
963 (Note that <quote>the first row</> is not
964 well-defined unless you've used <literal>ORDER BY</>.) Any result rows
965 after the first row are discarded.
966 You can check the special <literal>FOUND</literal> variable (see
967 <xref linkend="plpgsql-statements-diagnostics">) to
968 determine whether a row was returned:
970 <programlisting>
971 SELECT * INTO myrec FROM emp WHERE empname = myname;
972 IF NOT FOUND THEN
973 RAISE EXCEPTION 'employee % not found', myname;
974 END IF;
975 </programlisting>
977 If the <literal>STRICT</literal> option is specified, the query must
978 return exactly one row or a run-time error will be reported, either
979 <literal>NO_DATA_FOUND</> (no rows) or <literal>TOO_MANY_ROWS</>
980 (more than one row). You can use an exception block if you wish
981 to catch the error, for example:
983 <programlisting>
984 BEGIN
985 SELECT * INTO STRICT myrec FROM emp WHERE empname = myname;
986 EXCEPTION
987 WHEN NO_DATA_FOUND THEN
988 RAISE EXCEPTION 'employee % not found', myname;
989 WHEN TOO_MANY_ROWS THEN
990 RAISE EXCEPTION 'employee % not unique', myname;
991 END;
992 </programlisting>
993 Successful execution of a command with <literal>STRICT</>
994 always sets <literal>FOUND</literal> to true.
995 </para>
997 <para>
998 For <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> with
999 <literal>RETURNING</>, <application>PL/pgSQL</application> reports
1000 an error for more than one returned row, even when
1001 <literal>STRICT</literal> is not specified. This is because there
1002 is no option such as <literal>ORDER BY</> with which to determine
1003 which affected row should be returned.
1004 </para>
1006 <note>
1007 <para>
1008 The <literal>STRICT</> option matches the behavior of
1009 Oracle PL/SQL's <command>SELECT INTO</command> and related statements.
1010 </para>
1011 </note>
1013 <para>
1014 To handle cases where you need to process multiple result rows
1015 from a SQL query, see <xref linkend="plpgsql-records-iterating">.
1016 </para>
1018 </sect2>
1020 <sect2 id="plpgsql-statements-executing-dyn">
1021 <title>Executing Dynamic Commands</title>
1023 <para>
1024 Oftentimes you will want to generate dynamic commands inside your
1025 <application>PL/pgSQL</application> functions, that is, commands
1026 that will involve different tables or different data types each
1027 time they are executed. <application>PL/pgSQL</application>'s
1028 normal attempts to cache plans for commands (as discussed in
1029 <xref linkend="plpgsql-plan-caching">) will not work in such
1030 scenarios. To handle this sort of problem, the
1031 <command>EXECUTE</command> statement is provided:
1033 <synopsis>
1034 EXECUTE <replaceable class="command">command-string</replaceable> <optional> INTO <optional>STRICT</optional> <replaceable>target</replaceable> </optional> <optional> USING <replaceable>expression</replaceable> <optional>, ...</optional> </optional>;
1035 </synopsis>
1037 where <replaceable>command-string</replaceable> is an expression
1038 yielding a string (of type <type>text</type>) containing the
1039 command to be executed. The optional <replaceable>target</replaceable>
1040 is a record variable, a row variable, or a comma-separated list of
1041 simple variables and record/row fields, into which the results of
1042 the command will be stored. The optional <literal>USING</> expressions
1043 supply values to be inserted into the command.
1044 </para>
1046 <para>
1047 No substitution of <application>PL/pgSQL</> variables is done on the
1048 computed command string. Any required variable values must be inserted
1049 in the command string as it is constructed; or you can use parameters
1050 as described below.
1051 </para>
1053 <para>
1054 Also, there is no plan caching for commands executed via
1055 <command>EXECUTE</command>. Instead, the
1056 command is prepared each time the statement is run. Thus the command
1057 string can be dynamically created within the function to perform
1058 actions on different tables and columns.
1059 </para>
1061 <para>
1062 The <literal>INTO</literal> clause specifies where the results of
1063 a SQL command returning rows should be assigned. If a row
1064 or variable list is provided, it must exactly match the structure
1065 of the query's results (when a
1066 record variable is used, it will configure itself to match the
1067 result structure automatically). If multiple rows are returned,
1068 only the first will be assigned to the <literal>INTO</literal>
1069 variable. If no rows are returned, NULL is assigned to the
1070 <literal>INTO</literal> variable(s). If no <literal>INTO</literal>
1071 clause is specified, the query results are discarded.
1072 </para>
1074 <para>
1075 If the <literal>STRICT</> option is given, an error is reported
1076 unless the query produces exactly one row.
1077 </para>
1079 <para>
1080 The command string can use parameter values, which are referenced
1081 in the command as <literal>$1</>, <literal>$2</>, etc.
1082 These symbols refer to values supplied in the <literal>USING</>
1083 clause. This method is often preferable to inserting data values
1084 into the command string as text: it avoids run-time overhead of
1085 converting the values to text and back, and it is much less prone
1086 to SQL-injection attacks since there is no need for quoting or escaping.
1087 An example is:
1088 <programlisting>
1089 EXECUTE 'SELECT count(*) FROM mytable WHERE inserted_by = $1 AND inserted &lt;= $2'
1090 INTO c
1091 USING checked_user, checked_date;
1092 </programlisting>
1094 Note that parameter symbols can only be used for data values
1095 &mdash; if you want to use dynamically determined table or column
1096 names, you must insert them into the command string textually.
1097 For example, if the preceding query needed to be done against a
1098 dynamically selected table, you could do this:
1099 <programlisting>
1100 EXECUTE 'SELECT count(*) FROM '
1101 || tabname::regclass
1102 || ' WHERE inserted_by = $1 AND inserted &lt;= $2'
1103 INTO c
1104 USING checked_user, checked_date;
1105 </programlisting>
1106 </para>
1108 <para>
1109 An <command>EXECUTE</> with a simple constant command string and some
1110 <literal>USING</> parameters, as in the first example above, is
1111 functionally equivalent to just writing the command directly in
1112 <application>PL/pgSQL</application> and allowing replacement of
1113 <application>PL/pgSQL</application> variables to happen automatically.
1114 The important difference is that <command>EXECUTE</> will re-plan
1115 the command on each execution, generating a plan that is specific
1116 to the current parameter values; whereas
1117 <application>PL/pgSQL</application> normally creates a generic plan
1118 and caches it for re-use. In situations where the best plan depends
1119 strongly on the parameter values, <command>EXECUTE</> can be
1120 significantly faster; while when the plan is not sensitive to parameter
1121 values, re-planning will be a waste.
1122 </para>
1124 <para>
1125 <command>SELECT INTO</command> is not currently supported within
1126 <command>EXECUTE</command>; instead, execute a plain <command>SELECT</>
1127 command and specify <literal>INTO</> as part of the <command>EXECUTE</>
1128 itself.
1129 </para>
1131 <note>
1132 <para>
1133 The <application>PL/pgSQL</application>
1134 <command>EXECUTE</command> statement is not related to the
1135 <xref linkend="sql-execute" endterm="sql-execute-title"> SQL
1136 statement supported by the
1137 <productname>PostgreSQL</productname> server. The server's
1138 <command>EXECUTE</command> statement cannot be used directly within
1139 <application>PL/pgSQL</> functions (and is not needed).
1140 </para>
1141 </note>
1143 <example id="plpgsql-quote-literal-example">
1144 <title>Quoting values in dynamic queries</title>
1146 <indexterm>
1147 <primary>quote_ident</primary>
1148 <secondary>use in PL/PgSQL</secondary>
1149 </indexterm>
1151 <indexterm>
1152 <primary>quote_literal</primary>
1153 <secondary>use in PL/PgSQL</secondary>
1154 </indexterm>
1156 <indexterm>
1157 <primary>quote_nullable</primary>
1158 <secondary>use in PL/PgSQL</secondary>
1159 </indexterm>
1161 <para>
1162 When working with dynamic commands you will often have to handle escaping
1163 of single quotes. The recommended method for quoting fixed text in your
1164 function body is dollar quoting. (If you have legacy code that does
1165 not use dollar quoting, please refer to the
1166 overview in <xref linkend="plpgsql-quote-tips">, which can save you
1167 some effort when translating said code to a more reasonable scheme.)
1168 </para>
1170 <para>
1171 Dynamic values that are to be inserted into the constructed
1172 query require careful handling since they might themselves contain
1173 quote characters.
1174 An example (this assumes that you are using dollar quoting for the
1175 function as a whole, so the quote marks need not be doubled):
1176 <programlisting>
1177 EXECUTE 'UPDATE tbl SET '
1178 || quote_ident(colname)
1179 || ' = '
1180 || quote_literal(newvalue)
1181 || ' WHERE key = '
1182 || quote_literal(keyvalue);
1183 </programlisting>
1184 </para>
1186 <para>
1187 This example demonstrates the use of the
1188 <function>quote_ident</function> and
1189 <function>quote_literal</function> functions (see <xref
1190 linkend="functions-string">). For safety, expressions containing column
1191 or table identifiers should be passed through
1192 <function>quote_ident</function> before insertion in a dynamic query.
1193 Expressions containing values that should be literal strings in the
1194 constructed command should be passed through <function>quote_literal</>.
1195 These functions take the appropriate steps to return the input text
1196 enclosed in double or single quotes respectively, with any embedded
1197 special characters properly escaped.
1198 </para>
1200 <para>
1201 Because <function>quote_literal</function> is labelled
1202 <literal>STRICT</literal>, it will always return null when called with a
1203 null argument. In the above example, if <literal>newvalue</> or
1204 <literal>keyvalue</> were null, the entire dynamic query string would
1205 become null, leading to an error from <command>EXECUTE</command>.
1206 You can avoid this problem by using the <function>quote_nullable</>
1207 function, which works the same as <function>quote_literal</> except that
1208 when called with a null argument it returns the string <literal>NULL</>.
1209 For example,
1210 <programlisting>
1211 EXECUTE 'UPDATE tbl SET '
1212 || quote_ident(colname)
1213 || ' = '
1214 || quote_nullable(newvalue)
1215 || ' WHERE key = '
1216 || quote_nullable(keyvalue);
1217 </programlisting>
1218 If you are dealing with values that might be null, you should usually
1219 use <function>quote_nullable</> in place of <function>quote_literal</>.
1220 </para>
1222 <para>
1223 As always, care must be taken to ensure that null values in a query do
1224 not deliver unintended results. For example the <literal>WHERE</> clause
1225 <programlisting>
1226 'WHERE key = ' || quote_nullable(keyvalue)
1227 </programlisting>
1228 will never succeed if <literal>keyvalue</> is null, because the
1229 result of using the equality operator <literal>=</> with a null operand
1230 is always null. If you wish null to work like an ordinary key value,
1231 you would need to rewrite the above as
1232 <programlisting>
1233 'WHERE key IS NOT DISTINCT FROM ' || quote_nullable(keyvalue)
1234 </programlisting>
1235 (At present, <literal>IS NOT DISTINCT FROM</> is handled much less
1236 efficiently than <literal>=</>, so don't do this unless you must.
1237 See <xref linkend="functions-comparison"> for
1238 more information on nulls and <literal>IS DISTINCT</>.)
1239 </para>
1241 <para>
1242 Note that dollar quoting is only useful for quoting fixed text.
1243 It would be a very bad idea to try to write this example as:
1244 <programlisting>
1245 EXECUTE 'UPDATE tbl SET '
1246 || quote_ident(colname)
1247 || ' = $$'
1248 || newvalue
1249 || '$$ WHERE key = '
1250 || quote_literal(keyvalue);
1251 </programlisting>
1252 because it would break if the contents of <literal>newvalue</>
1253 happened to contain <literal>$$</>. The same objection would
1254 apply to any other dollar-quoting delimiter you might pick.
1255 So, to safely quote text that is not known in advance, you
1256 <emphasis>must</> use <function>quote_literal</>,
1257 <function>quote_nullable</>, or <function>quote_ident</>, as appropriate.
1258 </para>
1259 </example>
1261 <para>
1262 A much larger example of a dynamic command and
1263 <command>EXECUTE</command> can be seen in <xref
1264 linkend="plpgsql-porting-ex2">, which builds and executes a
1265 <command>CREATE FUNCTION</> command to define a new function.
1266 </para>
1267 </sect2>
1269 <sect2 id="plpgsql-statements-diagnostics">
1270 <title>Obtaining the Result Status</title>
1272 <para>
1273 There are several ways to determine the effect of a command. The
1274 first method is to use the <command>GET DIAGNOSTICS</command>
1275 command, which has the form:
1277 <synopsis>
1278 GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replaceable> <optional> , ... </optional>;
1279 </synopsis>
1281 This command allows retrieval of system status indicators. Each
1282 <replaceable>item</replaceable> is a key word identifying a state
1283 value to be assigned to the specified variable (which should be
1284 of the right data type to receive it). The currently available
1285 status items are <varname>ROW_COUNT</>, the number of rows
1286 processed by the last <acronym>SQL</acronym> command sent to
1287 the <acronym>SQL</acronym> engine, and <varname>RESULT_OID</>,
1288 the OID of the last row inserted by the most recent
1289 <acronym>SQL</acronym> command. Note that <varname>RESULT_OID</>
1290 is only useful after an <command>INSERT</command> command into a
1291 table containing OIDs.
1292 </para>
1294 <para>
1295 An example:
1296 <programlisting>
1297 GET DIAGNOSTICS integer_var = ROW_COUNT;
1298 </programlisting>
1299 </para>
1301 <para>
1302 The second method to determine the effects of a command is to check the
1303 special variable named <literal>FOUND</literal>, which is of
1304 type <type>boolean</type>. <literal>FOUND</literal> starts out
1305 false within each <application>PL/pgSQL</application> function call.
1306 It is set by each of the following types of statements:
1307 <itemizedlist>
1308 <listitem>
1309 <para>
1310 A <command>SELECT INTO</command> statement sets
1311 <literal>FOUND</literal> true if a row is assigned, false if no
1312 row is returned.
1313 </para>
1314 </listitem>
1315 <listitem>
1316 <para>
1317 A <command>PERFORM</> statement sets <literal>FOUND</literal>
1318 true if it produces (and discards) one or more rows, false if
1319 no row is produced.
1320 </para>
1321 </listitem>
1322 <listitem>
1323 <para>
1324 <command>UPDATE</>, <command>INSERT</>, and <command>DELETE</>
1325 statements set <literal>FOUND</literal> true if at least one
1326 row is affected, false if no row is affected.
1327 </para>
1328 </listitem>
1329 <listitem>
1330 <para>
1331 A <command>FETCH</> statement sets <literal>FOUND</literal>
1332 true if it returns a row, false if no row is returned.
1333 </para>
1334 </listitem>
1335 <listitem>
1336 <para>
1337 A <command>MOVE</> statement sets <literal>FOUND</literal>
1338 true if it successfully repositions the cursor, false otherwise.
1339 </para>
1340 </listitem>
1342 <listitem>
1343 <para>
1344 A <command>FOR</> statement sets <literal>FOUND</literal> true
1345 if it iterates one or more times, else false. This applies to
1346 all four variants of the <command>FOR</> statement (integer
1347 <command>FOR</> loops, record-set <command>FOR</> loops,
1348 dynamic record-set <command>FOR</> loops, and cursor
1349 <command>FOR</> loops).
1350 <literal>FOUND</literal> is set this way when the
1351 <command>FOR</> loop exits; inside the execution of the loop,
1352 <literal>FOUND</literal> is not modified by the
1353 <command>FOR</> statement, although it might be changed by the
1354 execution of other statements within the loop body.
1355 </para>
1356 </listitem>
1357 <listitem>
1358 <para>
1359 A <command>RETURN QUERY</command> and <command>RETURN QUERY
1360 EXECUTE</command> statements set <literal>FOUND</literal>
1361 true if the query returns at least one row, false if no row
1362 is returned.
1363 </para>
1364 </listitem>
1365 </itemizedlist>
1367 <literal>FOUND</literal> is a local variable within each
1368 <application>PL/pgSQL</application> function; any changes to it
1369 affect only the current function.
1370 </para>
1372 </sect2>
1374 <sect2 id="plpgsql-statements-null">
1375 <title>Doing Nothing At All</title>
1377 <para>
1378 Sometimes a placeholder statement that does nothing is useful.
1379 For example, it can indicate that one arm of an if/then/else
1380 chain is deliberately empty. For this purpose, use the
1381 <command>NULL</command> statement:
1383 <synopsis>
1384 NULL;
1385 </synopsis>
1386 </para>
1388 <para>
1389 For example, the following two fragments of code are equivalent:
1390 <programlisting>
1391 BEGIN
1392 y := x / 0;
1393 EXCEPTION
1394 WHEN division_by_zero THEN
1395 NULL; -- ignore the error
1396 END;
1397 </programlisting>
1399 <programlisting>
1400 BEGIN
1401 y := x / 0;
1402 EXCEPTION
1403 WHEN division_by_zero THEN -- ignore the error
1404 END;
1405 </programlisting>
1406 Which is preferable is a matter of taste.
1407 </para>
1409 <note>
1410 <para>
1411 In Oracle's PL/SQL, empty statement lists are not allowed, and so
1412 <command>NULL</> statements are <emphasis>required</> for situations
1413 such as this. <application>PL/pgSQL</application> allows you to
1414 just write nothing, instead.
1415 </para>
1416 </note>
1418 </sect2>
1419 </sect1>
1421 <sect1 id="plpgsql-control-structures">
1422 <title>Control Structures</title>
1424 <para>
1425 Control structures are probably the most useful (and
1426 important) part of <application>PL/pgSQL</>. With
1427 <application>PL/pgSQL</>'s control structures,
1428 you can manipulate <productname>PostgreSQL</> data in a very
1429 flexible and powerful way.
1430 </para>
1432 <sect2 id="plpgsql-statements-returning">
1433 <title>Returning From a Function</title>
1435 <para>
1436 There are two commands available that allow you to return data
1437 from a function: <command>RETURN</command> and <command>RETURN
1438 NEXT</command>.
1439 </para>
1441 <sect3>
1442 <title><command>RETURN</></title>
1444 <synopsis>
1445 RETURN <replaceable>expression</replaceable>;
1446 </synopsis>
1448 <para>
1449 <command>RETURN</command> with an expression terminates the
1450 function and returns the value of
1451 <replaceable>expression</replaceable> to the caller. This form
1452 is to be used for <application>PL/pgSQL</> functions that do
1453 not return a set.
1454 </para>
1456 <para>
1457 When returning a scalar type, any expression can be used. The
1458 expression's result will be automatically cast into the
1459 function's return type as described for assignments. To return a
1460 composite (row) value, you must write a record or row variable
1461 as the <replaceable>expression</replaceable>.
1462 </para>
1464 <para>
1465 If you declared the function with output parameters, write just
1466 <command>RETURN</command> with no expression. The current values
1467 of the output parameter variables will be returned.
1468 </para>
1470 <para>
1471 If you declared the function to return <type>void</type>, a
1472 <command>RETURN</command> statement can be used to exit the function
1473 early; but do not write an expression following
1474 <command>RETURN</command>.
1475 </para>
1477 <para>
1478 The return value of a function cannot be left undefined. If
1479 control reaches the end of the top-level block of the function
1480 without hitting a <command>RETURN</command> statement, a run-time
1481 error will occur. This restriction does not apply to functions
1482 with output parameters and functions returning <type>void</type>,
1483 however. In those cases a <command>RETURN</command> statement is
1484 automatically executed if the top-level block finishes.
1485 </para>
1486 </sect3>
1488 <sect3>
1489 <title><command>RETURN NEXT</> and <command>RETURN QUERY</command></title>
1490 <indexterm>
1491 <primary>RETURN NEXT</primary>
1492 <secondary>in PL/PgSQL</secondary>
1493 </indexterm>
1494 <indexterm>
1495 <primary>RETURN QUERY</primary>
1496 <secondary>in PL/PgSQL</secondary>
1497 </indexterm>
1499 <synopsis>
1500 RETURN NEXT <replaceable>expression</replaceable>;
1501 RETURN QUERY <replaceable>query</replaceable>;
1502 RETURN QUERY EXECUTE <replaceable class="command">command-string</replaceable> <optional> USING <replaceable>expression</replaceable> <optional>, ...</optional> </optional>;
1503 </synopsis>
1505 <para>
1506 When a <application>PL/pgSQL</> function is declared to return
1507 <literal>SETOF <replaceable>sometype</></literal>, the procedure
1508 to follow is slightly different. In that case, the individual
1509 items to return are specified by a sequence of <command>RETURN
1510 NEXT</command> or <command>RETURN QUERY</command> commands, and
1511 then a final <command>RETURN</command> command with no argument
1512 is used to indicate that the function has finished executing.
1513 <command>RETURN NEXT</command> can be used with both scalar and
1514 composite data types; with a composite result type, an entire
1515 <quote>table</quote> of results will be returned.
1516 <command>RETURN QUERY</command> appends the results of executing
1517 a query to the function's result set. <command>RETURN
1518 NEXT</command> and <command>RETURN QUERY</command> can be freely
1519 intermixed in a single set-returning function, in which case
1520 their results will be concatenated.
1521 </para>
1523 <para>
1524 <command>RETURN NEXT</command> and <command>RETURN
1525 QUERY</command> do not actually return from the function &mdash;
1526 they simply append zero or more rows to the function's result
1527 set. Execution then continues with the next statement in the
1528 <application>PL/pgSQL</> function. As successive
1529 <command>RETURN NEXT</command> or <command>RETURN
1530 QUERY</command> commands are executed, the result set is built
1531 up. A final <command>RETURN</command>, which should have no
1532 argument, causes control to exit the function (or you can just
1533 let control reach the end of the function).
1534 </para>
1536 <para>
1537 <command>RETURN QUERY</command> has a variant
1538 <command>RETURN QUERY EXECUTE</command>, which specifies the
1539 query to be executed dynamically. Parameter expressions can
1540 be inserted into the computed query string via <literal>USING</>,
1541 in just the same way as in the <command>EXECUTE</> command.
1542 </para>
1544 <para>
1545 If you declared the function with output parameters, write just
1546 <command>RETURN NEXT</command> with no expression. On each
1547 execution, the current values of the output parameter
1548 variable(s) will be saved for eventual return as a row of the
1549 result. Note that you must declare the function as returning
1550 <literal>SETOF record</literal> when there are multiple output
1551 parameters, or <literal>SETOF <replaceable>sometype</></literal>
1552 when there is just one output parameter of type
1553 <replaceable>sometype</>, in order to create a set-returning
1554 function with output parameters.
1555 </para>
1557 <para>
1558 Here is an example of a function using <command>RETURN
1559 NEXT</command>:
1561 <programlisting>
1562 CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT);
1563 INSERT INTO foo VALUES (1, 2, 'three');
1564 INSERT INTO foo VALUES (4, 5, 'six');
1566 CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS
1567 $BODY$
1568 DECLARE
1569 r foo%rowtype;
1570 BEGIN
1571 FOR r IN SELECT * FROM foo
1572 WHERE fooid &gt; 0
1573 LOOP
1574 -- can do some processing here
1575 RETURN NEXT r; -- return current row of SELECT
1576 END LOOP;
1577 RETURN;
1579 $BODY$
1580 LANGUAGE 'plpgsql' ;
1582 SELECT * FROM getallfoo();
1583 </programlisting>
1584 </para>
1586 <note>
1587 <para>
1588 The current implementation of <command>RETURN NEXT</command>
1589 and <command>RETURN QUERY</command> stores the entire result set
1590 before returning from the function, as discussed above. That
1591 means that if a <application>PL/pgSQL</> function produces a
1592 very large result set, performance might be poor: data will be
1593 written to disk to avoid memory exhaustion, but the function
1594 itself will not return until the entire result set has been
1595 generated. A future version of <application>PL/pgSQL</> might
1596 allow users to define set-returning functions
1597 that do not have this limitation. Currently, the point at
1598 which data begins being written to disk is controlled by the
1599 <xref linkend="guc-work-mem">
1600 configuration variable. Administrators who have sufficient
1601 memory to store larger result sets in memory should consider
1602 increasing this parameter.
1603 </para>
1604 </note>
1605 </sect3>
1606 </sect2>
1608 <sect2 id="plpgsql-conditionals">
1609 <title>Conditionals</title>
1611 <para>
1612 <command>IF</> and <command>CASE</> statements let you execute
1613 alternative commands based on certain conditions.
1614 <application>PL/pgSQL</> has three forms of <command>IF</>:
1615 <itemizedlist>
1616 <listitem>
1617 <para><literal>IF ... THEN</></>
1618 </listitem>
1619 <listitem>
1620 <para><literal>IF ... THEN ... ELSE</></>
1621 </listitem>
1622 <listitem>
1623 <para><literal>IF ... THEN ... ELSIF ... THEN ... ELSE</></>
1624 </listitem>
1625 </itemizedlist>
1627 and two forms of <command>CASE</>:
1628 <itemizedlist>
1629 <listitem>
1630 <para><literal>CASE ... WHEN ... THEN ... ELSE ... END CASE</></>
1631 </listitem>
1632 <listitem>
1633 <para><literal>CASE WHEN ... THEN ... ELSE ... END CASE</></>
1634 </listitem>
1635 </itemizedlist>
1636 </para>
1638 <sect3>
1639 <title><literal>IF-THEN</></title>
1641 <synopsis>
1642 IF <replaceable>boolean-expression</replaceable> THEN
1643 <replaceable>statements</replaceable>
1644 END IF;
1645 </synopsis>
1647 <para>
1648 <literal>IF-THEN</literal> statements are the simplest form of
1649 <literal>IF</literal>. The statements between
1650 <literal>THEN</literal> and <literal>END IF</literal> will be
1651 executed if the condition is true. Otherwise, they are
1652 skipped.
1653 </para>
1655 <para>
1656 Example:
1657 <programlisting>
1658 IF v_user_id &lt;&gt; 0 THEN
1659 UPDATE users SET email = v_email WHERE user_id = v_user_id;
1660 END IF;
1661 </programlisting>
1662 </para>
1663 </sect3>
1665 <sect3>
1666 <title><literal>IF-THEN-ELSE</></title>
1668 <synopsis>
1669 IF <replaceable>boolean-expression</replaceable> THEN
1670 <replaceable>statements</replaceable>
1671 ELSE
1672 <replaceable>statements</replaceable>
1673 END IF;
1674 </synopsis>
1676 <para>
1677 <literal>IF-THEN-ELSE</literal> statements add to
1678 <literal>IF-THEN</literal> by letting you specify an
1679 alternative set of statements that should be executed if the
1680 condition is not true. (Note this includes the case where the
1681 condition evaluates to NULL.)
1682 </para>
1684 <para>
1685 Examples:
1686 <programlisting>
1687 IF parentid IS NULL OR parentid = ''
1688 THEN
1689 RETURN fullname;
1690 ELSE
1691 RETURN hp_true_filename(parentid) || '/' || fullname;
1692 END IF;
1693 </programlisting>
1695 <programlisting>
1696 IF v_count &gt; 0 THEN
1697 INSERT INTO users_count (count) VALUES (v_count);
1698 RETURN 't';
1699 ELSE
1700 RETURN 'f';
1701 END IF;
1702 </programlisting>
1703 </para>
1704 </sect3>
1706 <sect3>
1707 <title><literal>IF-THEN-ELSIF</></title>
1709 <synopsis>
1710 IF <replaceable>boolean-expression</replaceable> THEN
1711 <replaceable>statements</replaceable>
1712 <optional> ELSIF <replaceable>boolean-expression</replaceable> THEN
1713 <replaceable>statements</replaceable>
1714 <optional> ELSIF <replaceable>boolean-expression</replaceable> THEN
1715 <replaceable>statements</replaceable>
1717 </optional>
1718 </optional>
1719 <optional> ELSE
1720 <replaceable>statements</replaceable> </optional>
1721 END IF;
1722 </synopsis>
1724 <para>
1725 Sometimes there are more than just two alternatives.
1726 <literal>IF-THEN-ELSIF</> provides a convenient
1727 method of checking several alternatives in turn.
1728 The <literal>IF</> conditions are tested successively
1729 until the first one that is true is found. Then the
1730 associated statement(s) are executed, after which control
1731 passes to the next statement after <literal>END IF</>.
1732 (Any subsequent <literal>IF</> conditions are <emphasis>not</>
1733 tested.) If none of the <literal>IF</> conditions is true,
1734 then the <literal>ELSE</> block (if any) is executed.
1735 </para>
1737 <para>
1738 Here is an example:
1740 <programlisting>
1741 IF number = 0 THEN
1742 result := 'zero';
1743 ELSIF number &gt; 0 THEN
1744 result := 'positive';
1745 ELSIF number &lt; 0 THEN
1746 result := 'negative';
1747 ELSE
1748 -- hmm, the only other possibility is that number is null
1749 result := 'NULL';
1750 END IF;
1751 </programlisting>
1752 </para>
1754 <para>
1755 The key word <literal>ELSIF</> can also be spelled
1756 <literal>ELSEIF</>.
1757 </para>
1759 <para>
1760 An alternative way of accomplishing the same task is to nest
1761 <literal>IF-THEN-ELSE</literal> statements, as in the
1762 following example:
1764 <programlisting>
1765 IF demo_row.sex = 'm' THEN
1766 pretty_sex := 'man';
1767 ELSE
1768 IF demo_row.sex = 'f' THEN
1769 pretty_sex := 'woman';
1770 END IF;
1771 END IF;
1772 </programlisting>
1773 </para>
1775 <para>
1776 However, this method requires writing a matching <literal>END IF</>
1777 for each <literal>IF</>, so it is much more cumbersome than
1778 using <literal>ELSIF</> when there are many alternatives.
1779 </para>
1780 </sect3>
1782 <sect3>
1783 <title>Simple <literal>CASE</></title>
1785 <synopsis>
1786 CASE <replaceable>search-expression</replaceable>
1787 WHEN <replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> <optional> ... </optional></optional> THEN
1788 <replaceable>statements</replaceable>
1789 <optional> WHEN <replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> <optional> ... </optional></optional> THEN
1790 <replaceable>statements</replaceable>
1791 ... </optional>
1792 <optional> ELSE
1793 <replaceable>statements</replaceable> </optional>
1794 END CASE;
1795 </synopsis>
1797 <para>
1798 The simple form of <command>CASE</> provides conditional execution
1799 based on equality of operands. The <replaceable>search-expression</>
1800 is evaluated (once) and successively compared to each
1801 <replaceable>expression</> in the <literal>WHEN</> clauses.
1802 If a match is found, then the corresponding
1803 <replaceable>statements</replaceable> are executed, and then control
1804 passes to the next statement after <literal>END CASE</>. (Subsequent
1805 <literal>WHEN</> expressions are not evaluated.) If no match is
1806 found, the <literal>ELSE</> <replaceable>statements</replaceable> are
1807 executed; but if <literal>ELSE</> is not present, then a
1808 <literal>CASE_NOT_FOUND</literal> exception is raised.
1809 </para>
1811 <para>
1812 Here is a simple example:
1814 <programlisting>
1815 CASE x
1816 WHEN 1, 2 THEN
1817 msg := 'one or two';
1818 ELSE
1819 msg := 'other value than one or two';
1820 END CASE;
1821 </programlisting>
1822 </para>
1823 </sect3>
1825 <sect3>
1826 <title>Searched <literal>CASE</></title>
1828 <synopsis>
1829 CASE
1830 WHEN <replaceable>boolean-expression</replaceable> THEN
1831 <replaceable>statements</replaceable>
1832 <optional> WHEN <replaceable>boolean-expression</replaceable> THEN
1833 <replaceable>statements</replaceable>
1834 ... </optional>
1835 <optional> ELSE
1836 <replaceable>statements</replaceable> </optional>
1837 END CASE;
1838 </synopsis>
1840 <para>
1841 The searched form of <command>CASE</> provides conditional execution
1842 based on truth of boolean expressions. Each <literal>WHEN</> clause's
1843 <replaceable>boolean-expression</replaceable> is evaluated in turn,
1844 until one is found that yields <literal>true</>. Then the
1845 corresponding <replaceable>statements</replaceable> are executed, and
1846 then control passes to the next statement after <literal>END CASE</>.
1847 (Subsequent <literal>WHEN</> expressions are not evaluated.)
1848 If no true result is found, the <literal>ELSE</>
1849 <replaceable>statements</replaceable> are executed;
1850 but if <literal>ELSE</> is not present, then a
1851 <literal>CASE_NOT_FOUND</literal> exception is raised.
1852 </para>
1854 <para>
1855 Here is an example:
1857 <programlisting>
1858 CASE
1859 WHEN x BETWEEN 0 AND 10 THEN
1860 msg := 'value is between zero and ten';
1861 WHEN x BETWEEN 11 AND 20 THEN
1862 msg := 'value is between eleven and twenty';
1863 END CASE;
1864 </programlisting>
1865 </para>
1867 <para>
1868 This form of <command>CASE</> is entirely equivalent to
1869 <literal>IF-THEN-ELSIF</>, except for the rule that reaching
1870 an omitted <literal>ELSE</> clause results in an error rather
1871 than doing nothing.
1872 </para>
1874 </sect3>
1875 </sect2>
1877 <sect2 id="plpgsql-control-structures-loops">
1878 <title>Simple Loops</title>
1880 <indexterm zone="plpgsql-control-structures-loops">
1881 <primary>loop</primary>
1882 <secondary>in PL/pgSQL</secondary>
1883 </indexterm>
1885 <para>
1886 With the <literal>LOOP</>, <literal>EXIT</>,
1887 <literal>CONTINUE</>, <literal>WHILE</>, and <literal>FOR</>
1888 statements, you can arrange for your <application>PL/pgSQL</>
1889 function to repeat a series of commands.
1890 </para>
1892 <sect3>
1893 <title><literal>LOOP</></title>
1895 <synopsis>
1896 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
1897 LOOP
1898 <replaceable>statements</replaceable>
1899 END LOOP <optional> <replaceable>label</replaceable> </optional>;
1900 </synopsis>
1902 <para>
1903 <literal>LOOP</> defines an unconditional loop that is repeated
1904 indefinitely until terminated by an <literal>EXIT</> or
1905 <command>RETURN</command> statement. The optional
1906 <replaceable>label</replaceable> can be used by <literal>EXIT</>
1907 and <literal>CONTINUE</literal> statements within nested loops to
1908 specify which loop those statements refer to.
1909 </para>
1910 </sect3>
1912 <sect3>
1913 <title><literal>EXIT</></title>
1915 <indexterm>
1916 <primary>EXIT</primary>
1917 <secondary>in PL/pgSQL</secondary>
1918 </indexterm>
1920 <synopsis>
1921 EXIT <optional> <replaceable>label</replaceable> </optional> <optional> WHEN <replaceable>boolean-expression</replaceable> </optional>;
1922 </synopsis>
1924 <para>
1925 If no <replaceable>label</replaceable> is given, the innermost
1926 loop is terminated and the statement following <literal>END
1927 LOOP</> is executed next. If <replaceable>label</replaceable>
1928 is given, it must be the label of the current or some outer
1929 level of nested loop or block. Then the named loop or block is
1930 terminated and control continues with the statement after the
1931 loop's/block's corresponding <literal>END</>.
1932 </para>
1934 <para>
1935 If <literal>WHEN</> is specified, the loop exit occurs only if
1936 <replaceable>boolean-expression</> is true. Otherwise, control passes
1937 to the statement after <literal>EXIT</>.
1938 </para>
1940 <para>
1941 <literal>EXIT</> can be used with all types of loops; it is
1942 not limited to use with unconditional loops.
1943 </para>
1945 <para>
1946 When used with a
1947 <literal>BEGIN</literal> block, <literal>EXIT</literal> passes
1948 control to the next statement after the end of the block.
1949 Note that a label must be used for this purpose; an unlabelled
1950 <literal>EXIT</literal> is never considered to match a
1951 <literal>BEGIN</literal> block. (This is a change from
1952 pre-8.4 releases of <productname>PostgreSQL</productname>, which
1953 would allow an unlabelled <literal>EXIT</literal> to match
1954 a <literal>BEGIN</literal> block.)
1955 </para>
1957 <para>
1958 Examples:
1959 <programlisting>
1960 LOOP
1961 -- some computations
1962 IF count &gt; 0 THEN
1963 EXIT; -- exit loop
1964 END IF;
1965 END LOOP;
1967 LOOP
1968 -- some computations
1969 EXIT WHEN count &gt; 0; -- same result as previous example
1970 END LOOP;
1972 &lt;&lt;ablock&gt;&gt;
1973 BEGIN
1974 -- some computations
1975 IF stocks &gt; 100000 THEN
1976 EXIT ablock; -- causes exit from the BEGIN block
1977 END IF;
1978 -- computations here will be skipped when stocks &gt; 100000
1979 END;
1980 </programlisting>
1981 </para>
1982 </sect3>
1984 <sect3>
1985 <title><literal>CONTINUE</></title>
1987 <indexterm>
1988 <primary>CONTINUE</primary>
1989 <secondary>in PL/pgSQL</secondary>
1990 </indexterm>
1992 <synopsis>
1993 CONTINUE <optional> <replaceable>label</replaceable> </optional> <optional> WHEN <replaceable>boolean-expression</replaceable> </optional>;
1994 </synopsis>
1996 <para>
1997 If no <replaceable>label</> is given, the next iteration of
1998 the innermost loop is begun. That is, all statements remaining
1999 in the loop body are skipped, and control returns
2000 to the loop control expression (if any) to determine whether
2001 another loop iteration is needed.
2002 If <replaceable>label</> is present, it
2003 specifies the label of the loop whose execution will be
2004 continued.
2005 </para>
2007 <para>
2008 If <literal>WHEN</> is specified, the next iteration of the
2009 loop is begun only if <replaceable>boolean-expression</> is
2010 true. Otherwise, control passes to the statement after
2011 <literal>CONTINUE</>.
2012 </para>
2014 <para>
2015 <literal>CONTINUE</> can be used with all types of loops; it
2016 is not limited to use with unconditional loops.
2017 </para>
2019 <para>
2020 Examples:
2021 <programlisting>
2022 LOOP
2023 -- some computations
2024 EXIT WHEN count &gt; 100;
2025 CONTINUE WHEN count &lt; 50;
2026 -- some computations for count IN [50 .. 100]
2027 END LOOP;
2028 </programlisting>
2029 </para>
2030 </sect3>
2033 <sect3>
2034 <title><literal>WHILE</></title>
2036 <indexterm>
2037 <primary>WHILE</primary>
2038 <secondary>in PL/pgSQL</secondary>
2039 </indexterm>
2041 <synopsis>
2042 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2043 WHILE <replaceable>boolean-expression</replaceable> LOOP
2044 <replaceable>statements</replaceable>
2045 END LOOP <optional> <replaceable>label</replaceable> </optional>;
2046 </synopsis>
2048 <para>
2049 The <literal>WHILE</> statement repeats a
2050 sequence of statements so long as the
2051 <replaceable>boolean-expression</replaceable>
2052 evaluates to true. The expression is checked just before
2053 each entry to the loop body.
2054 </para>
2056 <para>
2057 For example:
2058 <programlisting>
2059 WHILE amount_owed &gt; 0 AND gift_certificate_balance &gt; 0 LOOP
2060 -- some computations here
2061 END LOOP;
2063 WHILE NOT done LOOP
2064 -- some computations here
2065 END LOOP;
2066 </programlisting>
2067 </para>
2068 </sect3>
2070 <sect3 id="plpgsql-integer-for">
2071 <title><literal>FOR</> (integer variant)</title>
2073 <synopsis>
2074 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2075 FOR <replaceable>name</replaceable> IN <optional> REVERSE </optional> <replaceable>expression</replaceable> .. <replaceable>expression</replaceable> <optional> BY <replaceable>expression</replaceable> </optional> LOOP
2076 <replaceable>statements</replaceable>
2077 END LOOP <optional> <replaceable>label</replaceable> </optional>;
2078 </synopsis>
2080 <para>
2081 This form of <literal>FOR</> creates a loop that iterates over a range
2082 of integer values. The variable
2083 <replaceable>name</replaceable> is automatically defined as type
2084 <type>integer</> and exists only inside the loop (any existing
2085 definition of the variable name is ignored within the loop).
2086 The two expressions giving
2087 the lower and upper bound of the range are evaluated once when entering
2088 the loop. If the <literal>BY</> clause isn't specified the iteration
2089 step is 1, otherwise it's the value specified in the <literal>BY</>
2090 clause, which again is evaluated once on loop entry.
2091 If <literal>REVERSE</> is specified then the step value is
2092 subtracted, rather than added, after each iteration.
2093 </para>
2095 <para>
2096 Some examples of integer <literal>FOR</> loops:
2097 <programlisting>
2098 FOR i IN 1..10 LOOP
2099 -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
2100 END LOOP;
2102 FOR i IN REVERSE 10..1 LOOP
2103 -- i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loop
2104 END LOOP;
2106 FOR i IN REVERSE 10..1 BY 2 LOOP
2107 -- i will take on the values 10,8,6,4,2 within the loop
2108 END LOOP;
2109 </programlisting>
2110 </para>
2112 <para>
2113 If the lower bound is greater than the upper bound (or less than,
2114 in the <literal>REVERSE</> case), the loop body is not
2115 executed at all. No error is raised.
2116 </para>
2118 <para>
2119 If a <replaceable>label</replaceable> is attached to the
2120 <literal>FOR</> loop then the integer loop variable can be
2121 referenced with a qualified name, using that
2122 <replaceable>label</replaceable>.
2123 </para>
2124 </sect3>
2125 </sect2>
2127 <sect2 id="plpgsql-records-iterating">
2128 <title>Looping Through Query Results</title>
2130 <para>
2131 Using a different type of <literal>FOR</> loop, you can iterate through
2132 the results of a query and manipulate that data
2133 accordingly. The syntax is:
2134 <synopsis>
2135 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2136 FOR <replaceable>target</replaceable> IN <replaceable>query</replaceable> LOOP
2137 <replaceable>statements</replaceable>
2138 END LOOP <optional> <replaceable>label</replaceable> </optional>;
2139 </synopsis>
2140 The <replaceable>target</replaceable> is a record variable, row variable,
2141 or comma-separated list of scalar variables.
2142 The <replaceable>target</replaceable> is successively assigned each row
2143 resulting from the <replaceable>query</replaceable> and the loop body is
2144 executed for each row. Here is an example:
2145 <programlisting>
2146 CREATE FUNCTION cs_refresh_mviews() RETURNS integer AS $$
2147 DECLARE
2148 mviews RECORD;
2149 BEGIN
2150 PERFORM cs_log('Refreshing materialized views...');
2152 FOR mviews IN SELECT * FROM cs_materialized_views ORDER BY sort_key LOOP
2154 -- Now "mviews" has one record from cs_materialized_views
2156 PERFORM cs_log('Refreshing materialized view ' || quote_ident(mviews.mv_name) || ' ...');
2157 EXECUTE 'TRUNCATE TABLE ' || quote_ident(mviews.mv_name);
2158 EXECUTE 'INSERT INTO ' || quote_ident(mviews.mv_name) || ' ' || mviews.mv_query;
2159 END LOOP;
2161 PERFORM cs_log('Done refreshing materialized views.');
2162 RETURN 1;
2163 END;
2164 $$ LANGUAGE plpgsql;
2165 </programlisting>
2167 If the loop is terminated by an <literal>EXIT</> statement, the last
2168 assigned row value is still accessible after the loop.
2169 </para>
2171 <para>
2172 The <replaceable>query</replaceable> used in this type of <literal>FOR</>
2173 statement can be any SQL command that returns rows to the caller:
2174 <command>SELECT</> is the most common case,
2175 but you can also use <command>INSERT</>, <command>UPDATE</>, or
2176 <command>DELETE</> with a <literal>RETURNING</> clause. Some utility
2177 commands such as <command>EXPLAIN</> will work too.
2178 </para>
2180 <para>
2181 <application>PL/pgSQL</> variables are substituted into the query text,
2182 and the query plan is cached for possible re-use, as discussed in
2183 detail in <xref linkend="plpgsql-var-subst"> and
2184 <xref linkend="plpgsql-plan-caching">.
2185 </para>
2187 <para>
2188 The <literal>FOR-IN-EXECUTE</> statement is another way to iterate over
2189 rows:
2190 <synopsis>
2191 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2192 FOR <replaceable>target</replaceable> IN EXECUTE <replaceable>text_expression</replaceable> <optional> USING <replaceable>expression</replaceable> <optional>, ...</optional> </optional> LOOP
2193 <replaceable>statements</replaceable>
2194 END LOOP <optional> <replaceable>label</replaceable> </optional>;
2195 </synopsis>
2196 This is like the previous form, except that the source query
2197 is specified as a string expression, which is evaluated and replanned
2198 on each entry to the <literal>FOR</> loop. This allows the programmer to
2199 choose the speed of a preplanned query or the flexibility of a dynamic
2200 query, just as with a plain <command>EXECUTE</command> statement.
2201 As with <command>EXECUTE</command>, parameter values can be inserted
2202 into the dynamic command via <literal>USING</>.
2203 </para>
2205 <para>
2206 Another way to specify the query whose results should be iterated
2207 through is to declare it as a cursor. This is described in
2208 <xref linkend="plpgsql-cursor-for-loop">.
2209 </para>
2210 </sect2>
2212 <sect2 id="plpgsql-error-trapping">
2213 <title>Trapping Errors</title>
2215 <indexterm>
2216 <primary>exceptions</primary>
2217 <secondary>in PL/PgSQL</secondary>
2218 </indexterm>
2220 <para>
2221 By default, any error occurring in a <application>PL/pgSQL</>
2222 function aborts execution of the function, and indeed of the
2223 surrounding transaction as well. You can trap errors and recover
2224 from them by using a <command>BEGIN</> block with an
2225 <literal>EXCEPTION</> clause. The syntax is an extension of the
2226 normal syntax for a <command>BEGIN</> block:
2228 <synopsis>
2229 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2230 <optional> DECLARE
2231 <replaceable>declarations</replaceable> </optional>
2232 BEGIN
2233 <replaceable>statements</replaceable>
2234 EXCEPTION
2235 WHEN <replaceable>condition</replaceable> <optional> OR <replaceable>condition</replaceable> ... </optional> THEN
2236 <replaceable>handler_statements</replaceable>
2237 <optional> WHEN <replaceable>condition</replaceable> <optional> OR <replaceable>condition</replaceable> ... </optional> THEN
2238 <replaceable>handler_statements</replaceable>
2239 ... </optional>
2240 END;
2241 </synopsis>
2242 </para>
2244 <para>
2245 If no error occurs, this form of block simply executes all the
2246 <replaceable>statements</replaceable>, and then control passes
2247 to the next statement after <literal>END</>. But if an error
2248 occurs within the <replaceable>statements</replaceable>, further
2249 processing of the <replaceable>statements</replaceable> is
2250 abandoned, and control passes to the <literal>EXCEPTION</> list.
2251 The list is searched for the first <replaceable>condition</replaceable>
2252 matching the error that occurred. If a match is found, the
2253 corresponding <replaceable>handler_statements</replaceable> are
2254 executed, and then control passes to the next statement after
2255 <literal>END</>. If no match is found, the error propagates out
2256 as though the <literal>EXCEPTION</> clause were not there at all:
2257 the error can be caught by an enclosing block with
2258 <literal>EXCEPTION</>, or if there is none it aborts processing
2259 of the function.
2260 </para>
2262 <para>
2263 The <replaceable>condition</replaceable> names can be any of
2264 those shown in <xref linkend="errcodes-appendix">. A category
2265 name matches any error within its category. The special
2266 condition name <literal>OTHERS</> matches every error type except
2267 <literal>QUERY_CANCELED</>. (It is possible, but often unwise,
2268 to trap <literal>QUERY_CANCELED</> by name.) Condition names are
2269 not case-sensitive. Also, an error condition can be specified
2270 by <literal>SQLSTATE</> code; for example these are equivalent:
2271 <programlisting>
2272 WHEN division_by_zero THEN ...
2273 WHEN SQLSTATE '22012' THEN ...
2274 </programlisting>
2275 </para>
2277 <para>
2278 If a new error occurs within the selected
2279 <replaceable>handler_statements</replaceable>, it cannot be caught
2280 by this <literal>EXCEPTION</> clause, but is propagated out.
2281 A surrounding <literal>EXCEPTION</> clause could catch it.
2282 </para>
2284 <para>
2285 When an error is caught by an <literal>EXCEPTION</> clause,
2286 the local variables of the <application>PL/pgSQL</> function
2287 remain as they were when the error occurred, but all changes
2288 to persistent database state within the block are rolled back.
2289 As an example, consider this fragment:
2291 <programlisting>
2292 INSERT INTO mytab(firstname, lastname) VALUES('Tom', 'Jones');
2293 BEGIN
2294 UPDATE mytab SET firstname = 'Joe' WHERE lastname = 'Jones';
2295 x := x + 1;
2296 y := x / 0;
2297 EXCEPTION
2298 WHEN division_by_zero THEN
2299 RAISE NOTICE 'caught division_by_zero';
2300 RETURN x;
2301 END;
2302 </programlisting>
2304 When control reaches the assignment to <literal>y</>, it will
2305 fail with a <literal>division_by_zero</> error. This will be caught by
2306 the <literal>EXCEPTION</> clause. The value returned in the
2307 <command>RETURN</> statement will be the incremented value of
2308 <literal>x</>, but the effects of the <command>UPDATE</> command will
2309 have been rolled back. The <command>INSERT</> command preceding the
2310 block is not rolled back, however, so the end result is that the database
2311 contains <literal>Tom Jones</> not <literal>Joe Jones</>.
2312 </para>
2314 <tip>
2315 <para>
2316 A block containing an <literal>EXCEPTION</> clause is significantly
2317 more expensive to enter and exit than a block without one. Therefore,
2318 don't use <literal>EXCEPTION</> without need.
2319 </para>
2320 </tip>
2322 <para>
2323 Within an exception handler, the <varname>SQLSTATE</varname>
2324 variable contains the error code that corresponds to the
2325 exception that was raised (refer to <xref
2326 linkend="errcodes-table"> for a list of possible error
2327 codes). The <varname>SQLERRM</varname> variable contains the
2328 error message associated with the exception. These variables are
2329 undefined outside exception handlers.
2330 </para>
2332 <example id="plpgsql-upsert-example">
2333 <title>Exceptions with <command>UPDATE</>/<command>INSERT</></title>
2334 <para>
2336 This example uses exception handling to perform either
2337 <command>UPDATE</> or <command>INSERT</>, as appropriate:
2339 <programlisting>
2340 CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
2342 CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
2344 BEGIN
2345 LOOP
2346 -- first try to update the key
2347 UPDATE db SET b = data WHERE a = key;
2348 IF found THEN
2349 RETURN;
2350 END IF;
2351 -- not there, so try to insert the key
2352 -- if someone else inserts the same key concurrently,
2353 -- we could get a unique-key failure
2354 BEGIN
2355 INSERT INTO db(a,b) VALUES (key, data);
2356 RETURN;
2357 EXCEPTION WHEN unique_violation THEN
2358 -- do nothing, and loop to try the UPDATE again
2359 END;
2360 END LOOP;
2361 END;
2363 LANGUAGE plpgsql;
2365 SELECT merge_db(1, 'david');
2366 SELECT merge_db(1, 'dennis');
2367 </programlisting>
2369 </para>
2370 </example>
2371 </sect2>
2372 </sect1>
2374 <sect1 id="plpgsql-cursors">
2375 <title>Cursors</title>
2377 <indexterm zone="plpgsql-cursors">
2378 <primary>cursor</primary>
2379 <secondary>in PL/pgSQL</secondary>
2380 </indexterm>
2382 <para>
2383 Rather than executing a whole query at once, it is possible to set
2384 up a <firstterm>cursor</> that encapsulates the query, and then read
2385 the query result a few rows at a time. One reason for doing this is
2386 to avoid memory overrun when the result contains a large number of
2387 rows. (However, <application>PL/pgSQL</> users do not normally need
2388 to worry about that, since <literal>FOR</> loops automatically use a cursor
2389 internally to avoid memory problems.) A more interesting usage is to
2390 return a reference to a cursor that a function has created, allowing the
2391 caller to read the rows. This provides an efficient way to return
2392 large row sets from functions.
2393 </para>
2395 <sect2 id="plpgsql-cursor-declarations">
2396 <title>Declaring Cursor Variables</title>
2398 <para>
2399 All access to cursors in <application>PL/pgSQL</> goes through
2400 cursor variables, which are always of the special data type
2401 <type>refcursor</>. One way to create a cursor variable
2402 is just to declare it as a variable of type <type>refcursor</>.
2403 Another way is to use the cursor declaration syntax,
2404 which in general is:
2405 <synopsis>
2406 <replaceable>name</replaceable> <optional> <optional> NO </optional> SCROLL </optional> CURSOR <optional> ( <replaceable>arguments</replaceable> ) </optional> FOR <replaceable>query</replaceable>;
2407 </synopsis>
2408 (<literal>FOR</> can be replaced by <literal>IS</> for
2409 <productname>Oracle</productname> compatibility.)
2410 If <literal>SCROLL</> is specified, the cursor will be capable of
2411 scrolling backward; if <literal>NO SCROLL</> is specified, backward
2412 fetches will be rejected; if neither specification appears, it is
2413 query-dependent whether backward fetches will be allowed.
2414 <replaceable>arguments</replaceable>, if specified, is a
2415 comma-separated list of pairs <literal><replaceable>name</replaceable>
2416 <replaceable>datatype</replaceable></literal> that define names to be
2417 replaced by parameter values in the given query. The actual
2418 values to substitute for these names will be specified later,
2419 when the cursor is opened.
2420 </para>
2421 <para>
2422 Some examples:
2423 <programlisting>
2424 DECLARE
2425 curs1 refcursor;
2426 curs2 CURSOR FOR SELECT * FROM tenk1;
2427 curs3 CURSOR (key integer) IS SELECT * FROM tenk1 WHERE unique1 = key;
2428 </programlisting>
2429 All three of these variables have the data type <type>refcursor</>,
2430 but the first can be used with any query, while the second has
2431 a fully specified query already <firstterm>bound</> to it, and the last
2432 has a parameterized query bound to it. (<literal>key</> will be
2433 replaced by an integer parameter value when the cursor is opened.)
2434 The variable <literal>curs1</>
2435 is said to be <firstterm>unbound</> since it is not bound to
2436 any particular query.
2437 </para>
2438 </sect2>
2440 <sect2 id="plpgsql-cursor-opening">
2441 <title>Opening Cursors</title>
2443 <para>
2444 Before a cursor can be used to retrieve rows, it must be
2445 <firstterm>opened</>. (This is the equivalent action to the SQL
2446 command <command>DECLARE CURSOR</>.) <application>PL/pgSQL</> has
2447 three forms of the <command>OPEN</> statement, two of which use unbound
2448 cursor variables while the third uses a bound cursor variable.
2449 </para>
2451 <note>
2452 <para>
2453 Bound cursor variables can also be used without explicitly opening the cursor,
2454 via the <command>FOR</> statement described in
2455 <xref linkend="plpgsql-cursor-for-loop">.
2456 </para>
2457 </note>
2459 <sect3>
2460 <title><command>OPEN FOR</command> <replaceable>query</replaceable></title>
2462 <synopsis>
2463 OPEN <replaceable>unbound_cursorvar</replaceable> <optional> <optional> NO </optional> SCROLL </optional> FOR <replaceable>query</replaceable>;
2464 </synopsis>
2466 <para>
2467 The cursor variable is opened and given the specified query to
2468 execute. The cursor cannot be open already, and it must have been
2469 declared as an unbound cursor variable (that is, as a simple
2470 <type>refcursor</> variable). The query must be a
2471 <command>SELECT</command>, or something else that returns rows
2472 (such as <command>EXPLAIN</>). The query
2473 is treated in the same way as other SQL commands in
2474 <application>PL/pgSQL</>: <application>PL/pgSQL</>
2475 variable names are substituted, and the query plan is cached for
2476 possible reuse. When a <application>PL/pgSQL</>
2477 variable is substituted into the cursor query, the value that is
2478 substituted is the one it has at the time of the <command>OPEN</>;
2479 subsequent changes to the variable will not affect the cursor's
2480 behavior.
2481 The <literal>SCROLL</> and <literal>NO SCROLL</>
2482 options have the same meanings as for a bound cursor.
2483 </para>
2485 <para>
2486 An example:
2487 <programlisting>
2488 OPEN curs1 FOR SELECT * FROM foo WHERE key = mykey;
2489 </programlisting>
2490 </para>
2491 </sect3>
2493 <sect3>
2494 <title><command>OPEN FOR EXECUTE</command></title>
2496 <synopsis>
2497 OPEN <replaceable>unbound_cursorvar</replaceable> <optional> <optional> NO </optional> SCROLL </optional> FOR EXECUTE <replaceable class="command">query_string</replaceable>;
2498 </synopsis>
2500 <para>
2501 The cursor variable is opened and given the specified query to
2502 execute. The cursor cannot be open already, and it must have been
2503 declared as an unbound cursor variable (that is, as a simple
2504 <type>refcursor</> variable). The query is specified as a string
2505 expression, in the same way as in the <command>EXECUTE</command>
2506 command. As usual, this gives flexibility so the query plan can vary
2507 from one run to the next (see <xref linkend="plpgsql-plan-caching">),
2508 and it also means that variable substitution is not done on the
2509 command string.
2510 The <literal>SCROLL</> and
2511 <literal>NO SCROLL</> options have the same meanings as for a bound
2512 cursor.
2513 </para>
2515 <para>
2516 An example:
2517 <programlisting>
2518 OPEN curs1 FOR EXECUTE 'SELECT * FROM ' || quote_ident($1);
2519 </programlisting>
2520 </para>
2521 </sect3>
2523 <sect3>
2524 <title>Opening a Bound Cursor</title>
2526 <synopsis>
2527 OPEN <replaceable>bound_cursorvar</replaceable> <optional> ( <replaceable>argument_values</replaceable> ) </optional>;
2528 </synopsis>
2530 <para>
2531 This form of <command>OPEN</command> is used to open a cursor
2532 variable whose query was bound to it when it was declared. The
2533 cursor cannot be open already. A list of actual argument value
2534 expressions must appear if and only if the cursor was declared to
2535 take arguments. These values will be substituted in the query.
2536 The query plan for a bound cursor is always considered cacheable;
2537 there is no equivalent of <command>EXECUTE</command> in this case.
2538 Notice that <literal>SCROLL</> and
2539 <literal>NO SCROLL</> cannot be specified, as the cursor's scrolling
2540 behavior was already determined.
2541 </para>
2543 <para>
2544 Note that because variable substitution is done on the bound
2545 cursor's query, there are two ways to pass values into the cursor:
2546 either with an explicit argument to <command>OPEN</>, or
2547 implicitly by referencing a <application>PL/pgSQL</> variable
2548 in the query. However, only variables declared before the bound
2549 cursor was declared will be substituted into it. In either case
2550 the value to be passed is determined at the time of the
2551 <command>OPEN</>.
2552 </para>
2554 <para>
2555 Examples:
2556 <programlisting>
2557 OPEN curs2;
2558 OPEN curs3(42);
2559 </programlisting>
2560 </para>
2561 </sect3>
2562 </sect2>
2564 <sect2 id="plpgsql-cursor-using">
2565 <title>Using Cursors</title>
2567 <para>
2568 Once a cursor has been opened, it can be manipulated with the
2569 statements described here.
2570 </para>
2572 <para>
2573 These manipulations need not occur in the same function that
2574 opened the cursor to begin with. You can return a <type>refcursor</>
2575 value out of a function and let the caller operate on the cursor.
2576 (Internally, a <type>refcursor</> value is simply the string name
2577 of a so-called portal containing the active query for the cursor. This name
2578 can be passed around, assigned to other <type>refcursor</> variables,
2579 and so on, without disturbing the portal.)
2580 </para>
2582 <para>
2583 All portals are implicitly closed at transaction end. Therefore
2584 a <type>refcursor</> value is usable to reference an open cursor
2585 only until the end of the transaction.
2586 </para>
2588 <sect3>
2589 <title><literal>FETCH</></title>
2591 <synopsis>
2592 FETCH <optional> <replaceable>direction</replaceable> { FROM | IN } </optional> <replaceable>cursor</replaceable> INTO <replaceable>target</replaceable>;
2593 </synopsis>
2595 <para>
2596 <command>FETCH</command> retrieves the next row from the
2597 cursor into a target, which might be a row variable, a record
2598 variable, or a comma-separated list of simple variables, just like
2599 <command>SELECT INTO</command>. If there is no next row, the
2600 target is set to NULL(s). As with <command>SELECT
2601 INTO</command>, the special variable <literal>FOUND</literal> can
2602 be checked to see whether a row was obtained or not.
2603 </para>
2605 <para>
2606 The <replaceable>direction</replaceable> clause can be any of the
2607 variants allowed in the SQL <xref linkend="sql-fetch"
2608 endterm="sql-fetch-title"> command except the ones that can fetch
2609 more than one row; namely, it can be
2610 <literal>NEXT</>,
2611 <literal>PRIOR</>,
2612 <literal>FIRST</>,
2613 <literal>LAST</>,
2614 <literal>ABSOLUTE</> <replaceable>count</replaceable>,
2615 <literal>RELATIVE</> <replaceable>count</replaceable>,
2616 <literal>FORWARD</>, or
2617 <literal>BACKWARD</>.
2618 Omitting <replaceable>direction</replaceable> is the same
2619 as specifying <literal>NEXT</>.
2620 <replaceable>direction</replaceable> values that require moving
2621 backward are likely to fail unless the cursor was declared or opened
2622 with the <literal>SCROLL</> option.
2623 </para>
2625 <para>
2626 <replaceable>cursor</replaceable> must be the name of a <type>refcursor</>
2627 variable that references an open cursor portal.
2628 </para>
2630 <para>
2631 Examples:
2632 <programlisting>
2633 FETCH curs1 INTO rowvar;
2634 FETCH curs2 INTO foo, bar, baz;
2635 FETCH LAST FROM curs3 INTO x, y;
2636 FETCH RELATIVE -2 FROM curs4 INTO x;
2637 </programlisting>
2638 </para>
2639 </sect3>
2641 <sect3>
2642 <title><literal>MOVE</></title>
2644 <synopsis>
2645 MOVE <optional> <replaceable>direction</replaceable> { FROM | IN } </optional> <replaceable>cursor</replaceable>;
2646 </synopsis>
2648 <para>
2649 <command>MOVE</command> repositions a cursor without retrieving
2650 any data. <command>MOVE</command> works exactly like the
2651 <command>FETCH</command> command, except it only repositions the
2652 cursor and does not return the row moved to. As with <command>SELECT
2653 INTO</command>, the special variable <literal>FOUND</literal> can
2654 be checked to see whether there was a next row to move to.
2655 </para>
2657 <para>
2658 The options for the <replaceable>direction</replaceable> clause are
2659 the same as for <command>FETCH</>, namely
2660 <literal>NEXT</>,
2661 <literal>PRIOR</>,
2662 <literal>FIRST</>,
2663 <literal>LAST</>,
2664 <literal>ABSOLUTE</> <replaceable>count</replaceable>,
2665 <literal>RELATIVE</> <replaceable>count</replaceable>,
2666 <literal>FORWARD</>, or
2667 <literal>BACKWARD</>.
2668 Omitting <replaceable>direction</replaceable> is the same
2669 as specifying <literal>NEXT</>.
2670 <replaceable>direction</replaceable> values that require moving
2671 backward are likely to fail unless the cursor was declared or opened
2672 with the <literal>SCROLL</> option.
2673 </para>
2675 <para>
2676 Examples:
2677 <programlisting>
2678 MOVE curs1;
2679 MOVE LAST FROM curs3;
2680 MOVE RELATIVE -2 FROM curs4;
2681 </programlisting>
2682 </para>
2683 </sect3>
2685 <sect3>
2686 <title><literal>UPDATE/DELETE WHERE CURRENT OF</></title>
2688 <synopsis>
2689 UPDATE <replaceable>table</replaceable> SET ... WHERE CURRENT OF <replaceable>cursor</replaceable>;
2690 DELETE FROM <replaceable>table</replaceable> WHERE CURRENT OF <replaceable>cursor</replaceable>;
2691 </synopsis>
2693 <para>
2694 When a cursor is positioned on a table row, that row can be updated
2695 or deleted using the cursor to identify the row. There are
2696 restrictions on what the cursor's query can be (in particular,
2697 no grouping) and it's best to use <literal>FOR UPDATE</> in the
2698 cursor. For additional information see the
2699 <xref linkend="sql-declare" endterm="sql-declare-title">
2700 reference page.
2701 </para>
2703 <para>
2704 An example:
2705 <programlisting>
2706 UPDATE foo SET dataval = myval WHERE CURRENT OF curs1;
2707 </programlisting>
2708 </para>
2709 </sect3>
2711 <sect3>
2712 <title><literal>CLOSE</></title>
2714 <synopsis>
2715 CLOSE <replaceable>cursor</replaceable>;
2716 </synopsis>
2718 <para>
2719 <command>CLOSE</command> closes the portal underlying an open
2720 cursor. This can be used to release resources earlier than end of
2721 transaction, or to free up the cursor variable to be opened again.
2722 </para>
2724 <para>
2725 An example:
2726 <programlisting>
2727 CLOSE curs1;
2728 </programlisting>
2729 </para>
2730 </sect3>
2732 <sect3>
2733 <title>Returning Cursors</title>
2735 <para>
2736 <application>PL/pgSQL</> functions can return cursors to the
2737 caller. This is useful to return multiple rows or columns,
2738 especially with very large result sets. To do this, the function
2739 opens the cursor and returns the cursor name to the caller (or simply
2740 opens the cursor using a portal name specified by or otherwise known
2741 to the caller). The caller can then fetch rows from the cursor. The
2742 cursor can be closed by the caller, or it will be closed automatically
2743 when the transaction closes.
2744 </para>
2746 <para>
2747 The portal name used for a cursor can be specified by the
2748 programmer or automatically generated. To specify a portal name,
2749 simply assign a string to the <type>refcursor</> variable before
2750 opening it. The string value of the <type>refcursor</> variable
2751 will be used by <command>OPEN</> as the name of the underlying portal.
2752 However, if the <type>refcursor</> variable is null,
2753 <command>OPEN</> automatically generates a name that does not
2754 conflict with any existing portal, and assigns it to the
2755 <type>refcursor</> variable.
2756 </para>
2758 <note>
2759 <para>
2760 A bound cursor variable is initialized to the string value
2761 representing its name, so that the portal name is the same as
2762 the cursor variable name, unless the programmer overrides it
2763 by assignment before opening the cursor. But an unbound cursor
2764 variable defaults to the null value initially, so it will receive
2765 an automatically-generated unique name, unless overridden.
2766 </para>
2767 </note>
2769 <para>
2770 The following example shows one way a cursor name can be supplied by
2771 the caller:
2773 <programlisting>
2774 CREATE TABLE test (col text);
2775 INSERT INTO test VALUES ('123');
2777 CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS '
2778 BEGIN
2779 OPEN $1 FOR SELECT col FROM test;
2780 RETURN $1;
2781 END;
2782 ' LANGUAGE plpgsql;
2784 BEGIN;
2785 SELECT reffunc('funccursor');
2786 FETCH ALL IN funccursor;
2787 COMMIT;
2788 </programlisting>
2789 </para>
2791 <para>
2792 The following example uses automatic cursor name generation:
2794 <programlisting>
2795 CREATE FUNCTION reffunc2() RETURNS refcursor AS '
2796 DECLARE
2797 ref refcursor;
2798 BEGIN
2799 OPEN ref FOR SELECT col FROM test;
2800 RETURN ref;
2801 END;
2802 ' LANGUAGE plpgsql;
2804 BEGIN;
2805 SELECT reffunc2();
2807 reffunc2
2808 --------------------
2809 &lt;unnamed cursor 1&gt;
2810 (1 row)
2812 FETCH ALL IN "&lt;unnamed cursor 1&gt;";
2813 COMMIT;
2814 </programlisting>
2815 </para>
2817 <para>
2818 The following example shows one way to return multiple cursors
2819 from a single function:
2821 <programlisting>
2822 CREATE FUNCTION myfunc(refcursor, refcursor) RETURNS SETOF refcursor AS $$
2823 BEGIN
2824 OPEN $1 FOR SELECT * FROM table_1;
2825 RETURN NEXT $1;
2826 OPEN $2 FOR SELECT * FROM table_2;
2827 RETURN NEXT $2;
2828 END;
2829 $$ LANGUAGE plpgsql;
2831 -- need to be in a transaction to use cursors.
2832 BEGIN;
2834 SELECT * FROM myfunc('a', 'b');
2836 FETCH ALL FROM a;
2837 FETCH ALL FROM b;
2838 COMMIT;
2839 </programlisting>
2840 </para>
2841 </sect3>
2842 </sect2>
2844 <sect2 id="plpgsql-cursor-for-loop">
2845 <title>Looping Through a Cursor's Result</title>
2847 <para>
2848 There is a variant of the <command>FOR</> statement that allows
2849 iterating through the rows returned by a cursor. The syntax is:
2851 <synopsis>
2852 <optional> &lt;&lt;<replaceable>label</replaceable>&gt;&gt; </optional>
2853 FOR <replaceable>recordvar</replaceable> IN <replaceable>bound_cursorvar</replaceable> <optional> ( <replaceable>argument_values</replaceable> ) </optional> LOOP
2854 <replaceable>statements</replaceable>
2855 END LOOP <optional> <replaceable>label</replaceable> </optional>;
2856 </synopsis>
2858 The cursor variable must have been bound to some query when it was
2859 declared, and it <emphasis>cannot</> be open already. The
2860 <command>FOR</> statement automatically opens the cursor, and it closes
2861 the cursor again when the loop exits. A list of actual argument value
2862 expressions must appear if and only if the cursor was declared to take
2863 arguments. These values will be substituted in the query, in just
2864 the same way as during an <command>OPEN</>.
2865 The variable <replaceable>recordvar</replaceable> is automatically
2866 defined as type <type>record</> and exists only inside the loop (any
2867 existing definition of the variable name is ignored within the loop).
2868 Each row returned by the cursor is successively assigned to this
2869 record variable and the loop body is executed.
2870 </para>
2871 </sect2>
2873 </sect1>
2875 <sect1 id="plpgsql-errors-and-messages">
2876 <title>Errors and Messages</title>
2878 <indexterm>
2879 <primary>RAISE</primary>
2880 </indexterm>
2882 <indexterm>
2883 <primary>reporting errors</primary>
2884 <secondary>in PL/PgSQL</secondary>
2885 </indexterm>
2887 <para>
2888 Use the <command>RAISE</command> statement to report messages and
2889 raise errors.
2891 <synopsis>
2892 RAISE <optional> <replaceable class="parameter">level</replaceable> </optional> '<replaceable class="parameter">format</replaceable>' <optional>, <replaceable class="parameter">expression</replaceable> <optional>, ...</optional></optional> <optional> USING <replaceable class="parameter">option</replaceable> = <replaceable class="parameter">expression</replaceable> <optional>, ... </optional> </optional>;
2893 RAISE <optional> <replaceable class="parameter">level</replaceable> </optional> <replaceable class="parameter">condition_name</> <optional> USING <replaceable class="parameter">option</replaceable> = <replaceable class="parameter">expression</replaceable> <optional>, ... </optional> </optional>;
2894 RAISE <optional> <replaceable class="parameter">level</replaceable> </optional> SQLSTATE '<replaceable class="parameter">sqlstate</>' <optional> USING <replaceable class="parameter">option</replaceable> = <replaceable class="parameter">expression</replaceable> <optional>, ... </optional> </optional>;
2895 RAISE <optional> <replaceable class="parameter">level</replaceable> </optional> USING <replaceable class="parameter">option</replaceable> = <replaceable class="parameter">expression</replaceable> <optional>, ... </optional>;
2896 RAISE ;
2897 </synopsis>
2899 The <replaceable class="parameter">level</replaceable> option specifies
2900 the error severity. Allowed levels are <literal>DEBUG</literal>,
2901 <literal>LOG</literal>, <literal>INFO</literal>,
2902 <literal>NOTICE</literal>, <literal>WARNING</literal>,
2903 and <literal>EXCEPTION</literal>, with <literal>EXCEPTION</literal>
2904 being the default.
2905 <literal>EXCEPTION</literal> raises an error (which normally aborts the
2906 current transaction); the other levels only generate messages of different
2907 priority levels.
2908 Whether messages of a particular priority are reported to the client,
2909 written to the server log, or both is controlled by the
2910 <xref linkend="guc-log-min-messages"> and
2911 <xref linkend="guc-client-min-messages"> configuration
2912 variables. See <xref linkend="runtime-config"> for more
2913 information.
2914 </para>
2916 <para>
2917 After <replaceable class="parameter">level</replaceable> if any,
2918 you can write a <replaceable class="parameter">format</replaceable>
2919 (which must be a simple string literal, not an expression). The
2920 format string specifies the error message text to be reported.
2921 The format string can be followed
2922 by optional argument expressions to be inserted into the message.
2923 Inside the format string, <literal>%</literal> is replaced by the
2924 string representation of the next optional argument's value. Write
2925 <literal>%%</literal> to emit a literal <literal>%</literal>.
2926 </para>
2928 <para>
2929 In this example, the value of <literal>v_job_id</> will replace the
2930 <literal>%</literal> in the string:
2931 <programlisting>
2932 RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
2933 </programlisting>
2934 </para>
2936 <para>
2937 You can attach additional information to the error report by writing
2938 <literal>USING</> followed by <replaceable
2939 class="parameter">option</replaceable> = <replaceable
2940 class="parameter">expression</replaceable> items. The allowed
2941 <replaceable class="parameter">option</replaceable> keywords are
2942 <literal>MESSAGE</>, <literal>DETAIL</>, <literal>HINT</>, and
2943 <literal>ERRCODE</>, while each <replaceable
2944 class="parameter">expression</replaceable> can be any string-valued
2945 expression.
2946 <literal>MESSAGE</> sets the error message text (this option can't
2947 be used in the form of <command>RAISE</> that includes a format
2948 string before <literal>USING</>).
2949 <literal>DETAIL</> supplies an error detail message, while
2950 <literal>HINT</> supplies a hint message.
2951 <literal>ERRCODE</> specifies the error code (SQLSTATE) to report,
2952 either by condition name as shown in <xref linkend="errcodes-appendix">,
2953 or directly as a five-character SQLSTATE code.
2954 </para>
2956 <para>
2957 This example will abort the transaction with the given error message
2958 and hint:
2959 <programlisting>
2960 RAISE EXCEPTION 'Nonexistent ID --> %', user_id USING HINT = 'Please check your user id';
2961 </programlisting>
2962 </para>
2964 <para>
2965 These two examples show equivalent ways of setting the SQLSTATE:
2966 <programlisting>
2967 RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation';
2968 RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
2969 </programlisting>
2970 </para>
2972 <para>
2973 There is a second <command>RAISE</> syntax in which the main argument
2974 is the condition name or SQLSTATE to be reported, for example:
2975 <programlisting>
2976 RAISE division_by_zero;
2977 RAISE SQLSTATE '22012';
2978 </programlisting>
2979 In this syntax, <literal>USING</> can be used to supply a custom
2980 error message, detail, or hint. Another way to do the earlier
2981 example is
2982 <programlisting>
2983 RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
2984 </programlisting>
2985 </para>
2987 <para>
2988 Still another variant is to write <literal>RAISE USING</> or <literal>RAISE
2989 <replaceable class="parameter">level</replaceable> USING</> and put
2990 everything else into the <literal>USING</> list.
2991 </para>
2993 <para>
2994 The last variant of <command>RAISE</> has no parameters at all.
2995 This form can only be used inside a <literal>BEGIN</> block's
2996 <literal>EXCEPTION</> clause;
2997 it causes the error currently being handled to be re-thrown to the
2998 next enclosing block.
2999 </para>
3001 <para>
3002 If no condition name nor SQLSTATE is specified in a
3003 <command>RAISE EXCEPTION</command> command, the default is to use
3004 <literal>RAISE_EXCEPTION</> (<literal>P0001</>). If no message
3005 text is specified, the default is to use the condition name or
3006 SQLSTATE as message text.
3007 </para>
3009 <note>
3010 <para>
3011 When specifying an error code by SQLSTATE code, you are not
3012 limited to the predefined error codes, but can select any
3013 error code consisting of five digits and/or upper-case ASCII
3014 letters, other than <literal>00000</>. It is recommended that
3015 you avoid throwing error codes that end in three zeroes, because
3016 these are category codes and can only be trapped by trapping
3017 the whole category.
3018 </para>
3019 </note>
3021 </sect1>
3023 <sect1 id="plpgsql-trigger">
3024 <title>Trigger Procedures</title>
3026 <indexterm zone="plpgsql-trigger">
3027 <primary>trigger</primary>
3028 <secondary>in PL/pgSQL</secondary>
3029 </indexterm>
3031 <para>
3032 <application>PL/pgSQL</application> can be used to define trigger
3033 procedures. A trigger procedure is created with the
3034 <command>CREATE FUNCTION</> command, declaring it as a function with
3035 no arguments and a return type of <type>trigger</type>. Note that
3036 the function must be declared with no arguments even if it expects
3037 to receive arguments specified in <command>CREATE TRIGGER</> &mdash;
3038 trigger arguments are passed via <varname>TG_ARGV</>, as described
3039 below.
3040 </para>
3042 <para>
3043 When a <application>PL/pgSQL</application> function is called as a
3044 trigger, several special variables are created automatically in the
3045 top-level block. They are:
3047 <variablelist>
3048 <varlistentry>
3049 <term><varname>NEW</varname></term>
3050 <listitem>
3051 <para>
3052 Data type <type>RECORD</type>; variable holding the new
3053 database row for <command>INSERT</>/<command>UPDATE</> operations in row-level
3054 triggers. This variable is <symbol>NULL</symbol> in statement-level triggers
3055 and for <command>DELETE</command> operations.
3056 </para>
3057 </listitem>
3058 </varlistentry>
3060 <varlistentry>
3061 <term><varname>OLD</varname></term>
3062 <listitem>
3063 <para>
3064 Data type <type>RECORD</type>; variable holding the old
3065 database row for <command>UPDATE</>/<command>DELETE</> operations in row-level
3066 triggers. This variable is <symbol>NULL</symbol> in statement-level triggers
3067 and for <command>INSERT</command> operations.
3068 </para>
3069 </listitem>
3070 </varlistentry>
3072 <varlistentry>
3073 <term><varname>TG_NAME</varname></term>
3074 <listitem>
3075 <para>
3076 Data type <type>name</type>; variable that contains the name of the trigger actually
3077 fired.
3078 </para>
3079 </listitem>
3080 </varlistentry>
3082 <varlistentry>
3083 <term><varname>TG_WHEN</varname></term>
3084 <listitem>
3085 <para>
3086 Data type <type>text</type>; a string of either
3087 <literal>BEFORE</literal> or <literal>AFTER</literal>
3088 depending on the trigger's definition.
3089 </para>
3090 </listitem>
3091 </varlistentry>
3093 <varlistentry>
3094 <term><varname>TG_LEVEL</varname></term>
3095 <listitem>
3096 <para>
3097 Data type <type>text</type>; a string of either
3098 <literal>ROW</literal> or <literal>STATEMENT</literal>
3099 depending on the trigger's definition.
3100 </para>
3101 </listitem>
3102 </varlistentry>
3104 <varlistentry>
3105 <term><varname>TG_OP</varname></term>
3106 <listitem>
3107 <para>
3108 Data type <type>text</type>; a string of
3109 <literal>INSERT</literal>, <literal>UPDATE</literal>,
3110 <literal>DELETE</literal>, or <literal>TRUNCATE</>
3111 telling for which operation the trigger was fired.
3112 </para>
3113 </listitem>
3114 </varlistentry>
3116 <varlistentry>
3117 <term><varname>TG_RELID</varname></term>
3118 <listitem>
3119 <para>
3120 Data type <type>oid</type>; the object ID of the table that caused the
3121 trigger invocation.
3122 </para>
3123 </listitem>
3124 </varlistentry>
3126 <varlistentry>
3127 <term><varname>TG_RELNAME</varname></term>
3128 <listitem>
3129 <para>
3130 Data type <type>name</type>; the name of the table that caused the trigger
3131 invocation. This is now deprecated, and could disappear in a future
3132 release. Use <literal>TG_TABLE_NAME</> instead.
3133 </para>
3134 </listitem>
3135 </varlistentry>
3137 <varlistentry>
3138 <term><varname>TG_TABLE_NAME</varname></term>
3139 <listitem>
3140 <para>
3141 Data type <type>name</type>; the name of the table that
3142 caused the trigger invocation.
3143 </para>
3144 </listitem>
3145 </varlistentry>
3147 <varlistentry>
3148 <term><varname>TG_TABLE_SCHEMA</varname></term>
3149 <listitem>
3150 <para>
3151 Data type <type>name</type>; the name of the schema of the
3152 table that caused the trigger invocation.
3153 </para>
3154 </listitem>
3155 </varlistentry>
3157 <varlistentry>
3158 <term><varname>TG_NARGS</varname></term>
3159 <listitem>
3160 <para>
3161 Data type <type>integer</type>; the number of arguments given to the trigger
3162 procedure in the <command>CREATE TRIGGER</command> statement.
3163 </para>
3164 </listitem>
3165 </varlistentry>
3167 <varlistentry>
3168 <term><varname>TG_ARGV[]</varname></term>
3169 <listitem>
3170 <para>
3171 Data type array of <type>text</type>; the arguments from
3172 the <command>CREATE TRIGGER</command> statement.
3173 The index counts from 0. Invalid
3174 indices (less than 0 or greater than or equal to <varname>tg_nargs</>) result in a null value.
3175 </para>
3176 </listitem>
3177 </varlistentry>
3178 </variablelist>
3179 </para>
3181 <para>
3182 A trigger function must return either <symbol>NULL</symbol> or a
3183 record/row value having exactly the structure of the table the
3184 trigger was fired for.
3185 </para>
3187 <para>
3188 Row-level triggers fired <literal>BEFORE</> can return null to signal the
3189 trigger manager to skip the rest of the operation for this row
3190 (i.e., subsequent triggers are not fired, and the
3191 <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> does not occur
3192 for this row). If a nonnull
3193 value is returned then the operation proceeds with that row value.
3194 Returning a row value different from the original value
3195 of <varname>NEW</> alters the row that will be inserted or updated
3196 (but has no direct effect in the <command>DELETE</> case).
3197 To alter the row to be stored, it is possible to replace single values
3198 directly in <varname>NEW</> and return the modified <varname>NEW</>,
3199 or to build a complete new record/row to return.
3200 </para>
3202 <para>
3203 The return value of a <literal>BEFORE</> or <literal>AFTER</>
3204 statement-level trigger or an <literal>AFTER</> row-level trigger is
3205 always ignored; it might as well be null. However, any of these types of
3206 triggers might still abort the entire operation by raising an error.
3207 </para>
3209 <para>
3210 <xref linkend="plpgsql-trigger-example"> shows an example of a
3211 trigger procedure in <application>PL/pgSQL</application>.
3212 </para>
3214 <example id="plpgsql-trigger-example">
3215 <title>A <application>PL/pgSQL</application> Trigger Procedure</title>
3217 <para>
3218 This example trigger ensures that any time a row is inserted or updated
3219 in the table, the current user name and time are stamped into the
3220 row. And it checks that an employee's name is given and that the
3221 salary is a positive value.
3222 </para>
3224 <programlisting>
3225 CREATE TABLE emp (
3226 empname text,
3227 salary integer,
3228 last_date timestamp,
3229 last_user text
3232 CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
3233 BEGIN
3234 -- Check that empname and salary are given
3235 IF NEW.empname IS NULL THEN
3236 RAISE EXCEPTION 'empname cannot be null';
3237 END IF;
3238 IF NEW.salary IS NULL THEN
3239 RAISE EXCEPTION '% cannot have null salary', NEW.empname;
3240 END IF;
3242 -- Who works for us when she must pay for it?
3243 IF NEW.salary &lt; 0 THEN
3244 RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
3245 END IF;
3247 -- Remember who changed the payroll when
3248 NEW.last_date := current_timestamp;
3249 NEW.last_user := current_user;
3250 RETURN NEW;
3251 END;
3252 $emp_stamp$ LANGUAGE plpgsql;
3254 CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
3255 FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
3256 </programlisting>
3257 </example>
3259 <para>
3260 Another way to log changes to a table involves creating a new table that
3261 holds a row for each insert, update, or delete that occurs. This approach
3262 can be thought of as auditing changes to a table.
3263 <xref linkend="plpgsql-trigger-audit-example"> shows an example of an
3264 audit trigger procedure in <application>PL/pgSQL</application>.
3265 </para>
3267 <example id="plpgsql-trigger-audit-example">
3268 <title>A <application>PL/pgSQL</application> Trigger Procedure For Auditing</title>
3270 <para>
3271 This example trigger ensures that any insert, update or delete of a row
3272 in the <literal>emp</literal> table is recorded (i.e., audited) in the <literal>emp_audit</literal> table.
3273 The current time and user name are stamped into the row, together with
3274 the type of operation performed on it.
3275 </para>
3277 <programlisting>
3278 CREATE TABLE emp (
3279 empname text NOT NULL,
3280 salary integer
3283 CREATE TABLE emp_audit(
3284 operation char(1) NOT NULL,
3285 stamp timestamp NOT NULL,
3286 userid text NOT NULL,
3287 empname text NOT NULL,
3288 salary integer
3291 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
3292 BEGIN
3294 -- Create a row in emp_audit to reflect the operation performed on emp,
3295 -- make use of the special variable TG_OP to work out the operation.
3297 IF (TG_OP = 'DELETE') THEN
3298 INSERT INTO emp_audit SELECT 'D', now(), user, OLD.*;
3299 RETURN OLD;
3300 ELSIF (TG_OP = 'UPDATE') THEN
3301 INSERT INTO emp_audit SELECT 'U', now(), user, NEW.*;
3302 RETURN NEW;
3303 ELSIF (TG_OP = 'INSERT') THEN
3304 INSERT INTO emp_audit SELECT 'I', now(), user, NEW.*;
3305 RETURN NEW;
3306 END IF;
3307 RETURN NULL; -- result is ignored since this is an AFTER trigger
3308 END;
3309 $emp_audit$ LANGUAGE plpgsql;
3311 CREATE TRIGGER emp_audit
3312 AFTER INSERT OR UPDATE OR DELETE ON emp
3313 FOR EACH ROW EXECUTE PROCEDURE process_emp_audit();
3314 </programlisting>
3315 </example>
3317 <para>
3318 One use of triggers is to maintain a summary table
3319 of another table. The resulting summary can be used in place of the
3320 original table for certain queries &mdash; often with vastly reduced run
3321 times.
3322 This technique is commonly used in Data Warehousing, where the tables
3323 of measured or observed data (called fact tables) might be extremely large.
3324 <xref linkend="plpgsql-trigger-summary-example"> shows an example of a
3325 trigger procedure in <application>PL/pgSQL</application> that maintains
3326 a summary table for a fact table in a data warehouse.
3327 </para>
3330 <example id="plpgsql-trigger-summary-example">
3331 <title>A <application>PL/pgSQL</application> Trigger Procedure For Maintaining A Summary Table</title>
3333 <para>
3334 The schema detailed here is partly based on the <emphasis>Grocery Store
3335 </emphasis> example from <emphasis>The Data Warehouse Toolkit</emphasis>
3336 by Ralph Kimball.
3337 </para>
3339 <programlisting>
3341 -- Main tables - time dimension and sales fact.
3343 CREATE TABLE time_dimension (
3344 time_key integer NOT NULL,
3345 day_of_week integer NOT NULL,
3346 day_of_month integer NOT NULL,
3347 month integer NOT NULL,
3348 quarter integer NOT NULL,
3349 year integer NOT NULL
3351 CREATE UNIQUE INDEX time_dimension_key ON time_dimension(time_key);
3353 CREATE TABLE sales_fact (
3354 time_key integer NOT NULL,
3355 product_key integer NOT NULL,
3356 store_key integer NOT NULL,
3357 amount_sold numeric(12,2) NOT NULL,
3358 units_sold integer NOT NULL,
3359 amount_cost numeric(12,2) NOT NULL
3361 CREATE INDEX sales_fact_time ON sales_fact(time_key);
3364 -- Summary table - sales by time.
3366 CREATE TABLE sales_summary_bytime (
3367 time_key integer NOT NULL,
3368 amount_sold numeric(15,2) NOT NULL,
3369 units_sold numeric(12) NOT NULL,
3370 amount_cost numeric(15,2) NOT NULL
3372 CREATE UNIQUE INDEX sales_summary_bytime_key ON sales_summary_bytime(time_key);
3375 -- Function and trigger to amend summarized column(s) on UPDATE, INSERT, DELETE.
3377 CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER AS $maint_sales_summary_bytime$
3378 DECLARE
3379 delta_time_key integer;
3380 delta_amount_sold numeric(15,2);
3381 delta_units_sold numeric(12);
3382 delta_amount_cost numeric(15,2);
3383 BEGIN
3385 -- Work out the increment/decrement amount(s).
3386 IF (TG_OP = 'DELETE') THEN
3388 delta_time_key = OLD.time_key;
3389 delta_amount_sold = -1 * OLD.amount_sold;
3390 delta_units_sold = -1 * OLD.units_sold;
3391 delta_amount_cost = -1 * OLD.amount_cost;
3393 ELSIF (TG_OP = 'UPDATE') THEN
3395 -- forbid updates that change the time_key -
3396 -- (probably not too onerous, as DELETE + INSERT is how most
3397 -- changes will be made).
3398 IF ( OLD.time_key != NEW.time_key) THEN
3399 RAISE EXCEPTION 'Update of time_key : % -&gt; % not allowed', OLD.time_key, NEW.time_key;
3400 END IF;
3402 delta_time_key = OLD.time_key;
3403 delta_amount_sold = NEW.amount_sold - OLD.amount_sold;
3404 delta_units_sold = NEW.units_sold - OLD.units_sold;
3405 delta_amount_cost = NEW.amount_cost - OLD.amount_cost;
3407 ELSIF (TG_OP = 'INSERT') THEN
3409 delta_time_key = NEW.time_key;
3410 delta_amount_sold = NEW.amount_sold;
3411 delta_units_sold = NEW.units_sold;
3412 delta_amount_cost = NEW.amount_cost;
3414 END IF;
3417 -- Insert or update the summary row with the new values.
3418 &lt;&lt;insert_update&gt;&gt;
3419 LOOP
3420 UPDATE sales_summary_bytime
3421 SET amount_sold = amount_sold + delta_amount_sold,
3422 units_sold = units_sold + delta_units_sold,
3423 amount_cost = amount_cost + delta_amount_cost
3424 WHERE time_key = delta_time_key;
3426 EXIT insert_update WHEN found;
3428 BEGIN
3429 INSERT INTO sales_summary_bytime (
3430 time_key,
3431 amount_sold,
3432 units_sold,
3433 amount_cost)
3434 VALUES (
3435 delta_time_key,
3436 delta_amount_sold,
3437 delta_units_sold,
3438 delta_amount_cost
3441 EXIT insert_update;
3443 EXCEPTION
3444 WHEN UNIQUE_VIOLATION THEN
3445 -- do nothing
3446 END;
3447 END LOOP insert_update;
3449 RETURN NULL;
3451 END;
3452 $maint_sales_summary_bytime$ LANGUAGE plpgsql;
3454 CREATE TRIGGER maint_sales_summary_bytime
3455 AFTER INSERT OR UPDATE OR DELETE ON sales_fact
3456 FOR EACH ROW EXECUTE PROCEDURE maint_sales_summary_bytime();
3458 INSERT INTO sales_fact VALUES(1,1,1,10,3,15);
3459 INSERT INTO sales_fact VALUES(1,2,1,20,5,35);
3460 INSERT INTO sales_fact VALUES(2,2,1,40,15,135);
3461 INSERT INTO sales_fact VALUES(2,3,1,10,1,13);
3462 SELECT * FROM sales_summary_bytime;
3463 DELETE FROM sales_fact WHERE product_key = 1;
3464 SELECT * FROM sales_summary_bytime;
3465 UPDATE sales_fact SET units_sold = units_sold * 2;
3466 SELECT * FROM sales_summary_bytime;
3467 </programlisting>
3468 </example>
3470 </sect1>
3472 <sect1 id="plpgsql-implementation">
3473 <title><application>PL/pgSQL</> Under the Hood</title>
3475 <para>
3476 This section discusses some implementation details that are
3477 frequently important for <application>PL/pgSQL</> users to know.
3478 </para>
3480 <sect2 id="plpgsql-var-subst">
3481 <title>Variable Substitution</title>
3483 <para>
3484 When <application>PL/pgSQL</> prepares a SQL statement or expression
3485 for execution, any <application>PL/pgSQL</application> variable name
3486 appearing in the statement or expression is replaced by a parameter symbol,
3487 <literal>$<replaceable>n</replaceable></literal>. The current value
3488 of the variable is then provided as the value for the parameter whenever
3489 the statement or expression is executed. As an example, consider the
3490 function
3491 <programlisting>
3492 CREATE FUNCTION logfunc(logtxt text) RETURNS void AS $$
3493 DECLARE
3494 curtime timestamp := now();
3495 BEGIN
3496 INSERT INTO logtable VALUES (logtxt, curtime);
3497 END;
3498 $$ LANGUAGE plpgsql;
3499 </programlisting>
3500 The <command>INSERT</> statement will effectively be processed as
3501 <programlisting>
3502 PREPARE <replaceable>statement_name</>(text, timestamp) AS
3503 INSERT INTO logtable VALUES ($1, $2);
3504 </programlisting>
3505 followed on each execution by <command>EXECUTE</> with the current
3506 actual values of the two variables. (Note: here we are speaking of
3507 the main SQL engine's
3508 <xref linkend="sql-execute" endterm="sql-execute-title"> command,
3509 not <application>PL/pgSQL</application>'s <command>EXECUTE</>.)
3510 </para>
3512 <para>
3513 <emphasis>The substitution mechanism will replace any token that matches a
3514 known variable's name.</> This poses various traps for the unwary.
3515 For example, it is a bad idea
3516 to use a variable name that is the same as any table or column name
3517 that you need to reference in queries within the function, because
3518 what you think is a table or column name will still get replaced.
3519 In the above example, suppose that <structname>logtable</> has
3520 column names <structfield>logtxt</> and <structfield>logtime</>,
3521 and we try to write the <command>INSERT</> as
3522 <programlisting>
3523 INSERT INTO logtable (logtxt, logtime) VALUES (logtxt, curtime);
3524 </programlisting>
3525 This will be fed to the main SQL parser as
3526 <programlisting>
3527 INSERT INTO logtable ($1, logtime) VALUES ($1, $2);
3528 </programlisting>
3529 resulting in a syntax error like this:
3530 <screen>
3531 ERROR: syntax error at or near "$1"
3532 LINE 1: INSERT INTO logtable ( $1 , logtime) VALUES ( $1 , $2 )
3534 QUERY: INSERT INTO logtable ( $1 , logtime) VALUES ( $1 , $2 )
3535 CONTEXT: SQL statement in PL/PgSQL function "logfunc2" near line 5
3536 </screen>
3537 </para>
3539 <para>
3540 This example is fairly easy to diagnose, since it leads to an
3541 obvious syntax error. Much nastier are cases where the substitution
3542 is syntactically permissible, since the only symptom may be misbehavior
3543 of the function. In one case, a user wrote something like this:
3544 <programlisting>
3545 DECLARE
3546 val text;
3547 search_key integer;
3548 BEGIN
3550 FOR val IN SELECT val FROM table WHERE key = search_key LOOP ...
3551 </programlisting>
3552 and wondered why all his table entries seemed to be NULL. Of course
3553 what happened here was that the query became
3554 <programlisting>
3555 SELECT $1 FROM table WHERE key = $2
3556 </programlisting>
3557 and thus it was just an expensive way of assigning <literal>val</>'s
3558 current value back to itself for each row.
3559 </para>
3561 <para>
3562 A commonly used coding rule for avoiding such traps is to use a
3563 different naming convention for <application>PL/pgSQL</application>
3564 variables than you use for table and column names. For example,
3565 if all your variables are named
3566 <literal>v_<replaceable>something</></literal> while none of your
3567 table or column names start with <literal>v_</>, you're pretty safe.
3568 </para>
3570 <para>
3571 Another workaround is to use qualified (dotted) names for SQL entities.
3572 For instance we could safely have written the above example as
3573 <programlisting>
3574 FOR val IN SELECT table.val FROM table WHERE key = search_key LOOP ...
3575 </programlisting>
3576 because <application>PL/pgSQL</application> will not substitute a
3577 variable for a trailing component of a qualified name.
3578 However this solution does not work in every case &mdash; you can't
3579 qualify a name in an <command>INSERT</>'s column name list, for instance.
3580 Another point is that record and row variable names will be matched to
3581 the first components of qualified names, so a qualified SQL name is
3582 still vulnerable in some cases.
3583 In such cases choosing a non-conflicting variable name is the only way.
3584 </para>
3586 <para>
3587 Another technique you can use is to attach a label to the block in
3588 which your variables are declared, and then qualify the variable names
3589 in your SQL commands (see <xref linkend="plpgsql-structure">).
3590 For example,
3591 <programlisting>
3592 &lt;&lt;pl&gt;&gt;
3593 DECLARE
3594 val text;
3595 BEGIN
3597 UPDATE table SET col = pl.val WHERE ...
3598 </programlisting>
3599 This is not in itself a solution to the problem of conflicts,
3600 since an unqualified name in a SQL command is still at risk of being
3601 interpreted the <quote>wrong</> way. But it is useful for clarifying
3602 the intent of potentially-ambiguous code.
3603 </para>
3605 <para>
3606 Variable substitution does not happen in the command string given
3607 to <command>EXECUTE</> or one of its variants. If you need to
3608 insert a varying value into such a command, do so as part of
3609 constructing the string value, as illustrated in
3610 <xref linkend="plpgsql-statements-executing-dyn">.
3611 </para>
3613 <para>
3614 Variable substitution currently works only in <command>SELECT</>,
3615 <command>INSERT</>, <command>UPDATE</>, and <command>DELETE</> commands,
3616 because the main SQL engine allows parameter symbols only in these
3617 commands. To use a non-constant name or value in other statement
3618 types (generically called utility statements), you must construct
3619 the utility statement as a string and <command>EXECUTE</> it.
3620 </para>
3622 </sect2>
3624 <sect2 id="plpgsql-plan-caching">
3625 <title>Plan Caching</title>
3627 <para>
3628 The <application>PL/pgSQL</> interpreter parses the function's source
3629 text and produces an internal binary instruction tree the first time the
3630 function is called (within each session). The instruction tree
3631 fully translates the
3632 <application>PL/pgSQL</> statement structure, but individual
3633 <acronym>SQL</acronym> expressions and <acronym>SQL</acronym> commands
3634 used in the function are not translated immediately.
3635 </para>
3637 <para>
3638 As each expression and <acronym>SQL</acronym> command is first
3639 executed in the function, the <application>PL/pgSQL</> interpreter
3640 creates a prepared execution plan (using the
3641 <acronym>SPI</acronym> manager's <function>SPI_prepare</function>
3642 and <function>SPI_saveplan</function>
3643 functions).<indexterm><primary>preparing a query</><secondary>in
3644 PL/pgSQL</></> Subsequent visits to that expression or command
3645 reuse the prepared plan. Thus, a function with conditional code
3646 that contains many statements for which execution plans might be
3647 required will only prepare and save those plans that are really
3648 used during the lifetime of the database connection. This can
3649 substantially reduce the total amount of time required to parse
3650 and generate execution plans for the statements in a
3651 <application>PL/pgSQL</> function. A disadvantage is that errors
3652 in a specific expression or command cannot be detected until that
3653 part of the function is reached in execution. (Trivial syntax
3654 errors will be detected during the initial parsing pass, but
3655 anything deeper will not be detected until execution.)
3656 </para>
3658 <para>
3659 Once <application>PL/pgSQL</> has made an execution plan for a particular
3660 command in a function, it will reuse that plan for the life of the
3661 database connection. This is usually a win for performance, but it
3662 can cause some problems if you dynamically
3663 alter your database schema. For example:
3665 <programlisting>
3666 CREATE FUNCTION populate() RETURNS integer AS $$
3667 DECLARE
3668 -- declarations
3669 BEGIN
3670 PERFORM my_function();
3671 END;
3672 $$ LANGUAGE plpgsql;
3673 </programlisting>
3675 If you execute the above function, it will reference the OID for
3676 <function>my_function()</function> in the execution plan produced for
3677 the <command>PERFORM</command> statement. Later, if you
3678 drop and recreate <function>my_function()</function>, then
3679 <function>populate()</function> will not be able to find
3680 <function>my_function()</function> anymore. You would then have to
3681 start a new database session so that <function>populate()</function>
3682 will be compiled afresh, before it will work again. You can avoid
3683 this problem by using <command>CREATE OR REPLACE FUNCTION</command>
3684 when updating the definition of
3685 <function>my_function</function>, since when a function is
3686 <quote>replaced</quote>, its OID is not changed.
3687 </para>
3689 <note>
3690 <para>
3691 In <productname>PostgreSQL</productname> 8.3 and later, saved plans
3692 will be replaced whenever any schema changes have occurred to any
3693 tables they reference. This eliminates one of the major disadvantages
3694 of saved plans. However, there is no such mechanism for function
3695 references, and thus the above example involving a reference to a
3696 deleted function is still valid.
3697 </para>
3698 </note>
3700 <para>
3701 Because <application>PL/pgSQL</application> saves execution plans
3702 in this way, SQL commands that appear directly in a
3703 <application>PL/pgSQL</application> function must refer to the
3704 same tables and columns on every execution; that is, you cannot use
3705 a parameter as the name of a table or column in an SQL command. To get
3706 around this restriction, you can construct dynamic commands using
3707 the <application>PL/pgSQL</application> <command>EXECUTE</command>
3708 statement &mdash; at the price of constructing a new execution plan on
3709 every execution.
3710 </para>
3712 <para>
3713 Another important point is that the prepared plans are parameterized
3714 to allow the values of <application>PL/pgSQL</application> variables
3715 to change from one use to the next, as discussed in detail above.
3716 Sometimes this means that a plan is less efficient than it would be
3717 if generated for a specific variable value. As an example, consider
3718 <programlisting>
3719 SELECT * INTO myrec FROM dictionary WHERE word LIKE search_term;
3720 </programlisting>
3721 where <literal>search_term</> is a <application>PL/pgSQL</application>
3722 variable. The cached plan for this query will never use an index on
3723 <structfield>word</>, since the planner cannot assume that the
3724 <literal>LIKE</> pattern will be left-anchored at run time. To use
3725 an index the query must be planned with a specific constant
3726 <literal>LIKE</> pattern provided. This is another situation where
3727 <command>EXECUTE</command> can be used to force a new plan to be
3728 generated for each execution.
3729 </para>
3731 <para>
3732 The mutable nature of record variables presents another problem in this
3733 connection. When fields of a record variable are used in
3734 expressions or statements, the data types of the fields must not
3735 change from one call of the function to the next, since each
3736 expression will be planned using the data type that is present
3737 when the expression is first reached. <command>EXECUTE</command> can be
3738 used to get around this problem when necessary.
3739 </para>
3741 <para>
3742 If the same function is used as a trigger for more than one table,
3743 <application>PL/pgSQL</application> prepares and caches plans
3744 independently for each such table &mdash; that is, there is a cache
3745 for each trigger function and table combination, not just for each
3746 function. This alleviates some of the problems with varying
3747 data types; for instance, a trigger function will be able to work
3748 successfully with a column named <literal>key</> even if it happens
3749 to have different types in different tables.
3750 </para>
3752 <para>
3753 Likewise, functions having polymorphic argument types have a separate
3754 plan cache for each combination of actual argument types they have been
3755 invoked for, so that data type differences do not cause unexpected
3756 failures.
3757 </para>
3759 <para>
3760 Plan caching can sometimes have surprising effects on the interpretation
3761 of time-sensitive values. For example there
3762 is a difference between what these two functions do:
3764 <programlisting>
3765 CREATE FUNCTION logfunc1(logtxt text) RETURNS void AS $$
3766 BEGIN
3767 INSERT INTO logtable VALUES (logtxt, 'now');
3768 END;
3769 $$ LANGUAGE plpgsql;
3770 </programlisting>
3772 and:
3774 <programlisting>
3775 CREATE FUNCTION logfunc2(logtxt text) RETURNS void AS $$
3776 DECLARE
3777 curtime timestamp;
3778 BEGIN
3779 curtime := 'now';
3780 INSERT INTO logtable VALUES (logtxt, curtime);
3781 END;
3782 $$ LANGUAGE plpgsql;
3783 </programlisting>
3784 </para>
3786 <para>
3787 In the case of <function>logfunc1</function>, the
3788 <productname>PostgreSQL</productname> main parser knows when
3789 preparing the plan for the <command>INSERT</command> that the
3790 string <literal>'now'</literal> should be interpreted as
3791 <type>timestamp</type>, because the target column of
3792 <classname>logtable</classname> is of that type. Thus,
3793 <literal>'now'</literal> will be converted to a constant when the
3794 <command>INSERT</command> is planned, and then used in all
3795 invocations of <function>logfunc1</function> during the lifetime
3796 of the session. Needless to say, this isn't what the programmer
3797 wanted.
3798 </para>
3800 <para>
3801 In the case of <function>logfunc2</function>, the
3802 <productname>PostgreSQL</productname> main parser does not know
3803 what type <literal>'now'</literal> should become and therefore
3804 it returns a data value of type <type>text</type> containing the string
3805 <literal>now</literal>. During the ensuing assignment
3806 to the local variable <varname>curtime</varname>, the
3807 <application>PL/pgSQL</application> interpreter casts this
3808 string to the <type>timestamp</type> type by calling the
3809 <function>text_out</function> and <function>timestamp_in</function>
3810 functions for the conversion. So, the computed time stamp is updated
3811 on each execution as the programmer expects.
3812 </para>
3814 </sect2>
3816 </sect1>
3818 <sect1 id="plpgsql-development-tips">
3819 <title>Tips for Developing in <application>PL/pgSQL</application></title>
3821 <para>
3822 One good way to develop in
3823 <application>PL/pgSQL</> is to use the text editor of your
3824 choice to create your functions, and in another window, use
3825 <application>psql</application> to load and test those functions.
3826 If you are doing it this way, it
3827 is a good idea to write the function using <command>CREATE OR
3828 REPLACE FUNCTION</>. That way you can just reload the file to update
3829 the function definition. For example:
3830 <programlisting>
3831 CREATE OR REPLACE FUNCTION testfunc(integer) RETURNS integer AS $$
3832 ....
3833 $$ LANGUAGE plpgsql;
3834 </programlisting>
3835 </para>
3837 <para>
3838 While running <application>psql</application>, you can load or reload such
3839 a function definition file with:
3840 <programlisting>
3841 \i filename.sql
3842 </programlisting>
3843 and then immediately issue SQL commands to test the function.
3844 </para>
3846 <para>
3847 Another good way to develop in <application>PL/pgSQL</> is with a
3848 GUI database access tool that facilitates development in a
3849 procedural language. One example of such as a tool is
3850 <application>pgAdmin</>, although others exist. These tools often
3851 provide convenient features such as escaping single quotes and
3852 making it easier to recreate and debug functions.
3853 </para>
3855 <sect2 id="plpgsql-quote-tips">
3856 <title>Handling of Quotation Marks</title>
3858 <para>
3859 The code of a <application>PL/pgSQL</> function is specified in
3860 <command>CREATE FUNCTION</command> as a string literal. If you
3861 write the string literal in the ordinary way with surrounding
3862 single quotes, then any single quotes inside the function body
3863 must be doubled; likewise any backslashes must be doubled (assuming
3864 escape string syntax is used).
3865 Doubling quotes is at best tedious, and in more complicated cases
3866 the code can become downright incomprehensible, because you can
3867 easily find yourself needing half a dozen or more adjacent quote marks.
3868 It's recommended that you instead write the function body as a
3869 <quote>dollar-quoted</> string literal (see <xref
3870 linkend="sql-syntax-dollar-quoting">). In the dollar-quoting
3871 approach, you never double any quote marks, but instead take care to
3872 choose a different dollar-quoting delimiter for each level of
3873 nesting you need. For example, you might write the <command>CREATE
3874 FUNCTION</command> command as:
3875 <programlisting>
3876 CREATE OR REPLACE FUNCTION testfunc(integer) RETURNS integer AS $PROC$
3877 ....
3878 $PROC$ LANGUAGE plpgsql;
3879 </programlisting>
3880 Within this, you might use quote marks for simple literal strings in
3881 SQL commands and <literal>$$</> to delimit fragments of SQL commands
3882 that you are assembling as strings. If you need to quote text that
3883 includes <literal>$$</>, you could use <literal>$Q$</>, and so on.
3884 </para>
3886 <para>
3887 The following chart shows what you have to do when writing quote
3888 marks without dollar quoting. It might be useful when translating
3889 pre-dollar quoting code into something more comprehensible.
3890 </para>
3892 <variablelist>
3893 <varlistentry>
3894 <term>1 quotation mark</term>
3895 <listitem>
3896 <para>
3897 To begin and end the function body, for example:
3898 <programlisting>
3899 CREATE FUNCTION foo() RETURNS integer AS '
3900 ....
3901 ' LANGUAGE plpgsql;
3902 </programlisting>
3903 Anywhere within a single-quoted function body, quote marks
3904 <emphasis>must</> appear in pairs.
3905 </para>
3906 </listitem>
3907 </varlistentry>
3909 <varlistentry>
3910 <term>2 quotation marks</term>
3911 <listitem>
3912 <para>
3913 For string literals inside the function body, for example:
3914 <programlisting>
3915 a_output := ''Blah'';
3916 SELECT * FROM users WHERE f_name=''foobar'';
3917 </programlisting>
3918 In the dollar-quoting approach, you'd just write:
3919 <programlisting>
3920 a_output := 'Blah';
3921 SELECT * FROM users WHERE f_name='foobar';
3922 </programlisting>
3923 which is exactly what the <application>PL/pgSQL</> parser would see
3924 in either case.
3925 </para>
3926 </listitem>
3927 </varlistentry>
3929 <varlistentry>
3930 <term>4 quotation marks</term>
3931 <listitem>
3932 <para>
3933 When you need a single quotation mark in a string constant inside the
3934 function body, for example:
3935 <programlisting>
3936 a_output := a_output || '' AND name LIKE ''''foobar'''' AND xyz''
3937 </programlisting>
3938 The value actually appended to <literal>a_output</literal> would be:
3939 <literal> AND name LIKE 'foobar' AND xyz</literal>.
3940 </para>
3941 <para>
3942 In the dollar-quoting approach, you'd write:
3943 <programlisting>
3944 a_output := a_output || $$ AND name LIKE 'foobar' AND xyz$$
3945 </programlisting>
3946 being careful that any dollar-quote delimiters around this are not
3947 just <literal>$$</>.
3948 </para>
3949 </listitem>
3950 </varlistentry>
3952 <varlistentry>
3953 <term>6 quotation marks</term>
3954 <listitem>
3955 <para>
3956 When a single quotation mark in a string inside the function body is
3957 adjacent to the end of that string constant, for example:
3958 <programlisting>
3959 a_output := a_output || '' AND name LIKE ''''foobar''''''
3960 </programlisting>
3961 The value appended to <literal>a_output</literal> would then be:
3962 <literal> AND name LIKE 'foobar'</literal>.
3963 </para>
3964 <para>
3965 In the dollar-quoting approach, this becomes:
3966 <programlisting>
3967 a_output := a_output || $$ AND name LIKE 'foobar'$$
3968 </programlisting>
3969 </para>
3970 </listitem>
3971 </varlistentry>
3973 <varlistentry>
3974 <term>10 quotation marks</term>
3975 <listitem>
3976 <para>
3977 When you want two single quotation marks in a string constant (which
3978 accounts for 8 quotation marks) and this is adjacent to the end of that
3979 string constant (2 more). You will probably only need that if
3980 you are writing a function that generates other functions, as in
3981 <xref linkend="plpgsql-porting-ex2">.
3982 For example:
3983 <programlisting>
3984 a_output := a_output || '' if v_'' ||
3985 referrer_keys.kind || '' like ''''''''''
3986 || referrer_keys.key_string || ''''''''''
3987 then return '''''' || referrer_keys.referrer_type
3988 || ''''''; end if;'';
3989 </programlisting>
3990 The value of <literal>a_output</literal> would then be:
3991 <programlisting>
3992 if v_... like ''...'' then return ''...''; end if;
3993 </programlisting>
3994 </para>
3995 <para>
3996 In the dollar-quoting approach, this becomes:
3997 <programlisting>
3998 a_output := a_output || $$ if v_$$ || referrer_keys.kind || $$ like '$$
3999 || referrer_keys.key_string || $$'
4000 then return '$$ || referrer_keys.referrer_type
4001 || $$'; end if;$$;
4002 </programlisting>
4003 where we assume we only need to put single quote marks into
4004 <literal>a_output</literal>, because it will be re-quoted before use.
4005 </para>
4006 </listitem>
4007 </varlistentry>
4008 </variablelist>
4010 </sect2>
4011 </sect1>
4013 <!-- **** Porting from Oracle PL/SQL **** -->
4015 <sect1 id="plpgsql-porting">
4016 <title>Porting from <productname>Oracle</productname> PL/SQL</title>
4018 <indexterm zone="plpgsql-porting">
4019 <primary>Oracle</primary>
4020 <secondary>porting from PL/SQL to PL/pgSQL</secondary>
4021 </indexterm>
4023 <indexterm zone="plpgsql-porting">
4024 <primary>PL/SQL (Oracle)</primary>
4025 <secondary>porting to PL/pgSQL</secondary>
4026 </indexterm>
4028 <para>
4029 This section explains differences between
4030 <productname>PostgreSQL</>'s <application>PL/pgSQL</application>
4031 language and Oracle's <application>PL/SQL</application> language,
4032 to help developers who port applications from
4033 <trademark class="registered">Oracle</> to <productname>PostgreSQL</>.
4034 </para>
4036 <para>
4037 <application>PL/pgSQL</application> is similar to PL/SQL in many
4038 aspects. It is a block-structured, imperative language, and all
4039 variables have to be declared. Assignments, loops, conditionals
4040 are similar. The main differences you should keep in mind when
4041 porting from <application>PL/SQL</> to
4042 <application>PL/pgSQL</application> are:
4044 <itemizedlist>
4045 <listitem>
4046 <para>
4047 There are no default values for parameters in <productname>PostgreSQL</>.
4048 </para>
4049 </listitem>
4051 <listitem>
4052 <para>
4053 You can overload function names in <productname>PostgreSQL</>. This is
4054 often used to work around the lack of default parameters.
4055 </para>
4056 </listitem>
4058 <listitem>
4059 <para>
4060 If a name used in a SQL command could be either a column name of a
4061 table or a reference to a variable of the function,
4062 <application>PL/SQL</> treats it as a column name, while
4063 <application>PL/pgSQL</> treats it as a variable name. It's best
4064 to avoid such ambiguities in the first place, but if necessary you
4065 can fix them by properly qualifying the ambiguous name.
4066 (See <xref linkend="plpgsql-var-subst">.)
4067 </para>
4068 </listitem>
4070 <listitem>
4071 <para>
4072 In <productname>PostgreSQL</> the function body must be written as
4073 a string literal. Therefore you need to use dollar quoting or escape
4074 single quotes in the function body. (See <xref
4075 linkend="plpgsql-quote-tips">.)
4076 </para>
4077 </listitem>
4079 <listitem>
4080 <para>
4081 Instead of packages, use schemas to organize your functions
4082 into groups.
4083 </para>
4084 </listitem>
4086 <listitem>
4087 <para>
4088 Since there are no packages, there are no package-level variables
4089 either. This is somewhat annoying. You can keep per-session state
4090 in temporary tables instead.
4091 </para>
4092 </listitem>
4094 <listitem>
4095 <para>
4096 Integer <command>FOR</> loops with <literal>REVERSE</> work
4097 differently: <application>PL/SQL</> counts down from the second
4098 number to the first, while <application>PL/pgSQL</> counts down
4099 from the first number to the second, requiring the loop bounds
4100 to be swapped when porting. This incompatibility is unfortunate
4101 but is unlikely to be changed. (See <xref
4102 linkend="plpgsql-integer-for">.)
4103 </para>
4104 </listitem>
4106 <listitem>
4107 <para>
4108 <command>FOR</> loops over queries (other than cursors) also work
4109 differently: the target variable(s) must have been declared,
4110 whereas <application>PL/SQL</> always declares them implicitly.
4111 An advantage of this is that the variable values are still accessible
4112 after the loop exits.
4113 </para>
4114 </listitem>
4116 <listitem>
4117 <para>
4118 There are various notational differences for the use of cursor
4119 variables.
4120 </para>
4121 </listitem>
4123 </itemizedlist>
4124 </para>
4126 <sect2>
4127 <title>Porting Examples</title>
4129 <para>
4130 <xref linkend="pgsql-porting-ex1"> shows how to port a simple
4131 function from <application>PL/SQL</> to <application>PL/pgSQL</>.
4132 </para>
4134 <example id="pgsql-porting-ex1">
4135 <title>Porting a Simple Function from <application>PL/SQL</> to <application>PL/pgSQL</></title>
4137 <para>
4138 Here is an <productname>Oracle</productname> <application>PL/SQL</> function:
4139 <programlisting>
4140 CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar,
4141 v_version varchar)
4142 RETURN varchar IS
4143 BEGIN
4144 IF v_version IS NULL THEN
4145 RETURN v_name;
4146 END IF;
4147 RETURN v_name || '/' || v_version;
4148 END;
4150 show errors;
4151 </programlisting>
4152 </para>
4154 <para>
4155 Let's go through this function and see the differences compared to
4156 <application>PL/pgSQL</>:
4158 <itemizedlist>
4159 <listitem>
4160 <para>
4161 The <literal>RETURN</literal> key word in the function
4162 prototype (not the function body) becomes
4163 <literal>RETURNS</literal> in
4164 <productname>PostgreSQL</productname>.
4165 Also, <literal>IS</> becomes <literal>AS</>, and you need to
4166 add a <literal>LANGUAGE</> clause because <application>PL/pgSQL</>
4167 is not the only possible function language.
4168 </para>
4169 </listitem>
4171 <listitem>
4172 <para>
4173 In <productname>PostgreSQL</>, the function body is considered
4174 to be a string literal, so you need to use quote marks or dollar
4175 quotes around it. This substitutes for the terminating <literal>/</>
4176 in the Oracle approach.
4177 </para>
4178 </listitem>
4180 <listitem>
4181 <para>
4182 The <literal>show errors</literal> command does not exist in
4183 <productname>PostgreSQL</>, and is not needed since errors are
4184 reported automatically.
4185 </para>
4186 </listitem>
4187 </itemizedlist>
4188 </para>
4190 <para>
4191 This is how this function would look when ported to
4192 <productname>PostgreSQL</>:
4194 <programlisting>
4195 CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar,
4196 v_version varchar)
4197 RETURNS varchar AS $$
4198 BEGIN
4199 IF v_version IS NULL THEN
4200 RETURN v_name;
4201 END IF;
4202 RETURN v_name || '/' || v_version;
4203 END;
4204 $$ LANGUAGE plpgsql;
4205 </programlisting>
4206 </para>
4207 </example>
4209 <para>
4210 <xref linkend="plpgsql-porting-ex2"> shows how to port a
4211 function that creates another function and how to handle the
4212 ensuing quoting problems.
4213 </para>
4215 <example id="plpgsql-porting-ex2">
4216 <title>Porting a Function that Creates Another Function from <application>PL/SQL</> to <application>PL/pgSQL</></title>
4218 <para>
4219 The following procedure grabs rows from a
4220 <command>SELECT</command> statement and builds a large function
4221 with the results in <literal>IF</literal> statements, for the
4222 sake of efficiency.
4223 </para>
4225 <para>
4226 This is the Oracle version:
4227 <programlisting>
4228 CREATE OR REPLACE PROCEDURE cs_update_referrer_type_proc IS
4229 CURSOR referrer_keys IS
4230 SELECT * FROM cs_referrer_keys
4231 ORDER BY try_order;
4232 func_cmd VARCHAR(4000);
4233 BEGIN
4234 func_cmd := 'CREATE OR REPLACE FUNCTION cs_find_referrer_type(v_host IN VARCHAR,
4235 v_domain IN VARCHAR, v_url IN VARCHAR) RETURN VARCHAR IS BEGIN';
4237 FOR referrer_key IN referrer_keys LOOP
4238 func_cmd := func_cmd ||
4239 ' IF v_' || referrer_key.kind
4240 || ' LIKE ''' || referrer_key.key_string
4241 || ''' THEN RETURN ''' || referrer_key.referrer_type
4242 || '''; END IF;';
4243 END LOOP;
4245 func_cmd := func_cmd || ' RETURN NULL; END;';
4247 EXECUTE IMMEDIATE func_cmd;
4248 END;
4250 show errors;
4251 </programlisting>
4252 </para>
4254 <para>
4255 Here is how this function would end up in <productname>PostgreSQL</>:
4256 <programlisting>
4257 CREATE OR REPLACE FUNCTION cs_update_referrer_type_proc() RETURNS void AS $func$
4258 DECLARE
4259 CURSOR referrer_keys IS
4260 SELECT * FROM cs_referrer_keys
4261 ORDER BY try_order;
4262 func_body text;
4263 func_cmd text;
4264 BEGIN
4265 func_body := 'BEGIN';
4267 FOR referrer_key IN referrer_keys LOOP
4268 func_body := func_body ||
4269 ' IF v_' || referrer_key.kind
4270 || ' LIKE ' || quote_literal(referrer_key.key_string)
4271 || ' THEN RETURN ' || quote_literal(referrer_key.referrer_type)
4272 || '; END IF;' ;
4273 END LOOP;
4275 func_body := func_body || ' RETURN NULL; END;';
4277 func_cmd :=
4278 'CREATE OR REPLACE FUNCTION cs_find_referrer_type(v_host varchar,
4279 v_domain varchar,
4280 v_url varchar)
4281 RETURNS varchar AS '
4282 || quote_literal(func_body)
4283 || ' LANGUAGE plpgsql;' ;
4285 EXECUTE func_cmd;
4286 END;
4287 $func$ LANGUAGE plpgsql;
4288 </programlisting>
4289 Notice how the body of the function is built separately and passed
4290 through <literal>quote_literal</> to double any quote marks in it. This
4291 technique is needed because we cannot safely use dollar quoting for
4292 defining the new function: we do not know for sure what strings will
4293 be interpolated from the <structfield>referrer_key.key_string</> field.
4294 (We are assuming here that <structfield>referrer_key.kind</> can be
4295 trusted to always be <literal>host</>, <literal>domain</>, or
4296 <literal>url</>, but <structfield>referrer_key.key_string</> might be
4297 anything, in particular it might contain dollar signs.) This function
4298 is actually an improvement on the Oracle original, because it will
4299 not generate broken code when <structfield>referrer_key.key_string</> or
4300 <structfield>referrer_key.referrer_type</> contain quote marks.
4301 </para>
4302 </example>
4304 <para>
4305 <xref linkend="plpgsql-porting-ex3"> shows how to port a function
4306 with <literal>OUT</> parameters and string manipulation.
4307 <productname>PostgreSQL</> does not have a built-in
4308 <function>instr</function> function, but you can create one
4309 using a combination of other
4310 functions.<indexterm><primary>instr</></indexterm> In <xref
4311 linkend="plpgsql-porting-appendix"> there is a
4312 <application>PL/pgSQL</application> implementation of
4313 <function>instr</function> that you can use to make your porting
4314 easier.
4315 </para>
4317 <example id="plpgsql-porting-ex3">
4318 <title>Porting a Procedure With String Manipulation and
4319 <literal>OUT</> Parameters from <application>PL/SQL</> to
4320 <application>PL/pgSQL</></title>
4322 <para>
4323 The following <productname>Oracle</productname> PL/SQL procedure is used
4324 to parse a URL and return several elements (host, path, and query).
4325 </para>
4327 <para>
4328 This is the Oracle version:
4329 <programlisting>
4330 CREATE OR REPLACE PROCEDURE cs_parse_url(
4331 v_url IN VARCHAR,
4332 v_host OUT VARCHAR, -- This will be passed back
4333 v_path OUT VARCHAR, -- This one too
4334 v_query OUT VARCHAR) -- And this one
4336 a_pos1 INTEGER;
4337 a_pos2 INTEGER;
4338 BEGIN
4339 v_host := NULL;
4340 v_path := NULL;
4341 v_query := NULL;
4342 a_pos1 := instr(v_url, '//');
4344 IF a_pos1 = 0 THEN
4345 RETURN;
4346 END IF;
4347 a_pos2 := instr(v_url, '/', a_pos1 + 2);
4348 IF a_pos2 = 0 THEN
4349 v_host := substr(v_url, a_pos1 + 2);
4350 v_path := '/';
4351 RETURN;
4352 END IF;
4354 v_host := substr(v_url, a_pos1 + 2, a_pos2 - a_pos1 - 2);
4355 a_pos1 := instr(v_url, '?', a_pos2 + 1);
4357 IF a_pos1 = 0 THEN
4358 v_path := substr(v_url, a_pos2);
4359 RETURN;
4360 END IF;
4362 v_path := substr(v_url, a_pos2, a_pos1 - a_pos2);
4363 v_query := substr(v_url, a_pos1 + 1);
4364 END;
4366 show errors;
4367 </programlisting>
4368 </para>
4370 <para>
4371 Here is a possible translation into <application>PL/pgSQL</>:
4372 <programlisting>
4373 CREATE OR REPLACE FUNCTION cs_parse_url(
4374 v_url IN VARCHAR,
4375 v_host OUT VARCHAR, -- This will be passed back
4376 v_path OUT VARCHAR, -- This one too
4377 v_query OUT VARCHAR) -- And this one
4378 AS $$
4379 DECLARE
4380 a_pos1 INTEGER;
4381 a_pos2 INTEGER;
4382 BEGIN
4383 v_host := NULL;
4384 v_path := NULL;
4385 v_query := NULL;
4386 a_pos1 := instr(v_url, '//');
4388 IF a_pos1 = 0 THEN
4389 RETURN;
4390 END IF;
4391 a_pos2 := instr(v_url, '/', a_pos1 + 2);
4392 IF a_pos2 = 0 THEN
4393 v_host := substr(v_url, a_pos1 + 2);
4394 v_path := '/';
4395 RETURN;
4396 END IF;
4398 v_host := substr(v_url, a_pos1 + 2, a_pos2 - a_pos1 - 2);
4399 a_pos1 := instr(v_url, '?', a_pos2 + 1);
4401 IF a_pos1 = 0 THEN
4402 v_path := substr(v_url, a_pos2);
4403 RETURN;
4404 END IF;
4406 v_path := substr(v_url, a_pos2, a_pos1 - a_pos2);
4407 v_query := substr(v_url, a_pos1 + 1);
4408 END;
4409 $$ LANGUAGE plpgsql;
4410 </programlisting>
4412 This function could be used like this:
4413 <programlisting>
4414 SELECT * FROM cs_parse_url('http://foobar.com/query.cgi?baz');
4415 </programlisting>
4416 </para>
4417 </example>
4419 <para>
4420 <xref linkend="plpgsql-porting-ex4"> shows how to port a procedure
4421 that uses numerous features that are specific to Oracle.
4422 </para>
4424 <example id="plpgsql-porting-ex4">
4425 <title>Porting a Procedure from <application>PL/SQL</> to <application>PL/pgSQL</></title>
4427 <para>
4428 The Oracle version:
4430 <programlisting>
4431 CREATE OR REPLACE PROCEDURE cs_create_job(v_job_id IN INTEGER) IS
4432 a_running_job_count INTEGER;
4433 PRAGMA AUTONOMOUS_TRANSACTION;<co id="co.plpgsql-porting-pragma">
4434 BEGIN
4435 LOCK TABLE cs_jobs IN EXCLUSIVE MODE;<co id="co.plpgsql-porting-locktable">
4437 SELECT count(*) INTO a_running_job_count FROM cs_jobs WHERE end_stamp IS NULL;
4439 IF a_running_job_count &gt; 0 THEN
4440 COMMIT; -- free lock<co id="co.plpgsql-porting-commit">
4441 raise_application_error(-20000, 'Unable to create a new job: a job is currently running.');
4442 END IF;
4444 DELETE FROM cs_active_job;
4445 INSERT INTO cs_active_job(job_id) VALUES (v_job_id);
4447 BEGIN
4448 INSERT INTO cs_jobs (job_id, start_stamp) VALUES (v_job_id, sysdate);
4449 EXCEPTION
4450 WHEN dup_val_on_index THEN NULL; -- don't worry if it already exists
4451 END;
4452 COMMIT;
4453 END;
4455 show errors
4456 </programlisting>
4457 </para>
4459 <para>
4460 Procedures like this can easily be converted into <productname>PostgreSQL</>
4461 functions returning <type>void</type>. This procedure in
4462 particular is interesting because it can teach us some things:
4464 <calloutlist>
4465 <callout arearefs="co.plpgsql-porting-pragma">
4466 <para>
4467 There is no <literal>PRAGMA</literal> statement in <productname>PostgreSQL</>.
4468 </para>
4469 </callout>
4471 <callout arearefs="co.plpgsql-porting-locktable">
4472 <para>
4473 If you do a <command>LOCK TABLE</command> in <application>PL/pgSQL</>,
4474 the lock will not be released until the calling transaction is
4475 finished.
4476 </para>
4477 </callout>
4479 <callout arearefs="co.plpgsql-porting-commit">
4480 <para>
4481 You cannot issue <command>COMMIT</> in a
4482 <application>PL/pgSQL</application> function. The function is
4483 running within some outer transaction and so <command>COMMIT</>
4484 would imply terminating the function's execution. However, in
4485 this particular case it is not necessary anyway, because the lock
4486 obtained by the <command>LOCK TABLE</command> will be released when
4487 we raise an error.
4488 </para>
4489 </callout>
4490 </calloutlist>
4491 </para>
4493 <para>
4494 This is how we could port this procedure to <application>PL/pgSQL</>:
4496 <programlisting>
4497 CREATE OR REPLACE FUNCTION cs_create_job(v_job_id integer) RETURNS void AS $$
4498 DECLARE
4499 a_running_job_count integer;
4500 BEGIN
4501 LOCK TABLE cs_jobs IN EXCLUSIVE MODE;
4503 SELECT count(*) INTO a_running_job_count FROM cs_jobs WHERE end_stamp IS NULL;
4505 IF a_running_job_count &gt; 0 THEN
4506 RAISE EXCEPTION 'Unable to create a new job: a job is currently running';<co id="co.plpgsql-porting-raise">
4507 END IF;
4509 DELETE FROM cs_active_job;
4510 INSERT INTO cs_active_job(job_id) VALUES (v_job_id);
4512 BEGIN
4513 INSERT INTO cs_jobs (job_id, start_stamp) VALUES (v_job_id, now());
4514 EXCEPTION
4515 WHEN unique_violation THEN <co id="co.plpgsql-porting-exception">
4516 -- don't worry if it already exists
4517 END;
4518 END;
4519 $$ LANGUAGE plpgsql;
4520 </programlisting>
4522 <calloutlist>
4523 <callout arearefs="co.plpgsql-porting-raise">
4524 <para>
4525 The syntax of <literal>RAISE</> is considerably different from
4526 Oracle's statement, although the basic case <literal>RAISE</>
4527 <replaceable class="parameter">exception_name</replaceable> works
4528 similarly.
4529 </para>
4530 </callout>
4531 <callout arearefs="co.plpgsql-porting-exception">
4532 <para>
4533 The exception names supported by <application>PL/pgSQL</> are
4534 different from Oracle's. The set of built-in exception names
4535 is much larger (see <xref linkend="errcodes-appendix">). There
4536 is not currently a way to declare user-defined exception names.
4537 </para>
4538 </callout>
4539 </calloutlist>
4541 The main functional difference between this procedure and the
4542 Oracle equivalent is that the exclusive lock on the <literal>cs_jobs</>
4543 table will be held until the calling transaction completes. Also, if
4544 the caller later aborts (for example due to an error), the effects of
4545 this procedure will be rolled back.
4546 </para>
4547 </example>
4548 </sect2>
4550 <sect2 id="plpgsql-porting-other">
4551 <title>Other Things to Watch For</title>
4553 <para>
4554 This section explains a few other things to watch for when porting
4555 Oracle <application>PL/SQL</> functions to
4556 <productname>PostgreSQL</productname>.
4557 </para>
4559 <sect3 id="plpgsql-porting-exceptions">
4560 <title>Implicit Rollback after Exceptions</title>
4562 <para>
4563 In <application>PL/pgSQL</>, when an exception is caught by an
4564 <literal>EXCEPTION</> clause, all database changes since the block's
4565 <literal>BEGIN</> are automatically rolled back. That is, the behavior
4566 is equivalent to what you'd get in Oracle with:
4568 <programlisting>
4569 BEGIN
4570 SAVEPOINT s1;
4571 ... code here ...
4572 EXCEPTION
4573 WHEN ... THEN
4574 ROLLBACK TO s1;
4575 ... code here ...
4576 WHEN ... THEN
4577 ROLLBACK TO s1;
4578 ... code here ...
4579 END;
4580 </programlisting>
4582 If you are translating an Oracle procedure that uses
4583 <command>SAVEPOINT</> and <command>ROLLBACK TO</> in this style,
4584 your task is easy: just omit the <command>SAVEPOINT</> and
4585 <command>ROLLBACK TO</>. If you have a procedure that uses
4586 <command>SAVEPOINT</> and <command>ROLLBACK TO</> in a different way
4587 then some actual thought will be required.
4588 </para>
4589 </sect3>
4591 <sect3>
4592 <title><command>EXECUTE</command></title>
4594 <para>
4595 The <application>PL/pgSQL</> version of
4596 <command>EXECUTE</command> works similarly to the
4597 <application>PL/SQL</> version, but you have to remember to use
4598 <function>quote_literal</function> and
4599 <function>quote_ident</function> as described in <xref
4600 linkend="plpgsql-statements-executing-dyn">. Constructs of the
4601 type <literal>EXECUTE 'SELECT * FROM $1';</literal> will not work
4602 reliably unless you use these functions.
4603 </para>
4604 </sect3>
4606 <sect3 id="plpgsql-porting-optimization">
4607 <title>Optimizing <application>PL/pgSQL</application> Functions</title>
4609 <para>
4610 <productname>PostgreSQL</> gives you two function creation
4611 modifiers to optimize execution: <quote>volatility</> (whether
4612 the function always returns the same result when given the same
4613 arguments) and <quote>strictness</quote> (whether the function
4614 returns null if any argument is null). Consult the <xref
4615 linkend="sql-createfunction" endterm="sql-createfunction-title">
4616 reference page for details.
4617 </para>
4619 <para>
4620 When making use of these optimization attributes, your
4621 <command>CREATE FUNCTION</command> statement might look something
4622 like this:
4624 <programlisting>
4625 CREATE FUNCTION foo(...) RETURNS integer AS $$
4627 $$ LANGUAGE plpgsql STRICT IMMUTABLE;
4628 </programlisting>
4629 </para>
4630 </sect3>
4631 </sect2>
4633 <sect2 id="plpgsql-porting-appendix">
4634 <title>Appendix</title>
4636 <para>
4637 This section contains the code for a set of Oracle-compatible
4638 <function>instr</function> functions that you can use to simplify
4639 your porting efforts.
4640 </para>
4642 <programlisting>
4644 -- instr functions that mimic Oracle's counterpart
4645 -- Syntax: instr(string1, string2, [n], [m]) where [] denotes optional parameters.
4647 -- Searches string1 beginning at the nth character for the mth occurrence
4648 -- of string2. If n is negative, search backwards. If m is not passed,
4649 -- assume 1 (search starts at first character).
4652 CREATE FUNCTION instr(varchar, varchar) RETURNS integer AS $$
4653 DECLARE
4654 pos integer;
4655 BEGIN
4656 pos:= instr($1, $2, 1);
4657 RETURN pos;
4658 END;
4659 $$ LANGUAGE plpgsql STRICT IMMUTABLE;
4662 CREATE FUNCTION instr(string varchar, string_to_search varchar, beg_index integer)
4663 RETURNS integer AS $$
4664 DECLARE
4665 pos integer NOT NULL DEFAULT 0;
4666 temp_str varchar;
4667 beg integer;
4668 length integer;
4669 ss_length integer;
4670 BEGIN
4671 IF beg_index &gt; 0 THEN
4672 temp_str := substring(string FROM beg_index);
4673 pos := position(string_to_search IN temp_str);
4675 IF pos = 0 THEN
4676 RETURN 0;
4677 ELSE
4678 RETURN pos + beg_index - 1;
4679 END IF;
4680 ELSE
4681 ss_length := char_length(string_to_search);
4682 length := char_length(string);
4683 beg := length + beg_index - ss_length + 2;
4685 WHILE beg &gt; 0 LOOP
4686 temp_str := substring(string FROM beg FOR ss_length);
4687 pos := position(string_to_search IN temp_str);
4689 IF pos &gt; 0 THEN
4690 RETURN beg;
4691 END IF;
4693 beg := beg - 1;
4694 END LOOP;
4696 RETURN 0;
4697 END IF;
4698 END;
4699 $$ LANGUAGE plpgsql STRICT IMMUTABLE;
4702 CREATE FUNCTION instr(string varchar, string_to_search varchar,
4703 beg_index integer, occur_index integer)
4704 RETURNS integer AS $$
4705 DECLARE
4706 pos integer NOT NULL DEFAULT 0;
4707 occur_number integer NOT NULL DEFAULT 0;
4708 temp_str varchar;
4709 beg integer;
4710 i integer;
4711 length integer;
4712 ss_length integer;
4713 BEGIN
4714 IF beg_index &gt; 0 THEN
4715 beg := beg_index;
4716 temp_str := substring(string FROM beg_index);
4718 FOR i IN 1..occur_index LOOP
4719 pos := position(string_to_search IN temp_str);
4721 IF i = 1 THEN
4722 beg := beg + pos - 1;
4723 ELSE
4724 beg := beg + pos;
4725 END IF;
4727 temp_str := substring(string FROM beg + 1);
4728 END LOOP;
4730 IF pos = 0 THEN
4731 RETURN 0;
4732 ELSE
4733 RETURN beg;
4734 END IF;
4735 ELSE
4736 ss_length := char_length(string_to_search);
4737 length := char_length(string);
4738 beg := length + beg_index - ss_length + 2;
4740 WHILE beg &gt; 0 LOOP
4741 temp_str := substring(string FROM beg FOR ss_length);
4742 pos := position(string_to_search IN temp_str);
4744 IF pos &gt; 0 THEN
4745 occur_number := occur_number + 1;
4747 IF occur_number = occur_index THEN
4748 RETURN beg;
4749 END IF;
4750 END IF;
4752 beg := beg - 1;
4753 END LOOP;
4755 RETURN 0;
4756 END IF;
4757 END;
4758 $$ LANGUAGE plpgsql STRICT IMMUTABLE;
4759 </programlisting>
4760 </sect2>
4762 </sect1>
4764 </chapter>