Re: SIMILAR TO expressions translate wildcards where they shouldn't

From: James Moore <james(dot)moore(at)g5labs(dot)ai>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: SIMILAR TO expressions translate wildcards where they shouldn't
Date: 2026-11-30 17:37:56
Message-ID: CAOYRsOQoaYp5eo=758sB8ttOh4Es_3_gVz7qai9e_x=Lij-Log@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello,

The bracket handling added for this report (e3ffc3e91 in 17.6, and its
follow-ups in 17.7, as far as we can tell) covers the single-byte path of
similar_escape_internal(), but not the path taken when the ESCAPE character
is multibyte. In that path charclass_pos is not updated, so a multibyte
character directly after "[" leaves the following "]" treated as a literal
inside the bracket, and the rest of the pattern is not translated.

UTF8 database, reproduced on 17.11, 18.7 and 19.0:

SELECT 'éx' SIMILAR TO '[é]%' ESCAPE 'ü'; -- f (expected t)
SELECT 'éx' SIMILAR TO '[é]%'; -- t
SELECT 'éx' SIMILAR TO '[é]%' ESCAPE '!'; -- t
SELECT similar_to_escape('[é]%', 'ü'); -- ^(?:[é]%)$
-- expected ^(?:[é].*)$

An ASCII character after "[" is not affected:

SELECT 'ax' SIMILAR TO '[a]%' ESCAPE 'ü'; -- t

The code in question is the multibyte branch of similar_escape_internal()
in src/backend/utils/adt/regexp.c (around lines 825-860 on 17.11).

We found this by comparing PostgreSQL's results against a formal model of
SQL pattern matching.

Regards,
James Moore

On Mon, Nov 30, 2026 at 12:34 PM Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> wrote:
>
> On Wed, 2025-05-28 at 09:00 +0900, Michael Paquier wrote:
> > On Tue, May 27, 2025 at 11:39:02PM +0200, Laurenz Albe wrote:
> > > I originally considered EXPLAIN (GENERIC_PLAN), but that would only
> > > backpatch so far.
> >
> > Nice trick that makes the test output much smaller. I've used this
> > idea and applied the fix down to v13 after a second lookup this
> > morning.
>
> Thanks for taking care of this!
>
> Yours,
> Laurenz Albe
>
>
>
>

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Nathan Bossart 2026-11-30 20:43:03 Re: BUG #19891: pg_restore ignores an explicitly empty -F argument
Previous Message James Moore 2026-11-30 17:37:07 Re: BUG #19362: Extremely log processing of jsonb_path_exists_opr