有什么比在 postgres 中使用的临时 table 更好的东西从函数 return a table (结果)?

Is there something better than the temporary table to be used in postgres to return a table (result) from the function?

我想知道是否有一些其他“更好”的选项可用于 return 来自 pgsql 函数的 table 结果?目前,开发者已经使用临时table来存储“临时”数据。

我正在查看这个函数:

CREATE OR REPLACE FUNCTION test.fetch_data(a_panel_id bigint, a_hh_id bigint, a_site bigint, a_insert_date timestamp with time zone)
 RETURNS TABLE(panel_id bigint, hh_id bigint, site_id bigint, meter_raw_data text)
 LANGUAGE plpgsql
 ROWS 2000
AS $function$
DECLARE 
-- Stores each row returned from the table
rec record;
session_insert_time date;
session_id_v integer;

BEGIN
    create temp table interim_data (
    panel_id bigint,
    source_location_id bigint, 
    site_num bigint, 
    meter_data text
    ) on commit drop;

        FOR rec IN
            select rmd.primary_key as "id",
            rmd.source_location_id as "hh_id_tmp", 
            rmd.site_num as "site_tmp", 
            rmd.meterdata as "meter_data",
            rmd.panel_id as "panel_id_tmp"
            from checkout.rawmeterdata rmd 
            where 
            rmd.source_location_id = a_hh_id and 
            rmd.site_num = a_site and 
            rmd.panel_id = a_panel_id and
            rmd.is_polled=false and 
            rmd.insert_time > a_insert_date
            order by (rmd.insert_time) asc
        LOOP
            INSERT INTO interim_data 
            VALUES (
                    rec."panel_id_tmp",
                    rec."hh_id_tmp",
                    rec."site_tmp",
                    convert_from(rec."meter_data", 'UTF-8')
            );
        END LOOP;   
    return query select * from interim_data;
END $function$;

谢谢

只是return查询的结果。甚至不需要 PL/pgSQL:

CREATE OR REPLACE FUNCTION test.fetch_data(a_panel_id bigint, a_hh_id bigint, a_site bigint, a_insert_date timestamp with time zone)
 RETURNS TABLE(panel_id bigint, hh_id bigint, site_id bigint, meter_raw_data text)
 LANGUAGE sql
 ROWS 2000
AS 
$function$
   select rmd.panel_id,
          rmd.source_location_id,
          rmd.site_num,
          convert_from(rmdmeterdata , 'UTF-8')
   from checkout.rawmeterdata rmd 
   where rmd.source_location_id = a_hh_id and 
         rmd.site_num = a_site and 
         rmd.panel_id = a_panel_id and
         rmd.is_polled=false and 
         rmd.insert_time > a_insert_date
   order by rmd.insert_time;
END 
$function$;