Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ntC4F-00021z-5S for pgsql-general@arkaria.postgresql.org; Mon, 23 May 2022 17:42:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ntC4D-000105-SN for pgsql-general@arkaria.postgresql.org; Mon, 23 May 2022 17:42:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ntC4D-0000z8-HJ for pgsql-general@lists.postgresql.org; Mon, 23 May 2022 17:42:05 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ntC48-0007Zg-RP for pgsql-general@postgresql.org; Mon, 23 May 2022 17:42:04 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 57E5032009A8; Mon, 23 May 2022 13:41:58 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Mon, 23 May 2022 13:41:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1653327717; x= 1653414117; bh=VS0rPe/DD5WyyRPZsKbYoA6xbjPEMLcruws1ZHrHieM=; b=O URBtZ2zfj/snNlC895jAcsQWnPZkY8FkENSlkZRZYjoOMxR7zjNxG4aE4VPljc1A t4105RaLJ43pwKttah0ba2wZtXBgNsGTRYXgHF3eFUmte46CNbsrouRYgR7IAZeI E+8SQgqZP7hRTsJNvd7ZYWD/Tmel/eoaDcWk6CZ6/JoD0TXhEUojDm6KjoFqBMXE 3ADlAwflZFiZRBD1BzRC5fnQQPRTx7oMQewiluKKWUB3lofXBSuiHeym2DNHIHec QkcieV65oDZIr4nHX6ywHsXKbAZTSv63097BBy9oE4XlAKAl1E30mDBYQZ1w60NI WKz2XjKrrJWqFjXZjpr6Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1653327717; x=1653414117; bh=V S0rPe/DD5WyyRPZsKbYoA6xbjPEMLcruws1ZHrHieM=; b=HvlGA7zj5I9vT9LsN yBE9vZx0D0Fx+jRLz6lHpwLQZw/WftLi/CQw8EgQe8uV8rCcfigm7cPG1yQMCrzN cvz3Ioy39KPo28zpwHEVQ9rHn0RA+1E/S35+3wok/uHTo9GCOvfIo7fLyyaBPJsJ ZAJIkhdF937k29qRBV/vE6sftM8FQtQuILFKod4AbCMkP9sGcNwRTLf6DlPXsifq Y233b4KXZWpo0R8CK3OAZMWRFk/bnOWeI7Mn7w6wVdAvV8Oeiog7HyrqqIDBOaYm pgofWbTE4m6s8vxZ16gg0BieGvhyJiSP+rKY4/QjdUs+Us2t2JZYAGpUpkxMrW/M GdyLQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrjedugdekgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepuddufeejteevffehgfduhedthfdvteekieeuheefveev udetkeelueehgeduhefhnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgr nhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 23 May 2022 13:41:57 -0400 (EDT) Subject: Re: Can I start Update row in After Insert trigger function? To: Durumdara , Postgres General References: From: Adrian Klaver Message-ID: Date: Mon, 23 May 2022 10:41:56 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/23/22 10:35 AM, Durumdara wrote: > Hello! > > I need to log some row changes in a table with JSON format (row_to_json). > > In the Before Update I can select the row into a column with JSON format. > And in After Update I can log this column into an archive table. > Plus I have the prior data in the JSON_TEXT field. You need to read: https://www.postgresql.org/docs/current/plpgsql-trigger.html > > But this technique isn't working in insert, because no OLD.id. and OLD row: > > select row_to_json(thistable) into json_text_var > from thistable where id = ???.id; > No row!!! Only NEW.nn variables. Yes an INSERT is a new value only there is no old row(value) available. In the doc link I posted: " TG_OP Data type text; a string of INSERT, UPDATE, DELETE, or TRUNCATE telling for which operation the trigger was fired. " Use this to modify what value(NEW/OLD) you are working with. Call trigger with UPDATE and INSERT. > > Version: PGSQL 9.6-11 > > Thank you for your help! > > Best regards, >    dd > -- Adrian Klaver adrian.klaver@aklaver.com