drop table _inner, middle, _outer;

create table middle(
  id1 bigint
);

create table _outer(
  id3 bigint,
  extra1 bigint
);

create table _inner(
  filler bigint,
  id2 bigint
);

insert into middle select n from generate_series(1,1000) n;
insert into _outer select n, n+10 from generate_series(1,10000) n;
insert into _outer select n, n+11 from generate_series(1,10000) n;
insert into _inner select n, n from generate_series(1, 100000) n;

alter table _outer add primary key(id3, extra1);

SET min_parallel_table_scan_size = 0;
SET min_parallel_index_scan_size = 0;
SET parallel_tuple_cost = 0.01;
SET parallel_setup_cost = 0;
SET max_parallel_workers_per_gather = 4;

ANALYZE _outer, _inner, middle;

explain (verbose, costs off)
select count(*) from _outer where exists (
    select 1 from middle
    where exists (
        select 1 from _inner where _inner.id2 = middle.id1
    )
    and middle.id1 = _outer.id3
);

select count(*) from _outer where exists (
    select 1 from middle
    where exists (
        select 1 from _inner where _inner.id2 = middle.id1
    )
    and middle.id1 = _outer.id3
);