|
| 1 | +-- |
| 2 | +-- Right Semi / Right Anti Join (GPORCA) |
| 3 | +-- |
| 4 | +-- GPORCA can build the hash table on the (smaller) left-hand side of a semi or |
| 5 | +-- anti join and probe it with the larger right-hand side, which avoids having |
| 6 | +-- to de-duplicate the large right side. The behaviour is controlled by the |
| 7 | +-- developer GUC optimizer_enable_right_semi_join (ON by default). |
| 8 | +-- |
| 9 | +-- These cases force GPORCA (set optimizer=on) so the new |
| 10 | +-- CPhysicalRightSemiHashJoin / CPhysicalRightAntiSemiHashJoin xforms are |
| 11 | +-- exercised; the matching answer file rightsemijoin_optimizer.out captures the |
| 12 | +-- right-semi/right-anti plan shapes, while rightsemijoin.out captures the |
| 13 | +-- Postgres planner plans. |
| 14 | +-- |
| 15 | +create schema rsj; |
| 16 | +set search_path = rsj, public; |
| 17 | +-- stabilize the join method across planners |
| 18 | +set enable_nestloop = off; |
| 19 | +set enable_mergejoin = off; |
| 20 | +create table rsj_small(a int, b int) distributed by (a); |
| 21 | +create table rsj_big(a int, b int) distributed by (a); |
| 22 | +-- small LHS: 1..50, plus two values absent from rsj_big |
| 23 | +insert into rsj_small select i, i from generate_series(1,50) i; |
| 24 | +insert into rsj_small values (600001, 1), (600002, 2); |
| 25 | +-- large, high-NDV RHS: de-duplicating it would be expensive, so the right-semi |
| 26 | +-- plan (build on the small LHS) wins. |
| 27 | +insert into rsj_big select i, i from generate_series(1,500000) i; |
| 28 | +analyze rsj_small; |
| 29 | +analyze rsj_big; |
| 30 | +-- |
| 31 | +-- Semi join (IN): GUC on -> Hash Right Semi Join (build on small LHS) |
| 32 | +-- |
| 33 | +set optimizer_enable_right_semi_join = on; |
| 34 | +explain (costs off) |
| 35 | +select * from rsj_small s where s.a in (select a from rsj_big b); |
| 36 | + QUERY PLAN |
| 37 | +------------------------------------------- |
| 38 | + Gather Motion 3:1 (slice1; segments: 3) |
| 39 | + -> Hash Right Semi Join |
| 40 | + Hash Cond: (b.a = s.a) |
| 41 | + -> Seq Scan on rsj_big b |
| 42 | + -> Hash |
| 43 | + -> Seq Scan on rsj_small s |
| 44 | + Optimizer: Postgres query optimizer |
| 45 | +(7 rows) |
| 46 | + |
| 47 | +-- GUC off -> regular Hash Semi Join (build on RHS) |
| 48 | +set optimizer_enable_right_semi_join = off; |
| 49 | +explain (costs off) |
| 50 | +select * from rsj_small s where s.a in (select a from rsj_big b); |
| 51 | + QUERY PLAN |
| 52 | +------------------------------------------- |
| 53 | + Gather Motion 3:1 (slice1; segments: 3) |
| 54 | + -> Hash Right Semi Join |
| 55 | + Hash Cond: (b.a = s.a) |
| 56 | + -> Seq Scan on rsj_big b |
| 57 | + -> Hash |
| 58 | + -> Seq Scan on rsj_small s |
| 59 | + Optimizer: Postgres query optimizer |
| 60 | +(7 rows) |
| 61 | + |
| 62 | +-- |
| 63 | +-- Anti join (NOT EXISTS): GUC on -> Hash Right Anti Join (build on small LHS) |
| 64 | +-- |
| 65 | +set optimizer_enable_right_semi_join = on; |
| 66 | +explain (costs off) |
| 67 | +select * from rsj_small s where not exists (select 1 from rsj_big b where b.a = s.a); |
| 68 | + QUERY PLAN |
| 69 | +------------------------------------------- |
| 70 | + Gather Motion 3:1 (slice1; segments: 3) |
| 71 | + -> Hash Right Anti Join |
| 72 | + Hash Cond: (b.a = s.a) |
| 73 | + -> Seq Scan on rsj_big b |
| 74 | + -> Hash |
| 75 | + -> Seq Scan on rsj_small s |
| 76 | + Optimizer: Postgres query optimizer |
| 77 | +(7 rows) |
| 78 | + |
| 79 | +-- GUC off -> regular Hash Anti Join |
| 80 | +set optimizer_enable_right_semi_join = off; |
| 81 | +explain (costs off) |
| 82 | +select * from rsj_small s where not exists (select 1 from rsj_big b where b.a = s.a); |
| 83 | + QUERY PLAN |
| 84 | +------------------------------------------- |
| 85 | + Gather Motion 3:1 (slice1; segments: 3) |
| 86 | + -> Hash Right Anti Join |
| 87 | + Hash Cond: (b.a = s.a) |
| 88 | + -> Seq Scan on rsj_big b |
| 89 | + -> Hash |
| 90 | + -> Seq Scan on rsj_small s |
| 91 | + Optimizer: Postgres query optimizer |
| 92 | +(7 rows) |
| 93 | + |
| 94 | +-- |
| 95 | +-- Correctness: results are identical regardless of the GUC / plan shape. |
| 96 | +-- |
| 97 | +set optimizer_enable_right_semi_join = on; |
| 98 | +select count(*) as semi_on from rsj_small s where s.a in (select a from rsj_big b); |
| 99 | + semi_on |
| 100 | +--------- |
| 101 | + 50 |
| 102 | +(1 row) |
| 103 | + |
| 104 | +select count(*) as anti_on from rsj_small s where not exists (select 1 from rsj_big b where b.a = s.a); |
| 105 | + anti_on |
| 106 | +--------- |
| 107 | + 2 |
| 108 | +(1 row) |
| 109 | + |
| 110 | +set optimizer_enable_right_semi_join = off; |
| 111 | +select count(*) as semi_off from rsj_small s where s.a in (select a from rsj_big b); |
| 112 | + semi_off |
| 113 | +---------- |
| 114 | + 50 |
| 115 | +(1 row) |
| 116 | + |
| 117 | +select count(*) as anti_off from rsj_small s where not exists (select 1 from rsj_big b where b.a = s.a); |
| 118 | + anti_off |
| 119 | +---------- |
| 120 | + 2 |
| 121 | +(1 row) |
| 122 | + |
| 123 | +reset optimizer_enable_right_semi_join; |
| 124 | +reset enable_nestloop; |
| 125 | +reset enable_mergejoin; |
| 126 | +reset search_path; |
| 127 | +set client_min_messages = warning; |
| 128 | +drop schema rsj cascade; |
| 129 | +reset client_min_messages; |
0 commit comments