Re: How can I use a dynamic column name after NEW. or OLD. in trigger procedure?

From: Scott Mead <scottm(at)openscg(dot)com>
To: Peter Devoy <peter(at)3xe(dot)co(dot)uk>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: How can I use a dynamic column name after NEW. or OLD. in trigger procedure?
Date: 2016-01-19 22:16:41
Message-ID: CAKq0gvJDYw26s9_sH8ShUbXvRm3wLgQMjKf2_a4JnxA=OnkROQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 19, 2016 at 5:05 PM, Peter Devoy <peter(at)3xe(dot)co(dot)uk> wrote:

> As part of the extension I am writing I am trying to create a trigger
> procedure in which the value of the primary key of the NEW or OLD row
> is used. The trigger will be fired by arbitrary tables so the column
> name must be dynamic. Something like:
>
> pk_column := 'foo_id'; --example assignment only
>
> EXECUTE 'INSERT INTO bar (baz) VALUES ($1)'
> USING NEW.quote_literal(pk_column);
>
> Out of desperation I have pretty much brute forced many weird
> combinations of quote_literal, quote_ident, ::regclass, || and USING.
> Unfortunately, I have not been able to get anything to work so any
> help would be very much appreciated.
>
> Thanks for reading
>
>
...
-- Dump into proper partition
sql := 'INSERT INTO ' || v_ets_destination || ' VALUES ( ($1).*)';

-- DEBUG
--RAISE NOTICE 'SQL: %',sql;

BEGIN
EXECUTE sql USING NEW;
...

--
Scott Mead
Sr. Architect
OpenSCG
PostgreSQL, Java & Linux Experts

Desk : (732) 339 3419 ext 116
Bridge: (585) 484-8032

http://openscg.com

> Peter Devoy
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vik Fearing 2016-01-19 22:23:28 Re: How can I use a dynamic column name after NEW. or OLD. in trigger procedure?
Previous Message Vik Fearing 2016-01-19 22:14:20 Re: How can I use a dynamic column name after NEW. or OLD. in trigger procedure?