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 1smGLs-004QJq-Js for pgsql-general@arkaria.postgresql.org; Thu, 05 Sep 2024 17:33:01 +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 1smGLr-00EcHN-65 for pgsql-general@arkaria.postgresql.org; Thu, 05 Sep 2024 17:32:59 +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 1smGLq-00EcHF-OE for pgsql-general@lists.postgresql.org; Thu, 05 Sep 2024 17:32:59 +0000 Received: from smtp-29.smtpout.orange.fr ([80.12.242.29] helo=smtp.smtpout.orange.fr) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1smGLn-000JK6-AA for pgsql-general@lists.postgresql.org; Thu, 05 Sep 2024 17:32:57 +0000 Received: from X250.home ([193.252.24.13]) by smtp.orange.fr with ESMTPA id mGLjs3drFaZQfmGLjssAhB; Thu, 05 Sep 2024 19:32:52 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1725557572; bh=+tB/bWfGyr6wv287gpzkoyW4fEIcFGn4MAbK2S82cQo=; h=Date:From:To:Subject:Message-Id:Mime-Version; b=o4+06QRp4ubnxiXR3ABrFoAelcG5IGf/FvULOaobkdrpDujaYeEslBmnHkMd8U6G+ y7fs3nWuzGScML9eJA50SVvv+mvbPTAJZ7sHHpuIr+4eZTtKxet6wsT7bTGWEVarOX F6LRIKthBK6uYU96UMDtqPFJfsskbglHa8OLY0JZbSx/NPVuKwxofmrJEXQF+lCwF7 i+mrOoy5sLI6jEmNRskfOJOhrTVQV8zCVBQ8tnE12E2/zcrq2tk0R1vtIfzO3Rynz+ VOzrSr01qNHhaYVPNuy+uFIab9GdjV9NPTkohYPOI7gzSlFI/xFYFghc2m7TRJgiVV De0NopySqx/nQ== X-ME-Helo: X250.home X-ME-Auth: dnYubGlzdHNAd2FuYWRvby5mcg== X-ME-Date: Thu, 05 Sep 2024 19:32:52 +0200 X-ME-IP: 193.252.24.13 Date: Thu, 5 Sep 2024 19:32:51 +0200 From: Vincent Veyron To: yudhi s Cc: pgsql-general Subject: Re: question on audit columns Message-Id: <20240905193251.8b872491a1789e7d5710704f@wanadoo.fr> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 4 Sep 2024 18:19:47 +0530 yudhi s wrote: Hi, > In postgres database , we have all the tables with audit columns like > created_by_user, created_timestamp,updated_by_user, updated_timestamp. So > we have these fields that were supposed to be populated by the time at > which the insert/update operation happened on the database but not at the > application level. So we are planning to populate the created_by_user, > created_timestamp columns by setting a default value of "current_timestam= p" > and "current_user" for the two columns, but no such this is available to > populate while we do the update of the row, so the only option seems to be > through a trigger. >=20 If you can live with the fact that updated_by_user and updated_timestamp ge= t the same values as created_by_user and created_timestamp when inserting t= he record, then you can do : vv=3D> create table audit (created_by_user text default current_user, creat= ed_timestamp timestamp default now(), updated_by_user text default current_= user, updated_timestamp timestamp default now(), data text); CREATE TABLE vv=3D> insert into audit (data) values ('abc'); INSERT 0 1 vv=3D> select * from audit; created_by_user | created_timestamp | updated_by_user | updat= ed_timestamp | data=20 -----------------+----------------------------+-----------------+----------= ------------------+------ vincent | 2024-09-05 19:17:53.446109 | vincent | 2024-09-0= 5 19:17:53.446109 | abc (1 row) --as user postgres update audit set updated_by_user =3D DEFAULT, updated_timestamp =3D DEFAULT= , data =3D 'def'; vv=3D> select * from audit; created_by_user | created_timestamp | updated_by_user | updat= ed_timestamp | data=20 -----------------+----------------------------+-----------------+----------= -----------------+------ vincent | 2024-09-05 19:17:53.446109 | postgres | 2024-09-0= 5 19:24:01.19186 | def (1 row) --=20 Bien =E0 vous, Vincent Veyron=20 https://marica.fr/=20 Logiciel de suivi des contentieux juridiques, des sinistres d'assurance et = des contrats