You can use the global hash <varname>%_SHARED</varname> to store
data, including code references, between function calls for the
lifetime of the current session.
現在のセッションの有効期間中の関数呼び出し間でデータ(コード参照を含む)を受け渡しするためにグローバルな%_SHAREDハッシュを使用することができます。
Here is a simple example for shared data: データの共有について簡単な例を以下に示します。
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
if ($_SHARED{$_[0]} = $_[1]) {
return 'ok';
} else {
return "cannot set shared variable $_[0] to $_[1]";
}
$$ LANGUAGE plperl;
CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
return $_SHARED{$_[0]};
$$ LANGUAGE plperl;
SELECT set_var('sample', 'Hello, PL/Perl! How''s tricks?');
SELECT get_var('sample');
Here is a slightly more complicated example using a code reference: 以下は、コード参照を使用した、多少複雑な例です。
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
$_SHARED{myquote} = sub {
my $arg = shift;
$arg =~ s/(['\\])/\\$1/g;
return "'$arg'";
};
$$ LANGUAGE plperl;
SELECT myfuncs(); /* initializes the function */
SELECT myfuncs(); /* 関数の初期化 */
/* Set up a function that uses the quote function */
/* 引用符関数を使用する関数を作成 */
CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
my $text_to_quote = shift;
my $qfunc = $_SHARED{myquote};
return &$qfunc($text_to_quote);
$$ LANGUAGE plperl;
(You could have replaced the above with the one-liner
<literal>return $_SHARED{myquote}->($_[0]);</literal>
at the expense of readability.)
(可読性を犠牲にすると、上はreturn $_SHARED{myquote}->($_[0]);という1行のみで置き換えることができます。)
For security reasons, PL/Perl executes functions called by any one SQL role
in a separate Perl interpreter for that role. This prevents accidental or
malicious interference by one user with the behavior of another user's
PL/Perl functions. Each such interpreter has its own value of the
<varname>%_SHARED</varname> variable and other global state. Thus, two
PL/Perl functions will share the same value of <varname>%_SHARED</varname>
if and only if they are executed by the same SQL role. In an application
wherein a single session executes code under multiple SQL roles (via
<literal>SECURITY DEFINER</literal> functions, use of <command>SET ROLE</command>, etc.)
you may need to take explicit steps to ensure that PL/Perl functions can
share data via <varname>%_SHARED</varname>. To do that, make sure that
functions that should communicate are owned by the same user, and mark
them <literal>SECURITY DEFINER</literal>. You must of course take care that
such functions can't be used to do anything unintended.
セキュリティ上の理由により、PL/Perlは、あるSQLロールで呼び出された関数をそのロール用に独立したPerlインタプリタ内で実行します。
これにより、あるユーザの事故または悪意によって他のユーザのPL/Perl関数の動作が干渉されてしまうことを防ぎます。
こうしたインタプリタはそれぞれ独自の%_SHAREDなどのグローバル状態を持ちます。
したがって、同一のSQLロールによって実行された場合のみ、2つのPL/Perl関数は同じ%_SHARED値を共有します。
1つのセッション内で複数のSQLロールの元でコードを(SECURITY DEFINER経由、SET ROLEの使用などで)実行するアプリケーションでは、確実にPL/Perl関数が%_SHAREDを介してデータを共有できるように、明示的な処理を行う必要があります。
このためには、通信しなければならない関数が同じユーザによって所有されること、およびSECURITY DEFINERと印付けられていることを確実にしなければなりません。
当然ながら、こうした関数が意図していないことを行うために使用できないように注意しなければなりません。