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 1sH7S3-003UaD-IP for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 19:46:40 +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 1sH7S1-009EBU-9J for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 19:46:38 +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 1sH7Rz-009EBM-ME for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 19:46:37 +0000 Received: from fout5-smtp.messagingengine.com ([103.168.172.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH7Rs-0019dw-3R for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 19:46:35 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id B811113800F3; Tue, 11 Jun 2024 15:46:25 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 11 Jun 2024 15:46:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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=1718135185; x=1718221585; bh=BWwb5Q08pqir5X4x45qQAtiHYJYzXC59Lbil/2ABN1E=; b= QYLfylLLzpzZ1+otKdGioci38cEVYHbAos3N92jRHVsSvfGSxgItpAMqnF39wS9T 9OvahIUV/QbWO9svrB/+Su9Yutd+xm8x0J4vcxqCsczOP53zUoE9CSIMAyGP+ghL OcHa+LHKy57AcpfLkVR780fnK/NUmPZXpiuaqP4Nj3CEEkoSEUSrjaQJ5prop2ID Cjx3xtOc78vMeF2HF1HHKJZf9VjAIwqYhAma1oM6+8v0sZaGFA5XFm2MefEBKr34 TbI7Z5OzPjCUwWP9yIEabbElDErtYdCeW9qlzmgfQVVGSLwGt4GRqK+Y9x+x+GFt EbPlniVdw9IufyILEGhB5g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1718135185; x= 1718221585; bh=BWwb5Q08pqir5X4x45qQAtiHYJYzXC59Lbil/2ABN1E=; b=c GawdMZY2q4Uwg0wf+4FYoODeteW4Tid1ufjyMY/p8s9YZuwpp+uGN3IPAgRGB7U3 ET0LD1bYVyIN8MVX/+A9t7X5thap+lnQViAIW4GTL4jfAEbpKyY7PuP/DOi1Oqw3 hvjZ6ElBGCZlcsGLoHivi+lbFjGCZNB37vU6ip26J41CpQk4BnLR68OxSzZSWYba 6SW+lt15hzVAnRsUIlgNw4O1hCLNavYwJzYjSckdDdbDFoEcbQo+ctaxdJXmVdhj kgGHcnM7QV4Zqj+fwKSLtjeAwL98HJEJZ/ihtuhZogFkwArnu8hdL9i7R3VY7ejT GCuLkNTLtycHe3i15gzqw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfeduvddgudeflecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddvjeenucfhrhhomheptegu rhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrd gtohhmqeenucggtffrrghtthgvrhhnpeeuffdutedtffdtudfghfetkeeuuedtvdeugffg ueevhfevtddtueeulefhhfetgfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorh hgpdgrkhhlrghvvghrrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghm pehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 11 Jun 2024 15:46:24 -0400 (EDT) Message-ID: <7169c618-3975-4149-b3a5-00b26aef4268@aklaver.com> Date: Tue, 11 Jun 2024 12:46:24 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Does trigger only accept functions? To: veem v , depesz@depesz.com Cc: 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/11/24 12:20, veem v wrote: > > > On Wed, 12 Jun 2024 at 00:26, hubert depesz lubaczewski > > 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; I'm guessing depesz meant using TG_TABLE_NAME to pull column information from: https://www.postgresql.org/docs/current/catalog-pg-attribute.html and use that to build the INSERT query. The issue with dynamic or a fixed SQL is going to be with audit_query, in particular audit_table_name := 'delete_audit2. If your source tables change, add or delete columns or column types change, your audit table will need to change to match. One possible solution is something I outlined here: https://aklaver.org/wordpress/2021/12/07/postgres-and-json/ Other folks have done similar things, you can search on postgresql audit tables using json for alternatives. -- Adrian Klaver adrian.klaver@aklaver.com