Combining scalar and row types in RETURNING

From: Ray O'Donnell <ray(at)rodonnell(dot)ie>
To: 'PostgreSQL' <pgsql-general(at)postgresql(dot)org>
Subject: Combining scalar and row types in RETURNING
Date: 2025-06-03 16:23:15
Message-ID: 01020197369aac89-01a7b7b4-b775-471e-ac8e-de8b28d87008-000000@eu-west-1.amazonses.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

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
ray(at)rodonnell(dot)ie

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2025-06-03 16:53:58 Re: Combining scalar and row types in RETURNING
Previous Message Dominique Devienne 2025-06-03 15:38:23 Re: current_role of caller of a DEFINER function