19
loading...
This website collects cookies to deliver better user experience
create table employee (
id serial primary key,
title text not null,
surname text not null,
forename text not null,
employment_start date not null,
employment_end date,
company_acl jsonb not null default '{}'::jsonb
);
create table employee_audit (
audit_ts timestamptz not null,
audit_action text not null,
id integer,
title text,
surname text,
forename text,
employment_start date,
employment_end date,
company_acl jsonb
);
create sequence exec_seq start with 1 no maxvalue increment by 1;
create function emp_audit() returns trigger
as $$
declare
call_count integer;
begin
select nextval('exec_seq'::regclass)
into call_count;
raise info 'Total Function Executions: %', call_count;
if TG_OP = 'DELETE'
then
insert into employee_audit
select now(), TG_OP, *
from old_employee;
else
insert into employee_audit
select now(), TG_OP, *
from new_employee;
end if;
return null;
end;
$$ language plpgsql;
old_employee
and new_employee
... where'd they come from? Read on, true believer!create trigger ins_audit_employee
after insert
on employee
referencing new table as new_employee
for each statement
execute function emp_audit();
create trigger udt_audit_employee
after update
on employee
referencing old table as old_employee
new table as new_employee
for each statement
execute function emp_audit();
create trigger del_audit_employee
after delete
on employee
referencing old table as old_employee
for each statement
execute function emp_audit();
old_employee
and new_employee
table names were specified in the trigger create. These names are made available to the trigger function.insert
into employee (
title,
surname,
forename,
employment_start,
employment_end,
company_acl
)
values
(
'Captain America',
'Rogers',
'Steve',
'1964-03-01'::date,
null,
'{"avengers_compound": true, "weapons": {"shield": true, "Mjolnir": true}}'::jsonb
),
(
'God of Thunder',
'Odinson',
'Thor',
'1963-11-01'::date,
null,
'{"avengers_compound": true, "weapons": {"Stormbreaker": true, "Mjolnir": true}}'::jsonb
),
(
'Spider-Man',
'Parker',
'Peter',
'1983-06-01'::date,
null,
'{"avengers_compound": true, "weapons": {"web-shooters": true}}'::jsonb
);
INFO: Total Function Executions: 1
INSERT 0 3
postgres=# select * from employee;
id | title | surname | forename | employment_start | employment_end | company_acl
----+-----------------+---------+----------+------------------+----------------+---------------------------------------------------------------------------------
1 | Captain America | Rogers | Steve | 1964-03-01 | | {"weapons": {"shield": true, "Mjolnir": true}, "avengers_compound": true}
2 | God of Thunder | Odinson | Thor | 1963-11-01 | | {"weapons": {"Mjolnir": true, "Stormbreaker": true}, "avengers_compound": true}
3 | Spider-Man | Parker | Peter | 1983-06-01 | | {"weapons": {"web-shooters": true}, "avengers_compound": true}
(3 rows)
postgres=# select * from employee_audit;
audit_ts | audit_action | id | title | surname | forename | employment_start | employment_end | company_acl
-------------------------------+--------------+----+-----------------+---------+----------+------------------+----------------+---------------------------------------------------------------------------------
2021-06-17 16:16:12.486884+00 | INSERT | 1 | Captain America | Rogers | Steve | 1964-03-01 | | {"weapons": {"shield": true, "Mjolnir": true}, "avengers_compound": true}
2021-06-17 16:16:12.486884+00 | INSERT | 2 | God of Thunder | Odinson | Thor | 1963-11-01 | | {"weapons": {"Mjolnir": true, "Stormbreaker": true}, "avengers_compound": true}
2021-06-17 16:16:12.486884+00 | INSERT | 3 | Spider-Man | Parker | Peter | 1983-06-01 | | {"weapons": {"web-shooters": true}, "avengers_compound": true}
(3 rows)
update employee
set employment_end = '1986-05-15'::date
where id = 3;
INFO: Total Function Executions: 2
UPDATE 1
postgres=# select * from employee where id = 3;
id | title | surname | forename | employment_start | employment_end | company_acl
---------+------------+---------+----------+------------------+----------------+----------------------------------------------------------------
3 | Spider-Man | Parker | Peter | 1983-06-01 | 1986-05-15 | {"weapons": {"web-shooters": true}, "avengers_compound": true}
(1 row)
postgres=# select * from employee_audit where id = 3 order by audit_ts;
audit_ts | audit_action | id | title | surname | forename | employment_start | employment_end | company_acl
------------------------------------+--------------+----+------------+---------+----------+------------------+----------------+----------------------------------------------------------------
2021-06-17 16:27:23.53353+00 | INSERT | 3 | Spider-Man | Parker | Peter | 1983-06-01 | | {"weapons": {"web-shooters": true}, "avengers_compound": true}
2021-06-17 16:29:28.923033+00 | UPDATE | 3 | Spider-Man | Parker | Peter | 1983-06-01 | 1986-05-15 | {"weapons": {"web-shooters": true}, "avengers_compound": true}
(2 rows)
employee
record has the change recorded and the employee_audit
table has 2 records for Spider-Man.delete from employee where id > 1;
INFO: Total Function Executions: 3
DELETE 2
postgres=# select * from employee;
id | title | surname | forename | employment_start | employment_end | company_acl
---------+-----------------+---------+----------+------------------+----------------+---------------------------------------------------------------------------
1 | Captain America | Rogers | Steve | 1964-03-01 | | {"weapons": {"shield": true, "Mjolnir": true}, "avengers_compound": true}
(1 row)
postgres=# select * from employee_audit order by id, audit_ts;
audit_ts | audit_action | id | title | surname | forename | employment_start | employment_end | company_acl
------------------------------------+--------------+----+-----------------+---------+----------+------------------+----------------+---------------------------------------------------------------------------------
2021-06-17 16:27:23.53353+00 | INSERT | 1 | Captain America | Rogers | Steve | 1964-03-01 | | {"weapons": {"shield": true, "Mjolnir": true}, "avengers_compound": true}
2021-06-17 16:27:23.53353+00 | INSERT | 2 | God of Thunder | Odinson | Thor | 1963-11-01 | | {"weapons": {"Mjolnir": true, "Stormbreaker": true}, "avengers_compound": true}
2021-06-17 16:33:36.526134+00 | DELETE | 2 | God of Thunder | Odinson | Thor | 1963-11-01 | | {"weapons": {"Mjolnir": true, "Stormbreaker": true}, "avengers_compound": true}
2021-06-17 16:27:23.53353+00 | INSERT | 3 | Spider-Man | Parker | Peter | 1983-06-01 | | {"weapons": {"web-shooters": true}, "avengers_compound": true}
2021-06-17 16:29:28.923033+00 | UPDATE | 3 | Spider-Man | Parker | Peter | 1983-06-01 | 1986-05-15 | {"weapons": {"web-shooters": true}, "avengers_compound": true}
2021-06-17 16:33:36.526134+00 | DELETE | 3 | Spider-Man | Parker | Peter | 1983-06-01 | 1986-05-15 | {"weapons": {"web-shooters": true}, "avengers_compound": true}
(6 rows)