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

43.10. トリガ関数 #

<title>Trigger Functions</title>

<application>PL/pgSQL</application> can be used to define trigger functions on data changes or database events. A trigger function is created with the <command>CREATE FUNCTION</command> command, declaring it as a function with no arguments and a return type of <type>trigger</type> (for data change triggers) or <type>event_trigger</type> (for database event triggers). Special local variables named <varname>TG_<replaceable>something</replaceable></varname> are automatically defined to describe the condition that triggered the call. PL/pgSQLはデータ変更やデータベースのイベントによるトリガ関数の定義に使用できます。 トリガ関数は、CREATE FUNCTIONコマンドを使って、(データ変更トリガには)trigger、(データベースイベントトリガには)event_triggerという戻り値の型を持った引数のない関数として作成されます。 その呼出しのトリガの原因となった条件を記述するため、TG_somethingという名前の特別な局所変数が自動的に定義されます。

43.10.1. データ変更によるトリガ #

<title>Triggers on Data Changes</title>

A <link linkend="triggers">data change trigger</link> is declared as a function with no arguments and a return type of <type>trigger</type>. Note that the function must be declared with no arguments even if it expects to receive some arguments specified in <command>CREATE TRIGGER</command> &mdash; such arguments are passed via <varname>TG_ARGV</varname>, as described below. データ変更トリガtriggerという戻り値の型を持った引数のない関数として宣言されます。 その関数は、たとえ、CREATE TRIGGERにて引数を取るものとしていたとしても、引数を持たないものと宣言しなければならないことに注意してください。 トリガの引数は、後述する通り、TG_ARGV経由で渡されます。

When a <application>PL/pgSQL</application> function is called as a trigger, several special variables are created automatically in the top-level block. They are: PL/pgSQL関数がトリガとして呼び出された場合、いくつかの特殊な変数が自動的に最上位レベルのブロックで作成されます。 それらを以下に示します。

NEW record #

new database row for <command>INSERT</command>/<command>UPDATE</command> operations in row-level triggers. This variable is null in statement-level triggers and for <command>DELETE</command> operations. 行レベルのトリガでのINSERT/UPDATE操作での新しいデータベースの行。 この変数は、文レベルのトリガおよびDELETE操作ではNULLになります。

OLD record #

old database row for <command>UPDATE</command>/<command>DELETE</command> operations in row-level triggers. This variable is null in statement-level triggers and for <command>INSERT</command> operations. 行レベルのトリガでのUPDATE/DELETE操作での更新される前のデータベースの行。 この変数は、文レベルのトリガおよびINSERT操作ではNULLになります。

TG_NAME name #

name of the trigger which fired. 発行されたトリガの名前。

TG_WHEN text #

<literal>BEFORE</literal>, <literal>AFTER</literal>, or <literal>INSTEAD OF</literal>, depending on the trigger's definition. トリガの定義に依存して、BEFOREAFTER、またはINSTEAD OF

TG_LEVEL text #

<literal>ROW</literal> or <literal>STATEMENT</literal>, depending on the trigger's definition. トリガの定義に依存して、ROWまたはSTATEMENT

TG_OP text #

operation for which the trigger was fired: <literal>INSERT</literal>, <literal>UPDATE</literal>, <literal>DELETE</literal>, or <literal>TRUNCATE</literal>. トリガを起動した操作: INSERTUPDATEDELETE、または TRUNCATE

TG_RELID oid (references pg_class.oid) #

object ID of the table that caused the trigger invocation. トリガの起動元のテーブルのオブジェクトID。

TG_RELNAME name #

table that caused the trigger invocation. This is now deprecated, and could disappear in a future release. Use <literal>TG_TABLE_NAME</literal> instead. トリガを起動したテーブル。 これは現在は非推奨であり、将来のリリースで削除される可能性があります。 代わりにTG_TABLE_NAMEを使用してください。

TG_TABLE_NAME name #

table that caused the trigger invocation. トリガを起動したテーブル。

TG_TABLE_SCHEMA name #

schema of the table that caused the trigger invocation. トリガを起動したテーブルのスキーマ。

TG_NARGS integer #

