1 <!-- doc/src/sgml/tcn.sgml -->
3 <sect1 id=
"tcn" xreflabel=
"tcn">
4 <title>tcn
— a trigger function to notify listeners of changes to table content
</title>
10 <indexterm zone=
"tcn">
11 <primary>triggered_change_notification
</primary>
15 The
<filename>tcn
</filename> module provides a trigger function that notifies
16 listeners of changes to any table on which it is attached. It must be
17 used as an
<literal>AFTER
</literal> trigger
<literal>FOR EACH ROW
</literal>.
21 This module is considered
<quote>trusted
</quote>, that is, it can be
22 installed by non-superusers who have
<literal>CREATE
</literal> privilege
23 on the current database.
27 Only one parameter may be supplied to the function in a
28 <literal>CREATE TRIGGER
</literal> statement, and that is optional. If supplied
29 it will be used for the channel name for the notifications. If omitted
30 <literal>tcn
</literal> will be used for the channel name.
34 The payload of the notifications consists of the table name, a letter to
35 indicate which type of operation was performed, and column name/value pairs
36 for primary key columns. Each part is separated from the next by a comma.
37 For ease of parsing using regular expressions, table and column names are
38 always wrapped in double quotes, and data values are always wrapped in
39 single quotes. Embedded quotes are doubled.
43 A brief example of using the extension follows.
46 test=# create table tcndata
48 test(# a int not null,
49 test(# b date not null,
51 test(# primary key (a, b)
54 test=# create trigger tcndata_tcn_trigger
55 test-# after insert or update or delete on tcndata
56 test-# for each row execute function triggered_change_notification();
60 test=# insert into tcndata values (
1, date '
2012-
12-
22', 'one'),
61 test-# (
1, date '
2012-
12-
23', 'another'),
62 test-# (
2, date '
2012-
12-
23', 'two');
64 Asynchronous notification
"tcn" with payload
""tcndata
",I,"a
"='1',"b
"='2012-12-22'" received from server process with PID
22770.
65 Asynchronous notification
"tcn" with payload
""tcndata
",I,"a
"='1',"b
"='2012-12-23'" received from server process with PID
22770.
66 Asynchronous notification
"tcn" with payload
""tcndata
",I,"a
"='2',"b
"='2012-12-23'" received from server process with PID
22770.
67 test=# update tcndata set c = 'uno' where a =
1;
69 Asynchronous notification
"tcn" with payload
""tcndata
",U,"a
"='1',"b
"='2012-12-22'" received from server process with PID
22770.
70 Asynchronous notification
"tcn" with payload
""tcndata
",U,"a
"='1',"b
"='2012-12-23'" received from server process with PID
22770.
71 test=# delete from tcndata where a =
1 and b = date '
2012-
12-
22';
73 Asynchronous notification
"tcn" with payload
""tcndata
",D,"a
"='1',"b
"='2012-12-22'" received from server process with PID
22770.