1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.11 2000/12/22 18:57:50 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.12 2001/01/06 12:26:08 petere Exp $
3
3
-->
4
4
5
5
<chapter id="plsql">
@@ -55,8 +55,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.11 2000/12/22 18:57:50
55
55
</para>
56
56
<para>
57
57
The PL/pgSQL call handler parses the functions source text and
58
- produces an internal binary instruction tree on the first time, the
59
- function is called by a backend . The produced bytecode is identified
58
+ produces an internal binary instruction tree on the first time the
59
+ function is called. The produced bytecode is identified
60
60
in the call handler by the object ID of the function. This ensures,
61
61
that changing a function by a DROP/CREATE sequence will take effect
62
62
without establishing a new database connection.
109
109
</para>
110
110
111
111
<para>
112
- There can be any number of subblocks in the statement section
113
- of a block. Subblocks can be used to hide variables from outside a
112
+ There can be any number of sub-blocks in the statement section
113
+ of a block. Sub-blocks can be used to hide variables from outside a
114
114
block of statements. The variables
115
115
declared in the declarations section preceding a block are
116
116
initialized to their default values every time the block is entered,
132
132
<para>
133
133
There are two types of comments in PL/pgSQL. A double dash '--'
134
134
starts a comment that extends to the end of the line. A '/*'
135
- starts a block comment that extends to the next occurence of '*/'.
135
+ starts a block comment that extends to the next occurrence of '*/'.
136
136
Block comments cannot be nested, but double dash comments can be
137
137
enclosed into a block comment and a double dash can hide
138
138
the block comment delimiters '/*' and '*/'.
145
145
<title>Declarations</title>
146
146
147
147
<para>
148
- All variables, rows and records used in a block or it's
149
- subblocks must be declared in the declarations section of a block
148
+ All variables, rows and records used in a block or its
149
+ sub-blocks must be declared in the declarations section of a block
150
150
except for the loop variable of a FOR loop iterating over a range
151
151
of integer values. Parameters given to a PL/pgSQL function are
152
152
automatically declared with the usual identifiers $n.
174
174
assigning '<replaceable>now</replaceable>' to a variable of type
175
175
<replaceable>datetime</replaceable> causes the variable to have the
176
176
time of the actual function call, not when the function was
177
- precompiled into it's bytecode.
177
+ precompiled into its bytecode.
178
178
</para>
179
179
</listitem>
180
180
</varlistentry>
186
186
<listitem>
187
187
<para>
188
188
Declares a row with the structure of the given class. Class must be
189
- an existing table- or viewname of the database. The fields of the row
189
+ an existing table- or view name of the database. The fields of the row
190
190
are accessed in the dot notation. Parameters to a function can
191
191
be composite types (complete table rows). In that case, the
192
192
corresponding identifier $n will be a rowtype, but it
196
196
don't have useful system attributes).
197
197
</para>
198
198
<para>
199
- The fields of the rowtype inherit the tables fieldsizes
199
+ The fields of the rowtype inherit the table's field sizes
200
200
or precision for char() etc. data types.
201
201
</para>
202
202
</listitem>
@@ -263,7 +263,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
263
263
<title>Data Types</title>
264
264
265
265
<para>
266
- The type of a varible can be any of the existing basetypes of
266
+ The type of a variable can be any of the existing base types of
267
267
the database. <replaceable>type</replaceable> in the declarations
268
268
section above is defined as:
269
269
</para>
@@ -298,14 +298,14 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
298
298
</para>
299
299
<para>
300
300
Using the <replaceable>class.field</replaceable>%TYPE
301
- causes PL/pgSQL to lookup the attributes definitions at the
301
+ causes PL/pgSQL to look up the attributes definitions at the
302
302
first call to the function during the lifetime of a backend.
303
303
Have a table with a char(20) attribute and some PL/pgSQL functions
304
- that deal with it's content in local variables. Now someone
304
+ that deal with its content in local variables. Now someone
305
305
decides that char(20) isn't enough, dumps the table, drops it,
306
306
recreates it now with the attribute in question defined as
307
307
char(40) and restores the data. Ha - he forgot about the
308
- funcitons . The computations inside them will truncate the values
308
+ functions . The computations inside them will truncate the values
309
309
to 20 characters. But if they are defined using the
310
310
<replaceable>class.field</replaceable>%TYPE
311
311
declarations, they will automagically handle the size change or
@@ -320,7 +320,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
320
320
321
321
<para>
322
322
All expressions used in PL/pgSQL statements are processed using
323
- the backends executor. Expressions that appear to contain
323
+ the backend's executor. Expressions that appear to contain
324
324
constants may in fact require run-time evaluation (e.g. 'now' for the
325
325
datetime type) so
326
326
it is impossible for the PL/pgSQL parser
@@ -329,11 +329,12 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
329
329
<programlisting>
330
330
SELECT <replaceable>expression</replaceable>
331
331
</programlisting>
332
- using the SPI manager. In the expression, occurences of variable
332
+ using the SPI manager. In the expression, occurrences of variable
333
333
identifiers are substituted by parameters and the actual values from
334
334
the variables are passed to the executor in the parameter array. All
335
335
expressions used in a PL/pgSQL function are only prepared and
336
- saved once.
336
+ saved once. The only exception to this rule is an EXECUTE statement
337
+ if parsing of a query is needed each time it is encountered.
337
338
</para>
338
339
<para>
339
340
The type checking done by the <productname>Postgres</productname>
@@ -379,7 +380,7 @@ CREATE FUNCTION logfunc2 (text) RETURNS datetime AS '
379
380
<para>
380
381
In the case of logfunc2(), the <productname>Postgres</productname>
381
382
main parser does not know
382
- what type 'now' should become and therefor it returns a datatype of
383
+ what type 'now' should become and therefore it returns a data type of
383
384
text containing the string 'now'. During the assignment
384
385
to the local variable curtime, the PL/pgSQL interpreter casts this
385
386
string to the datetime type by calling the text_out() and datetime_in()
@@ -467,7 +468,7 @@ END IF;
467
468
<term>Calling another function</term>
468
469
<listitem>
469
470
<para>
470
- All functions defined in a <productname>Prostgres </productname>
471
+ All functions defined in a <productname>Postgres </productname>
471
472
database return a value. Thus, the normal way to call a function
472
473
is to execute a SELECT query or doing an assignment (resulting
473
474
in a PL/pgSQL internal SELECT). But there are cases where someone
@@ -482,6 +483,49 @@ PERFORM <replaceable>query</replaceable>
482
483
</listitem>
483
484
</varlistentry>
484
485
486
+ <varlistentry>
487
+ <term>Executing dynamic queries</term>
488
+ <listitem>
489
+ <cmdsynopsis>
490
+ <command>EXECUTE</command>
491
+ <arg choice="req"><replaceable class="command">query</replaceable></arg>
492
+ </cmdsynopsis>
493
+
494
+ <para>
495
+ Unlike all other queries in PL/pgSQL, a
496
+ <replaceable>query</replaceable> run by an EXECUTE statement
497
+ is not prepared and saved just once during the life of the
498
+ server. Instead, the <replaceable>query</replaceable> is
499
+ prepared each time the statement is run. This allows the
500
+ <replaceable>query</replaceable> to be dynamically created
501
+ within the procedure to perform actions on variable tables and
502
+ fields.
503
+ </para>
504
+
505
+ <para>
506
+ An example:
507
+ <programlisting>
508
+ EXECUTE ''UPDATE tbl SET ''
509
+ || quote_ident(fieldname)
510
+ || '' = ''
511
+ || quote_literal(newvalue)
512
+ || '' WHERE ...'';
513
+ </programlisting>
514
+ This example shows use of the functions
515
+ <function>quote_ident</function>(<type>TEXT</type>) and
516
+ <function>quote_literal</function>(<type>TEXT</type>).
517
+ Variables containing field and table identifiers should be
518
+ passed to function <function>quote_ident()</function>.
519
+ Variables containing literal elements of the dynamic query
520
+ string should be passed to
521
+ <function>quote_literal()</function>. Both take the
522
+ appropriate steps to return the input text enclosed in single
523
+ or double quotes and with any embedded special characters
524
+ intact.
525
+ </para>
526
+ </listitem>
527
+ </varlistentry>
528
+
485
529
<varlistentry>
486
530
<term>Returning from the function</term>
487
531
<listitem>
@@ -491,7 +535,7 @@ RETURN <replaceable>expression</replaceable>
491
535
</programlisting>
492
536
The function terminates and the value of <replaceable>expression</replaceable>
493
537
will be returned to the upper executor. The return value of a function
494
- cannot be undefined. If control reaches the end of the toplevel block
538
+ cannot be undefined. If control reaches the end of the top-level block
495
539
of the function without hitting a RETURN statement, a runtime error
496
540
will occur.
497
541
</para>
@@ -619,15 +663,15 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
619
663
</para>
620
664
<para>
621
665
First they have some special variables created automatically in the
622
- toplevel blocks declaration section. They are
666
+ top-level blocks declaration section. They are
623
667
</para>
624
668
625
669
<variablelist>
626
670
<varlistentry>
627
671
<term>NEW</term>
628
672
<listitem>
629
673
<para>
630
- Datatype RECORD; variable holding the new database row on INSERT/UPDATE
674
+ Data type RECORD; variable holding the new database row on INSERT/UPDATE
631
675
operations on ROW level triggers.
632
676
</para>
633
677
</listitem>
@@ -637,7 +681,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
637
681
<term>OLD</term>
638
682
<listitem>
639
683
<para>
640
- Datatype RECORD; variable holding the old database row on UPDATE/DELETE
684
+ Data type RECORD; variable holding the old database row on UPDATE/DELETE
641
685
operations on ROW level triggers.
642
686
</para>
643
687
</listitem>
@@ -647,7 +691,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
647
691
<term>TG_NAME</term>
648
692
<listitem>
649
693
<para>
650
- Datatype name; variable that contains the name of the trigger actually
694
+ Data type name; variable that contains the name of the trigger actually
651
695
fired.
652
696
</para>
653
697
</listitem>
@@ -657,7 +701,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
657
701
<term>TG_WHEN</term>
658
702
<listitem>
659
703
<para>
660
- Datatype text; a string of either 'BEFORE' or 'AFTER' depending on the
704
+ Data type text; a string of either 'BEFORE' or 'AFTER' depending on the
661
705
triggers definition.
662
706
</para>
663
707
</listitem>
@@ -667,7 +711,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
667
711
<term>TG_LEVEL</term>
668
712
<listitem>
669
713
<para>
670
- Datatype text; a string of either 'ROW' or 'STATEMENT' depending on the
714
+ Data type text; a string of either 'ROW' or 'STATEMENT' depending on the
671
715
triggers definition.
672
716
</para>
673
717
</listitem>
@@ -677,7 +721,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
677
721
<term>TG_OP</term>
678
722
<listitem>
679
723
<para>
680
- Datatype text; a string of 'INSERT', 'UPDATE' or 'DELETE' telling
724
+ Data type text; a string of 'INSERT', 'UPDATE' or 'DELETE' telling
681
725
for which operation the trigger is actually fired.
682
726
</para>
683
727
</listitem>
@@ -687,7 +731,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
687
731
<term>TG_RELID</term>
688
732
<listitem>
689
733
<para>
690
- Datatype oid; the object ID of the table that caused the
734
+ Data type oid; the object ID of the table that caused the
691
735
trigger invocation.
692
736
</para>
693
737
</listitem>
@@ -697,7 +741,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
697
741
<term>TG_RELNAME</term>
698
742
<listitem>
699
743
<para>
700
- Datatype name; the name of the table that caused the trigger
744
+ Data type name; the name of the table that caused the trigger
701
745
invocation.
702
746
</para>
703
747
</listitem>
@@ -707,7 +751,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
707
751
<term>TG_NARGS</term>
708
752
<listitem>
709
753
<para>
710
- Datatype integer; the number of arguments given to the trigger
754
+ Data type integer; the number of arguments given to the trigger
711
755
procedure in the CREATE TRIGGER statement.
712
756
</para>
713
757
</listitem>
@@ -717,7 +761,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
717
761
<term>TG_ARGV[]</term>
718
762
<listitem>
719
763
<para>
720
- Datatype array of text; the arguments from the CREATE TRIGGER statement.
764
+ Data type array of text; the arguments from the CREATE TRIGGER statement.
721
765
The index counts from 0 and can be given as an expression. Invalid
722
766
indices (< 0 or >= tg_nargs) result in a NULL value.
723
767
</para>
@@ -748,11 +792,11 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
748
792
exception handling model. Whenever the parser, planner/optimizer
749
793
or executor decide that a statement cannot be processed any longer,
750
794
the whole transaction gets aborted and the system jumps back
751
- into the mainloop to get the next query from the client application.
795
+ into the main loop to get the next query from the client application.
752
796
</para>
753
797
<para>
754
798
It is possible to hook into the error mechanism to notice that this
755
- happens. But currently it's impossible to tell what really
799
+ happens. But currently it is impossible to tell what really
756
800
caused the abort (input/output conversion error, floating point
757
801
error, parse error). And it is possible that the database backend
758
802
is in an inconsistent state at this point so returning to the upper
@@ -787,7 +831,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
787
831
of single quotes. The functions source text on CREATE FUNCTION must
788
832
be a literal string. Single quotes inside of literal strings must be
789
833
either doubled or quoted with a backslash. We are still looking for
790
- an elegant alternative. In the meantime, doubling the single qoutes
834
+ an elegant alternative. In the meantime, doubling the single quotes
791
835
as in the examples below should be used. Any solution for this
792
836
in future versions of <productname>Postgres</productname> will be
793
837
upward compatible.
@@ -848,7 +892,7 @@ CREATE FUNCTION c_overpaid (EMP, int4) RETURNS bool AS '
848
892
849
893
<para>
850
894
This trigger ensures, that any time a row is inserted or updated
851
- in the table, the current username and time are stamped into the
895
+ in the table, the current user name and time are stamped into the
852
896
row. And it ensures that an employees name is given and that the
853
897
salary is a positive value.
854
898
0 commit comments