Watch a table for changes and push a notification with a payload describing the change.
In the Postgres shell:
-- Create the functions
\i watch.sql
-- Create a table to watch.
create table users (id int, name text);
-- Watch the table and send notifications to channel 'changefeed'
select watch_table(users, 'changefeed');
-- Listen on the channel. This is asynchronous.
listen changefeed;
-- Insert a record into the users table.
insert into users values (1, 'Joe');
You will see the following appear in the shell as a notification.
Asynchronous notification "changefeed" with payload "{"row_data": {"id": 1, "name": "Joe"}, "operation": "INSERT", "table_name": "users", "schema_name": "public", "capture_time": "2016-03-04T21:24:48.24746+00:00", "transaction_time": "2016-03-04T21:24:48.247295+00:00"}" received from server process with PID 84.
in this code:
IF (TG_OP = 'UPDATE' AND TG_LEVEL = 'ROW') THEN
rowdata = row_to_json(OLD.*);
better to use NEW.*
if use OLD - trigger cannot send actual updated data