From 057e4348a2c44816e19e9c5c4711626263a877c4 Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Tue, 2 Dec 2025 17:20:27 +0100
Subject: [PATCH v1] Fix greedy substring search for non-deterministic
 collations

Due to an off-by-one error, the code failed to find matches at the
end of the haystack.

Author: Laurenz Albe <laurenz.albe@cybertec.at>
Reported-By: Adam Warland <adam.warland@infor.com>
Discussion: https://postgr.es/m/19341-1d9a22915edfec58%40postgresql.org
---
 src/backend/utils/adt/varlena.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 3894457ab40..7fe109ba73e 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -1149,7 +1149,7 @@ text_position_next_internal(char *start_ptr, TextPositionState *state)
 			 * Else check if any of the possible substrings starting at hptr
 			 * are equal to the needle.
 			 */
-			for (const char *test_end = hptr; test_end < haystack_end; test_end += pg_mblen(test_end))
+			for (const char *test_end = hptr; test_end <= haystack_end; test_end += pg_mblen(test_end))
 			{
 				if (pg_strncoll(hptr, (test_end - hptr), needle, needle_len, state->locale) == 0)
 				{
-- 
2.52.0

