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

9.29. イベントトリガ関数 #

<title>Event Trigger Functions</title>

<productname>PostgreSQL</productname> provides these helper functions to retrieve information from event triggers. PostgreSQLはイベントトリガについての情報を取得するために以下のヘルパ関数を提供しています。

For more information about event triggers, see <xref linkend="event-triggers"/>. イベントトリガについての詳細は第40章を参照して下さい。

9.29.1. コマンド側での変更を捕らえる #

<title>Capturing Changes at Command End</title>
pg_event_trigger_ddl_commands () → setof record

<function>pg_event_trigger_ddl_commands</function> returns a list of <acronym>DDL</acronym> commands executed by each user action, when invoked in a function attached to a <literal>ddl_command_end</literal> event trigger. If called in any other context, an error is raised. <function>pg_event_trigger_ddl_commands</function> returns one row for each base command executed; some commands that are a single SQL sentence may return more than one row. This function returns the following columns: pg_event_trigger_ddl_commandsddl_command_endイベントトリガーに付与された関数から起動されると、各ユーザの操作によって実行されたDDLコマンドの一覧を返します。 それ以外の環境から呼び出された場合はエラーが発生します。 pg_event_trigger_ddl_commandsは、実行された基となるコマンドのそれぞれについて1行を返します。 1つのSQL文として実行されるいくつかのコマンドに対して、複数の行が返されることもあります。 この関数は以下の列を返します。

名前説明
classidoidオブジェクトが属するカタログのOID
objidoidカタログ内のオブジェクトのOID
objsubidintegerオブジェクトのサブID(例えば、列の列番号)
command_tagtextコマンドのタグ
object_typetextオブジェクトの型
schema_nametext Name of the schema the object belongs in, if any; otherwise <literal>NULL</literal>. No quoting is applied. オブジェクトが属するスキーマの名前(あれば)。 なければNULL。 引用符づけされない。
object_identitytext Text rendering of the object identity, schema-qualified. Each identifier included in the identity is quoted if necessary. オブジェクトの識別をテキスト表現したもので、スキーマ修飾される。 識別内に存在する各識別子は、必要なら引用符で括られる。
in_extensionbooleanコマンドが拡張のスクリプトの一部なら真
commandpg_ddl_command A complete representation of the command, in internal format. This cannot be output directly, but it can be passed to other functions to obtain different pieces of information about the command. コマンドを内部形式で完全に表現したもの。 これを直接出力することはできないが、コマンドについて他の情報を得るために、他の関数に渡すことができる。

9.29.2. DDLコマンドで削除されたオブジェクトの処理 #

<title>Processing Objects Dropped by a DDL Command</title>
pg_event_trigger_dropped_objects () → setof record

<function>pg_event_trigger_dropped_objects</function> returns a list of all objects dropped by the command in whose <literal>sql_drop</literal> event it is called. If called in any other context, an error is raised. This function returns the following columns: 関数pg_event_trigger_dropped_objectsは、それが呼ばれたsql_dropイベントのコマンドにより削除された全てのオブジェクトのリストを返します。 それ以外の状況で呼ばれた場合、エラーが生じます。 この関数は以下の列を返します。

名前説明
classidoidオブジェクトが所属するカタログのOID
objidoidカタログ内に所有するオブジェクトのOID
objsubidintegerオブジェクトのサブID(例えば、列の列番号)
originalbooleanこれが削除のルートオブジェクトの一つなら真
normalboolean True if there was a normal dependency relationship in the dependency graph leading to this object このオブジェクトへと至る依存関係グラフで、通常の依存があるなら真
is_temporaryboolean True if this was a temporary object オブジェクトが一時オブジェクトであったなら真
object_typetextオブジェクトの型
schema_nametext Name of the schema the object belonged in, if any; otherwise <literal>NULL</literal>. No quoting is applied. オブジェクトが所属しているスキーマの名前(あれば)。 なければNULL。 引用符づけされない。
object_nametext Name of the object, if the combination of schema and name can be used as a unique identifier for the object; otherwise <literal>NULL</literal>. No quoting is applied, and name is never schema-qualified. スキーマと名前の組み合わせがオブジェクトに対する一意の識別子として使用可能な場合はオブジェクトの名前。そうでないときはNULL。 引用符は適用されず、名前は決してスキーマで修飾されない。
object_identitytext Text rendering of the object identity, schema-qualified. Each identifier included in the identity is quoted if necessary. オブジェクト識別のテキスト表現で、スキーマ修飾される。 識別内に存在する各識別子は必要であれば引用符で括られる。
address_namestext[] An array that, together with <literal>object_type</literal> and <literal>address_args</literal>, can be used by the <function>pg_get_object_address</function> function to recreate the object address in a remote server containing an identically named object of the same kind. object_typeおよびaddress_argsと一緒にpg_get_object_address()で使うことで、同じ種類で全く同じ名前のオブジェクトを含むリモートサーバ内のオブジェクトアドレスを再作成できる配列。
address_argstext[] Complement for <literal>address_names</literal> address_namesの補足。

The <function>pg_event_trigger_dropped_objects</function> function can be used in an event trigger like this: 関数pg_event_trigger_dropped_objectsは以下のようにイベントトリガとして使用可能です。

CREATE FUNCTION test_event_trigger_for_drops()
        RETURNS event_trigger LANGUAGE plpgsql AS $$
DECLARE
    obj record;
BEGIN
    FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
    LOOP
        RAISE NOTICE '% dropped object: % %.% %',
                     tg_tag,
                     obj.object_type,
                     obj.schema_name,
                     obj.object_name,
                     obj.object_identity;
    END LOOP;
END;
$$;
CREATE EVENT TRIGGER test_event_trigger_for_drops
   ON sql_drop
   EXECUTE FUNCTION test_event_trigger_for_drops();

9.29.3. テーブル書き換えイベントの処理 #

<title>Handling a Table Rewrite Event</title>

The functions shown in <xref linkend="functions-event-trigger-table-rewrite"/> provide information about a table for which a <literal>table_rewrite</literal> event has just been called. If called in any other context, an error is raised. 表 9.104に示す関数は、table_rewriteイベントが呼び出されたばかりのテーブルについての情報を提供します。 それ以外の状況で呼び出された場合はエラーが発生します。

表9.104 テーブル書き換え情報関数

<title>Table Rewrite Information Functions</title>

Function 関数

Description 説明

pg_event_trigger_table_rewrite_oid () → oid

Returns the OID of the table about to be rewritten. 書き換えようとされているテーブルのOIDを返します。

pg_event_trigger_table_rewrite_reason () → integer

Returns a code explaining the reason(s) for rewriting. The exact meaning of the codes is release dependent. 書き換えの理由を説明する理由コードを返します。 コードの正確な意味はリリースに依存します。


These functions can be used in an event trigger like this: これらの関数はイベントトリガ中で次のように使用できます。

CREATE FUNCTION test_event_trigger_table_rewrite_oid()
 RETURNS event_trigger
 LANGUAGE plpgsql AS
$$
BEGIN
  RAISE NOTICE 'rewriting table % for reason %',
                pg_event_trigger_table_rewrite_oid()::regclass,
                pg_event_trigger_table_rewrite_reason();
END;
$$;

CREATE EVENT TRIGGER test_table_rewrite_oid
                  ON table_rewrite
   EXECUTE FUNCTION test_event_trigger_table_rewrite_oid();