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 1sH6c4-003Jy8-JM for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 18:52:57 +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 1sH6c2-008mfL-V9 for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 18:52:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH6c2-008mfD-KA for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 18:52:55 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sH6c0-000pta-TL for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 18:52:54 +0000 Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a6ef8bf500dso173878466b.0 for ; Tue, 11 Jun 2024 11:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718131971; x=1718736771; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=E0wF5MqpNqeIq9kTlcCSmoXhQvXFrZPRkRZzKMUqZO0=; b=Y/MxrEdMgqAu1cdHfKrk80sYPDkFuAKmhI+HyxD7Dp6gQL5WxkKBAtizCMnmM/ume9 4tI4ybV4wl3P3W14RQF/hqVMT+oPQHYIr4OpbTWO6EumrbIUcAcP2Nvhm5g4oBR2Yc/h DvK27Kdw5Rd7uQhD5p/cVYB5H8eiDxHFLkxm0ZsKM0pDO5KYpD7/azD6E0LrHbfOB5/E 9XiqNi1swy/TjXTy4+Zsg5mLtIPB0xlxNu7g//2F6hjVZS+iag0jm96ILxH+GrZWs/NH /rz8ikh6IzH3ZDoXp8BxsT0WKvFJfW7tx0itsQKDDlUxJCJI0ILpLL8Sd9QRqC/TOpog h3ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718131971; x=1718736771; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=E0wF5MqpNqeIq9kTlcCSmoXhQvXFrZPRkRZzKMUqZO0=; b=gXPqaNOCJgdC2OdZYwiDdLifAbavV2BnirlHvHOkwDX8oCha8Z57yzDjXvEr/qlh7h 1hNynZIXA+ScRLeXae/f0LcoeXXFfoLLKYkThcUgninV3LDKThWjfrL5wsoIxdqn9mRD Ru03pRtALFAf+IQLQC4c3R/657Qfm672pcGHI+snFsAlm+XQ+Sgp7FvrL8Tj9cbOUthe c2hSYYf6Zqbe0i/iq1WUJ21aCwDBs/8+u6PhOxNAUqTmIYbMWOJFJeBzLSzCBIuOx1nM fssu/aU5qJx8k1GVMDGavpGtjfxVKrIuMMqMoRewS+2LPZQkJhI4C6+KR5fkLf9c6M4a l07w== X-Gm-Message-State: AOJu0Yw6gtpbDApq5snIvb0iH/M16x7IgT8qvjmWkcLivl8JsvGfYPEn rmhLl8KhRiM4IK07bQokHMkw4XrDjup6Hj53PJ0d5ZBiU5Udx03wFOETRU6vZX/NctKeYUS5tSV dQyVL1sh0GKi6X27yFW3IVP5DcjSa8sQM X-Google-Smtp-Source: AGHT+IGv0lINbq9om/D67SIsaY6r8pfMOxgEQ2H2BMFBLYGoy7kEHCPbwJUGMCi3vZI7UhZFkRtJdUt9Jf9o01GjzJI= X-Received: by 2002:a17:906:1419:b0:a68:f493:4b58 with SMTP id a640c23a62f3a-a6cd7d686d0mr842060966b.40.1718131971121; Tue, 11 Jun 2024 11:52:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: veem v Date: Wed, 12 Jun 2024 00:19:55 +0530 Message-ID: Subject: Re: Does trigger only accept functions? To: depesz@depesz.com Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000d0e99f061aa1c5fa" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d0e99f061aa1c5fa Content-Type: text/plain; charset="UTF-8" On Tue, 11 Jun 2024 at 17:03, hubert depesz lubaczewski wrote: > On Tue, Jun 11, 2024 at 12:47:14AM +0530, veem v wrote: > > to be called from ~50 triggers? or any other better approach exists to > > handle this? > > pgaudit extension? > > Or just write all the changes to single table? > > Or use dynamic queries that will build the insert based on the name of > table the event happened on? > > Or pass arguments? > > Best regards, > > depesz > > Thank you so much. I hope you mean something as below when you say making it dynamic. Because we have the audit tables having more number of columns as compared to the source table and for a few the column name is a bit different. -- Trigger for deletes CREATE TRIGGER before_delete BEFORE DELETE ON source_table FOR EACH ROW EXECUTE FUNCTION log_deletes(); -- Trigger for source_table1 CREATE TRIGGER before_delete_source_table1 BEFORE DELETE ON source_table1 FOR EACH ROW EXECUTE FUNCTION log_deletes(); -- Trigger for source_table2 CREATE TRIGGER before_delete_source_table2 BEFORE DELETE ON source_table2 FOR EACH ROW EXECUTE FUNCTION log_deletes(); 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; --000000000000d0e99f061aa1c5fa Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Tue, 11 Jun 2024 at 17:03, hubert depe= sz lubaczewski <depesz@depesz.com> wrote:
On= Tue, Jun 11, 2024 at 12:47:14AM +0530, veem v wrote:
> to be called from ~50 triggers? or any other better approach exists to=
> handle this?

pgaudit extension?

Or just write all the changes to single table?

Or use dynamic queries that will build the insert based on the name of
table the event happened on?

Or pass arguments?

Best regards,

depesz



-- Trigger for deletes
CREATE TRIGGER before_deleteBEFORE DELETE ON source_table
FOR EACH ROW EXECUTE FUNCTION log_deletes= ();


-- Trigger for source_table1
CREATE TRIGGER before_delete= _source_table1
BEFORE DELETE ON source_table1
FOR EACH ROW EXECUTE FU= NCTION log_deletes();

-- Trigger for source_table2
CREATE TRIGGER= before_delete_source_table2
BEFORE DELETE ON source_table2
FOR EACH = ROW EXECUTE FUNCTION log_deletes();


CREATE OR REPLACE FUNCTION l= og_deletes()
RETURNS TRIGGER AS $$
BEGIN
=C2=A0 =C2=A0 IF TG_TABLE= _NAME =3D 'source_table1' THEN
=C2=A0 =C2=A0 =C2=A0 =C2=A0 INSER= T INTO delete_audit1 ( col1, col2, col3)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 VAL= UES (OLD.col1, OLD.col2, OLD.col3);
=C2=A0 =C2=A0 ELSIF TG_TABLE_NAME = =3D 'source_table2' THEN
=C2=A0 =C2=A0 =C2=A0 =C2=A0 INSERT INTO= delete_audit2 ( col4, col5, col6)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 VALUES (O= LD.col4, OLD.col5, OLD.col6);
=C2=A0 =C2=A0 -- Add more conditions for o= ther tables
=C2=A0 =C2=A0 ELSE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 RAISE EXCE= PTION 'Audit table not defined for %', TG_TABLE_NAME;
=C2=A0 =C2= =A0 END IF;
=C2=A0 =C2=A0 RETURN OLD;
END;
$$ LANGUAGE plpgsql;
=C2=A0
--000000000000d0e99f061aa1c5fa--