Re: Does trigger only accept functions?

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: veem v <veema0000(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Does trigger only accept functions?
Date: 2024-06-11 18:56:20
Message-ID: Zmid1OlE-4j0c2l1@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jun 12, 2024 at 12:19:55AM +0530, veem v wrote:
> 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;

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message veem v 2024-06-11 19:20:27 Re: Does trigger only accept functions?
Previous Message veem v 2024-06-11 18:49:55 Re: Does trigger only accept functions?