number of arguments given to the trigger function in the <command>CREATE TRIGGER</command> statement. CREATE TRIGGER文におけるトリガ関数に与えられる引数の数。

TG_ARGV text[] #

arguments from the <command>CREATE TRIGGER</command> statement. The index counts from 0. Invalid indexes (less than 0 or greater than or equal to <varname>tg_nargs</varname>) result in a null value. CREATE TRIGGER文での引数。 このインデックスは0から始まります。 無効なインデックス(0未満やtg_nargs以上)は、NULL値という結果になります。

A trigger function must return either <symbol>NULL</symbol> or a record/row value having exactly the structure of the table the trigger was fired for. トリガ関数はNULLまたは、トリガの発行元になったテーブルの構造を正確に持ったレコード/行を返さなければなりません。

Row-level triggers fired <literal>BEFORE</literal> can return null to signal the trigger manager to skip the rest of the operation for this row (i.e., subsequent triggers are not fired, and the <command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command> does not occur for this row). If a nonnull value is returned then the operation proceeds with that row value. Returning a row value different from the original value of <varname>NEW</varname> alters the row that will be inserted or updated. Thus, if the trigger function wants the triggering action to succeed normally without altering the row value, <varname>NEW</varname> (or a value equal thereto) has to be returned. To alter the row to be stored, it is possible to replace single values directly in <varname>NEW</varname> and return the modified <varname>NEW</varname>, or to build a complete new record/row to return. In the case of a before-trigger on <command>DELETE</command>, the returned value has no direct effect, but it has to be nonnull to allow the trigger action to proceed. Note that <varname>NEW</varname> is null in <command>DELETE</command> triggers, so returning that is usually not sensible. The usual idiom in <command>DELETE</command> triggers is to return <varname>OLD</varname>. BEFOREとして発行された行レベルトリガがNULLを返す場合には、トリガマネージャに実際の行への操作を取りやめるように通知します (つまり、その後にトリガが発行されず、そのINSERT/UPDATE/DELETEはその行に対して実行されません)。 非NULL値を返す場合には、その操作はその行値で処理されます。 元のNEWの値と異なる行値を返すことは、挿入、更新される値を変更します。 したがってトリガ関数が行値を変更せずにトリガ処理を普通に成功させたい場合は、NEW(またはその等価な値)を返さなければなりません。 格納する行を変更するために、NEWの個々の値を直接置き換え、変更したNEWを返すことも、新しいレコード/行を完全に作成して返すことも可能です。 DELETEに対するBEFOREトリガの場合、返される値は直接的な影響を与えませんが、トリガ動作を継続させるためには非NULLを返さなければなりません。 DELETEトリガではNEWがNULLであり、これを返すことは通常無意味であることに注意して下さい。 DELETEトリガにおける通常の慣例はOLDを返すことです。

<literal>INSTEAD OF</literal> triggers (which are always row-level triggers, and may only be used on views) can return null to signal that they did not perform any updates, and that the rest of the operation for this row should be skipped (i.e., subsequent triggers are not fired, and the row is not counted in the rows-affected status for the surrounding <command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command>). Otherwise a nonnull value should be returned, to signal that the trigger performed the requested operation. For <command>INSERT</command> and <command>UPDATE</command> operations, the return value should be <varname>NEW</varname>, which the trigger function may modify to support <command>INSERT RETURNING</command> and <command>UPDATE RETURNING</command> (this will also affect the row value passed to any subsequent triggers, or passed to a special <varname>EXCLUDED</varname> alias reference within an <command>INSERT</command> statement with an <literal>ON CONFLICT DO UPDATE</literal> clause). For <command>DELETE</command> operations, the return value should be <varname>OLD</varname>. INSTEAD OFトリガ(これは常に行レベルトリガであり、ビューに対してのみ使用可能です)は、まったく更新を行わなかったためにこの行に対する残りの操作を飛ばさなければならない(つまり後続のトリガは発行されず、 トリガの発生元のINSERT/UPDATE/DELETEにおいて影響を受けた行数として数えられない)ことを通知するNULLを返すことができます。 この他の場合は、トリガが要求された操作を実行したことを通知するために、非NULLの値を返さなければなりません。 INSERTおよびUPDATE操作では、戻り値は、トリガ関数がINSERT RETURNINGおよびUPDATE RETURNINGをサポートするために変更しているかもしれない、NEWとなるはずです (これは後続のトリガ、または、ON CONFLICT DO UPDATE句を伴うINSERT文の中で特別なEXCLUDED別名参照に渡される行値にも影響します)。 DELETE操作では、戻り値はOLDとなるはずです。

