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 1sGlVb-00GwXC-Qt for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 20:20:52 +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 1sGlVa-00FxG7-Eb for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 20:20:51 +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 1sGlVa-00FxFd-2j for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 20:20:50 +0000 Received: from fhigh5-smtp.messagingengine.com ([103.168.172.156]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGlVY-000zOh-0u for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 20:20:50 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 7AC1D11401F4; Mon, 10 Jun 2024 16:20:46 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Mon, 10 Jun 2024 16:20:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1718050846; x=1718137246; bh=bdzjNkq5Op8pewnF6o7QbJOve5A4v0MsOvtqvL7DLUo=; b= Qu3jEG/8TdgBsVffZNDGPbypy02DDT5cpOEG0aV0XEmRm57c+HXCgkQkQfkLuUu9 DHUT6pSxjIdsU+3rI/bqgo97bd9FxowKzClFoWYgJPn3kx1tZUwSB5CJIqeP+Hr9 JOmy/KwuZbfK12AS4ruaCXNt8OmgQEeiBjwl09ktK6vDTrtdUCxKDDX9BBjfm5Rx RyEIkps6nBGenTUtKVfOKpRDgZV9PO2nCpN14v5B8sd01x3Q1AqmbM4/+ZDrbr76 yKKAqRrwF4fDoRvBuo5vpj07yE65WWdZ1xVfLpbzDqxs7syJ+rhoubxBXZVnTHVW QzzZiKZ/UiTME/Q0q3XPcw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1718050846; x= 1718137246; bh=bdzjNkq5Op8pewnF6o7QbJOve5A4v0MsOvtqvL7DLUo=; b=S mA3X4YfSZpZiBVuhoAfZlqAaDQxviMWQXscI1fmdtlQ5wIq4KEAiUwvO8CBt7L0E niE+J2SfVG7d0Qa8mpl3HBY/T9VSQWzV2y4uEnXgoLU1WQSarqp1k2kJYpKIAn9o UBaDk0IZpy/ksxxI1hmb0VOPvwD2sxaMcgQZnH6tLCLzXI1NElok0qI1BNbYPG30 qkNDbj85TK3W4Szpp9Q913F7bvH8t1druF5GoLM4PDqJ2ISAJakcbHilrP4gxMff AqDs7vgyDXFV3WhrED0lwYWsbt+9Ehw015Frk4gENyxjBF2ZbaWczDv/PRbClKI1 9yN84rzQbSmmtHxrisKOg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedutddgudegiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepkedvheeigfevtdehudelledugfefueffudejueet vdffvefgveeggeffkeeggfdvnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrgh dpphhgrghuughithdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 10 Jun 2024 16:20:45 -0400 (EDT) Message-ID: <96d932ed-5883-4329-be64-9c4c20960365@aklaver.com> Date: Mon, 10 Jun 2024 13:20:45 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Does trigger only accept functions? To: veem v , pgsql-general References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/10/24 12:17, veem v wrote: > Hi, It's version 15.4 of postgres. We have a requirement to have the > audit enabled for the delete queries on the base table. And for that we > are planning to have one audit table created for each base table and > have triggers on each of the base tables to be fired on delete which > will insert records into the audit table. > > But I see the trigger is not accepting the insert query directly, rather > it's asking to call a function and to put the business logic inside that > function, something as below. So does that mean, to enable audit on the > ~50 base table ,  we will have ~50 functions to be created and also they > need to be called from ~50 triggers? or any other better approach exists > to handle this? The below tells you what you need: https://www.postgresql.org/docs/15/sql-createtrigger.html That is either a function or a procedure. You could create one function with dynamic SQL and call that from each trigger. Yes there would need to be trigger on each table in that case. As to alternatives: https://www.pgaudit.org/ > > CREATE OR REPLACE FUNCTION log_deletes_source_table() > RETURNS TRIGGER AS $$ > BEGIN >     INSERT INTO source_table_delete_history (record_id, > delete_timestamp, col1, col2,col3) >     VALUES (OLD.id, current_timestamp, old.col1,old.col2,old.col3); >     RETURN OLD; > END; > $$ LANGUAGE plpgsql; > > -- Trigger for deletes > CREATE TRIGGER before_delete > BEFORE DELETE ON source_table > FOR EACH ROW EXECUTE FUNCTION log_deletes_source_table (); > > Regards > Veem -- Adrian Klaver adrian.klaver@aklaver.com