バージョンごとのドキュメント一覧

45.6. PL/Perlトリガ #

<title>PL/Perl Triggers</title>

PL/Perl can be used to write trigger functions. In a trigger function, the hash reference <varname>$_TD</varname> contains information about the current trigger event. <varname>$_TD</varname> is a global variable, which gets a separate local value for each invocation of the trigger. The fields of the <varname>$_TD</varname> hash reference are: PL/Perlを使用してトリガ関数を作成することができます。 トリガ関数では、$_TDというハッシュへの参照に、現在のトリガイベントに関する情報が含まれています。 $_TDは大域変数であり、各トリガ呼び出しに対して局所的な値を別々に取り出します。 以下に$_TDというハッシュへの参照のフィールドを示します。

$_TD->{new}{foo}

<literal>NEW</literal> value of column <literal>foo</literal> NEWfoo列値。

$_TD->{old}{foo}

<literal>OLD</literal> value of column <literal>foo</literal> OLDfoo列値。

$_TD->{name}

Name of the trigger being called 呼び出されたトリガの名前。

$_TD->{event}

Trigger event: <literal>INSERT</literal>, <literal>UPDATE</literal>, <literal>DELETE</literal>, <literal>TRUNCATE</literal>, or <literal>UNKNOWN</literal> トリガイベント。 INSERTUPDATEDELETETRUNCATE、もしくはUNKNOWN

$_TD->{when}

When the trigger was called: <literal>BEFORE</literal>, <literal>AFTER</literal>, <literal>INSTEAD OF</literal>, or <literal>UNKNOWN</literal> トリガがいつ呼び出されたか。 BEFOREAFTERINSTEAD OFもしくはUNKNOWN

$_TD->{level}

The trigger level: <literal>ROW</literal>, <literal>STATEMENT</literal>, or <literal>UNKNOWN</literal> トリガレベル。 ROWSTATEMENT、もしくはUNKNOWN

$_TD->{relid}

OID of the table on which the trigger fired トリガの発行元テーブルのOID。

$_TD->{table_name}

Name of the table on which the trigger fired トリガの発行元テーブルの名前。

$_TD->{relname}

Name of the table on which the trigger fired. This has been deprecated, and could be removed in a future release. Please use $_TD-&gt;{table_name} instead. トリガの発行元テーブルの名前。 これは廃止予定で、将来のリリースで削除される可能性があります。 代わりに$_TD->{table_name}を使用してください。

$_TD->{table_schema}

Name of the schema in which the table on which the trigger fired, is トリガの発行元テーブルが存在するスキーマの名前。

$_TD->{argc}

Number of arguments of the trigger function トリガ関数の引数の数。

@{$_TD->{args}}

Arguments of the trigger function. Does not exist if <literal>$_TD-&gt;{argc}</literal> is 0. トリガ関数の引数。 $_TD->{argc}が0の場合は存在しません。

Row-level triggers can return one of the following: 行レベルトリガは以下のいずれかを返すことができます。

return;

Execute the operation 操作を実行します。

"SKIP"

Don't execute the operation 操作を実行しません。

"MODIFY"

Indicates that the <literal>NEW</literal> row was modified by the trigger function トリガ関数によってNEW行が変更されたことを示します。

Here is an example of a trigger function, illustrating some of the above: 以下はトリガ関数の例で、ここまでの説明の一部を例証するものです。

CREATE TABLE test (
    i int,
    v varchar
);

CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
    if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {

        return "SKIP";    # skip INSERT/UPDATE command

        return "SKIP";    # INSERT/UPDATEコマンドを取消します。
    } elsif ($_TD->{new}{v} ne "immortal") {
        $_TD->{new}{v} .= "(modified by trigger)";

        return "MODIFY";  # modify row and execute INSERT/UPDATE command

        return "MODIFY";  # 行を変更し、INSERT/UPDATEコマンドを実行します。
    } else {

        return;           # execute INSERT/UPDATE command

        return;           # INSERT/UPDATEコマンドを実行します。
    }
$$ LANGUAGE plperl;

CREATE TRIGGER test_valid_id_trig
    BEFORE INSERT OR UPDATE ON test
    FOR EACH ROW EXECUTE FUNCTION valid_id();