pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Moreno Andreo <moreno.andreo@evolu-s.it>
To: pgsql-general@lists.postgresql.org
Subject: Partial table duplication via triggger
Date: Thu, 22 Feb 2024 15:14:53 +0100
Message-ID: <c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it> (raw)

Hi *,
     suppose I have 2 tables

CREATE TABLE t1(
     id uuid,
     name text,
     surname text,
     ...
     PRIMARY KEY(id)
)

CREATE TABLE t2(
     id uuid,
     master_id uuid,
     op_ts timestamp with time zone,
     name text,
     surname text,
     ...
     PRIMARY KEY(id)
)

I need to write an AFTER TRIGGER on UPDATE so all columns of t1 go in 
the same columns in t2 (except for t1.id that goes in t2.master_id, and 
t2.op_ts gets now())

I cannot write an 1 to 1 column assignment (like NEW.name := OLD.name 
and so on) because the trigger has to be used on many tables, that has 
different (and evolving) schema and I don't want to write dozen of 
function that have to be frequently mantained.

I'm quite noob at pl-pgsql; at the moment I wrote this, but I can't 
"tell the function that fields are from OLD row" (the error is "missing 
FROM-clause entry for table 'old')

I tried also with field names alone (without OLD.), with no success.
Trigger is fired AFTER UPDATE in t1 (CREATE TRIGGER update_id AFTER 
UPDATE ON t1 FOR EACH ROW EXECUTE PROCEDURE update_op());

CREATE OR REPLACE FUNCTION update_op() RETURNS TRIGGER
AS $$
DECLARE
     fieldlist text := (select string_agg(column_name, ', ')
                         from information_schema.columns c
                         where table_name = TG_TABLE_NAME and
                               (column_name <> 'id'));

     oldfieldlist text := (select string_agg(column_name, ', OLD.')
                         from information_schema.columns c
                         where table_name = TG_TABLE_NAME and
                               (column_name <> 'id'));

BEGIN
         EXECUTE 'INSERT INTO t2 (master_id, op_ts, '|| fieldlist ||') 
VALUES (OLD.id, now(), OLD.'||oldfieldlist||')' USING OLD;
         RETURN NULL;
END;
$$
LANGUAGE 'plpgsql';

What am I missing?
Thanks,
Moreno

view thread (3+ messages)  latest in thread

Message-ID: <c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it>
Permalink:  ../c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it/
Also on:    postgresql.org/message-id/c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it

 · 

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: moreno.andreo@evolu-s.it
  Subject: Re: Partial table duplication via triggger
  In-Reply-To: <c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it>
  References: <c521dbf2-e1f0-4aa3-9d83-1b989bb8ac00@evolu-s.it>

* 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