public inbox for [email protected]  
help / color / mirror / Atom feed
BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape
2+ messages / 2 participants
[nested] [flat]

* BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape
@ 2026-05-09 02:22 PG Bug reporting form <[email protected]>
  2026-05-14 11:15 ` Re: BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape Nitin Motiani <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Bug reporting form @ 2026-05-09 02:22 UTC (permalink / raw)
  To: [email protected]; +Cc: [email protected]

The following bug has been logged on the website:

Bug reference:      19474
Logged by:          Bowen Shi
Email address:      [email protected]
PostgreSQL version: 18.3
Operating system:   centos
Description:        

After commit 85b7efa1cdd63c2fe2b70b725b8285743ee5787f ("Support LIKE with
nondeterministic collations"), LIKE on a nondeterministic collation can
return an incorrect result when the pattern contains a literal backslash.

The problem appears to be in MatchText() in
src/backend/utils/adt/like_match.c. In the nondeterministic-collation path,
when a pattern substring contains escape processing, the code builds an
unescaped copy of the substring. In that logic, a backslash that should
remain as a literal character can be dropped, so the substring compared by
pg_strncoll() is not the same as the original SQL pattern semantics.

As a result, a LIKE pattern that should match a string containing a literal
backslash can incorrectly return false.

SQL reproduction:

CREATE COLLATION ignore_accents (
    provider = icu,
    locale = 'und-u-ks-level1',
    deterministic = false
);

SELECT 'back\slash' COLLATE ignore_accents LIKE 'back\slash%' ESCAPE '#';

Expected result:
 t

Actual result:
 f

The same pattern works as expected without the nondeterministic collation
semantics.

A table-based reproduction:

CREATE COLLATION ignore_accents (
    provider = icu,
    locale = 'und-u-ks-level1',
    deterministic = false
);

CREATE TABLE like_test (val text);
INSERT INTO like_test VALUES ('back\slash');

SELECT val
FROM like_test
WHERE val COLLATE ignore_accents LIKE 'back\slash%' ESCAPE '#';

Expected result:
 one row: back\slash

Actual result:
 zero rows

This seems to be caused by the unescape logic in like_match.c for
nondeterministic collations, where a pattern fragment containing backslashes
is copied incorrectly before calling pg_strncoll().







^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape
  2026-05-09 02:22 BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape PG Bug reporting form <[email protected]>
@ 2026-05-14 11:15 ` Nitin Motiani <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Nitin Motiani @ 2026-05-14 11:15 UTC (permalink / raw)
  To: [email protected]; [email protected]

Hi,

I have proposed a fix for this on pgsql-hackers[1]. Please take a look
and let me know what you think.

Thanks & Regards,
Nitin Motiani
Google

[1] https://www.postgresql.org/message-id/CAH5HC94yU%2BK8Gcdy12M5BS8gwD_SXLSHzc9k5tNk7JDnpBiFMA%40mail.g...

On Sat, May 9, 2026 at 8:02 AM PG Bug reporting form
<[email protected]> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference:      19474
> Logged by:          Bowen Shi
> Email address:      [email protected]
> PostgreSQL version: 18.3
> Operating system:   centos
> Description:
>
> After commit 85b7efa1cdd63c2fe2b70b725b8285743ee5787f ("Support LIKE with
> nondeterministic collations"), LIKE on a nondeterministic collation can
> return an incorrect result when the pattern contains a literal backslash.
>
> The problem appears to be in MatchText() in
> src/backend/utils/adt/like_match.c. In the nondeterministic-collation path,
> when a pattern substring contains escape processing, the code builds an
> unescaped copy of the substring. In that logic, a backslash that should
> remain as a literal character can be dropped, so the substring compared by
> pg_strncoll() is not the same as the original SQL pattern semantics.
>
> As a result, a LIKE pattern that should match a string containing a literal
> backslash can incorrectly return false.
>
> SQL reproduction:
>
> CREATE COLLATION ignore_accents (
>     provider = icu,
>     locale = 'und-u-ks-level1',
>     deterministic = false
> );
>
> SELECT 'back\slash' COLLATE ignore_accents LIKE 'back\slash%' ESCAPE '#';
>
> Expected result:
>  t
>
> Actual result:
>  f
>
> The same pattern works as expected without the nondeterministic collation
> semantics.
>
> A table-based reproduction:
>
> CREATE COLLATION ignore_accents (
>     provider = icu,
>     locale = 'und-u-ks-level1',
>     deterministic = false
> );
>
> CREATE TABLE like_test (val text);
> INSERT INTO like_test VALUES ('back\slash');
>
> SELECT val
> FROM like_test
> WHERE val COLLATE ignore_accents LIKE 'back\slash%' ESCAPE '#';
>
> Expected result:
>  one row: back\slash
>
> Actual result:
>  zero rows
>
> This seems to be caused by the unescape logic in like_match.c for
> nondeterministic collations, where a pattern fragment containing backslashes
> is copied incorrectly before calling pg_strncoll().
>
>
>
>






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-05-14 11:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-05-09 02:22 BUG #19474: LIKE with nondeterministic collations mis-handle literal backslashes in patterns containing escape PG Bug reporting form <[email protected]>
2026-05-14 11:15 ` Nitin Motiani <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox