Documenting more pitfalls of non-default collations? - Mailing list pgsql-docs

From Will Mortensen
Subject Documenting more pitfalls of non-default collations?
Date
Msg-id CAMpnoC6sJ9=F6OiXJ4kKyrHUzwbWXjUKC3t-rL4FrDyhyiYf5A@mail.gmail.com
Whole thread Raw
Responses Re: Documenting more pitfalls of non-default collations?
List pgsql-docs
Hi there,

I mentioned to Jeremy at pgConf.dev that using non-default collations
in some SQL idioms can produce undesired results, and he asked me to
send an email. An example idiom is the way Django implements
case-insensitive comparisons using "upper(x) = upper(y)" [1][2][3] ,
which returns false if x = y but they have different collations that
produce different uppercase.

For example, assuming the default collation performs standard Unicode
case mapping:

# select upper('i') = upper('i' collate "tr-x-icu");
 f
# select upper('é') = upper('é' collate "C");
 f

Or with collations in DDL instead:

# create table t (
    tr text collate "tr-x-icu",
    c text collate "C"
);
CREATE TABLE
# insert into t values ('i', 'é');
INSERT 0 1

# select count(*) from t where upper('i') = upper(tr);
     0
# select count(*) from t where upper('é') = upper(c);
     0

This is expected, given a careful reading of the collation docs, but
it's not really highlighted in any of the examples--in each example
that doesn't produce an error, all of the collation-sensitive
functions/operators end up applying the same collation. Maybe there
should be an example that applies different collations in different
subexpressions, and/or a warning against constructions like "upper(x)
= upper(y)"?


[1] https://github.com/django/django/blame/stable/5.1.x/django/db/backends/postgresql/operations.py#L175
[2] https://github.com/django/django/blame/stable/5.1.x/django/db/backends/postgresql/base.py#L155
[3] https://code.djangoproject.com/ticket/32485



pgsql-docs by date:

Previous
From: Erwin Brandstetter
Date:
Subject: Re: Clarify the ordering guarantees in combining queries (or lack thereof)
Next
From: Amit Kapila
Date:
Subject: Re: Ambiguous description on new columns