public inbox for [email protected]  
help / color / mirror / Atom feed
Re: How to return seto records from seof record function?
2+ messages / 2 participants
[nested] [flat]

* Re: How to return seto records from seof record function?
@ 2025-02-25 14:47  Laurenz Albe <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Laurenz Albe @ 2025-02-25 14:47 UTC (permalink / raw)
  To: Олег Самойлов <[email protected]>; [email protected] <[email protected]>

On Tue, 2025-02-25 at 17:15 +0300, Олег Самойлов wrote:
> Postgresql 17.2
> 
> How to return seto records from seof record function? I tried pg_background extension:
>  
> CREATE OR REPLACE FUNCTION public.autonomous (p_script text)
> RETURNS SETOF record
> LANGUAGE plpgsql
> VOLATILE STRICT PARALLEL UNSAFE
> AS $autonomous$
> DECLARE
> l_id integer;
> BEGIN
> l_id := pg_background_launch(p_script);
> RETURN QUERY SELECT * FROM pg_background_result(l_id) AS (r record);
> END;
> $autonomous$;
>  
> SELECT * FROM autonomous('SELECT now()') AS (a timestamptz);
>  
> SQL Error [42804]: ERROR: structure of query does not match function result type
>   Detail: Returned type record does not match expected type timestamp with time zone in column 1.
>   Where: SQL statement "SELECT * FROM pg_background_result(l_id) AS (r record)"
> PL/pgSQL function autonomous(text) line 6 at RETURN QUERY

You need to be specific:

  SELECT * FROM pg_background_result(l_id) AS (col1 integer, col2 text, ...);

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"...

Yours,
Laurenz Albe






^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How to return seto records from seof record function?
@ 2025-02-26 23:31  Merlin Moncure <[email protected]>
  parent: Laurenz Albe <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Merlin Moncure @ 2025-02-26 23:31 UTC (permalink / raw)
  To: Laurenz Albe <[email protected]>; +Cc: Олег Самойлов <[email protected]>; [email protected] <[email protected]>

On Tue, Feb 25, 2025 at 8:47 AM Laurenz Albe <[email protected]>
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


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-02-26 23:31 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-02-25 14:47 Re: How to return seto records from seof record function? Laurenz Albe <[email protected]>
2025-02-26 23:31 ` Merlin Moncure <[email protected]>

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