支援的版本:目前 (17) / 16 / 15 / 14 / 13
開發版本:devel
不支援的版本:12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2

F.42. tcn — 用於通知監聽器表格內容變更的觸發函數 #

tcn 模組提供一個觸發函數,用於通知監聽器它所附加的任何表格的變更。它必須用作 AFTER 觸發器 FOR EACH ROW

此模組被視為 受信任的,也就是說,它可由在目前資料庫中具有 CREATE 權限的非超級使用者安裝。

CREATE TRIGGER 陳述式中,只能提供一個參數給該函數,而且是選擇性的。如果提供,它將用作通知的通道名稱。 如果省略,則 tcn 將用作通道名稱。

通知的 payload 包含表格名稱、一個字母表示執行的操作類型,以及主要索引鍵欄位的欄位名稱/值配對。每個部分之間用逗號分隔。 為了易於使用正則表達式進行解析,表格和欄位名稱始終用雙引號括起來,資料值始終用單引號括起來。 嵌入的引號會重複。

以下是使用該擴充功能的一個簡短範例。

test=# create table tcndata
test-#   (
test(#     a int not null,
test(#     b date not null,
test(#     c text,
test(#     primary key (a, b)
test(#   );
CREATE TABLE
test=# create trigger tcndata_tcn_trigger
test-#   after insert or update or delete on tcndata
test-#   for each row execute function triggered_change_notification();
CREATE TRIGGER
test=# listen tcn;
LISTEN
test=# insert into tcndata values (1, date '2012-12-22', 'one'),
test-#                            (1, date '2012-12-23', 'another'),
test-#                            (2, date '2012-12-23', 'two');
INSERT 0 3
Asynchronous notification "tcn" with payload ""tcndata",I,"a"='1',"b"='2012-12-22'" received from server process with PID 22770.
Asynchronous notification "tcn" with payload ""tcndata",I,"a"='1',"b"='2012-12-23'" received from server process with PID 22770.
Asynchronous notification "tcn" with payload ""tcndata",I,"a"='2',"b"='2012-12-23'" received from server process with PID 22770.
test=# update tcndata set c = 'uno' where a = 1;
UPDATE 2
Asynchronous notification "tcn" with payload ""tcndata",U,"a"='1',"b"='2012-12-22'" received from server process with PID 22770.
Asynchronous notification "tcn" with payload ""tcndata",U,"a"='1',"b"='2012-12-23'" received from server process with PID 22770.
test=# delete from tcndata where a = 1 and b = date '2012-12-22';
DELETE 1
Asynchronous notification "tcn" with payload ""tcndata",D,"a"='1',"b"='2012-12-22'" received from server process with PID 22770.

提交更正

如果您在文件中看到任何不正確、與您使用特定功能的經驗不符或需要進一步澄清的內容,請使用此表單來報告文件問題。