DO $proc$
	DECLARE
	StartTime timestamptz;
EndTime timestamptz;
Delta double precision;
k int;
m_run_no int;
j_res text;
i int;
c_size int;
t_size int4;
BEGIN
/*
    CREATE TABLE test_toast_p (id int, jb jsonb);
    CREATE TABLE json_res_size ( run_no int, a_key char(20), description varchar(50), id int, timing double precision, ex_result text, b_size int, n_level int, wal_size bigint, w_txt_sz text);
*/
    select (max(run_no)+1) into m_run_no from json_res_size;
    m_run_no = 1;
    k = 1;
    i = 1;
	while i <= 1000000 loop
    	for j in 1..10 loop
        select pg_column_size(jb ) into t_size FROM test_toast_p WHERE id = k;    
		StartTime := clock_timestamp();
        insert into test_toast_p values (k,  repeat('a', i*j)); 
        EndTime := clock_timestamp();
        Delta := 1000 * ( extract(epoch from EndTime) - extract(epoch from StartTime) );
        select pg_column_size(jb) into c_size FROM test_toast_p WHERE id = k;
        insert into json_res_size values ( m_run_no, 'insert', to_char(k, '999'), k, Delta, (j_res), c_size, t_size, 0, '');
        k = k + 1;
        end loop;
        if( i >= 10000000 ) then i = i + 1;
        else i = i * 10;
        end if;
	end loop;
    
	m_run_no = m_run_no + 1;
    for l in 1..100 loop
    k = 1;
    i = 1;
    select max(id) into k from test_toast_p;
	while k > 0 loop
        select pg_column_size(jb ) into t_size FROM test_toast_p WHERE id = k;
		StartTime := clock_timestamp();
        UPDATE test_toast_p set jb = repeat('b', pg_column_size(jb)+1) where id=k; 
        EndTime := clock_timestamp();
        Delta := 1000 * ( extract(epoch from EndTime) - extract(epoch from StartTime) );
        explain (analyze, buffers, format json) into j_res UPDATE test_toast_p set jb = repeat('b', pg_column_size(jb)+1) where id=k;
        select pg_column_size(jb ) into c_size FROM test_toast_p WHERE id = k;
        insert into json_res_size values ( m_run_no, 'update', to_char(k, '999'), k, Delta, (j_res), c_size, t_size, 0, '');
        k = k - 1;
	end loop;
    end loop;
    
	m_run_no = m_run_no + 1;
    for l in 1..100 loop
    k = 1;
    i = 1;
    select max(id) into k from test_toast_p;
	while k > 0 loop
        select pg_column_size(jb ) into t_size FROM test_toast_p WHERE id = k;
		StartTime := clock_timestamp();
        PERFORM jb from test_toast_p where id=k;
        EndTime := clock_timestamp();
        Delta := 1000 * ( extract(epoch from EndTime) - extract(epoch from StartTime) );
        explain (analyze, buffers, format json) into j_res select jb from test_toast_p where id=k;
        select pg_column_size(jb ) into c_size FROM test_toast_p WHERE id = k;
        insert into json_res_size values ( m_run_no, 'select', to_char(k, '999'), k, Delta, (j_res), c_size, t_size, 0, '');
        k = k - 1;
	end loop;
    end loop;
END;
$proc$;
