'WHERE on normal varchar column fails for some values, but works with trim, lower, ILIKE
I have a SQL table that looks like this (irrelevant parts excluded):
# \d users
Table "public.users"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
name | character varying(64) | | not null |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"users_name_key" UNIQUE CONSTRAINT, btree (name)
Now I encountered this very strange behavior, where I cannot select a specific row (of which I know it's there). However, if I use functions like trim, lower or also ILIKE (without wildcards) then the select works. All of those functions do not alter the value I filter for. It's already lower-case, trimmed & completely ASCII:
-- Does not work (no results returned):
SELECT * FROM users WHERE name = '[email protected]';
SELECT * FROM users WHERE name LIKE '[email protected]';
-- Works (selects the expected row):
SELECT * FROM users WHERE name LIKE '%[email protected]';
SELECT * FROM users WHERE name LIKE '[email protected]%';
SELECT * FROM users WHERE name ILIKE '[email protected]';
SELECT * FROM users WHERE lower(name) = '[email protected]';
SELECT * FROM users WHERE trim(name) = '[email protected]';
Attempts to export the value as bytea also did not show things like a nul-byte or other unprintable characters. The curious part is that the query used to work and still works on another machine that has the exact same PostgreSQL version and data (replicated from the machine that does not work). I use PostgreSQL version 14.0, as container from the official Docker Hub account (not Alpine).
Any clues?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
