public inbox for [email protected]  
help / color / mirror / Atom feed
From: Ray O'Donnell <[email protected]>
To: 'PostgreSQL' <[email protected]>
Subject: Combining scalar and row types in RETURNING
Date: Tue, 3 Jun 2025 16:23:15 +0000
Message-ID: <01020197369aac89-01a7b7b4-b775-471e-ac8e-de8b28d87008-000000@eu-west-1.amazonses.com> (raw)

Can you combine scalar and row types in a RETURNING clause?

My use-case is getting the results of a MERGE - I'd like to be able to 
capture both the action performed and the modified row, something like 
this (this is in a plpgsql function):

declare
     m_action text;
     m_new_data record;
begin
     merge into my_table t
     using (
         ....
     ) s
     on (t.id = s.id)
     when matched then
         update .....
     when not matched then
         insert .....
     returning
         merge_action(), t.*
     into
        m_action, m_new_data;

end;

In my case, m_new_data is actually a table row type rather than plain 
"record". The row is passed on to another function which calculates the 
altered columns and logs the changes.

I've tried it, and it doesn't seem to work; I get an error, "m_new_data 
is not a scalar variable", so I'm guessing it's not possible, but it's 
worth asking... I know I can list the returned columns individually in 
the RETURNING and then use a row constructor to construct the row.... 
but it'd be handier if I could just derive the row directly from the 
MERGE query.

Thanks in advance,

Ray.

-- 
Raymond O'Donnell // Galway // Ireland
[email protected]







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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: Combining scalar and row types in RETURNING
  In-Reply-To: <01020197369aac89-01a7b7b4-b775-471e-ac8e-de8b28d87008-000000@eu-west-1.amazonses.com>

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

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