public inbox for [email protected]  
help / color / mirror / Atom feed
From: 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:50:27 +0530
Message-ID: <CAB+=1TWb2ASHes8ynVcWs9DAuOgKsY-m-EAJfNwqE0=F=03Uug@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CAB+=1TWT1_kKdBtuWvLBKJ1hkjxsaRZfMQADQU7bsVtat9q-LA@mail.gmail.com>
	<[email protected]>
	<CAB+=1TUWFJ_9BsqVZkj+3V18WxpAZojzhbOLN1kGzUYqx=TLfQ@mail.gmail.com>
	<[email protected]>

On Wed, 12 Jun 2024 at 00:26, hubert depesz lubaczewski <[email protected]>
wrote:

>
>
> No, I meant building dynamic queries and then EXECUTE-ing, like docs
> show:
>
> https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> Best regards,
>
> depesz
>
>
My apology, if interpreting it wrong way. It doesn't make much difference
though, but do you mean something like below?

CREATE OR REPLACE FUNCTION log_deletes()
RETURNS TRIGGER AS $$
DECLARE
    audit_table_name TEXT;
    audit_query TEXT;
BEGIN
    IF TG_TABLE_NAME = 'source_table1' THEN
        audit_table_name := 'delete_audit1';
        audit_query := 'INSERT INTO ' || audit_table_name || ' (record_id,
delete_timestamp, col1, col2, col3) VALUES ( $2, $3, $4)';
        EXECUTE audit_query USING OLD.id, OLD.col1, OLD.col2, OLD.col3;
    ELSIF TG_TABLE_NAME = 'source_table2' THEN
        audit_table_name := 'delete_audit2';
        audit_query := 'INSERT INTO ' || audit_table_name || ' ( col4,
col5, col6) VALUES ( $2, $3, $4)';
        EXECUTE audit_query USING OLD.id, OLD.col4, OLD.col5, OLD.col6;

    ELSE
        RAISE EXCEPTION 'Audit table not defined for %', TG_TABLE_NAME;
    END IF;

    RETURN OLD;
END;
$$ LANGUAGE plpgsql;


view thread (4+ messages)  latest in thread

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+=1TWb2ASHes8ynVcWs9DAuOgKsY-m-EAJfNwqE0=F=03Uug@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