非同期通知

79) Postgres supports asynchronous notification via the LISTEN and NOTIFY commands. A backend registers its interest in a particular semaphore with the LISTEN command. All backends that are listening on a particular named semaphore will be notified asynchronously when a NOTIFY of that name is executed by another backend. No additional information is passed from the notifier to the listener. Thus, typically, any actual data that needs to be communicated is transferred through the relation.

Postgres は, LISTENNOTIFY コマンドによる非同期通知機能をサポートしています. バックエンドは LISTEN コマンドを受け取ると, 特定のセマフォへ関心を持ったことを登録します. 名前のついた特定のセマフォを見ているすべてのバックエンドは, 他のバックエンドによって同じ名前の NOTIFY コマンドが実行された時点で非同期的に通知されます. ただし通知の発行側から受信側へ渡される付加的な情報はありません. したがって,典型的にはリレーションを通じ, 両者の間で交換すべき実際のデータを転送することになります.

Note: 80) In the past, the documentation has associated the names used for asyncronous notification with relations or classes. However, there is in fact no direct linkage of the two concepts in the implementation, and the named semaphore in fact does not need to have a corresponding relation previously defined.

以前のドキュメントは, 非同期通知に使う名前とリレーション(クラス)を関連するものとしていました. しかし現実には,実装がこの二つの概念を直接結び付けている部分は何ひとつありません. そして以前定義していたように, 名前付きセマフォが対応するリレーションを持つ必要性も実際にはありません.

81) libpq++ applications are notified whenever a connected backend has received an asynchronous notification. However, the communication from the backend to the frontend is not asynchronous. The libpq++ application must poll the backend to see if there is any pending notification information. After the execution of a query, a frontend may call PgDatabase::Notifies to see if any notification data is currently available from the backend. PgDatabase::Notifies returns the notification from a list of unhandled notifications from the backend. The function eturns NULL if there is no pending notifications from the backend. PgDatabase::Notifies behaves like the popping of a stack. Once a notification is returned from PgDatabase::Notifies, it is considered handled and will be removed from the list of notifications.

接続しているバックエンドが非同期通知を受けるたびに, libpq++ アプリケーションもその通知を受けます. しかしバックエンドからフロントエンドへの通信は非同期的なものではありません. libpq++ アプリケーションは保留中の通知情報がないかどうか確認するために, バックエンドへのポーリングをしなければならないのです. フロントエンドは PgDatabase::Notifies を問い合わせ実行の後に呼び出し, その時点でバックエンドから通知データが届いていないか確認することができます. PgDatabase::Notifies はバックエンドから届いた通知の未処理分リストから,ひとつ通知を返します. バックエンドからの通知が何も保留されていなければ,この関数は NULL を返します. PgDatabase::Notifies の動作はスタックのポップのようなものです. いったん PgDatabase::Notifies が通知を返すとその通知はもはや処理が済んだものとみなされ, 通知リストから取り除かれます.

83) The second sample program gives an example of the use of asynchronous notification.

なお,二番目のサンプルプログラムが非同期通知を使用した例です.