11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.6 2000/09/29 20:21:34 petere Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.7 2001/02/09 03:06:38 tgl Exp $
33-->
44
55 <chapter id="pltcl">
@@ -9,7 +9,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.6 2000/09/29 20:21:34 petere
99 PL/Tcl is a loadable procedural language for the
1010 <productname>Postgres</productname> database system
1111 that enables the Tcl language to be used to create functions and
12- trigger- procedures.
12+ trigger procedures.
1313 </para>
1414
1515 <para>
@@ -26,24 +26,39 @@ $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.6 2000/09/29 20:21:34 petere
2626 writer has in the C language, except for some restrictions.
2727 </para>
2828 <para>
29- The good restriction is, that everything is executed in a safe
30- Tcl- interpreter. In addition to the limited command set of safe Tcl, only
31- a few commands are available to access the databaseover SPI and to raise
29+ The good restriction is that everything is executed in a safe
30+ Tcl interpreter. In addition to the limited command set of safe Tcl, only
31+ a few commands are available to access the databasevia SPI and to raise
3232 messages via elog(). There is no way to access internals of the
33- database backend orgaining OS-level access under the permissions of the
34- <productname>Postgres</productname> user ID like in C .
33+ database backend orto gain OS-level access under the permissions of the
34+ <productname>Postgres</productname> user ID, as a C function can do .
3535 Thus, any unprivileged database user may be
3636 permitted to use this language.
3737 </para>
3838 <para>
39- The other,internal given, restriction is, that Tcl procedures cannot
40- be used to create input- /output- functions for new data types.
39+ The other,implementation restriction is that Tcl procedures cannot
40+ be used to create input/output functions for new data types.
4141 </para>
4242 <para>
43- The shared object for the PL/Tcl call handler is automatically built
44- and installed in the <productname>Postgres</productname>
45- library directory if the Tcl/Tk support is specified
46- in the configuration step of the installation procedure.
43+ Sometimes it is desirable to write Tcl functions that are not restricted
44+ to safe Tcl --- for example, one might want a Tcl function that sends
45+ mail. To handle these cases, there is a variant of PL/Tcl called PL/TclU
46+ (for untrusted Tcl). This is the exact same language except that a full
47+ Tcl interpreter is used. <emphasis>If PL/TclU is used, it must be
48+ installed as an untrusted procedural language</emphasis> so that only
49+ database superusers can create functions in it. The writer of a PL/TclU
50+ function must take care that the function cannot be used to do anything
51+ unwanted, since it will be able to do anything that could be done by
52+ a user logged in as the database administrator.
53+ </para>
54+ <para>
55+ The shared object for the PL/Tcl and PL/TclU call handlers is
56+ automatically built and installed in the
57+ <productname>Postgres</productname>
58+ library directory if Tcl/Tk support is specified
59+ in the configuration step of the installation procedure. To install
60+ PL/Tcl and/or PL/TclU in a particular database, use the
61+ <filename>createlang</filename> script.
4762 </para>
4863 </sect1>
4964
@@ -61,7 +76,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.6 2000/09/29 20:21:34 petere
6176 different functions as long as the number of arguments or their types
6277 differ. This would collide with Tcl procedure names. To offer the same
6378 flexibility in PL/Tcl, the internal Tcl procedure names contain the object
64- ID of theprocedures pg_proc row as part of their name. Thus, different
79+ ID of theprocedure's pg_proc row as part of their name. Thus, different
6580 argtype versions of the same <productname>Postgres</productname>
6681 function are different for Tcl too.
6782 </para>
@@ -72,17 +87,18 @@ $Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.6 2000/09/29 20:21:34 petere
7287 <title>Defining Functions in PL/Tcl</title>
7388
7489 <para>
75- To create a function in the PL/Tcl language, use theknown syntax
90+ To create a function in the PL/Tcl language, use thestandard syntax
7691
7792 <programlisting>
78- CREATE FUNCTION <replaceable>funcname</replaceable> <replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS '
93+ CREATE FUNCTION <replaceable>funcname</replaceable>( <replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS '
7994 # PL/Tcl function body
8095' LANGUAGE 'pltcl';
8196 </programlisting>
8297
83- When calling this function in a query, the arguments are given as
84- variables $1 ... $n to the Tcl procedure body. So a little max function
85- returning the higher of two int4 values would be created as:
98+ When the function is called, the arguments are given as
99+ variables $1 ... $n to the Tcl procedure body. For example,
100+ a function
101+ returning the higher of two int4 values could be defined as:
86102
87103 <programlisting>
88104CREATE FUNCTION tcl_max (int4, int4) RETURNS int4 AS '
@@ -120,13 +136,18 @@ CREATE FUNCTION overpaid_2 (EMP) RETURNS bool AS '
120136 <para>
121137 Sometimes (especially when using the SPI functions described later) it
122138 is useful to have some global status data that is held between two
123- calls to a procedure.
124- All PL/Tcl procedures executed in one backend share the same
139+ calls to a procedure. This is easily done since
140+ all PL/Tcl procedures executed in one backend share the same
125141 safe Tcl interpreter.
126- To help protecting PL/Tcl procedures from side effects,
142+ </para>
143+ <para>
144+ To help protect PL/Tcl procedures from unwanted side effects,
127145 an array is made available to each procedure via the upvar
128- command. The global name of this variable is the procedures internal
129- name and the local name is GD.
146+ command. The global name of this variable is the procedure's internal
147+ name and the local name is GD. It is recommended that GD be used
148+ for private status data of a procedure. Use regular Tcl global variables
149+ only for values that you specifically intend to be shared among multiple
150+ procedures.
130151 </para>
131152 </sect2>
132153
@@ -140,7 +161,7 @@ CREATE FUNCTION overpaid_2 (EMP) RETURNS bool AS '
140161 language.
141162 </para>
142163 <para>
143- Theinformations from the trigger managerare given to the procedure body
164+ Theinformation from the trigger manageris given to the procedure body
144165 in the following variables:
145166
146167 <variablelist>
@@ -259,8 +280,8 @@ CREATE FUNCTION overpaid_2 (EMP) RETURNS bool AS '
259280 </para>
260281 <para>
261282 Here's a little example trigger procedure that forces an integer value
262- in a table to keep track of the# of updates that are performed on the
263- row. For newrow's inserted, the value is initialized to 0 and then
283+ in a table to keep track of thenumber of updates that are performed on the
284+ row. For newrows inserted, the value is initialized to 0 and then
264285 incremented on every update operation:
265286
266287 <programlisting>
@@ -305,7 +326,7 @@ CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
305326 <para>
306327Fire a log message. Possible levels are NOTICE, ERROR,
307328FATAL, DEBUG and NOIND
308- like for the <function>elog</function> C function.
329+ as for the <function>elog</function> C function.
309330 </para>
310331 </listitem>
311332 </varlistentry>
@@ -332,7 +353,7 @@ CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
332353"SELECT 'doesn't' AS ret"
333354</programlisting>
334355
335- what would cause a parse error during
356+ which would cause a parse error during
336357<function>spi_exec</function> or
337358<function>spi_prepare</function>.
338359It should contain