v2-0001-Fix-gin-logic-for-triconsistentShim-add-a-test.patch

application/octet-stream

Filename: v2-0001-Fix-gin-logic-for-triconsistentShim-add-a-test.patch
Type: application/octet-stream
Part: 0
Message: Re: BUG #18831: Particular queries using gin-indexes are not interruptible, resulting is resource usage concerns.

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v2-0001
Subject: Fix gin logic for triconsistentShim & add a test
File+
contrib/intarray/expected/_int.out 7 0
contrib/intarray/sql/_int.sql 4 0
src/backend/access/gin/ginlogic.c 8 1
From a5be09105ff337a46e3a81dc591d86baec026c33 Mon Sep 17 00:00:00 2001
From: Vinod Sridharan <vsridh90@gmail.com>
Date: Fri, 11 Apr 2025 22:54:28 -0700
Subject: [PATCH v2] Fix gin logic for triconsistentShim & add a test

---
 contrib/intarray/expected/_int.out | 7 +++++++
 contrib/intarray/sql/_int.sql      | 4 ++++
 src/backend/access/gin/ginlogic.c  | 9 ++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/contrib/intarray/expected/_int.out b/contrib/intarray/expected/_int.out
index b39ab82d43d..31e681aa16a 100644
--- a/contrib/intarray/expected/_int.out
+++ b/contrib/intarray/expected/_int.out
@@ -876,6 +876,13 @@ SELECT count(*) from test__int WHERE a @@ '!20 & !21';
   6344
 (1 row)
 
+-- test consistent function behavior with mixed mode AND/OR keys
+SELECT count(*) FROM test__int WHERE a @@ '!2733 & (2738 | 254)';
+ count 
+-------
+    12
+(1 row)
+
 DROP INDEX text_idx;
 -- Repeat the same queries with an extended data set. The data set is the
 -- same that we used before, except that each element in the array is
diff --git a/contrib/intarray/sql/_int.sql b/contrib/intarray/sql/_int.sql
index 2d4ed1c9ae2..2d9b7d61b5f 100644
--- a/contrib/intarray/sql/_int.sql
+++ b/contrib/intarray/sql/_int.sql
@@ -195,6 +195,10 @@ SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
 SELECT count(*) from test__int WHERE a @@ '20 | !21';
 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
 
+-- test consistent function behavior with mixed mode AND/OR keys
+SELECT count(*) FROM test__int WHERE a @@ '!2733 & (2738 | 254)';
+
+
 DROP INDEX text_idx;
 
 -- Repeat the same queries with an extended data set. The data set is the
diff --git a/src/backend/access/gin/ginlogic.c b/src/backend/access/gin/ginlogic.c
index 665ff9b9810..b3d66acba97 100644
--- a/src/backend/access/gin/ginlogic.c
+++ b/src/backend/access/gin/ginlogic.c
@@ -200,13 +200,20 @@ shimTriConsistentFn(GinScanKey key)
 		recheck |= key->recheckCurItem;
 
 		if (curResult != boolResult)
-			return GIN_MAYBE;
+		{
+			curResult = GIN_MAYBE;
+			break;
+		}
 	}
 
 	/* TRUE with recheck is taken to mean MAYBE */
 	if (curResult == GIN_TRUE && recheck)
 		curResult = GIN_MAYBE;
 
+	/* Restore the maybe entry state */
+	for (i = 0; i < nmaybe; i++)
+		key->entryRes[maybeEntries[i]] = GIN_MAYBE;
+
 	return curResult;
 }
 
-- 
2.25.1