-- needs a -DBARRIER_DEBUG build

\set ECHO all
\timing on

set min_parallel_table_scan_size = 0;
set parallel_setup_cost = 0;

-- Make a simple relation with well distributed keys and correctly
-- estimated size.
create table simple as
  select generate_series(1, 20000) AS id, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
alter table simple set (parallel_workers = 2);
analyze simple;

-- Make a relation whose size we will under-estimate.  We want stats
-- to say 1000 rows, but actually there are 20,000 rows.
create table bigger_than_it_looks as
  select generate_series(1, 20000) as id, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
alter table bigger_than_it_looks set (autovacuum_enabled = 'false');
alter table bigger_than_it_looks set (parallel_workers = 2);
delete from bigger_than_it_looks where id <= 19000;
vacuum bigger_than_it_looks;
analyze bigger_than_it_looks;
insert into bigger_than_it_looks
  select generate_series(1, 19000) as id, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

-- A parallel hash join that needs to increase both buckets and batches
set max_parallel_workers_per_gather = 2;
set work_mem = '128kB';
set enable_parallel_hash = on;
explain (costs off)
  select count(*) from simple r join bigger_than_it_looks s using (id);

-- participant #2 will arrive at every possible build_barrier phase
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,0';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,1';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,3';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,4';
select count(*) from simple r join bigger_than_it_looks s using (id);

-- participant #2 will arrive at every possible grow_batches_barrier phase
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_batches_barrier:-1,0,0';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_batches_barrier:-1,0,1';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_batches_barrier:-1,0,2';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_batches_barrier:-1,0,3';
select count(*) from simple r join bigger_than_it_looks s using (id);

-- participant #2 will arrive at every possible grow_buckets_barrier phase
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_buckets_barrier:-1,0,0';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_buckets_barrier:-1,0,1';
select count(*) from simple r join bigger_than_it_looks s using (id);
SET barrier_attach_phases = 'HashJoin.build_barrier:-1,0,2 HashJoin.grow_buckets_barrier:-1,0,2';
select count(*) from simple r join bigger_than_it_looks s using (id);

drop table bigger_than_it_looks;
drop table simple;
