Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fFb1V-000583-RQ for pgsql-general@arkaria.postgresql.org; Mon, 07 May 2018 07:57:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fFb1U-00074A-K5 for pgsql-general@arkaria.postgresql.org; Mon, 07 May 2018 07:57:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fFb1U-000740-CN for pgsql-general@lists.postgresql.org; Mon, 07 May 2018 07:57:28 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fFb1Q-0002z2-J9 for pgsql-general@postgresql.org; Mon, 07 May 2018 07:57:27 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 12824482E5; Mon, 7 May 2018 09:57:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id ywZ-86kLD-L3; Mon, 7 May 2018 09:57:21 +0200 (CEST) Message-ID: <1525679840.2414.3.camel@cybertec.at> Subject: Re: Is it possible to get username information while writing trigger? From: Laurenz Albe To: a <372660931@qq.com>, pgsql-general Date: Mon, 07 May 2018 09:57:20 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk a wrote: > What I want is to add a log entry at the last column of each row, which will record the > history update, insert automatically when relative statement is processed. > > I have read the documentation on triggers, which helps a lot. However, I may have few > more extra requirement to complete my wishes: > > 1, I would like to get the username of who executed the statement; > > 2, I would like to get the column name that is being updated; > > If it is possible and how should I do it?? You could use the "current_user" function to get the current user. Mind, however, that updates caused by a cascading update from a foreign key constraint will be executed as the owner of the table, so it would be better to use "session_user" to avoid surprises. You cannot get the column name, because PostgreSQL updates a whole row, not an individual column. The best you can do is to check which column values are different in OLD and NEW. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com