Re: Getting data from a record variable dynamically

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: "Rhys A(dot)D(dot) Stewart" <rhys(dot)stewart(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Getting data from a record variable dynamically
Date: 2022-07-05 07:01:57
Message-ID: 3ABEE3C9-E57F-4D52-BB4C-0CD9D9838235@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On 5 Jul 2022, at 1:02, Rhys A.D. Stewart <rhys(dot)stewart(at)gmail(dot)com> wrote:
>
> Greetings All,
>
> I have a trigger that is attached to several different tables. In the
> trigger function I retrieve a single row and I want the info from a
> specific column. This column is dependent on the table in question.
> and I have the column name stored in a variable as well. Without
> writing a conditional for each table, what is the best way to
> dynamically get the data from the record variable?

I would create a simple trigger function for each of those tables that just extracts the value(s) from the field(s) you mentioned, and then pass those values on to a generic function that does the actual table-independent work.

The usual trigger pseudo-columns and variables wouldn’t be available in that generic function, but considering that your triggers fire from different tables, you could either pass them on or they (probably) don’t make sense in the context of the generic function.

Alban Hertroys
--
There is always an exception to always.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Matthias Apitz 2022-07-05 07:51:19 lifetime of the old CTID
Previous Message David G. Johnston 2022-07-05 01:03:17 Re: Getting data from a record variable dynamically