Skip to content

Commit 1b04b8f

Browse files
committed
Add plperl.on_plperl_init and plperl.on_plperlu_init settings for language-specific startup. Rename recently added plperl.on_perl_init to plperl.on_init. Also, code cleanup for utf8 hack. Patch from Tim Bunce, reviewed by Alex Hunsaker.
1 parent ec4be2e commit 1b04b8f

File tree

8 files changed

+208
-57
lines changed

8 files changed

+208
-57
lines changed

doc/src/sgml/plperl.sgml

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.79 2010/02/05 18:11:46 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.80 2010/02/12 19:35:25 adunstan Exp $ -->
22

33
<chapter id="plperl">
44
<title>PL/Perl - Perl Procedural Language</title>
@@ -831,6 +831,13 @@ $$ LANGUAGE plperl;
831831
<literal>return $_SHARED{myquote}-&gt;($_[0]);</literal>
832832
at the expense of readability.)
833833
</para>
834+
835+
<para>
836+
The <varname>%_SHARED</varname> variable and other global state within
837+
the language is public data, available to all PL/Perl functions within a
838+
session. Use with care, especially in situations that involve use of
839+
multiple roles or <literal>SECURITY DEFINER</> functions.
840+
</para>
834841
</sect1>
835842

836843
<sect1 id="plperl-trusted">
@@ -1127,26 +1134,27 @@ CREATE TRIGGER test_valid_id_trig
11271134

11281135
<variablelist>
11291136

1130-
<varlistentry id="guc-plperl-on-perl-init" xreflabel="plperl.on_perl_init">
1131-
<term><varname>plperl.on_perl_init</varname> (<type>string</type>)</term>
1137+
<varlistentry id="guc-plperl-on-init" xreflabel="plperl.on_init">
1138+
<term><varname>plperl.on_init</varname> (<type>string</type>)</term>
11321139
<indexterm>
1133-
<primary><varname>plperl.on_perl_init</> configuration parameter</primary>
1140+
<primary><varname>plperl.on_init</> configuration parameter</primary>
11341141
</indexterm>
11351142
<listitem>
11361143
<para>
1137-
Specifies perl code to be executed when a perl interpreter is first initialized.
1144+
Specifies Perl code to be executed when a Perl interpreter is first initialized
1145+
and before it is specialized for use by <literal>plperl</> or <literal>plperlu</>.
11381146
The SPI functions are not available when this code is executed.
11391147
If the code fails with an error it will abort the initialization of the interpreter
11401148
and propagate out to the calling query, causing the current transaction
11411149
or subtransaction to be aborted.
11421150
</para>
11431151
<para>
1144-
The perl code is limited to a single string. Longer code can be placed
1145-
into a module and loaded by the <literal>on_perl_init</> string.
1152+
The Perl code is limited to a single string. Longer code can be placed
1153+
into a module and loaded by the <literal>on_init</> string.
11461154
Examples:
11471155
<programlisting>
1148-
plplerl.on_perl_init = '$ENV{NYTPROF}="start=no"; require Devel::NYTProf::PgPLPerl'
1149-
plplerl.on_perl_init = 'use lib "/my/app"; use MyApp::PgInit;'
1156+
plplerl.on_init = '$ENV{NYTPROF}="start=no"; require Devel::NYTProf::PgPLPerl'
1157+
plplerl.on_init = 'use lib "/my/app"; use MyApp::PgInit;'
11501158
</programlisting>
11511159
</para>
11521160
<para>
@@ -1160,6 +1168,56 @@ plplerl.on_perl_init = 'use lib "/my/app"; use MyApp::PgInit;'
11601168
</listitem>
11611169
</varlistentry>
11621170