The return value of a row-level trigger fired <literal>AFTER</literal> or a statement-level trigger fired <literal>BEFORE</literal> or <literal>AFTER</literal> is always ignored; it might as well be null. However, any of these types of triggers might still abort the entire operation by raising an error. 行レベルのAFTERトリガ、文レベルのBEFOREまたはAFTERトリガの戻り値は常に無視されます。 NULLとしても構いません。 しかし、これらの種類のトリガでも、エラーを発生させることで操作全体を中断させることが可能です。

<xref linkend="plpgsql-trigger-example"/> shows an example of a trigger function in <application>PL/pgSQL</application>. 例 43.3PL/pgSQLのトリガ関数の例を示します。

例43.3 PL/pgSQLトリガ関数

<title>A <application>PL/pgSQL</application> Trigger Function</title>

This example trigger ensures that any time a row is inserted or updated in the table, the current user name and time are stamped into the row. And it checks that an employee's name is given and that the salary is a positive value. このトリガの例では、テーブルの行が挿入または更新された時には必ず、現在のユーザ名と時刻がその行に入っていることを確実にします。 そして、従業員名が与えられていることとその給料が正の値であることを確認します。

CREATE TABLE emp (
    empname           text,
    salary            integer,
    last_date         timestamp,
    last_user         text
);

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
    BEGIN

        &#45;- Check that empname and salary are given

        -- empnameとsalaryが与えられていることをチェック
        IF NEW.empname IS NULL THEN
            RAISE EXCEPTION 'empname cannot be null';
        END IF;
        IF NEW.salary IS NULL THEN
            RAISE EXCEPTION '% cannot have null salary', NEW.empname;
        END IF;


        &#45;- Who works for us when they must pay for it?

        -- 支払時に問題が起こらないように
        IF NEW.salary < 0 THEN
            RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
        END IF;


        &#45;- Remember who changed the payroll when

        -- 誰がいつ変更したかを記録
        NEW.last_date := current_timestamp;
        NEW.last_user := current_user;
        RETURN NEW;
    END;
$emp_stamp$ LANGUAGE plpgsql;

CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
    FOR EACH ROW EXECUTE FUNCTION emp_stamp();

Another way to log changes to a table involves creating a new table that holds a row for each insert, update, or delete that occurs. This approach can be thought of as auditing changes to a table. <xref linkend="plpgsql-trigger-audit-example"/> shows an example of an audit trigger function in <application>PL/pgSQL</application>. テーブルにおける変更のログを取る他の方法は、挿入、更新または削除の各々に対する行を保有する新テーブルを作成することです。 この方法はテーブルにおける変更の監査と考えることができます。 例 43.4PL/pgSQLによる監査用トリガ関数の一例を示します。

例43.4 PL/pgSQLによる監査用のトリガ関数

<title>A <application>PL/pgSQL</application> Trigger Function for Auditing</title>

This example trigger ensures that any insert, update or delete of a row in the <literal>emp</literal> table is recorded (i.e., audited) in the <literal>emp_audit</literal> table. The current time and user name are stamped into the row, together with the type of operation performed on it. このトリガの例では、empテーブルにおける行の挿入、更新または削除のどれもがemp_auditテーブルの中へ確実に記録(すなわち監査)されます。 現在時刻とユーザ名は、行った操作の種類とともにemp_auditの行の中に記録されます。

CREATE TABLE emp (
    empname           text NOT NULL,
    salary            integer
);

CREATE TABLE emp_audit(
    operation         char(1)   NOT NULL,
    stamp             timestamp NOT NULL,
    userid            text      NOT NULL,
    empname           text      NOT NULL,
    salary            integer
);

CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
    BEGIN
        --

        &#45;- Create a row in emp_audit to reflect the operation performed on emp,
        &#45;- making use of the special variable TG_OP to work out the operation.

        -- empで行った操作を反映する行をemp_auditに作成
        -- 操作の種類を決定するために、特殊な変数TG_OPを活用
        --
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO emp_audit SELECT 'D', now(), user, OLD.*;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO emp_audit SELECT 'U', now(), user, NEW.*;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO emp_audit SELECT 'I', now(), user, NEW.*;
        END IF;

        RETURN NULL; &#45;- result is ignored since this is an AFTER trigger

        RETURN NULL; -- AFTERトリガですので、結果は無視されます
    END;
$emp_audit$ LANGUAGE plpgsql;

CREATE TRIGGER emp_audit
AFTER INSERT OR UPDATE OR DELETE ON emp
    FOR EACH ROW EXECUTE FUNCTION process_emp_audit();

A variation of the previous example uses a view joining the main table to the audit table, to show when each entry was last modified. This approach still records the full audit trail of changes to the table, but also presents a simplified view of the audit trail, showing just the last modified timestamp derived from the audit trail for each entry. <xref linkend="plpgsql-view-trigger-audit-example"/> shows an example of an audit trigger on a view in <application>PL/pgSQL</application>. 前例の変形では、各エントリが最終修正された時を表示するため、主テーブルを監査テーブルに結合したビューを使用します。 この方法でもテーブルの変化の監査証跡を全て記録できますが、監査証跡から抽出した各エントリの最終修正のタイムスタンプ表示することにより、監査証跡の簡単なビューを表示することにもなります。 例 43.5で示すものは、PL/pgSQLを用いたビューの監査トリガの例です。

例43.5 監査のためのPL/pgSQLビュートリガ関数

<title>A <application>PL/pgSQL</application> View Trigger Function for Auditing</title>

This example uses a trigger on the view to make it updatable, and ensure that any insert, update or delete of a row in the view is recorded (i.e., audited) in the <literal>emp_audit</literal> table. The current time and user name are recorded, together with the type of operation performed, and the view displays the last modified time of each row. この例では、ビューを更新可能とし、その行の挿入、更新、削除をemp_auditテーブルに確実に記録(つまり監査)するためにビューに対するトリガを使用します。 現在時刻とユーザ名が実行された操作種類と一緒に記録されます。 ビューは各行の最終更新時間を表示します。

CREATE TABLE emp (
    empname           text PRIMARY KEY,
    salary            integer
);

CREATE TABLE emp_audit(
    operation         char(1)   NOT NULL,
    userid            text      NOT NULL,
    empname           text      NOT NULL,
    salary            integer,
    stamp             timestamp NOT NULL
);

CREATE VIEW emp_view AS
    SELECT e.empname,
           e.salary,
           max(ea.stamp) AS last_updated
      FROM emp e
      LEFT JOIN emp_audit ea ON ea.empname = e.empname
     GROUP BY 1, 2;

CREATE OR REPLACE FUNCTION update_emp_view() RETURNS TRIGGER AS $$
    BEGIN
        --

        &#45;- Perform the required operation on emp, and create a row in emp_audit
        &#45;- to reflect the change made to emp.

        -- 要求された操作を emp に実行し
        -- emp_audit に行を作成し
        -- emp の変化を反映する
        --
        IF (TG_OP = 'DELETE') THEN
            DELETE FROM emp WHERE empname = OLD.empname;
            IF NOT FOUND THEN RETURN NULL; END IF;

            OLD.last_updated = now();
            INSERT INTO emp_audit VALUES('D', user, OLD.*);
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            UPDATE emp SET salary = NEW.salary WHERE empname = OLD.empname;
            IF NOT FOUND THEN RETURN NULL; END IF;

            NEW.last_updated = now();
            INSERT INTO emp_audit VALUES('U', user, NEW.*);
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO emp VALUES(NEW.empname, NEW.salary);

            NEW.last_updated = now();
            INSERT INTO emp_audit VALUES('I', user, NEW.*);
            RETURN NEW;
        END IF;
    END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER emp_audit
