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 1tnQsw-005Egp-Ii for pgsql-general@arkaria.postgresql.org; Wed, 26 Feb 2025 23:32:14 +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 1tnQsv-00H5mh-Gj for pgsql-general@arkaria.postgresql.org; Wed, 26 Feb 2025 23:32:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tnQsv-00H5mW-40 for pgsql-general@lists.postgresql.org; Wed, 26 Feb 2025 23:32:13 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tnQss-000HIS-0v for pgsql-general@lists.postgresql.org; Wed, 26 Feb 2025 23:32:12 +0000 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-54529eeb38aso193172e87.2 for ; Wed, 26 Feb 2025 15:32:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740612730; x=1741217530; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=RSxlEBE1dSz2j7nvm0cNVbwiGCCyJ+tYwd9zoziInwo=; b=fo7pQ6ccgytRsG7p1PCqFND+KyGTQIlOGt6EPydzZX9MIDWV4/rKEt0ETQVVfLjuh3 Pyka54ktAGLRCRnnoaSeO3nChguAprV0VDRf12vZd9ynBh7zeJHanywTOhuEPLaZBz7D FDc6SkfWazdjpkHbVBbNYM7Jq0NcGZYm+nUxGZN3U1rFIe2zg2XTq7P72M3a/YzgfGzi UBiTJjKPXUTeeGP7TGChYWWANAJ1u+GEeOqztKXgvS+POFENVSfQerqqs6ewI0Sr6eiZ kYPYu9GYxWujAdzU+o/Lig+gx6H9QYlyioEMx+YOE91UET2u6e+3jKiskgKsqL9Hkxg1 XdgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740612730; x=1741217530; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RSxlEBE1dSz2j7nvm0cNVbwiGCCyJ+tYwd9zoziInwo=; b=Fs8JLLhZsDp+KtcKp2I/Rjm6iEDT/rKPQzxQD68TQ6mFlvqPFdpMVrzpIyRGTP04E9 wSdsNU0KSBLDLbObX2prrRCKLs6ivmVi1o8ASr/Z3rzyyWcxOV8vbz3+FitnJ3C9O3ex Ja82ZumGzjip10fTgZslUaKNaijTm3OHJpxzoYdnpL8bm3mFOidpTgn5uN8/dZQBwc0l zA2Nlrpznbi+RG9eOgbl7HKVJOqgn3+EtuHgg91cSkz6PsFzQoNQlQvMgiIEritfqnWi GtCS3cnXuL/uwGjc7JytocwEoGgdI4dB/UbKFFyVGvkRXMKCii097m9bEs9BAPhbg+5U e9Yw== X-Forwarded-Encrypted: i=1; AJvYcCXhlu1U7rB34OuPF34iAJpuEKFL3tzNr8wjF9+/WOgPaK+pf2wVdsEyS049quh5FVpsIkpY+QHmOQ20DNG1@lists.postgresql.org X-Gm-Message-State: AOJu0YyfQFtNoqXYtkNLr6JChof9npWwtRRQXxW+3AUgFm42IruvBJ7j 7goG6nk/PWdLtDNwxlpBpBRFsR0jjd0YoYtyGGlmZjmg8uyopJTc/b5v16Or0QD89FzpKJetjc0 qDwIuDNEyUWNd1rQLeCVV0Lmt/IA= X-Gm-Gg: ASbGncuKXTVclWK/v4ogH052KCqc1D9G0N3CBUePqAjFHRF7EBuEj7kR08ulxRWDO0T CwdzVdoW8JXMrGs6PY0lgiM77/axSrqAP1CFjm4YnHJdJNc8TQz0vwFcy0++dzGWwvaSrS9sL1L 4gduTyz9JGo/kBZUJw33e7oNXD6ln5lwNJaZINRi3/1g== X-Google-Smtp-Source: AGHT+IFm2bLDZTV9k3uMvZqDrWpgpDwYgogN5PVjwqa7tEXsO1WC9zaat9Z+UnyvPoxlmuS07IpylrAumUDg3dogHpQ= X-Received: by 2002:a05:6512:280e:b0:544:ead:e1d6 with SMTP id 2adb3069b0e04-5493c5aeb85mr3138461e87.38.1740612729545; Wed, 26 Feb 2025 15:32:09 -0800 (PST) MIME-Version: 1.0 References: <56861740492680@mail.yandex.ru> In-Reply-To: From: Merlin Moncure Date: Wed, 26 Feb 2025 17:31:56 -0600 X-Gm-Features: AQ5f1JrZKO4beh0g0EXJGuf8dcBOFpTHfIJSAqArHsYhB2maVbW-ZsYjzlmSw4s Message-ID: Subject: Re: How to return seto records from seof record function? To: Laurenz Albe Cc: =?UTF-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000006fb311062f13fb4d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006fb311062f13fb4d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 25, 2025 at 8:47=E2=80=AFAM Laurenz Albe wrote: > > I don't think there is a way to get a generic "record" as result. > And even if you could, you would still have to specify a column list > when you call autonomous(). > > Attempts to write functions with polymorphic return type are usually > futile. > > Perhaps you can return a "SETOF jsonb"... There is only one non-jsonb method I'm aware of to convert string query to result without specifying result structure, and that's via refcursors, something like: begin; BEGIN; CREATE FUNCTION f() RETURNS TEXT AS $$ DECLARE r REFCURSOR DEFAULT 'test'; BEGIN OPEN r FOR EXECUTE $z$SELECT 'a' AS a, 1 AS b$z$; RETURN r; END; $$ LANGUAGE PLPGSQL; SELECT f(); FETCH test; ... ..I doubt it works in OP's case though as this only works to push all the way back to the client app. but it's a neat artifact from yore. In modern postgres, I think jsonb is the way to go. Function output syntax is one of the clunkiest parts of the language, you are on a freight train to deep dynamic SQL; it sure would be nice if we could somehow pass an output definition somehow in a way the calling function or query could use. This mostly comes up in my experience with analytics, where the column needs are very dynamic and layered. merlin --0000000000006fb311062f13fb4d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Feb 25, 2025 at 8:47=E2=80=AFAM L= aurenz Albe <laurenz.albe@cy= bertec.at> wrote:

I don't think there is a way to get a generic "record" as res= ult.
And even if you could, you would still have to specify a column list
when you call autonomous().

Attempts to write functions with polymorphic return type are usually futile= .

Perhaps you can return a "SETOF jsonb"...

There is only one non-jsonb method I'm aware of to convert str= ing query to result without specifying result structure, and that's via= refcursors, something=C2=A0like:
begin;
BEGIN;

CREATE FUNCTION f() RETURNS TEXT AS
$$
DECLARE
=C2=A0 r REF= CURSOR DEFAULT 'test';
BEGIN
=C2=A0 OPEN r FOR EXECUTE $z$SEL= ECT 'a' AS a, 1 AS b$z$;
=C2=A0 RETURN r;
END;
$$ LANGUAGE= PLPGSQL;
=C2=A0
SELECT f();
=C2=A0
FETCH test;
...

..I doubt it works in OP's case though as this only wo= rks to push all the way back to the client app. but it's a neat artifac= t from yore.=C2=A0 =C2=A0

In modern postgres, I th= ink=C2=A0jsonb is the way to go.=C2=A0 Function output syntax is one of the= clunkiest parts of the language, you are on a freight train to deep dynami= c SQL; it sure would be nice if we could somehow pass an output definition = somehow in a way the calling function or query could use.=C2=A0 This mostly= comes up in my experience=C2=A0with analytics, where the column needs are = very dynamic and layered.

merlin

=C2=A0
--0000000000006fb311062f13fb4d--