

create table testtimes (elem int4, duration_ms numeric);

create or replace function jsonbtest() returns void as $$
declare
   ja jsonb;
   i int4;
   begin_time timestamptz;
   end_time timestamptz;
   step int4;
   max_elems int4;
   elem_no int4;
begin
   step := 500;
   max_elems := 10000;

   select json_agg(g) into ja from generate_series(1, max_elems) g;

   elem_no := 10;
   loop
     elem_no = (elem_no::numeric * 1.1)::int4;
     if elem_no > max_elems then
       exit;
     end if;

     begin_time = clock_timestamp();
     for i in 1..100000 loop
       perform ja ->> elem_no;
     end loop;
     end_time = clock_timestamp();
     insert into testtimes values (elem_no, extract(epoch from end_time - begin_time));
     raise notice '%', elem_no;
  end loop;
end;
$$ language plpgsql;


-- Run the test
select jsonbtest();