INSTEAD OF INSERT OR UPDATE OR DELETE ON emp_view
    FOR EACH ROW EXECUTE FUNCTION update_emp_view();

One use of triggers is to maintain a summary table of another table. The resulting summary can be used in place of the original table for certain queries &mdash; often with vastly reduced run times. This technique is commonly used in Data Warehousing, where the tables of measured or observed data (called fact tables) might be extremely large. <xref linkend="plpgsql-trigger-summary-example"/> shows an example of a trigger function in <application>PL/pgSQL</application> that maintains a summary table for a fact table in a data warehouse. トリガの使用目的の1つは、あるテーブルのサマリテーブルを維持することです。 結果のサマリテーブルは、元のテーブルに代わって、ある種の問い合わせに対して使用でき、しばしば実行時間を大幅に縮小します。 通常この手法は、計測または観測データ(ファクトテーブルと言います)が非常に大きくなるかもしれない、データウェアハウスに使用されます。 データウェアハウス内のファクトテーブルに対してサマリテーブルを維持するPL/pgSQLのトリガ関数の例を例 43.6に示します。

例43.6 サマリテーブルを維持するためのPL/pgSQLトリガ関数

<title>A <application>PL/pgSQL</application> Trigger Function for Maintaining a Summary Table</title>

The schema detailed here is partly based on the <emphasis>Grocery Store </emphasis> example from <emphasis>The Data Warehouse Toolkit</emphasis> by Ralph Kimball. ここに述べるスキーマの一部はRalph KimballによるThe Data Warehouse ToolkitGrocery Storeの例に基づいています。

--

&#45;- Main tables - time dimension and sales fact.

-- time dimensionとsales factの主テーブル
--
CREATE TABLE time_dimension (
    time_key                    integer NOT NULL,
    day_of_week                 integer NOT NULL,
    day_of_month                integer NOT NULL,
    month                       integer NOT NULL,
    quarter                     integer NOT NULL,
    year                        integer NOT NULL
);
CREATE UNIQUE INDEX time_dimension_key ON time_dimension(time_key);

CREATE TABLE sales_fact (
    time_key                    integer NOT NULL,
    product_key                 integer NOT NULL,
    store_key                   integer NOT NULL,
    amount_sold                 numeric(12,2) NOT NULL,
    units_sold                  integer NOT NULL,
    amount_cost                 numeric(12,2) NOT NULL
);
CREATE INDEX sales_fact_time ON sales_fact(time_key);

--

&#45;- Summary table - sales by time.

-- sales by timeのサマリテーブル
--
CREATE TABLE sales_summary_bytime (
    time_key                    integer NOT NULL,
    amount_sold                 numeric(15,2) NOT NULL,
    units_sold                  numeric(12) NOT NULL,
    amount_cost                 numeric(15,2) NOT NULL
);
CREATE UNIQUE INDEX sales_summary_bytime_key ON sales_summary_bytime(time_key);

--

&#45;- Function and trigger to amend summarized column(s) on UPDATE, INSERT, DELETE.

