-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.sql
63 lines (52 loc) · 1.35 KB
/
setup.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
CREATE OR REPLACE FUNCTION notify_mqtt()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify(
'mqtt',
json_build_object(
'operation', TG_OP,
'record', row_to_json(NEW)
)::text
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION notify_mqtt_delete()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify(
'mqtt',
json_build_object(
'operation', TG_OP,
'record', row_to_json(OLD)
)::text
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Drop table
-- DROP TABLE public.table_with_pk;
CREATE TABLE public.table_with_pk (
a serial NOT NULL,
b varchar(30) NULL,
c timestamp NOT NULL DEFAULT now(),
CONSTRAINT table_with_pk_pkey PRIMARY KEY (a, c)
);
-- Table Triggers
-- DROP TRIGGER table_with_pk_changed ON public.table_with_pk;
create trigger table_with_pk_changed after
insert
or
update
on
public.table_with_pk for each row execute function notify_mqtt();
-- DROP TRIGGER table_with_pk_deleted ON public.table_with_pk;
create trigger table_with_pk_deleted after
delete
on
public.table_with_pk for each row execute function notify_mqtt_delete();
-- DROP TRIGGER table_with_pk_truncated ON public.table_with_pk;
create trigger table_with_pk_truncated after
truncate
on
public.table_with_pk for each statement execute function notify_mqtt();