public inbox for [email protected]  
help / color / mirror / Atom feed
From: Laurenz Albe <[email protected]>
To: Олег Самойлов <[email protected]>
To: [email protected] <[email protected]>
Subject: Re: How to return seto records from seof record function?
Date: Tue, 25 Feb 2025 15:47:23 +0100
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[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






view thread (2+ messages)  latest in thread

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], [email protected], [email protected]
  Subject: Re: How to return seto records from seof record function?
  In-Reply-To: <[email protected]>

* 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