Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uMWDw-00E5JG-U4 for pgsql-general@arkaria.postgresql.org; Tue, 03 Jun 2025 18:18:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uMWDu-00347H-Ts for pgsql-general@arkaria.postgresql.org; Tue, 03 Jun 2025 18:18:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from <01020197370480a4-f9b53911-e5a8-4bdb-81e5-de41d5db4d8c-000000@mail.rodonnell.ie>) id 1uMWDu-003479-DA for pgsql-general@lists.postgresql.org; Tue, 03 Jun 2025 18:18:55 +0000 Received: from a7-35.smtp-out.eu-west-1.amazonses.com ([54.240.7.35]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from <01020197370480a4-f9b53911-e5a8-4bdb-81e5-de41d5db4d8c-000000@mail.rodonnell.ie>) id 1uMWDt-0002iN-0Y for pgsql-general@postgresql.org; Tue, 03 Jun 2025 18:18:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=dv7fx26oobputowqxkzhnujragfvk7eg; d=rodonnell.ie; t=1748974731; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=+jgwEEz2E42xTqqMl43BgB/BlOiwuDFeR7yGfKhXgUw=; b=iZzJ+GT608Ew0c5+ZDUrcVrKIrSnojo8GLVE8ZXHih9kr6ZWXkNeIDpVGmg92Eee DQGv5aIA6zekvxURfA6AxeoS1ycc39ghI6eGQ4xj0eD68KtI+h/XaiM2Y+vFsctMlPj z2/d30f2fNV80cNVNsM9iUkWWE2UOXu1S7KJi/bzgnUx7DzsrgCKGMKMf9H2KSC/3VY SUvQME6uBR/DT63AVsxbXqwUpfh/0w7UT3wRSqOpZSn+JRdNTwjq+8XyvWnyj+UqBfB XTbeaRp1OkrnPm5JnUMtC0N+KZzqeMQOOvZP0ZO8HAlwPn9G11kH+eqUj29OqWcvevC +x+22TcXqw== DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ihchhvubuqgjsxyuhssfvqohv7z3u4hn; d=amazonses.com; t=1748974731; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Content-Type:Content-Transfer-Encoding:Feedback-ID; bh=+jgwEEz2E42xTqqMl43BgB/BlOiwuDFeR7yGfKhXgUw=; b=l47kBuN7SVyvxAKbEUQe+ATb0yQqR1M2sbfS7fO6fCXoZvthaM9gdeKC7JRBZs6s hWDspX7o8E0eRu+nTzepYI+pXQ9+vdYrSpi3MraKP1ysiUaDW4eep81Ta1GYWMxyXbF zsltMj0A9xpNqnqpZwrB/yaibGjqXOGHwxOHLum8= Message-ID: <01020197370480a4-f9b53911-e5a8-4bdb-81e5-de41d5db4d8c-000000@eu-west-1.amazonses.com> Date: Tue, 3 Jun 2025 18:18:51 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Combining scalar and row types in RETURNING To: Tom Lane Cc: 'PostgreSQL' References: <01020197369aac89-01a7b7b4-b775-471e-ac8e-de8b28d87008-000000@eu-west-1.amazonses.com> <858949.1748969638@sss.pgh.pa.us> Content-Language: en-GB, de-DE From: Ray O'Donnell In-Reply-To: <858949.1748969638@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Feedback-ID: ::1.eu-west-1.Bw1eu/7Uepg3xcsiFBL71JJRCh2A2yTFrgBhhiulXmU=:AmazonSES X-SES-Outgoing: 2025.06.03-54.240.7.35 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 03/06/2025 17:53, Tom Lane wrote: > "Ray O'Donnell" writes: >> Can you combine scalar and row types in a RETURNING clause? > I think so. > >> 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; > I think the problem here is that "t.*" gets expanded into a list of > all of t's columns, just as would happen in a SELECT's output list. > Try > > returning merge_action(), t > > It might also be necessary to declare the target variable > "m_new_data" as being of type my_table rather than generic > "record"; not sure about that. Thanks a million for the explanation, Tom - that makes sense. I tried what you suggested, with mixed results: (i) Running the MERGE as a stand-alone query, with just RETURNING... , worked - I got a scalar and a row as expected. (ii) Running it in a function (actually a DO block), with m_new correctly declared as the table type, failed with the same error as before. (iii) Running (ii) but with the order of the items in RETURNING reversed -     ... returning t, merge_action() into m_new, m_action - gave me a different error: ERROR:  record variable cannot be part of multiple-item INTO list LINE 53:         m, merge_action() into m_new, m_action ...which seems to answer my question definitively. Thanks once more, Ray. -- Raymond O'Donnell // Galway // Ireland ray@rodonnell.ie