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 1sH7dp-003Wrg-MM for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 19:58:50 +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 1sH7do-009MM1-6X for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2024 19:58:49 +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 1sH7dn-009MLg-Pj for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 19:58:48 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH7dl-000qJk-Tt for pgsql-general@lists.postgresql.org; Tue, 11 Jun 2024 19:58:47 +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=UnKXJ8Kn9VJ4g0WATx11+ein9WT5aLiEkqP5In7J7SI=; b=J1ykcHPaP7hQpbi3h6Ipuq814X yJXJgPzc2ERD+hhc0oLCzkIKmzFOb2txDNC/d7/HVVLBEBF2RJjXVGHPwYIi9v8HTwuHu1fP3DRfr CKKe+uqhVt34ECpWpCABAEInNtST6QWcQK9EfhGEQET/xJfToAU0WGL7tTJQljQt3YCE=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1sH7dj-00FNed-28; Tue, 11 Jun 2024 21:58:43 +0200 Date: Tue, 11 Jun 2024 21:58:43 +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:50:27AM +0530, veem v wrote: > My apology, if interpreting it wrong way. It doesn't make much difference > though, but do you mean something like below? if you really have totally different structures across all tables, and you don't want to use pgaudit (which is the best solution), and you don't want to have custom function per table, then i'd use hstore datatype, and store all deleted rows, regardless of where they came from, in single log table (potentially partitioned). Something like: create table deleted_rows ( id int8 generated always as identity primary key, source_schema text, source_table text, deleting_user text, deleted_at timestamptz, deleted_row hstore ); create function log_deletes() returns trigger as $$ DECLARE BEGIN INSERT INTO deleted_rows (source_schema, source_table, deleting_user, deleted_at, deleted_row) VALUES (TG_TABLE_SCHEMA, TG_TABLE_NAME, CURRENT_USER, now(), hstore(OLD) ); return OLD; END; $$ language plpgsql; and then just: create trigger x after delete on tablex for each row execute function log_deletes(); or something like this, if I made any typos. Best regards, depesz