| From: | David Geier <geidav(dot)pg(at)gmail(dot)com> |
|---|---|
| To: | Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com> |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org, solai v <solai(dot)cdac(at)gmail(dot)com> |
| Subject: | Re: Use correct collation in pg_trgm |
| Date: | 2026-11-24 13:45:59 |
| Message-ID: | f6385e58-b18b-474b-8ccd-1123e65d0a19@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
> show_trgm is still affected with the same issue:
>
> CREATE EXTENSION IF NOT EXISTS pg_trgm;
> CREATE TEMP TABLE st_a(x text COLLATE "C"); INSERT INTO st_a
> VALUES ('hello');
> CREATE TEMP TABLE st_b(y text COLLATE "POSIX"); INSERT INTO st_b
> VALUES ('world');
> SELECT show_trgm(x || y) FROM st_a, st_b;
I incorrectly reasoned that only multi argument functions can be
affected. But for e.g. x || y, PostgreSQL can join the strings but the
resulting collation is invalid. So show_trgm() fails. I now use
trgm_get_collation() in all pg_trgm code that previously used
PG_GET_COLLATION(), not only in the similarity functions.
Fixed patch set attached.
> And I noticed one more thing, is this change expected, as it doesn't
> seem intentional? (also with posix or ucs_basic)
>
> SELECT show_trgm('Привет мир' COLLATE "C");
> -- {}
>
> postgres=# SELECT similarity('Привет мир' COLLATE "C", 'Привет мир'
> COLLATE "C");
> -- 0
>
> Looking back this is caused by 0002, and was there since v2 and I
> completely missed it before.
Yes, this is expected with 0002. Previously, pg_trgm classified word
characters using the database default collation, regardless of the
expression's collation. With 0002, character classification also follows
the expression's collation, not just lowercasing.
For "C", "POSIX", and "ucs_basic", only ASCII letters and digits qualify
as word characters. Consequently, none of the Cyrillic characters in
this example are recognized as word characters, so no trigrams are
generated. That explains both the empty array and the similarity of 0:
pg_trgm returns 0 when either trigram set is empty, even for identical
inputs.
--
David Geier
| Attachment | Content-Type | Size |
|---|---|---|
| v10-0001-Use-correct-collation-for-lowercasing.patch | text/plain | 22.2 KB |
| v10-0002-Use-correct-collation-for-finding-word-boundarie.patch | text/plain | 9.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Andrei Lepikhov | 2026-11-24 13:47:57 | Numeric equality and hashing without detoasting |
| Previous Message | Andrey Borodin | 2026-11-24 13:28:38 | Re: Fix network subset support with custom operator classes |