-- needs a -DBARRIER_DEBUG build

DROP TABLE IF EXISTS tab;
CREATE TABLE tab (id int);
INSERT INTO tab SELECT generate_series(1, 500000);
ALTER TABLE tab SET (parallel_workers = 2);
ANALYZE tab;

SET enable_mergejoin = false;
SET enable_nestloop = false;
SET force_parallel_mode = on;
SET parallel_synchronization_cost = 0;

-- no leader + 2 workers, single batch, second worker joins at every possible
-- phase
SET max_parallel_workers_per_gather = 2;

EXPLAIN SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,1';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,2';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,3';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,4';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,5';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,6';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

-- same again, inner join
EXPLAIN SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,1';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,2';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,3';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,4';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,5';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,6';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

-- leader + worker, single batch, leader joins at every possible phase
SET max_parallel_workers_per_gather = 2;

EXPLAIN SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:0,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:1,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:2,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:3,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:4,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:5,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:6,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

-- same again, inner join
EXPLAIN SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:0,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:1,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:2,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:3,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:4,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:5,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:6,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);


SET work_mem = '64kB';
SET barrier_attach_phases = '';

-- no leader + 2 workers, multi batch, worker 2 joins at every possible
-- phase
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,1';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,2';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,3';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,4';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,5';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,6';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,7';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,8';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,9';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,10';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,11';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,12';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,13';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

-- same again, inner join
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,1';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,2';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,3';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,4';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,5';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,6';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,7';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,8';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,9';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,10';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,11';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,12';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:-1,0,13';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

-- no leader + 2 workers, multi batch, leader joins at every possible
-- phase
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:0,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:1,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:2,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:3,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:4,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:5,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:6,0,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

-- same again, inner join
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:0,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:1,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:2,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:3,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:4,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:5,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.barrier:6,0,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);


-- no leader + 2 workers, multi batch, worker 2 joins main barrier before
-- building phase, and attaches to the shrink barrier at every possible phase
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,0';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,1';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,2';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,3';
SELECT COUNT(*) FROM tab t1 FULL OUTER JOIN tab t2 USING (id);

-- same again, inner join
EXPLAIN analyze SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,0';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,1';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,2';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);

SET barrier_attach_phases = 'HashJoin.shrink_barrier:-1,-1,3';
SELECT COUNT(*) FROM tab t1 JOIN tab t2 USING (id);


