2 42.4. Global Data in PL/Tcl #
4 Sometimes it is useful to have some global data that is held between
5 two calls to a function or is shared between different functions. This
6 is easily done in PL/Tcl, but there are some restrictions that must be
9 For security reasons, PL/Tcl executes functions called by any one SQL
10 role in a separate Tcl interpreter for that role. This prevents
11 accidental or malicious interference by one user with the behavior of
12 another user's PL/Tcl functions. Each such interpreter will have its
13 own values for any “global” Tcl variables. Thus, two PL/Tcl functions
14 will share the same global variables if and only if they are executed
15 by the same SQL role. In an application wherein a single session
16 executes code under multiple SQL roles (via SECURITY DEFINER functions,
17 use of SET ROLE, etc.) you may need to take explicit steps to ensure
18 that PL/Tcl functions can share data. To do that, make sure that
19 functions that should communicate are owned by the same user, and mark
20 them SECURITY DEFINER. You must of course take care that such functions
21 can't be used to do anything unintended.
23 All PL/TclU functions used in a session execute in the same Tcl
24 interpreter, which of course is distinct from the interpreter(s) used
25 for PL/Tcl functions. So global data is automatically shared between
26 PL/TclU functions. This is not considered a security risk because all
27 PL/TclU functions execute at the same trust level, namely that of a
30 To help protect PL/Tcl functions from unintentionally interfering with
31 each other, a global array is made available to each function via the
32 upvar command. The global name of this variable is the function's
33 internal name, and the local name is GD. It is recommended that GD be
34 used for persistent private data of a function. Use regular Tcl global
35 variables only for values that you specifically intend to be shared
36 among multiple functions. (Note that the GD arrays are only global
37 within a particular interpreter, so they do not bypass the security
38 restrictions mentioned above.)
40 An example of using GD appears in the spi_execp example below.