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 1sGkWJ-00GiG5-31 for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 19:17:31 +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 1sGkWH-00FAoc-51 for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 19:17:30 +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 1sGkWG-00FAoT-Ps for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 19:17:29 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGkWF-000yvl-Ay for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 19:17:29 +0000 Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-57a4d7ba501so6395507a12.2 for ; Mon, 10 Jun 2024 12:17:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718047046; x=1718651846; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=UDrm4DTMg6UYJLTm2lFbioeOEZjHk6Z1qEoWFkmdqw0=; b=AFvdIgNrFt1zTIXY9zUnqGEZGNYViA/+5fl+QHgcRIp7oAj1aFCXfUgcuunZVQemen 62p/Eb38AMCqsdX/kL1Ge7wyOLq0cefEyb0VBzjPGaYJ2xHg+WEWwtUOThjHGtAvvtCv cwi77nWtFXftuxt+39rfvMFlwvYyaTYXNvcKNpf/1RRbR0LwmQmn6chqFOmXF0JepA2V V4WoCLcR7Ze3s7MjHUPNz6SES0VRDVQ5IPrawMD/eIQQLX/xoV+5e9eLZ0xNh+laNuU7 Gbxp1RbfF8+LDVqxDNI+54oeyxqY9PEcYTPA3VAGv8gfcv4mNJiXGmf9juofdLsRQJcr xxbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718047046; x=1718651846; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=UDrm4DTMg6UYJLTm2lFbioeOEZjHk6Z1qEoWFkmdqw0=; b=YgWPF91MLYnJQNNSd98vHMODXzW1/CxK0BiWCRf32ZUNCiJYRFJDKsiFr4h71MGNxZ ZKuWGN+qZV3k/0steXpH3rUtEYLq1hPbGYtleOLLSoOe8GXN6Grm/uCOSEgXaqmqMtxN Z/upvOxqbK+KaC6yNLDD0Pd9xMS12RpSfWI3q9j56vRJFbDPuJItLGRfSRg90ylFpK+X wHQRyUt0ITwwGv9qAEN/7N0MMZV9WxrZXWq2mrpG2ejZJb82GO5kJ2Kx9yBkVPkRIQDA ShvYfeVMM0Z/mnUiyiOm/MIdTQsK7w3t+to8+vLjv0C6zOnDbTYbNiJEEJxGY+Td5pzJ RY9Q== X-Gm-Message-State: AOJu0YxRBfWp2WrLLVhckDOYH4m/l/H0IG9j8Zrr5bi5A5dRg+kjFlOu epCZr+KkTfKYYCzhY5zwWwKy6BYPIFt6lZH8Y++XRZI7PgKA5tjfBQ0DNu0AvKaCywu6C/oVo5d tG90Du31qaJ8OO7uVMtxhHvoj9efWSRlW X-Google-Smtp-Source: AGHT+IFkZgJbRTmSHcmlkrj5moUCQO2JpfOFmldhnDn8mX+euHCIbIwZqCIMyq6SAuNoi98Vm15NGmwx5Lw3jEjFgDU= X-Received: by 2002:a17:906:b85a:b0:a6f:1036:98cf with SMTP id a640c23a62f3a-a6f10369abcmr375763266b.54.1718047046029; Mon, 10 Jun 2024 12:17:26 -0700 (PDT) MIME-Version: 1.0 From: veem v Date: Tue, 11 Jun 2024 00:47:14 +0530 Message-ID: Subject: Does trigger only accept functions? To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000e2d827061a8dff18" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e2d827061a8dff18 Content-Type: text/plain; charset="UTF-8" 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? 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 --000000000000e2d827061a8dff18 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi, It's version 15.4 of postgres. We have a requ= irement 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 w= hich will insert records into the audit table.

But I see the t= rigger is not accepting the insert query directly, rather it's asking t= o call a function and to put the business logic inside that function, somet= hing as below. So does that mean, to enable audit on the ~50 base table , = =C2=A0we will have ~50 functions to be created and also they need to be cal= led from ~50 triggers? or any other better approach exists to handle this? =

CREATE OR REPLACE FUNCTION log_deletes_sou= rce_table()
RETURNS TRIGGER AS $$
BEGIN
=C2=A0 =C2=A0 INSERT INTO = source_table_delete_history (record_id, delete_timestamp, col1, col2,col3)<= br>=C2=A0 =C2=A0 VALUES (OLD.id, current_timestamp, old.col1,old.col2,old.c= ol3);
=C2=A0 =C2=A0 RETURN OLD;
END;
$$ LANGUAGE plpgsql;

-= - Trigger for deletes
CREATE TRIGGER before_delete
BEFORE DELETE ON s= ource_table
FOR EACH ROW EXECUTE FUNCTION=C2=A0
log_deletes_source_table=C2=A0= =C2=A0();
=
Regards
Veem
--000000000000e2d827061a8dff18--