Skip to content

Commit e2f7c54

Browse files
committed
Update README.md
1 parent 310b8ff commit e2f7c54

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

postgres/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,49 @@ This notifications database contains 2 tables, 1 regular function and 1 trigger
2424

2525
1. notify_notification_changes
2626

27+
```
28+
CREATE FUNCTION public.notify_notification_changes()
29+
RETURNS trigger
30+
LANGUAGE 'plpgsql'
31+
COST 100
32+
VOLATILE NOT LEAKPROOF
33+
AS $BODY$
34+
DECLARE
35+
tr_record RECORD;
36+
trigger_data json;
37+
notification_data json;
38+
BEGIN
39+
IF (TG_OP = 'DELETE') THEN
40+
tr_record = OLD;
41+
ELSE
42+
tr_record = NEW;
43+
END IF;
44+
45+
trigger_data = row_to_json(r)
46+
FROM (
47+
SELECT
48+
n.*,
49+
nt.name notification_type
50+
FROM
51+
(SELECT tr_record.*) n
52+
JOIN public.notification_type nt on n.notification_type_id = nt.id
53+
) r;
54+
55+
notification_data = json_build_object(
56+
'table', TG_TABLE_NAME,
57+
'operation', TG_OP,
58+
'data', trigger_data
59+
);
60+
61+
PERFORM pg_notify(
62+
'notifications_data_changed',
63+
notification_data::text
64+
);
65+
66+
RETURN tr_record;
67+
END;
68+
$BODY$;
69+
```
2770

2871
## function calls:
2972

@@ -36,4 +79,4 @@ SELECT public.new_notification(
3679

3780

3881
## pgadmin
39-
![files](./pgadmin.png)
82+
![files](./pgadmin.png)

0 commit comments

Comments
 (0)