SIMILAR TO expressions translate wildcards where they shouldn't
Laurenz Albe <laurenz.albe@cybertec.at>
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: pgsql-bugs@lists.postgresql.org
Date: 2025-05-22T21:18:44Z
Lists: pgsql-bugs
Attachments
- v1-0001-Fix-SIMILAR-TO-regex-translation.patch (text/x-patch) patch v1-0001
The following surprising result
SELECT 'a_b' SIMILAR TO '[_[:alpha:]]*',
'a_b' SIMILAR TO '[[:alpha:]_]*';
?column? │ ?column?
══════════╪══════════
t │ f
(1 row)
becomes clear when we look how the expressions are translated to
regular expressions:
EXPLAIN (VERBOSE, GENERIC_PLAN, COSTS OFF)
SELECT $1 SIMILAR TO '[_[:alpha:]]*',
$1 SIMILAR TO '[[:alpha:]_]*';
QUERY PLAN
══════════════════════════════════════════════════════════════════════════════════
Result
Output: ($1 ~ '^(?:[_[:alpha:]]*)$'::text), ($1 ~ '^(?:[[:alpha:].]*)$'::text)
(2 rows)
The underscore before the [:alpha:] is left alone, but the one after
it gets translated to a period. Now the underscore is a wildcard
that corresponds to the period in regular expressions, but characters
in square brackets should lose their special meaning. The code in
utils/adt/regexp.c doesn't expect that square brackets can be nested.
The attached patch fixes the bug.
Yours,
Laurenz Albe
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Adjust regex for test with opening parenthesis in character classes
- 31ee5ec698ed 13.22 landed
- 0c09922c04ea 14.19 landed
- 4dc642e75f1c 15.14 landed
- 52d08620e48c 16.10 landed
- a3c6d92f3cb3 17.6 landed
- 4fbb46f61271 18.0 landed
-
Fix conversion of SIMILAR TO regexes for character classes
- 9481d1614c2a 13.22 landed
- 1fe15d25e65c 14.19 landed
- b3e99115e44c 15.14 landed
- e9e535d61120 16.10 landed
- e3ffc3e91d04 17.6 landed
- d46911e584d4 18.0 landed