CREATE OR REPLACE FUNCTION create_parts ()
RETURNS text AS $_$
DECLARE
    i     int;
    sql   text;
BEGIN
   sql := 'CREATE TABLE foo (
           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)';
    EXECUTE sql;
    FOR i IN 1..100 LOOP
      sql := 'CREATE TABLE foo_' || i || ' () INHERITS (foo)';
      EXECUTE sql;
      sql := $$
               INSERT INTO foo_$$ || i || $$ 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,200000) as t(id)) ss
             $$;
      EXECUTE sql;
    END LOOP;
    RETURN 'OK';
END;
$_$ LANGUAGE plpgsql VOLATILE;

SELECT create_parts ();

CREATE OR REPLACE FUNCTION selectDetailed ()
RETURNS setof foo AS $_$
DECLARE
    result_rec                  record;
    sql                         text;
BEGIN
    sql := 'SELECT * FROM foo';
    FOR result_rec IN EXECUTE sql LOOP
        RETURN NEXT result_rec;
    END LOOP;
    RETURN;
END;
$_$ LANGUAGE plpgsql STABLE;

select count(*) from selectDetailed();



