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