public inbox for [email protected]
help / color / mirror / Atom feedFrom: veem v <[email protected]>
To: [email protected]
Cc: pgsql-general <[email protected]>
Subject: Re: Does trigger only accept functions?
Date: Wed, 12 Jun 2024 00:19:55 +0530
Message-ID: <CAB+=1TUWFJ_9BsqVZkj+3V18WxpAZojzhbOLN1kGzUYqx=TLfQ@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CAB+=1TWT1_kKdBtuWvLBKJ1hkjxsaRZfMQADQU7bsVtat9q-LA@mail.gmail.com>
<[email protected]>
On Tue, 11 Jun 2024 at 17:03, hubert depesz lubaczewski <[email protected]>
wrote:
> On Tue, Jun 11, 2024 at 12:47:14AM +0530, veem v wrote:
> > to be called from ~50 triggers? or any other better approach exists to
> > handle this?
>
> pgaudit extension?
>
> Or just write all the changes to single table?
>
> Or use dynamic queries that will build the insert based on the name of
> table the event happened on?
>
> Or pass arguments?
>
> Best regards,
>
> depesz
>
>
Thank you so much. I hope you mean something as below when you say making
it dynamic. Because we have the audit tables having more number of columns
as compared to the source table and for a few the column name is a bit
different.
-- Trigger for deletes
CREATE TRIGGER before_delete
BEFORE DELETE ON source_table
FOR EACH ROW EXECUTE FUNCTION log_deletes();
-- Trigger for source_table1
CREATE TRIGGER before_delete_source_table1
BEFORE DELETE ON source_table1
FOR EACH ROW EXECUTE FUNCTION log_deletes();
-- Trigger for source_table2
CREATE TRIGGER before_delete_source_table2
BEFORE DELETE ON source_table2
FOR EACH ROW EXECUTE FUNCTION log_deletes();
CREATE OR REPLACE FUNCTION log_deletes()
RETURNS TRIGGER AS $$
BEGIN
IF TG_TABLE_NAME = 'source_table1' THEN
INSERT INTO delete_audit1 ( col1, col2, col3)
VALUES (OLD.col1, OLD.col2, OLD.col3);
ELSIF TG_TABLE_NAME = 'source_table2' THEN
INSERT INTO delete_audit2 ( col4, col5, col6)
VALUES (OLD.col4, OLD.col5, OLD.col6);
-- Add more conditions for other tables
ELSE
RAISE EXCEPTION 'Audit table not defined for %', TG_TABLE_NAME;
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: Does trigger only accept functions?
In-Reply-To: <CAB+=1TUWFJ_9BsqVZkj+3V18WxpAZojzhbOLN1kGzUYqx=TLfQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox