CREATE OR REPLACE FUNCTION selectDetailed ()
RETURNS setof record AS $_$
DECLARE
    result_rec                  record;
    sql                         text;
BEGIN
    sql := $$
           --EXPLAIN
           SELECT * FROM (
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           UNION ALL
           SELECT now(),
                  ss.id, ss.id, ss.id, ss.id, ss.id, ss.id, 
                  'a'::text, 'bbbbbbbb'::text,
                  'cccccccccccccccccc'::text,
                  'ddddddddddddddddddddddddddddddddddd'::text,
                  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'::text,
                  'fffffffffffffffffffffffffffffffffff'::text
           FROM (SELECT id::bigint FROM generate_series(1,2000000) as t(id)) ss
           ) as t_inner order by 1
           $$;
    FOR result_rec IN EXECUTE sql LOOP
        RETURN NEXT result_rec;
    END LOOP;
    RETURN;
END;
$_$ LANGUAGE plpgsql STABLE;

select count(*) from selectDetailed() AS t(
    ts timestamp with time zone,
    d1 bigint, d2 bigint, d3 bigint,
    d4 bigint, d5 bigint, d6 bigint,
    s1 text, s2 text, s3 text,
    s4 text, s5 text, s6 text
);

