41
41
<para>
42
42
Users of source packages must specially enable the build of
43
43
PL/Perl during the installation process. (Refer to <xref
44
- linkend="install-short "> for more information.) Users of
44
+ linkend="installation "> for more information.) Users of
45
45
binary packages might find PL/Perl in a separate subpackage.
46
46
</para>
47
47
</note>
@@ -101,7 +101,7 @@ $$ LANGUAGE plperl;
101
101
most convenient to use dollar quoting (see <xref
102
102
linkend="sql-syntax-dollar-quoting">) for the string constant.
103
103
If you choose to use escape string syntax <literal>E''</>,
104
- you must double the single quote marks (<literal>'</>) and backslashes
104
+ you must double any single quote marks (<literal>'</>) and backslashes
105
105
(<literal>\</>) used in the body of the function
106
106
(see <xref linkend="sql-syntax-strings">).
107
107
</para>
@@ -829,10 +829,20 @@ $$ LANGUAGE plperl;
829
829
</para>
830
830
831
831
<para>
832
- The <varname>%_SHARED</varname> variable and other global state within
833
- the language are public data, available to all PL/Perl functions within a
834
- session. Use with care, especially in situations that involve use of
835
- multiple roles or <literal>SECURITY DEFINER</> functions.
832
+ For security reasons, PL/Perl executes functions called by any one SQL role
833
+ in a separate Perl interpreter for that role. This prevents accidental or
834
+ malicious interference by one user with the behavior of another user's
835
+ PL/Perl functions. Each such interpreter has its own value of the
836
+ <varname>%_SHARED</varname> variable and other global state. Thus, two
837
+ PL/Perl functions will share the same value of <varname>%_SHARED</varname>
838
+ if and only if they are executed by the same SQL role. In an application
839
+ wherein a single session executes code under multiple SQL roles (via
840
+ <literal>SECURITY DEFINER</> functions, use of <command>SET ROLE</>, etc)
841
+ you may need to take explicit steps to ensure that PL/Perl functions can
842
+ share data via <varname>%_SHARED</varname>. To do that, make sure that
843
+ functions that should communicate are owned by the same user, and mark
844
+ them <literal>SECURITY DEFINER</>. You must of course take care that
845
+ such functions can't be used to do anything unintended.
836
846
</para>
837
847
</sect1>
838
848
@@ -908,22 +918,31 @@ $$ LANGUAGE plperl;
908
918
</para>
909
919
910
920
<note>
911
- <para>
912
- For security reasons, to stop a leak of privileged operations from
913
- <application>PL/PerlU</> to <application>PL/Perl</>, these two languages
914
- have to run in separate instances of the Perl interpreter. If your
915
- Perl installation has been appropriately compiled, this is not a problem.
916
- However, not all installations are compiled with the requisite flags.
917
- If <productname>PostgreSQL</> detects that this is the case then it will
918
- not start a second interpreter, but instead create an error. In
919
- consequence, in such an installation, you cannot use both
920
- <application>PL/PerlU</> and <application>PL/Perl</> in the same backend
921
- process. The remedy for this is to obtain a Perl installation configured
922
- with the appropriate flags, namely either <literal>usemultiplicity</>
923
- or <literal>useithreads</>. <literal>usemultiplicity</> is preferred
924
- unless you actually need to use threads. For more details, see the
925
- <citerefentry><refentrytitle>perlembed</></citerefentry> man page.
926
- </para>
921
+ <para>
922
+ While <application>PL/Perl</> functions run in a separate Perl
923
+ interpreter for each SQL role, all <application>PL/PerlU</> functions
924
+ executed in a given session run in a single Perl interpreter (which is
925
+ not any of the ones used for <application>PL/Perl</> functions).
926
+ This allows <application>PL/PerlU</> functions to share data freely,
927
+ but no communication can occur between <application>PL/Perl</> and
928
+ <application>PL/PerlU</> functions.
929
+ </para>
930
+ </note>
931
+
932
+ <note>
933
+ <para>
934
+ Perl cannot support multiple interpreters within one process unless
935
+ it was built with the appropriate flags, namely either
936
+ <literal>usemultiplicity</> or <literal>useithreads</>.
937
+ (<literal>usemultiplicity</> is preferred unless you actually need
938
+ to use threads. For more details, see the
939
+ <citerefentry><refentrytitle>perlembed</></citerefentry> man page.)
940
+ If <application>PL/Perl</> is used with a copy of Perl that was not built
941
+ this way, then it is only possible to have one Perl interpreter per
942
+ session, and so any one session can only execute either
943
+ <application>PL/PerlU</> functions, or <application>PL/Perl</> functions
944
+ that are all called by the same SQL role.
945
+ </para>
927
946
</note>
928
947
929
948
</sect1>
@@ -1137,12 +1156,13 @@ CREATE TRIGGER test_valid_id_trig
1137
1156
</indexterm>
1138
1157
<listitem>
1139
1158
<para>
1140
- Specifies Perl code to be executed when a Perl interpreter is first initialized
1141
- and before it is specialized for use by <literal>plperl</> or <literal>plperlu</>.
1142
- The SPI functions are not available when this code is executed.
1143
- If the code fails with an error it will abort the initialization of the interpreter
1144
- and propagate out to the calling query, causing the current transaction
1145
- or subtransaction to be aborted.
1159
+ Specifies Perl code to be executed when a Perl interpreter is first
1160
+ initialized, before it is specialized for use by <literal>plperl</> or
1161
+ <literal>plperlu</>.
1162
+ The SPI functions are not available when this code is executed.
1163
+ If the code fails with an error it will abort the initialization of
1164
+ the interpreter and propagate out to the calling query, causing the
1165
+ current transaction or subtransaction to be aborted.
1146
1166
</para>
1147
1167
<para>
1148
1168
The Perl code is limited to a single string. Longer code can be placed
@@ -1162,9 +1182,21 @@ DO 'elog(WARNING, join ", ", sort keys %INC)' language plperl;
1162
1182
</programlisting>
1163
1183
</para>
1164
1184
<para>
1165
- Initialization will happen in the postmaster if the plperl library is included
1166
- in <literal>shared_preload_libraries</> (see <xref linkend="guc-shared-preload-libraries">),
1167
- in which case extra consideration should be given to the risk of destabilizing the postmaster.
1185
+ Initialization will happen in the postmaster if the plperl library is
1186
+ included in <xref linkend="guc-shared-preload-libraries">, in which
1187
+ case extra consideration should be given to the risk of destabilizing
1188
+ the postmaster. The principal reason for making use of this feature
1189
+ is that Perl modules loaded by <literal>plperl.on_init</> need be
1190
+ loaded only at postmaster start, and will be instantly available
1191
+ without loading overhead in individual database sessions. However,
1192
+ keep in mind that the overhead is avoided only for the first Perl
1193
+ interpreter used by a database session — either PL/PerlU, or
1194
+ PL/Perl for the first SQL role that calls a PL/Perl function. Any
1195
+ additional Perl interpreters created in a database session will have
1196
+ to execute <literal>plperl.on_init</> afresh. Also, on Windows there
1197
+ will be no savings whatsoever from preloading, since the Perl
1198
+ interpreter created in the postmaster process does not propagate to
1199
+ child processes.
1168
1200
</para>
1169
1201
<para>
1170
1202
This parameter can only be set in the postgresql.conf file or on the server command line.
@@ -1183,41 +1215,30 @@ DO 'elog(WARNING, join ", ", sort keys %INC)' language plperl;
1183
1215
</indexterm>
1184
1216
<listitem>
1185
1217
<para>
1186
- These parameters specify Perl code to be executed when the
1187
- <literal>plperl</>, or <literal>plperlu</> language is first used in a
1188
- session. Changes to these parameters after the corresponding language
1189
- has been used will have no effect.
1190
- The SPI functions are not available when this code is executed.
1191
- Only superusers can change these settings.
1192
- The Perl code in <literal>plperl.on_plperl_init</> can only perform trusted operations.
1193
- </para>
1194
- <para>
1195
- The effect of setting these parameters is very similar to executing a
1196
- <literal>DO</> command with the Perl code before any other use of the
1197
- language. The parameters are useful when you want to execute the Perl
1198
- code automatically on every connection, or when a connection is not
1199
- interactive. The parameters can be used by non-superusers by having a
1200
- superuser execute an <literal>ALTER USER ... SET ...</> command.
1201
- For example:
1202
- <programlisting>
1203
- ALTER USER joe SET plperl.on_plperl_init = '$_SHARED{debug} = 1';
1204
- </programlisting>
1218
+ These parameters specify Perl code to be executed when a Perl
1219
+ interpreter is specialized for <literal>plperl</> or
1220
+ <literal>plperlu</> respectively. This will happen when a PL/Perl or
1221
+ PL/PerlU function is first executed in a database session, or when
1222
+ an additional interpreter has to be created because the other language
1223
+ is called or a PL/Perl function is called by a new SQL role. This
1224
+ follows any initialization done by <literal>plperl.on_init</>.
1225
+ The SPI functions are not available when this code is executed.
1226
+ The Perl code in <literal>plperl.on_plperl_init</> is executed after
1227
+ <quote>locking down</> the interpreter, and thus it can only perform
1228
+ trusted operations.
1205
1229
</para>
1206
1230
<para>
1207
- If the code fails with an error it will abort the initialization and
1208
- propagate out to the calling query, causing the current transaction or
1209
- subtransaction to be aborted. Any changes within Perl won't be undone.
1210
- If the language is used again the initialization will be repeated.
1231
+ If the code fails with an error it will abort the initialization and
1232
+ propagate out to the calling query, causing the current transaction or
1233
+ subtransaction to be aborted. Any actions already done within Perl
1234
+ won't be undone; however, that interpreter won't be used again.
1235
+ If the language is used again the initialization will be attempted
1236
+ again within a fresh Perl interpreter.
1211
1237
</para>
1212
1238
<para>
1213
- The difference between these two settings and the
1214
- <literal>plperl.on_init</> setting is that these can be used for
1215
- settings specific to the trusted or untrusted language variant, such
1216
- as setting values in the <varname>%_SHARED</> variable. By contrast,
1217
- <literal>plperl.on_init</> is more useful for doing things like
1218
- setting the library search path for <productname>Perl</> or
1219
- loading Perl modules that don't interact directly with
1220
- <productname>PostgreSQL</>.
1239
+ Only superusers can change these settings. Although these settings
1240
+ can be changed within a session, such changes will not affect Perl
1241
+ interpreters that have already been used to execute functions.
1221
1242
</para>
1222
1243
</listitem>
1223
1244
</varlistentry>
@@ -1229,8 +1250,9 @@ ALTER USER joe SET plperl.on_plperl_init = '$_SHARED{debug} = 1';
1229
1250
</indexterm>
1230
1251
<listitem>
1231
1252
<para>
1232
- When set true subsequent compilations of PL/Perl functions have the <literal>strict</> pragma enabled.
1233
- This parameter does not affect functions already compiled in the current session.
1253
+ When set true subsequent compilations of PL/Perl functions will have
1254
+ the <literal>strict</> pragma enabled. This parameter does not affect
1255
+ functions already compiled in the current session.
1234
1256
</para>
1235
1257
</listitem>
1236
1258
</varlistentry>
0 commit comments