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

NOTIFY

NOTIFY <refpurpose>generate a notification</refpurpose> — 通知を生成する

概要

NOTIFY channel [ , payload ]

説明

<title>Description</title>

The <command>NOTIFY</command> command sends a notification event together with an optional <quote>payload</quote> string to each client application that has previously executed <command>LISTEN <replaceable class="parameter">channel</replaceable></command> for the specified channel name in the current database. Notifications are visible to all users. NOTIFYコマンドは、現在のデータベース内で事前に指定チャネル名についてLISTEN channelコマンドを実行したクライアントアプリケーションにペイロード文字列(省略可能)を持つ通知イベントを送ります。 通知はすべてのユーザから可視です。

<command>NOTIFY</command> provides a simple interprocess communication mechanism for a collection of processes accessing the same <productname>PostgreSQL</productname> database. A payload string can be sent along with the notification, and higher-level mechanisms for passing structured data can be built by using tables in the database to pass additional data from notifier to listener(s). NOTIFYは同一のPostgreSQLデータベースにアクセスするプロセスの集合に対する単純なプロセス間通信の仕組みを提供します。 通知の際にペイロード文字列を送信することができます。 また、データベース内のテーブルを使用して通知者から(1つまたは複数の)リスナに追加的なデータを渡すことにより、構造化されたデータを渡す高度な仕組みを構築することができます。

The information passed to the client for a notification event includes the notification channel name, the notifying session's server process <acronym>PID</acronym>, and the payload string, which is an empty string if it has not been specified. 通知イベントとしてクライアントに渡される情報には、通知チャネル名と通知を行うセッションのサーバプロセスのPID、ペイロード文字列(指定されていなければ空文字列)が含まれます。

It is up to the database designer to define the channel names that will be used in a given database and what each one means. Commonly, the channel name is the same as the name of some table in the database, and the notify event essentially means, <quote>I changed this table, take a look at it to see what's new</quote>. But no such association is enforced by the <command>NOTIFY</command> and <command>LISTEN</command> commands. For example, a database designer could use several different channel names to signal different sorts of changes to a single table. Alternatively, the payload string could be used to differentiate various cases. 各データベースにおいて使用される通知チャネル名とその意味についての定義は、データベース設計者に任されています。 通知チャネル名には、データベース内のテーブル名と同じものを使用するのが一般的です。 通知イベントは本質的にこのテーブルを変更しました。変更された箇所を確認してくださいということを意味するものです。 しかし、NOTIFYコマンドとLISTENコマンドでは、そのような関連付けは強制されていません。 例えば、データベース設計者は、1つのテーブルに対する異なる種類の変更を通知するために、複数の異なる通知チャネル名を使用することができます。 他の方法としてペイロード文字列を使用して各種様々な状況に対応させることもできます。

When <command>NOTIFY</command> is used to signal the occurrence of changes to a particular table, a useful programming technique is to put the <command>NOTIFY</command> in a statement trigger that is triggered by table updates. In this way, notification happens automatically when the table is changed, and the application programmer cannot accidentally forget to do it. 特定のテーブルが変更されたことを通知するためにNOTIFYを使用する場合、NOTIFYをテーブル更新時に発行される文トリガー内に配置すると便利です。 こうすると、通知はテーブルが変更された時に自動的に行われるので、アプリケーションプログラマが通知の実行を忘れるといった事故を防ぐことができます。

<command>NOTIFY</command> interacts with SQL transactions in some important ways. Firstly, if a <command>NOTIFY</command> is executed inside a transaction, the notify events are not delivered until and unless the transaction is committed. This is appropriate, since if the transaction is aborted, all the commands within it have had no effect, including <command>NOTIFY</command>. But it can be disconcerting if one is expecting the notification events to be delivered immediately. Secondly, if a listening session receives a notification signal while it is within a transaction, the notification event will not be delivered to its connected client until just after the transaction is completed (either committed or aborted). Again, the reasoning is that if a notification were delivered within a transaction that was later aborted, one would want the notification to be undone somehow &mdash; but the server cannot <quote>take back</quote> a notification once it has sent it to the client. So notification events are only delivered between transactions. The upshot of this is that applications using <command>NOTIFY</command> for real-time signaling should try to keep their transactions short. NOTIFYとSQLトランザクションの間には、いくつかの重要な相互作用があります。 まず、NOTIFYがトランザクション内部で実行された場合、通知イベントはトランザクションがコミットされない限り配送されません。 トランザクションがアボートされた場合、NOTIFYだけでなく、そのトランザクション内で行われたコマンドが全て無効化されるので、これは妥当といえます。 しかし、通知イベントが即座に配送されることを期待していた場合、当惑するかもしれません。 次に、監視中のセッションがトランザクション処理中に通知シグナルを受け取った場合、そのトランザクションが(コミットもしくはアボートされて)完了するまで、通知イベントは接続しているクライアントに配送されません。 この理由も同じです。トランザクションに通知が配送された後にそのトランザクションがアボートされた場合、何とかして通知を取り消したくなりますが、サーバはいったんクライアントに送信した通知を取り戻すことはできません。 したがって、通知イベントはトランザクションとトランザクションの合間にのみ配送されます。 結論として、NOTIFYを使用してシグナルの実時間処理をするアプリケーションではトランザクションを短くしておかなければなりません。

