From a00591629687fe281826c5979a11b46a93d8e817 Mon Sep 17 00:00:00 2001
From: Chengpeng Yan <chengpeng_yan@outlook.com>
Date: Wed, 18 Nov 2026 08:07:42 +0800
Subject: [PATCH v1] Count characters, not bytes, in pattern selectivity

like_selectivity() and regex_selectivity_sub() applied FIXED_CHAR_SEL
once per byte, so a multibyte character was charged several times and
the estimate for the same pattern depended on the database encoding.
Advance by whole characters for text patterns, and pass the regex
fixed-prefix length in characters to match.  Bytea patterns remain
byte-oriented.
---
 src/backend/utils/adt/like_support.c       | 19 +++++++++++++-----
 src/test/regress/expected/collate.utf8.out | 23 ++++++++++++++++++++++
 src/test/regress/sql/collate.utf8.sql      | 11 +++++++++++
 3 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c
index 588425b3812..8cae0d74c90 100644
--- a/src/backend/utils/adt/like_support.c
+++ b/src/backend/utils/adt/like_support.c
@@ -99,7 +99,7 @@ static Selectivity prefix_selectivity(PlannerInfo *root,
 									  Oid collation,
 									  Const *prefixcon);
 static Selectivity like_selectivity(const char *patt, int pattlen,
-									bool case_insensitive);
+									bool is_bytea);
 static Selectivity regex_selectivity(const char *patt, int pattlen,
 									 bool case_insensitive,
 									 int fixed_prefix_len);
@@ -1052,7 +1052,8 @@ like_fixed_prefix(Const *patt_const, Const **prefix_const,
 		*prefix_const = string_to_bytea_const(match, match_pos);
 
 	if (rest_selec != NULL)
-		*rest_selec = like_selectivity(&patt[pos], pattlen - pos, false);
+		*rest_selec = like_selectivity(&patt[pos], pattlen - pos,
+									   typeid == BYTEAOID);
 
 	pfree(patt);
 	pfree(match);
@@ -1157,7 +1158,7 @@ like_fixed_prefix_ci(Const *patt_const, Oid collation, Const **prefix_const,
 		rest = palloc(pg_database_encoding_max_length() * wrestlen + 1);
 		rest_mblen = pg_wchar2mb_with_len(&wpatt[wpos], rest, wrestlen);
 
-		*rest_selec = like_selectivity(rest, rest_mblen, true);
+		*rest_selec = like_selectivity(rest, rest_mblen, false);
 		pfree(rest);
 	}
 
@@ -1228,7 +1229,7 @@ regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
 
 			*rest_selec = regex_selectivity(patt, strlen(patt),
 											case_insensitive,
-											strlen(prefix));
+											pg_mbstrlen(prefix));
 			pfree(patt);
 		}
 	}
@@ -1397,7 +1398,7 @@ prefix_selectivity(PlannerInfo *root, VariableStatData *vardata,
 #define PARTIAL_WILDCARD_SEL 2.0
 
 static Selectivity
-like_selectivity(const char *patt, int pattlen, bool case_insensitive)
+like_selectivity(const char *patt, int pattlen, bool is_bytea)
 {
 	Selectivity sel = 1.0;
 	int			pos;
@@ -1423,9 +1424,15 @@ like_selectivity(const char *patt, int pattlen, bool case_insensitive)
 			if (pos >= pattlen)
 				break;
 			sel *= FIXED_CHAR_SEL;
+			if (!is_bytea)
+				pos += pg_mblen_range(patt + pos, patt + pattlen) - 1;
 		}
 		else
+		{
 			sel *= FIXED_CHAR_SEL;
+			if (!is_bytea)
+				pos += pg_mblen_range(patt + pos, patt + pattlen) - 1;
+		}
 	}
 	/* Could get sel > 1 if multiple wildcards */
 	if (sel > 1.0)
@@ -1515,11 +1522,13 @@ regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
 				break;
 			if (paren_depth == 0)
 				sel *= FIXED_CHAR_SEL;
+			pos += pg_mblen_range(patt + pos, patt + pattlen) - 1;
 		}
 		else
 		{
 			if (paren_depth == 0)
 				sel *= FIXED_CHAR_SEL;
+			pos += pg_mblen_range(patt + pos, patt + pattlen) - 1;
 		}
 	}
 	/* Could get sel > 1 if multiple wildcards */
diff --git a/src/test/regress/expected/collate.utf8.out b/src/test/regress/expected/collate.utf8.out
index cdd1a37ba18..b1ea81638c1 100644
--- a/src/test/regress/expected/collate.utf8.out
+++ b/src/test/regress/expected/collate.utf8.out
@@ -41,6 +41,29 @@ SELECT 'café' ILIKE 'caf_' COLLATE "C";
  t
 (1 row)
 
+-- pattern selectivity counts characters, not encoded bytes
+EXPLAIN
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text LIKE '%é'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text ILIKE '%é'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text ~ 'é$'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i)
+  WHERE convert_to(i::text, 'UTF8') LIKE decode('25c3a9', 'hex');
+                                   QUERY PLAN                                    
+---------------------------------------------------------------------------------
+ Append  (cost=0.00..151.41 rows=1280 width=4)
+   ->  Function Scan on generate_series g  (cost=0.00..35.00 rows=400 width=4)
+         Filter: ((i)::text ~~ '%é'::text)
+   ->  Function Scan on generate_series g_1  (cost=0.00..35.00 rows=400 width=4)
+         Filter: ((i)::text ~~* '%é'::text)
+   ->  Function Scan on generate_series g_2  (cost=0.00..35.00 rows=400 width=4)
+         Filter: ((i)::text ~ 'é$'::text)
+   ->  Function Scan on generate_series g_3  (cost=0.00..40.00 rows=80 width=4)
+         Filter: (convert_to((i)::text, 'UTF8'::name) ~~ '\x25c3a9'::bytea)
+(9 rows)
+
 --
 -- Test PG_C_UTF8
 --
diff --git a/src/test/regress/sql/collate.utf8.sql b/src/test/regress/sql/collate.utf8.sql
index 52cf068dd0c..fd9adf367ca 100644
--- a/src/test/regress/sql/collate.utf8.sql
+++ b/src/test/regress/sql/collate.utf8.sql
@@ -29,6 +29,17 @@ DROP COLLATION regress_builtin_c;
 -- a '_' matches a multibyte character
 SELECT 'café' ILIKE 'caf_' COLLATE "C";
 
+-- pattern selectivity counts characters, not encoded bytes
+EXPLAIN
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text LIKE '%é'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text ILIKE '%é'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i) WHERE i::text ~ 'é$'
+UNION ALL
+SELECT * FROM generate_series(1, 2000) g(i)
+  WHERE convert_to(i::text, 'UTF8') LIKE decode('25c3a9', 'hex');
+
 --
 -- Test PG_C_UTF8
 --
-- 
2.50.1 (Apple Git-155)

