ILIKE '%' never matches a non-ASCII byte in single-byte C-ctype databases
Hi Hackers, Jeff,
In v19, ILIKE in a database with a single-byte encoding and a C ctype
never matches when '%' is followed by a byte >= 0x80:
CREATE DATABASE l1 TEMPLATE template0 ENCODING 'LATIN1' LOCALE 'C';
\c l1
SET client_encoding = 'UTF8';
SELECT 'xÆy' ILIKE '%Æ%'; -- 18.6: t 19rc1: f
SELECT 'abÆ' ILIKE '%Æ'; -- 18.6: t 19rc1: f
SELECT 'Æcd' ILIKE 'Æ%'; -- t on both
SELECT 'xÆy' LIKE '%Æ%'; -- t on both
The same happens in WIN1252 and KOI8R databases with LOCALE 'C'. It
reproduces on 19rc1 and on master and REL_19_STABLE as of today.
The cause is in commit 1e493158d3d ("Remove char_tolower() API"). That
path is C_IMatchText, where like_match.c's GETCHAR() became
pg_ascii_tolower(), which returns unsigned char. The '%' branch of
MatchText keeps the first pattern byte in a char:
char firstpat;
...
firstpat = GETCHAR(*p);
...
if (GETCHAR(*t) == firstpat || nondeterministic)
For 0xC6, firstpat holds -58 where char is signed, while GETCHAR(*t)
promotes to 198, so the test never succeeds and the scan after '%' skips
every position. Before that commit GETCHAR was SB_lower_char(), which
returned char, so both sides had the same signedness. The other
comparisons in MatchText compare GETCHAR() with GETCHAR(), unsigned on
both sides, which is why a pattern without a leading '%' still matches.
Platforms where char is unsigned (ARM, POWER) are not affected.
The attached patch declares firstpat as int. That covers the
non-MATCH_LOWER instantiations too, where GETCHAR() is the plain char
and both sides are promoted from the same type.
The main regression database is normally UTF-8, which never reaches
C_IMatchText, so the patch adds a TAP test,
src/test/modules/test_misc/t/016_ilike_single_byte.pl, that creates a
LATIN1 / C database and checks seven ILIKE cases. It builds its
strings with convert_from() so it doesn't depend on the client
encoding. On master without the fix it fails:
got: 'false,false,false,false,true,true,false'
expected: 'true,true,false,true,true,false,false'
and with it, it passes, as do make check (239/239) and the rest of
test_misc (FreeBSD 15.1 amd64, clang 19; built and tested on master at
45fc51fcbf4).
I found this through pg_tre's regression suite, which compares its
index with a sequential scan in LATIN1, WIN1252 and KOI8R databases.
The ILIKE checks for words that start with a non-ASCII letter came back
empty on 19rc1 for both scans, because both end in core's ILIKE.
This should go into 19 before release; the patch carries
"Backpatch-through: 19".
what do you think? bug, or misunderstanding on my part, or...? :)
-greg
Hi Greg,
what do you think? bug, or misunderstanding on my part, or...? :)
Bug, and I think v1 is the right and complete fix. I reproduced it on x86-64
Linux with gcc, so another platform and compiler than yours: 'xÆy' ILIKE '%Æ%'
is t on 18.6 and f on 19rc1 and master (@45fc51fcbf4), in LATIN1, WIN1252 and
KOI8R, and master+v1 makes it t again. Your 016 test fails on master with the
same got: and passes with v1, and regression (239/239), pg_trgm and test_misc
all pass. master built with -funsigned-char (a stand-in for ARM/POWER) gives 0
differences from 18.6, which matches your note that those are unaffected.
I swept 1e493158d3d too: firstpat is the only place it left a signed char
compared against an unsigned char -- the other MatchText comparisons are
GETCHAR vs GETCHAR, and the non-MATCH_LOWER instantiations stay char vs char.
Targeted cases (escape \, ESCAPE, bytes 0x80 and 0xFF, NOT ILIKE, and the name
and bpchar types) all fail on 19 and v1 fixes every one; a 20,000-pair
differential fuzz against 18.6 and a separate oracle finds 1,540 differing
rows on 19, all ILIKE and none LIKE, and 0 with v1.
One scope point, maybe worth a word in the commit message or a test: the
trigger is a C collation on the expression, not a C-ctype database as such. In
any single-byte database, including one with a non-C locale,
SELECT 'xÆy' COLLATE "C" ILIKE '%Æ%';
returns t on 18.6 and f on 19. The same reaches name columns (which collate
"C"), an explicit COLLATE "C", and convert_from(), which inherits "C" from its
name argument. v1 fixes all of them, since they share the path.
The index side matches what you saw in pg_tre: like_fixed_prefix_ci is fine
(it keeps the Æ in the prefix), so a plain Æ% scan is correct, but a pattern
that rechecks (Æ%Æ, %Æ%) returns nothing on 19 because the recheck runs the
same ILIKE -- seq scan and index agree with each other, both empty. v1 fixes
those as well.
regards,
Manu
On Sun, 2026-10-18 at 15:30 +0000, Greg Burd wrote:
Hi Hackers, Jeff,
In v19, ILIKE in a database with a single-byte encoding and a C ctype
never matches when '%' is followed by a byte >= 0x80:
Thank you for the report!
I attached a patch with a different approach though, which is to cast
the result of pg_ascii_tolower() so that GETCHAR() consistently returns
a 'char'. That addresses the real problem of commit 1e493158d3d, which
is that it unintentionally changed GETCHAR() from returning 'char' to
returning 'unsigned char' for MATCH_LOWER.
It passes the same test that you submitted, but I don't plan to commit
the tests with the change. We are missing coverage for non-UTF8
encodings, but if we're going to add tests for those, it should
probably be a whole suite rather than a one-off.
I plan to commit it very soon and backport to 19.
Regards,
Jeff Davis
On Tuesday, October 20th, 2026 at 9:29 AM, Jeff Davis <pgsql@j-davis.com> wrote:
On Sun, 2026-10-18 at 15:30 +0000, Greg Burd wrote:
Hi Hackers, Jeff,
In v19, ILIKE in a database with a single-byte encoding and a C ctype
never matches when '%' is followed by a byte >= 0x80:Thank you for the report!
Thank you for the fast review! :)
I attached a patch with a different approach though, which is to cast
the result of pg_ascii_tolower() so that GETCHAR() consistently returns
a 'char'. That addresses the real problem of commit 1e493158d3d, which
is that it unintentionally changed GETCHAR() from returning 'char' to
returning 'unsigned char' for MATCH_LOWER.
Agreed, v2j is the better fix. Casting at the macro restores exactly what
1e493158d3d changed, and keeps both operands the same type in every
MATCH_LOWER comparison, not just the firstpat one. My v1 only widened the
one variable; yours fixes the cause. +1.
It passes the same test that you submitted, but I don't plan to commit
the tests with the change. We are missing coverage for non-UTF8
encodings, but if we're going to add tests for those, it should
probably be a whole suite rather than a one-off.
Fair. A one-off LATIN1/C test next to the others would be odd, and a
non-UTF8 suite is the right home. I'd like to help build that. I have a
differential harness that runs the pattern operators against a sequential
scan in LATIN1, WIN1252 and KOI8R, which is what turned this up; it would
drop in as the generator for such a suite. I'll start a separate thread
rather than hold up your commit.
I plan to commit it very soon and backport to 19.
Awesome, thanks!
One scope note for the commit message, from Manu's reply upthread: the
trigger is a C collation on the expression, not a C-ctype database as such.
The same wrong answer appears in a non-C-locale single-byte database via
SELECT 'xÆy' COLLATE "C" ILIKE '%Æ%';
and reaches name columns and convert_from() output (both collate "C"). v2j
fixes all of them since they share the path; it may be worth a word so the
fix isn't read as database-locale-specific.
Regards,
Jeff Davis
Separately, while checking the archive I found shihao zhong's thread
"[PG19] ILIKE index scan misses rows with a libc UTF-8 collation on macOS"
[1]: /messages/by-id/CAGRkXqS6pr-hG-g=K6RTdRw53SnMZWz3iXvh_wi8rqLSf972sA@mail.gmail.com
folding), different cause and path: there the wide-char classifier
wc_iscased_libc_utf8() uses iswupper()/iswlower() while ILIKE folds with
towlower(), so a code point like U+2160 that towlower changes but the
classifier doesn't flag stays in the index prefix. That's multi-byte libc
UTF-8; this one is the single-byte byte-compare path in like_match.c. The
two don't overlap in code and v2j doesn't touch his case (nor his patch
this one), but together they're two instances of the same v19 regression
family, so the non-UTF8 test suite above would ideally cover both if/when
we have one.
Thanks Jeff for picking this up and for the quick turnaround.
-greg
[1]: /messages/by-id/CAGRkXqS6pr-hG-g=K6RTdRw53SnMZWz3iXvh_wi8rqLSf972sA@mail.gmail.com