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

9.28. トリガ関数 #

<title>Trigger Functions</title>

While many uses of triggers involve user-written trigger functions, <productname>PostgreSQL</productname> provides a few built-in trigger functions that can be used directly in user-defined triggers. These are summarized in <xref linkend="builtin-triggers-table"/>. (Additional built-in trigger functions exist, which implement foreign key constraints and deferred index constraints. Those are not documented here since users need not use them directly.) 多くの場合トリガにはユーザ記述のトリガ関数が必要になりますが、PostgreSQLはユーザ定義トリガで直接使用できる小数の組み込みの取り化関数を提供しています。 これらは表 9.103にまとめられています。 (追加の組み込みトリガ関数があり、外部キー制約と遅延インデックス制約を実装しています。 ユーザがこれらを直接必要とすることはないので、ここには記述されていません。)

For more information about creating triggers, see <xref linkend="sql-createtrigger"/>. トリガ作成についてより詳細はCREATE TRIGGERを参照ください。

表9.103 組み込みトリガ関数

<title>Built-In Trigger Functions</title>

Function 関数

Description 説明

Example Usage 使用例

suppress_redundant_updates_trigger ( ) → trigger

Suppresses do-nothing update operations. See below for details. do-nothing更新操作を抑止します。 詳細は以下を参照してください。

CREATE TRIGGER ... suppress_redundant_updates_trigger()

tsvector_update_trigger ( ) → trigger

Automatically updates a <type>tsvector</type> column from associated plain-text document column(s). The text search configuration to use is specified by name as a trigger argument. See <xref linkend="textsearch-update-triggers"/> for details. 関連付けされた平文文書列から自動的にtsvector列を更新します。 使用するテキスト検索設定はトリガ引数で指定します。 詳細は12.4.3をご覧ください。

CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)

tsvector_update_trigger_column ( ) → trigger

Automatically updates a <type>tsvector</type> column from associated plain-text document column(s). The text search configuration to use is taken from a <type>regconfig</type> column of the table. See <xref linkend="textsearch-update-triggers"/> for details. 関連付けされた平文文書列から自動的にtsvector列を更新します。 使用するテキスト検索設定はテーブルのregconfig列が用いられます。 詳細は12.4.3をご覧ください。

CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, tsconfigcol, title, body)


The <function>suppress_redundant_updates_trigger</function> function, when applied as a row-level <literal>BEFORE UPDATE</literal> trigger, will prevent any update that does not actually change the data in the row from taking place. This overrides the normal behavior which always performs a physical row update regardless of whether or not the data has changed. (This normal behavior makes updates run faster, since no checking is required, and is also useful in certain cases.) 行レベルBEFORE UPDATEトリガとしてsuppress_redundant_updates_trigger関数が適用されると、実際には行の中でデータを変更しない更新が行われるのを防ぎます。 これはデータが変更されるかどうかに関わらず、物理的に行の更新を行う通常の振る舞いを置き換えます。 (この通常の動作は、検査を必要としないため更新をより迅速に行い、場合によっては便利です。)

Ideally, you should avoid running updates that don't actually change the data in the record. Redundant updates can cost considerable unnecessary time, especially if there are lots of indexes to alter, and space in dead rows that will eventually have to be vacuumed. However, detecting such situations in client code is not always easy, or even possible, and writing expressions to detect them can be error-prone. An alternative is to use <function>suppress_redundant_updates_trigger</function>, which will skip updates that don't change the data. You should use this with care, however. The trigger takes a small but non-trivial time for each record, so if most of the records affected by updates do actually change, use of this trigger will make updates run slower on average. 理想的には、通常実際レコード内のデータを変更しない更新の実行を避けるべきです。 冗長な更新により、特に変更対象の多くのインデックスが存在する場合、無視できない不要な時間にかかるコストが発生することがあります。 また、最後にはバキュームしなければならなくなる不要行が場所を取ることになります。 しかし、こうした状況をクライアント側で判定することは常に簡単ではありません。 また、可能であったとしても、それを検知するための式の記述はエラーを招きがちです。 他の方法として、suppress_redundant_updates_triggerを使用することがあります。 これはデータを変更しない更新を飛ばします。 しかしこの関数は注意して使用しなければなりません。 このトリガはレコードごとに小さな、しかし僅かではない時間がかかります。 このため、更新が影響するレコードのほとんどが実際に変更された場合、このトリガは平均すると更新の実行を低速にします。

The <function>suppress_redundant_updates_trigger</function> function can be added to a table like this: suppress_redundant_updates_trigger関数は以下のようにテーブルに追加できます。

CREATE TRIGGER z_min_update
BEFORE UPDATE ON tablename
FOR EACH ROW EXECUTE FUNCTION suppress_redundant_updates_trigger();

In most cases, you need to fire this trigger last for each row, so that it does not override other triggers that might wish to alter the row. Bearing in mind that triggers fire in name order, you would therefore choose a trigger name that comes after the name of any other trigger you might have on the table. (Hence the <quote>z</quote> prefix in the example.) ほとんどの場合、行を変更するかも知れない他のトリガを置き換えないために、それぞれの行に対しこのトリガを最後に起動させる必要があります。 トリガは名前順に起動されることを判っているとして、テーブル上に存在する可能性のある他のトリガの名前の後に続くようトリガ名を選択できます。 (それで例中にz接頭辞があります。)