From 949482f82157317f3212bf2f777b986045caa679 Mon Sep 17 00:00:00 2001
From: Richard Guo <guofenglinux@gmail.com>
Date: Tue, 28 Oct 2025 12:22:30 +0900
Subject: [PATCH v1] Disable parallel plans for RIGHT_SEMI joins

RIGHT_SEMI joins rely on the HEAP_TUPLE_HAS_MATCH flag to guarantee
that only the first match for each inner tuple is considered.
However, in a parallel hash join, the inner relation is stored in a
shared global hash table that can be probed by multiple workers
concurrently.  This allows different workers to inspect and set the
match flags of the same inner tuples at the same time.

If two workers probe the same inner tuple concurrently, both may see
the match flag as unset and emit the same tuple, leading to duplicate
output rows and violating RIGHT_SEMI join semantics.

For now, we disable parallel plans for RIGHT_SEMI joins.  In the long
term, it may be possible to support parallel execution by performing
atomic operations on the match flag, for example using a CAS or
similar mechanism.

Backpatch to v18, where RIGHT_SEMI join was introduced.

Bug: #19094
Reported-by: Lori Corbani <Lori.Corbani@jax.org>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/19094-6ed410eb5b256abd@postgresql.org
Backpatch-through: 18
---
 src/backend/optimizer/path/joinpath.c | 21 ++++++++++++-----
 src/test/regress/expected/join.out    | 34 +++++++++++++++++++++++++++
 src/test/regress/sql/join.sql         | 26 ++++++++++++++++++++
 3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c
index 3b9407eb2eb..ea5b6415186 100644
--- a/src/backend/optimizer/path/joinpath.c
+++ b/src/backend/optimizer/path/joinpath.c
@@ -2260,10 +2260,20 @@ hash_inner_and_outer(PlannerInfo *root,
 
 		/*
 		 * If the joinrel is parallel-safe, we may be able to consider a
-		 * partial hash join.  However, the resulting path must not be
-		 * parameterized.
+		 * partial hash join.
+		 *
+		 * However, we can't handle JOIN_RIGHT_SEMI, because the hash table is
+		 * either a shared hash table or a private hash table per backend.  In
+		 * the shared case, there is no concurrency protection for the match
+		 * flags, so multiple workers could inspect and set the flags
+		 * concurrently, potentially producing incorrect results.  In the
+		 * private case, each worker has its own copy of the hash table, so no
+		 * single process has all the match flags.
+		 *
+		 * Also, the resulting path must not be parameterized.
 		 */
 		if (joinrel->consider_parallel &&
+			jointype != JOIN_RIGHT_SEMI &&
 			outerrel->partial_pathlist != NIL &&
 			bms_is_empty(joinrel->lateral_relids))
 		{
@@ -2294,13 +2304,12 @@ hash_inner_and_outer(PlannerInfo *root,
 			 * Normally, given that the joinrel is parallel-safe, the cheapest
 			 * total inner path will also be parallel-safe, but if not, we'll
 			 * have to search for the cheapest safe, unparameterized inner
-			 * path.  If full, right, right-semi or right-anti join, we can't
-			 * use parallelism (building the hash table in each backend)
-			 * because no one process has all the match bits.
+			 * path.  If full, right, or right-anti join, we can't use
+			 * parallelism (building the hash table in each backend) because
+			 * no one process has all the match bits.
 			 */
 			if (jointype == JOIN_FULL ||
 				jointype == JOIN_RIGHT ||
-				jointype == JOIN_RIGHT_SEMI ||
 				jointype == JOIN_RIGHT_ANTI)
 				cheapest_safe_inner = NULL;
 			else if (cheapest_total_inner->parallel_safe)
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index d10095de70f..f1ac627b74e 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -3080,6 +3080,40 @@ select * from tbl_rs t1 join
  3 | 3 | 4 | 4
 (6 rows)
 
+--
+-- regression test for bug with parallel-hash-right-semi join
+--
+create table tbl_prs(a int);
+insert into tbl_prs select i from generate_series(1, 1000) i;
+analyze tbl_prs;
+-- encourage use of parallel plans
+set parallel_setup_cost=0;
+set parallel_tuple_cost=0;
+set min_parallel_table_scan_size=0;
+set max_parallel_workers_per_gather=4;
+-- ensure we don't get parallel hash right semi join
+explain (costs off)
+select * from tbl_prs t1
+where exists (select 1 from tbl_prs t2 where a = t1.a) and t1.a < 2;
+                    QUERY PLAN                     
+---------------------------------------------------
+ Hash Right Semi Join
+   Hash Cond: (t2.a = t1.a)
+   ->  Gather
+         Workers Planned: 2
+         ->  Parallel Seq Scan on tbl_prs t2
+   ->  Hash
+         ->  Gather
+               Workers Planned: 2
+               ->  Parallel Seq Scan on tbl_prs t1
+                     Filter: (a < 2)
+(10 rows)
+
+reset parallel_setup_cost;
+reset parallel_tuple_cost;
+reset min_parallel_table_scan_size;
+reset max_parallel_workers_per_gather;
+drop table tbl_prs;
 --
 -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
 --
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index b1732453e8d..b045844500b 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -759,6 +759,32 @@ select * from tbl_rs t1 join
             (select t1.a+t3.a from tbl_rs t3) and t2.a < 5)
   on true;
 
+--
+-- regression test for bug with parallel-hash-right-semi join
+--
+
+create table tbl_prs(a int);
+insert into tbl_prs select i from generate_series(1, 1000) i;
+analyze tbl_prs;
+
+-- encourage use of parallel plans
+set parallel_setup_cost=0;
+set parallel_tuple_cost=0;
+set min_parallel_table_scan_size=0;
+set max_parallel_workers_per_gather=4;
+
+-- ensure we don't get parallel hash right semi join
+explain (costs off)
+select * from tbl_prs t1
+where exists (select 1 from tbl_prs t2 where a = t1.a) and t1.a < 2;
+
+reset parallel_setup_cost;
+reset parallel_tuple_cost;
+reset min_parallel_table_scan_size;
+reset max_parallel_workers_per_gather;
+
+drop table tbl_prs;
+
 --
 -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
 --
-- 
2.39.5 (Apple Git-154)

