]> begriffs open source - ai-pg/blob - full-docs/txt/tcn.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / tcn.txt
1
2 F.44. tcn — a trigger function to notify listeners of changes to table
3 content #
4
5    The tcn module provides a trigger function that notifies listeners of
6    changes to any table on which it is attached. It must be used as an
7    AFTER trigger FOR EACH ROW.
8
9    This module is considered “trusted”, that is, it can be installed by
10    non-superusers who have CREATE privilege on the current database.
11
12    Only one parameter may be supplied to the function in a CREATE TRIGGER
13    statement, and that is optional. If supplied it will be used for the
14    channel name for the notifications. If omitted tcn will be used for the
15    channel name.
16
17    The payload of the notifications consists of the table name, a letter
18    to indicate which type of operation was performed, and column
19    name/value pairs for primary key columns. Each part is separated from
20    the next by a comma. For ease of parsing using regular expressions,
21    table and column names are always wrapped in double quotes, and data
22    values are always wrapped in single quotes. Embedded quotes are
23    doubled.
24
25    A brief example of using the extension follows.
26 test=# create table tcndata
27 test-#   (
28 test(#     a int not null,
29 test(#     b date not null,
30 test(#     c text,
31 test(#     primary key (a, b)
32 test(#   );
33 CREATE TABLE
34 test=# create trigger tcndata_tcn_trigger
35 test-#   after insert or update or delete on tcndata
36 test-#   for each row execute function triggered_change_notification();
37 CREATE TRIGGER
38 test=# listen tcn;
39 LISTEN
40 test=# insert into tcndata values (1, date '2012-12-22', 'one'),
41 test-#                            (1, date '2012-12-23', 'another'),
42 test-#                            (2, date '2012-12-23', 'two');
43 INSERT 0 3
44 Asynchronous notification "tcn" with payload ""tcndata",I,"a"='1',"b"='2012-12-2
45 2'" received from server process with PID 22770.
46 Asynchronous notification "tcn" with payload ""tcndata",I,"a"='1',"b"='2012-12-2
47 3'" received from server process with PID 22770.
48 Asynchronous notification "tcn" with payload ""tcndata",I,"a"='2',"b"='2012-12-2
49 3'" received from server process with PID 22770.
50 test=# update tcndata set c = 'uno' where a = 1;
51 UPDATE 2
52 Asynchronous notification "tcn" with payload ""tcndata",U,"a"='1',"b"='2012-12-2
53 2'" received from server process with PID 22770.
54 Asynchronous notification "tcn" with payload ""tcndata",U,"a"='1',"b"='2012-12-2
55 3'" received from server process with PID 22770.
56 test=# delete from tcndata where a = 1 and b = date '2012-12-22';
57 DELETE 1
58 Asynchronous notification "tcn" with payload ""tcndata",D,"a"='1',"b"='2012-12-2
59 2'" received from server process with PID 22770.