2025年9月25日: PostgreSQL 18 釋出!
支援的版本: 當前 (18) / 17 / 16 / 15 / 14 / 13
開發版本: devel
不支援的版本: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2

F.44. tcn — 通知監聽器表內容更改的觸發器函式 #

tcn 模組提供了一個觸發器函式,用於通知在該觸發器函式上附加的任何表內容更改的監聽器。它必須用作 AFTER 觸發器 FOR EACH ROW

此模組被認為是受信任的,這意味著非超級使用者也可以在其擁有的資料庫上安裝它,前提是他們具有 CREATE 許可權。

CREATE TRIGGER 語句中,該函式最多隻能提供一個引數,並且該引數是可選的。如果提供了引數,則它將用於通知的頻道名稱。如果省略,則 tcn 將用作頻道名稱。

通知的有效載荷包含表名、指示執行何種操作的字母以及主鍵列的列名/值對。每個部分之間用逗號分隔。為了方便使用正則表示式進行解析,表名和列名始終用雙引號括起來,資料值始終用單引號括起來。嵌入的引號會加倍。

下面是一個使用該擴充套件的簡短示例。

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.

提交更正

如果您在文件中發現任何不正確之處,與您在使用特定功能時的體驗不符,或者需要進一步澄清,請使用 此表單 來報告文件問題。