pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Rhys A.D. Stewart <rhys.stewart@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Dynamically accessing columns from a row type in a trigger
Date: Sat, 12 Aug 2023 21:28:10 -0700
Message-ID: <40a49cf0-c1ae-3c96-ad62-35fc44746774@aklaver.com> (raw)
In-Reply-To: <CACg0vTkrqqnuNrHN2iNgQdAdB5JiVJthj31M1OHYSeeZwMpWXw@mail.gmail.com>
References: <CACg0vTm8in3pdagru2Y-Q5Ru8UqfgVaqfTiW+kRw0jAj2wWQkw@mail.gmail.com>
	<068f5479-5b58-6084-1141-fd320b722782@aklaver.com>
	<CACg0vTkrqqnuNrHN2iNgQdAdB5JiVJthj31M1OHYSeeZwMpWXw@mail.gmail.com>

On 8/12/23 20:21, Rhys A.D. Stewart wrote:
> Hey Adrian,
> 
> Thanks for your response. I don't think I explained myself clearly.
> pk_col is not the column name. pk_col is a variable that holds the
> name of a column. This is one trigger for several tables with varying
> structures. So pk_col would be a column specific to the current
> TG_TABLE_NAME when the trigger is invoked. This is why in my example I
> had to use EXECUTE to get the value of the pk_col from OLD.

So you are looking for a generic solution.

>  
> Actually, now that I'm thinking about it, I don't really want to store
> the value into a variable because the pk_col might be of any given
> type. So ideally, I'd love a way to just get the value from OLD and
> use it directly in another query. Something along the lines of:

The issue then is determining what value to get from OLD.

Is that generic e.g always the Primary Key or will it vary?

> 
> `EXECUTE format('SELECT * FROM %1$I.sometable WHERE pk = $1', myschma)
> USING OLD['pk_col']`.
> 
> I reckon I may have to look at just generating a trigger function per
> table, or maybe look into using TG_ARGS.
> 
> 
> Rhys
> Peace & Love | Live Long & Prosper
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (10+ messages)  latest in thread

Message-ID: <40a49cf0-c1ae-3c96-ad62-35fc44746774@aklaver.com>
Permalink:  ../40a49cf0-c1ae-3c96-ad62-35fc44746774@aklaver.com/
Also on:    postgresql.org/message-id/40a49cf0-c1ae-3c96-ad62-35fc44746774@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, rhys.stewart@gmail.com
  Subject: Re: Dynamically accessing columns from a row type in a trigger
  In-Reply-To: <40a49cf0-c1ae-3c96-ad62-35fc44746774@aklaver.com>
  References: <CACg0vTm8in3pdagru2Y-Q5Ru8UqfgVaqfTiW+kRw0jAj2wWQkw@mail.gmail.com> <068f5479-5b58-6084-1141-fd320b722782@aklaver.com> <CACg0vTkrqqnuNrHN2iNgQdAdB5JiVJthj31M1OHYSeeZwMpWXw@mail.gmail.com> <40a49cf0-c1ae-3c96-ad62-35fc44746774@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