File: trigger_sess_role.sql

package info (click to toggle)
pgq 3.2.6-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 836 kB
  • sloc: sql: 3,412; ansic: 1,990; python: 302; makefile: 89; sh: 2
file content (71 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download
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
64
65
66
67
68
69
70
71
\set VERBOSITY 'terse'
set client_min_messages = 'warning';

create or replace function pgq.insert_event(queue_name text, ev_type text, ev_data text, ev_extra1 text, ev_extra2 text, ev_extra3 text, ev_extra4 text)
returns bigint as $$
declare
    ev_id int8;
begin
    raise warning 'calling insert_event_raw';
    ev_id := pgq.insert_event_raw(queue_name, null, now(), null, null,
        ev_type, ev_data, ev_extra1, ev_extra2, ev_extra3, ev_extra4);
    raise warning 'insert_event(q=[%], t=[%], d=[%], 1=[%], 2=[%], 3=[%], 4=[%]) = %',
        queue_name, ev_type, ev_data, ev_extra1, ev_extra2, ev_extra3, ev_extra4, ev_id;
    return ev_id;
end;
$$ language plpgsql;

select pgq.create_queue('jsontriga_role');
select pgq.create_queue('logutriga_role');
select pgq.create_queue('sqltriga_role');
update pgq.queue set queue_disable_insert = true where queue_name = 'jsontriga_role';
update pgq.queue set queue_disable_insert = true where queue_name = 'logutriga_role';
update pgq.queue set queue_disable_insert = true where queue_name = 'sqltriga_role';


-- create tables
create table jsontriga_role (dat1 text primary key);
create table logutriga_role (dat1 text primary key);
create table sqltriga_role (dat1 text primary key);
create trigger trig after insert or update or delete on jsontriga_role
for each row execute procedure pgq.jsontriga('jsontriga_role');
create trigger trig after insert or update or delete on logutriga_role
for each row execute procedure pgq.logutriga('logutriga_role');
create trigger trig after insert or update or delete on sqltriga_role
for each row execute procedure pgq.sqltriga('sqltriga_role');

-- origin: expect insert_event error
show session_replication_role;
insert into jsontriga_role values ('a');
insert into logutriga_role values ('a');
insert into sqltriga_role values ('a');

-- local: silence, trigger does not call insert_event
set session_replication_role = 'local';
show session_replication_role;
insert into jsontriga_role values ('b');
insert into logutriga_role values ('b');
insert into sqltriga_role values ('b');

-- replica: silence, trigger does not call insert_event
set session_replication_role = 'replica';
show session_replication_role;
insert into jsontriga_role values ('c');
insert into logutriga_role values ('c');
insert into sqltriga_role values ('c');

select * from jsontriga_role;
select * from logutriga_role;
select * from sqltriga_role;

-- restore
set session_replication_role = 'origin';
drop table jsontriga_role;
drop table logutriga_role;
drop table sqltriga_role;
select pgq.drop_queue('jsontriga_role');
select pgq.drop_queue('logutriga_role');
select pgq.drop_queue('sqltriga_role');
\set ECHO none
\i functions/pgq.insert_event.sql