-- 更新、挿入および削除によりサマリテーブルの列を修正する関数とトリガ
--
CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER
AS $maint_sales_summary_bytime$
    DECLARE
        delta_time_key          integer;
        delta_amount_sold       numeric(15,2);
        delta_units_sold        numeric(12);
        delta_amount_cost       numeric(15,2);
    BEGIN


        &#45;- Work out the increment/decrement amount(s).

        -- 増加または減少量を算出
        IF (TG_OP = 'DELETE') THEN

            delta_time_key = OLD.time_key;
            delta_amount_sold = -1 * OLD.amount_sold;
            delta_units_sold = -1 * OLD.units_sold;
            delta_amount_cost = -1 * OLD.amount_cost;

        ELSIF (TG_OP = 'UPDATE') THEN


            &#45;- forbid updates that change the time_key -
            &#45;- (probably not too onerous, as DELETE + INSERT is how most
            &#45;- changes will be made).

            -- time_keyを変更する更新を禁止します
            -- (削除 + 挿入の方法により大部分の変更を行うため
            -- それほど厄介ではありません)。
            IF ( OLD.time_key != NEW.time_key) THEN
                RAISE EXCEPTION 'Update of time_key : % -> % not allowed',
                                                      OLD.time_key, NEW.time_key;
            END IF;

            delta_time_key = OLD.time_key;
            delta_amount_sold = NEW.amount_sold - OLD.amount_sold;
            delta_units_sold = NEW.units_sold - OLD.units_sold;
            delta_amount_cost = NEW.amount_cost - OLD.amount_cost;

        ELSIF (TG_OP = 'INSERT') THEN

            delta_time_key = NEW.time_key;
            delta_amount_sold = NEW.amount_sold;
            delta_units_sold = NEW.units_sold;
            delta_amount_cost = NEW.amount_cost;

        END IF;



        &#45;- Insert or update the summary row with the new values.

        -- サマリテーブルの行を挿入または新しい値で更新します。
        <<insert_update>>
        LOOP
            UPDATE sales_summary_bytime
                SET amount_sold = amount_sold + delta_amount_sold,
                    units_sold = units_sold + delta_units_sold,
                    amount_cost = amount_cost + delta_amount_cost
                WHERE time_key = delta_time_key;

            EXIT insert_update WHEN found;

            BEGIN
                INSERT INTO sales_summary_bytime (
                            time_key,
                            amount_sold,
                            units_sold,
                            amount_cost)
                    VALUES (
                            delta_time_key,
                            delta_amount_sold,
                            delta_units_sold,
                            delta_amount_cost
                           );

                EXIT insert_update;

            EXCEPTION
                WHEN UNIQUE_VIOLATION THEN

                    &#45;- do nothing

                    -- 何もしません
            END;
        END LOOP insert_update;

        RETURN NULL;

    END;
$maint_sales_summary_bytime$ LANGUAGE plpgsql;

CREATE TRIGGER maint_sales_summary_bytime
AFTER INSERT OR UPDATE OR DELETE ON sales_fact
    FOR EACH ROW EXECUTE FUNCTION maint_sales_summary_bytime();

INSERT INTO sales_fact VALUES(1,1,1,10,3,15);
INSERT INTO sales_fact VALUES(1,2,1,20,5,35);
INSERT INTO sales_fact VALUES(2,2,1,40,15,135);
INSERT INTO sales_fact VALUES(2,3,1,10,1,13);
SELECT * FROM sales_summary_bytime;
DELETE FROM sales_fact WHERE product_key = 1;
SELECT * FROM sales_summary_bytime;
UPDATE sales_fact SET units_sold = units_sold * 2;
SELECT * FROM sales_summary_bytime;

<literal>AFTER</literal> triggers can also make use of <firstterm>transition tables</firstterm> to inspect the entire set of rows changed by the triggering statement. The <command>CREATE TRIGGER</command> command assigns names to one or both transition tables, and then the function can refer to those names as though they were read-only temporary tables. <xref linkend="plpgsql-trigger-audit-transition-example"/> shows an example. AFTERトリガは、トリガ文により変更された行の集合全体を調べるために遷移テーブルを使うこともできます。 CREATE TRIGGERコマンドで名前を1つまたは2つの遷移テーブルに割り当てると、関数はその名前を読み込み専用の一時テーブルであるかのように参照できます。 例 43.7に例を示します。

例43.7 遷移テーブルでの監査

<title>Auditing with Transition Tables</title>

This example produces the same results as <xref linkend="plpgsql-trigger-audit-example"/>, but instead of using a trigger that fires for every row, it uses a trigger that fires once per statement, after collecting the relevant information in a transition table. This can be significantly faster than the row-trigger approach when the invoking statement has modified many rows. Notice that we must make a separate trigger declaration for each kind of event, since the <literal>REFERENCING</literal> clauses must be different for each case. But this does not stop us from using a single trigger function if we choose. (In practice, it might be better to use three separate functions and avoid the run-time tests on <varname>TG_OP</varname>.) この例は例 43.4と同じ結果になりますが、行毎に起動するトリガを使う代わりに、関係のある情報を遷移テーブルに集めた後に文毎に1回起動するトリガを使っています。 これは、呼び出された文が多くの行を変更する場合には行トリガの方法よりとても速くなる場合があります。 REFERENCING句はそれぞれの場合で異ならなければなりませんので、それぞれの種類のイベントに対して別々のトリガ宣言をしなければならないことに注意してください。 ですが、もし選ぶのなら、このために単一のトリガ関数が使えなくなることはありません。 (実際には、3つに別れた関数を使い、実行時のTG_OPの確認を避ける方が良いでしょう。)

