drop table t;

create table t as select (random() * 100000000)::int i, 42 j from generate_series(1, 20000000);
create index on t(i);

select pg_prewarm('t');
select pg_prewarm('t_i_idx');
set max_parallel_workers_per_gather = 0;
set enable_bitmapscan = off;



\timing off
explain select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
\timing on
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;

create index i0 on t using brin(i);
create index i1 on t using brin(i);
create index i2 on t using brin(i);
create index i3 on t using brin(i);
create index i4 on t using brin(i);
create index i5 on t using brin(i);
create index i6 on t using brin(i);
create index i7 on t using brin(i);
create index i8 on t using brin(i);
create index i9 on t using brin(i);

select pg_prewarm('t');
select pg_prewarm('t_i_idx');
\timing off
explain select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
\timing on
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;

create index i10 on t using brin(i);
create index i11 on t using brin(i);
create index i12 on t using brin(i);
create index i13 on t using brin(i);
create index i14 on t using brin(i);
create index i15 on t using brin(i);
create index i16 on t using brin(i);
create index i17 on t using brin(i);
create index i18 on t using brin(i);
create index i19 on t using brin(i);
create index i20 on t using brin(i);
create index i21 on t using brin(i);
create index i22 on t using brin(i);
create index i23 on t using brin(i);
create index i24 on t using brin(i);
create index i25 on t using brin(i);
create index i26 on t using brin(i);
create index i27 on t using brin(i);
create index i28 on t using brin(i);
create index i29 on t using brin(i);

select pg_prewarm('t');
select pg_prewarm('t_i_idx');
\timing off
explain select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
\timing on
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;
select count(*) from t where i between 5000000 and 15000000 and j = 42;

