Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH6fR-003KiO-Eo for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 18:56:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sH6fP-008qWJ-MD for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 18:56:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH6fP-008qWB-7s for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 18:56:24 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH6fN-0019Dd-AE for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 18:56:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description; bh=aGy42yfoXzjgos4NTRgmCkBGBmjNYpiywhZhXsKcGGU=; b=d+1xm1uaLEO//bHrVH1exwOXOa BxhoqlR9OTrBuYf2A/RFbA9hymeMXXoSa5tXi/n7HS0zgIM6U3IZfkOrmiCeb72AvfBbOsOk1z5GZ yNEr4hqLwvxtQk15tWLtjk3F61+AdYKv+MaDERr7yH+HjPZr4cATyQ2Ra2G3pdyW49M8=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1sH6fM-00Ezhe-05; Tue, 11 Jun 2024 20:56:20 +0200 Date: Tue, 11 Jun 2024 20:56:20 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: veem v Cc: pgsql-general Subject: Re: Does trigger only accept functions? Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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