天天看點

【轉】PostgreSQL學習第十四篇 listen與notify

PostgreSQL提供了client端之間通過伺服器端進行消息通信的機制。這種機制是通過listen和notify指令來完成的。

session1:

postgres=# listen postgres;

LISTEN

postgres=# listen liming

postgres-# ;

session2:

postgres=# notify postgres,'hello word';

NOTIFY

postgres=# notify liming,'hello liming';

postgres=#

session1;

postgres=# select 1;   --随便執行了一個指令

 ?column?

----------

        1

(1 row)

Asynchronous notification "postgres" with payload "hello word" received from server process with PID 12241.

Asynchronous notification "liming" with payload "hello liming" received from server process with PID 12241.

postgres=#  

listen和notify的相關指令:

    1. listen:監聽消息通道

    2. unlisten:取消先前的監聽

    3. notify:發送消息到消息通道中

    4. pg_notify():與notify指令功能相同

    5. pg_listening_channels():調用此函數可以查詢目前session已注冊了哪些消息監聽

繼續閱讀