To identify changes to the database structure executed by DDL
commands, use the EVENT TRIGGER
feature of PostgreSQL. It is a feature that is available from version 9.3.
It would be possible to store in a table the data of the tables that are being created by the legacy system and then consult the data.
Create a table to store the data as events occur.
create table auditoria (
id serial,
data_ocorrencia timestamp,
dados jsonb
);
Create a function that returns event_trigger
. Note the use of the pg_event_trigger_ddl_commands()
function, it will return the data of the object being modified.
CREATE or replace FUNCTION registra_evento_create()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
declare
r record;
begin
for r in
select jsonb_agg(evento) as eventos
from (
select
pg_identify_object(classid,objid,objsubid) as obj
from pg_event_trigger_ddl_commands()
) as evento
loop
execute 'insert into auditoria (data_ocorrencia, dados) values (current_timestamp,$1);'
using (r.eventos);
end loop;
end
$$;
Create a EVENT TRIGGER
. In this case the trigger will be triggered when the command is terminated and is a CREATE TABLE
command. The WHEN
parameter is not required, so it would be possible to audit other DDL
command types, the options are indicated in the documentation.
CREATE EVENT TRIGGER tr_evento_create
ON ddl_command_end WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE registra_evento_create();
After this, each CREATE TABLE
command will fire the registra_evento_create()
function, and with queries in the auditoria
table it would be possible to identify the tables being created by the legacy system.
Below you'll find links to the subject in the postgres documentation:
link
link
link
link
link
link