CREATE TABLE emp (
    empname           text NOT NULL,
    salary            integer
);

CREATE TABLE emp_audit(
    operation         char(1)   NOT NULL,
    stamp             timestamp NOT NULL,
    userid            text      NOT NULL,
    empname           text      NOT NULL,
    salary            integer
);

CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
    BEGIN
        --

        &#45;- Create rows in emp_audit to reflect the operations performed on emp,
        &#45;- making use of the special variable TG_OP to work out the operation.

        -- empで実行された操作を反映するためにemp_auditに行を作り、
        -- 操作を完了するために特殊な変数TG_OPを使う。
        --
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO emp_audit
                SELECT 'D', now(), user, o.* FROM old_table o;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO emp_audit
                SELECT 'U', now(), user, n.* FROM new_table n;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO emp_audit
                SELECT 'I', now(), user, n.* FROM new_table n;
        END IF;

        RETURN NULL; &#45;- result is ignored since this is an AFTER trigger

        RETURN NULL; -- これはAFTERトリガなので結果は無視される
    END;
$emp_audit$ LANGUAGE plpgsql;

CREATE TRIGGER emp_audit_ins
    AFTER INSERT ON emp
    REFERENCING NEW TABLE AS new_table
    FOR EACH STATEMENT EXECUTE FUNCTION process_emp_audit();
CREATE TRIGGER emp_audit_upd
    AFTER UPDATE ON emp
    REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
    FOR EACH STATEMENT EXECUTE FUNCTION process_emp_audit();
CREATE TRIGGER emp_audit_del
    AFTER DELETE ON emp
    REFERENCING OLD TABLE AS old_table
    FOR EACH STATEMENT EXECUTE FUNCTION process_emp_audit();

43.10.2. イベントによるトリガ #

<title>Triggers on Events</title>

<application>PL/pgSQL</application> can be used to define <link linkend="event-triggers">event triggers</link>. <productname>PostgreSQL</productname> requires that a function that is to be called as an event trigger must be declared as a function with no arguments and a return type of <literal>event_trigger</literal>. PL/pgSQLイベントトリガの定義に使用できます。 イベントトリガとして呼び出される関数は、引数のない関数として宣言され、戻り値の型はevent_triggerとなることがPostgreSQLでは必須です。

When a <application>PL/pgSQL</application> function is called as an event trigger, several special variables are created automatically in the top-level block. They are: PL/pgSQL関数がイベントトリガとして呼び出された場合、数個の特別な変数が最高レベルのブロックで自動的に作成されます。 以下に示します。

TG_EVENT text #

event the trigger is fired for. トリガが起動されるイベント。

TG_TAG text #

command tag for which the trigger is fired. トリガが起動されるコマンドタグ。

<xref linkend="plpgsql-event-trigger-example"/> shows an example of an event trigger function in <application>PL/pgSQL</application>. 例 43.8PL/pgSQLにおけるイベントトリガ関数の一例を示します。

例43.8 PL/pgSQLイベントトリガ関数

<title>A <application>PL/pgSQL</application> Event Trigger Function</title>

This example trigger simply raises a <literal>NOTICE</literal> message each time a supported command is executed. 以下の例では、サポートされたコマンドが実行されたとき、トリガはNOTICEを発生させるだけです。

CREATE OR REPLACE FUNCTION snitch() RETURNS event_trigger AS $$
BEGIN
    RAISE NOTICE 'snitch: % %', tg_event, tg_tag;
END;
$$ LANGUAGE plpgsql;

CREATE EVENT TRIGGER snitch ON ddl_command_start EXECUTE FUNCTION snitch();