If the same channel name is signaled multiple times with identical payload strings within the same transaction, only one instance of the notification event is delivered to listeners. On the other hand, notifications with distinct payload strings will always be delivered as distinct notifications. Similarly, notifications from different transactions will never get folded into one notification. Except for dropping later instances of duplicate notifications, <command>NOTIFY</command> guarantees that notifications from the same transaction get delivered in the order they were sent. It is also guaranteed that messages from different transactions are delivered in the order in which the transactions committed. 同じチャネル名が、同一トランザクションから同じペイロード文字列で複数回通知される場合、1つの通知インスタンスのみをリスナに伝えます。 一方、異なるペイロード文字列を持つ通知は常に別の通知として伝えられます。 同様に別のトランザクションからの通知が1つの通知にまとめられることは決してありません。 重複する通知インスタンスを後で削除する場合は例外ですが、NOTIFYは同一トランザクションからの通知は送信された順番に配送されることを保証します。 また異なるトランザクションからのメッセージがトランザクションのコミット順で配送されることも保証します。

It is common for a client that executes <command>NOTIFY</command> to be listening on the same notification channel itself. In that case it will get back a notification event, just like all the other listening sessions. Depending on the application logic, this could result in useless work, for example, reading a database table to find the same updates that that session just wrote out. It is possible to avoid such extra work by noticing whether the notifying session's server process <acronym>PID</acronym> (supplied in the notification event message) is the same as one's own session's <acronym>PID</acronym> (available from <application>libpq</application>). When they are the same, the notification event is one's own work bouncing back, and can be ignored. NOTIFYを実行するクライアント自身が、その通知の通知チャネルを監視していることはよくあります。 この場合、同じ通知名を監視する他のセッションに対するのと同じように通知イベントが戻ってきます。 アプリケーションのロジックにもよりますが、これは無駄な作業になることがあります。 例えば、そのセッションが書き出したばかりのデータベースに対する更新を調べるためにテーブルの再読み込みを行う場合などが考えられます。 通知元セッションのサーバプロセスのPID(通知イベントメッセージ内にあります)と、自分自身のPID(libpqで得られます)が同じかどうか調べることで、こういった余計な作業を回避できます。 PIDが同じであれば、その通知イベントは自分自身から跳ね返ってきたものであり、無視することができます。

パラメータ

<title>Parameters</title>
channel

Name of the notification channel to be signaled (any identifier). シグナルとして送られる通知チャネル名です(任意の識別子)。

payload

The <quote>payload</quote> string to be communicated along with the notification. This must be specified as a simple string literal. In the default configuration it must be shorter than 8000 bytes. (If binary data or large amounts of information need to be communicated, it's best to put it in a database table and send the key of the record.) 通知と一緒に通信されるペイロード文字列です。 これは単純な文字列リテラルとして指定されなければなりません。 デフォルトの設定では、8000バイト未満でなければなりません。 (バイナリデータまたは大規模な情報を渡さなければならないのであれば、データベーステーブル内に格納しレコードのキーを送信することが最善です。)

注釈

<title>Notes</title>

There is a queue that holds notifications that have been sent but not yet processed by all listening sessions. If this queue becomes full, transactions calling <command>NOTIFY</command> will fail at commit. The queue is quite large (8GB in a standard installation) and should be sufficiently sized for almost every use case. However, no cleanup can take place if a session executes <command>LISTEN</command> and then enters a transaction for a very long time. Once the queue is half full you will see warnings in the log file pointing you to the session that is preventing cleanup. In this case you should make sure that this session ends its current transaction so that cleanup can proceed. 送信済みだがすべての監視セッションでは処理されていない通知を保持するためのキューが存在します。 このキューがいっぱいになると、NOTIFYを呼び出すトランザクションのコミットに失敗します。 キューはかなり大きなもの(標準のインストレーションで8ギガバイト)であり、ほとんどすべての環境で十分な大きさであるはずです。 しかしセッションがNOTIFYを実行した後に長期間のトランザクションに入った場合、キューからクリーンアップできなくなります。 キューの半分までたまると、ログファイル内にクリーンアップを妨げているセッションを指し示す警告が現れるようになります。 この場合、クリーンアップ処理が進むように、確実にそのセッションでその現在のトランザクションを完了させるようにしなければなりません

The function <function>pg_notification_queue_usage</function> returns the fraction of the queue that is currently occupied by pending notifications. See <xref linkend="functions-info"/> for more information. 関数pg_notification_queue_usageは現在、保留中の通知によって占められているキューの割合を返します。 詳細な情報については9.26を参照してください。

A transaction that has executed <command>NOTIFY</command> cannot be prepared for two-phase commit. NOTIFYを実行したトランザクションでは二相コミットを準備することはできません。

pg_notify

To send a notification you can also use the function <literal><function>pg_notify</function>(<type>text</type>, <type>text</type>)</literal>. The function takes the channel name as the first argument and the payload as the second. The function is much easier to use than the <command>NOTIFY</command> command if you need to work with non-constant channel names and payloads. 通知を送信するために関数pg_notify(text,text)を使用することもできます。 この関数は第1引数としてチャネル名、第2引数としてペイロードを取ります。 不定のチャネル名、ペイロードで作業しなければならない場合は、NOTIFYコマンドよりこの関数を使用する方がかなり簡単です。

<title>Examples</title>

Configure and execute a listen/notify sequence from <application>psql</application>: psqlから監視/通知処理の設定と実行を行います。

LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
NOTIFY virtual, 'This is the payload';
Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.

LISTEN foo;
SELECT pg_notify('fo' || 'o', 'pay' || 'load');
Asynchronous notification "foo" with payload "payload" received from server process with PID 14728.

互換性

<title>Compatibility</title>

There is no <command>NOTIFY</command> statement in the SQL standard. 標準SQLにはNOTIFYはありません。

関連項目

<title>See Also</title> LISTEN, UNLISTEN