CREATE DATABASE jitbench;

\c jitbanch

create extension pg_prewarm;

CREATE TABLE jitbench (
    id  INT4,
    a   INT4
);

INSERT INTO jitbench SELECT serie, serie FROM generate_series(1,1000000) as serie;

vacuum freeze analyze jitbench;
select pg_prewarm('jitbench');

CREATE PROCEDURE jitbenchproc(num INT4, cycles INT4) LANGUAGE plpgsql AS $_$
DECLARE
v_out INT8;
v_i     INT4;
v_j     INT4;
BEGIN
    FOR v_j IN 1 .. cycles LOOP
        FOR v_i IN 0 .. (num - 1)  LOOP
            SELECT SUM(a) INTO v_out FROM jitbench WHERE MOD(a,num) = v_i;
        END LOOP;
    END LOOP;
END;
$_$;

\timing
call jitbenchproc(1,10);