1171+
<varlistentry id="guc-plperl-on-plperl-init" xreflabel="plperl.on_plperl_init">
1172+
<term><varname>plperl.on_plperl_init</varname> (<type>string</type>)</term>
1173+
<term><varname>plperl.on_plperlu_init</varname> (<type>string</type>)</term>
1174+
<indexterm>
1175+
<primary><varname>plperl.on_plperl_init</> configuration parameter</primary>
1176+
</indexterm>
1177+
<indexterm>
1178+
<primary><varname>plperl.on_plperlu_init</> configuration parameter</primary>
1179+
</indexterm>
1180+
<listitem>
1181+
<para>
1182+
These parameters specify Perl code to be executed when the
1183+
<literal>plperl</>, or <literal>plperlu</> language is first used in a
1184+
session. Changes to these parameters after the corresponding language
1185+
has been used will have no effect.
1186+
The SPI functions are not available when this code is executed.
1187+
Only superusers can change these settings.
1188+
The Perl code in <literal>plperl.on_plperl_init</> can only perform trusted operations.
1189+
</para>
1190+
<para>
1191+
The effect of setting these parameters is very similar to executing a
1192+
<literal>DO</> command with the Perl code before any other use of the
1193+
language. The parameters are useful when you want to execute the Perl
1194+
code automatically on every connection, or when a connection is not
1195+
interactive. The parameters can be used by non-superusers by having a
1196+
superuser execute an <literal>ALTER USER ... SET ...</> command.
1197+
For example:
1198+
<programlisting>
1199+
ALTER USER joe SET plplerl.on_plperl_init = '$_SHARED{debug} = 1';
1200+
</programlisting>
1201+
</para>
1202+
<para>
1203+
If the code fails with an error it will abort the initialization and
1204+
propagate out to the calling query, causing the current transaction or
1205+
subtransaction to be aborted. Any changes within Perl won't be undone.
1206+
If the language is used again the initialization will be repeated.
1207+
</para>
1208+
<para>
1209+
The difference between these two settings and the
1210+
<literal>plperl.on_init</> setting is that these can be used for
1211+
settings specific to the trusted or untrusted language variant, such
1212+
as setting values in the <varname>%_SHARED</> variable. By contrast,
1213+
<literal>plperl.on_init</> is more useful for doing things like
1214+
setting the library search path for <productname>Perl</> or
1215+
loading Perl modules that don't interact directly with
1216+
<productname>PostgreSQL</>.
1217+
</para>
1218+
</listitem>
1219+
</varlistentry>
1220+
11631221
<varlistentry id="guc-plperl-use-strict" xreflabel="plperl.use_strict">
11641222
<term><varname>plperl.use_strict</varname> (<type>boolean</type>)</term>
11651223
<indexterm>

src/pl/plperl/GNUmakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Makefile for PL/Perl
2-
# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.42 2010/01/20 01:08:21 adunstan Exp $
2+
# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.43 2010/02/12 19:35:25 adunstan Exp $
33

44
subdir = src/pl/plperl
55
top_builddir = ../../..
@@ -41,7 +41,7 @@ PERLCHUNKS = plc_perlboot.pl plc_safe_bad.pl plc_safe_ok.pl
4141
SHLIB_LINK = $(perl_embed_ldflags)
4242

4343
REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-language=plperl --load-language=plperlu
44-
REGRESS = plperl plperl_trigger plperl_shared plperl_elog plperl_util plperlu
44+
REGRESS = plperl plperl_trigger plperl_shared plperl_elog plperl_util plperl_init plperlu
4545
# if Perl can support two interpreters in one backend,
4646
# test plperl-and-plperlu cases
4747
ifneq ($(PERL),)

src/pl/plperl/expected/plperl_shared.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
-- test plperl.on_plperl_init via the shared hash
2+
-- (must be done before plperl is first used)
3+
-- Avoid need for custom_variable_classes = 'plperl'
4+
LOAD 'plperl';
5+
-- testing on_plperl_init gets run, and that it can alter %_SHARED
6+
SET plperl.on_plperl_init = '$_SHARED{on_init} = 42';
17
-- test the shared hash
28
create function setme(key text, val text) returns void language plperl as $$
39

@@ -24,3 +30,9 @@ select getme('ourkey');
2430
ourval
2531
(1 row)
2632

33+
select getme('on_init');
34+
getme
35+
-------
36+
42
37+
(1 row)
38+

src/pl/plperl/expected/plperlu.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
-- Use ONLY plperlu tests here. For plperl/plerlu combined tests
22
-- see plperl_plperlu.sql
3+
-- Avoid need for custom_variable_classes = 'plperl'
4+
LOAD 'plperl';
5+
-- Test plperl.on_plperlu_init gets run
6+
SET plperl.on_plperlu_init = '$_SHARED{init} = 42';
7+
DO $$ warn $_SHARED{init} $$ language plperlu;
8+
NOTICE: 42 at line 1.
9+
CONTEXT: PL/Perl anonymous code block
310
--
411
-- Test compilation of unicode regex - regardless of locale.
512
-- This code fails in plain plperl in a non-UTF8 database.

src/pl/plperl/plc_safe_ok.pl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
# $PostgreSQL: pgsql/src/pl/plperl/plc_safe_ok.pl,v 1.3 2010/01/26 23:11:56 adunstan Exp $
3+
# $PostgreSQL: pgsql/src/pl/plperl/plc_safe_ok.pl,v 1.4 2010/02/12 19:35:25 adunstan Exp $
44

55
use strict;
66
use vars qw($PLContainer);
@@ -31,6 +31,7 @@
3131
}) or die $@;
3232
$PLContainer->deny(qw[caller]);
3333

34+
# called directly for plperl.on_plperl_init
3435
sub ::safe_eval {
3536
my $ret = $PLContainer->reval(shift);
3637
$@ =~ s/\(eval \d+\) //g if $@;

0 commit comments

Comments
 (0)