pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Durumdara <durumdara@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Can I start Update row in After Insert trigger function?
Date: Mon, 23 May 2022 10:41:56 -0700
Message-ID: <d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com> (raw)
In-Reply-To: <CAEcMXhmxEhD=7-uHDivP5Z76PYkgGOqBZikv8Jqtyja3S8Q4rw@mail.gmail.com>
References: <CAEcMXhmxEhD=7-uHDivP5Z76PYkgGOqBZikv8Jqtyja3S8Q4rw@mail.gmail.com>

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





view thread (4+ messages)  latest in thread

Message-ID: <d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com>
Permalink:  ../d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com/
Also on:    postgresql.org/message-id/d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, durumdara@gmail.com
  Subject: Re: Can I start Update row in After Insert trigger function?
  In-Reply-To: <d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com>
  References: <CAEcMXhmxEhD=7-uHDivP5Z76PYkgGOqBZikv8Jqtyja3S8Q4rw@mail.gmail.com> <d5b04cb8-99c2-8af5-d028-273c4fe245e5@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox