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

LISTEN

LISTEN <refpurpose>listen for a notification</refpurpose> — 通知を監視する

概要

LISTEN channel

説明

<title>Description</title>

<command>LISTEN</command> registers the current session as a listener on the notification channel named <replaceable class="parameter">channel</replaceable>. If the current session is already registered as a listener for this notification channel, nothing is done. LISTENは現在のセッションを、通知チャネルchannelのリスナとして登録します。 現在のセッションが既に指定した通知チャネルのリスナとして登録されている場合は、何も起こりません。

Whenever the command <command>NOTIFY <replaceable class="parameter">channel</replaceable></command> is invoked, either by this session or another one connected to the same database, all the sessions currently listening on that notification channel are notified, and each will in turn notify its connected client application. このセッションまたは同一データベースに接続している別のセッションによってNOTIFY channelが実行されると、現在その通知チャネルを監視している全てのセッションに対して通知されます。 次に、各セッションは接続中のクライアントアプリケーションにこれを通知します。

A session can be unregistered for a given notification channel with the <command>UNLISTEN</command> command. A session's listen registrations are automatically cleared when the session ends. UNLISTENコマンドを使って、セッションに登録された指定通知チャネルを解除できます。 また、セッションの監視登録はそのセッションが終了した時点で自動的に削除されます。

The method a client application must use to detect notification events depends on which <productname>PostgreSQL</productname> application programming interface it uses. With the <application>libpq</application> library, the application issues <command>LISTEN</command> as an ordinary SQL command, and then must periodically call the function <function>PQnotifies</function> to find out whether any notification events have been received. Other interfaces such as <application>libpgtcl</application> provide higher-level methods for handling notify events; indeed, with <application>libpgtcl</application> the application programmer should not even issue <command>LISTEN</command> or <command>UNLISTEN</command> directly. See the documentation for the interface you are using for more details. クライアントアプリケーションが通知イベントを検出する方法は、使用しているPostgreSQLアプリケーションプログラミングインタフェースに依存します。 libpqライブラリを使用するアプリケーションでは、LISTENを通常のSQLコマンドとして発行し、その後、PQnotifiesルーチンを定期的に呼び出して通知イベントが受信されたかどうかを調べる必要があります。 libpgtcl等の他のインタフェースには、通知イベントを扱うより高レベルな方法が用意されています。 実際、libpgtclを使ったアプリケーションの場合、プログラマがLISTENUNLISTENを直接発行する必要すらありません。 詳細については、使用中のインタフェースのドキュメントを参照してください。

パラメータ

<title>Parameters</title>
channel

Name of a notification channel (any identifier). 通知チャネルの名前です(任意の識別子)。

注釈

<title>Notes</title>

<command>LISTEN</command> takes effect at transaction commit. If <command>LISTEN</command> or <command>UNLISTEN</command> is executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged. LISTENはトランザクションのコミット時に有効になります。 LISTENまたはUNLISTENがトランザクション内で実行され、それがロールバックされた場合、監視している通知チャネルの集合は変更されません。

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

There is a race condition when first setting up a listening session: if concurrently-committing transactions are sending notify events, exactly which of those will the newly listening session receive? The answer is that the session will receive all events committed after an instant during the transaction's commit step. But that is slightly later than any database state that the transaction could have observed in queries. This leads to the following rule for using <command>LISTEN</command>: first execute (and commit!) that command, then in a new transaction inspect the database state as needed by the application logic, then rely on notifications to find out about subsequent changes to the database state. The first few received notifications might refer to updates already observed in the initial database inspection, but this is usually harmless. 監視するセッションを最初に設定する時に、競合状態があります。 同時にコミット中の複数のトランザクションが通知イベントを送った場合、新しく監視を始めたセッションはそのうちのどれをまさに受信するでしょうか。 答は、トランザクションのコミット段階のある瞬間の後にコミットされたすべてのイベントを受信する、です。 しかし、これは問い合わせにおいてトランザクションが気づくデータベースの状態よりもわずかに後です。 ここからLISTENを使う場合の以下のような規則が導かれます。 まずそのコマンドを実行する(そしてコミットする!)、それから新しいトランザクションでアプリケーションのロジックの必要に応じてデータベースの状態を検査する、それから通知に基づいてデータベースの状態に対するその後の変更を確認する。 最初に受信した通知のいくつかはデータベースの最初の検査ですでに確認した更新を参照しているかもしれませんが、これは普通は無害です。

<xref linkend="sql-notify"/> contains a more extensive discussion of the use of <command>LISTEN</command> and <command>NOTIFY</command>. NOTIFYには、LISTENおよび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.

互換性

<title>Compatibility</title>

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

関連項目

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