The custom domains trap: your own datatypes skip the index
We love PostgreSQL domains. Our schema has more of our own datatypes than plain ones
Then we found that every one of them quietly skips its index when compared to current_user
TL;DR
-- index skipped: your domain is cast down to text
where t.client = (select get_client(current_user))
-- index used
where t.client = (select get_client(current_user::text collate "default"))
Your own datatype
A domain is a type you define once and use everywhere:
create domain capitalshorttext as text
check (length(value) > 0)
check (length(value) < 60)
check (upper(value) = value);
create domain email as text
check (value ~* '^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,}$');
Every customer code is a capitalshorttext. Every address is an email
The rules live in the type, not in twenty copies of a CHECK. Bad data never gets in
We have 52 capitalshorttext columns, 12 email, 12 shorttext and more. Most of our keys are domains
The setup
Our client panel runs on PostgREST
Every customer logs in as their own database role, so current_user tells us who is asking
Views filter rows to that customer:
create view my_tokens as
select t.*
from tokens t
where t.client = (select get_client(current_user));
tokens.client is a capitalshorttext with a primary key on it
We tested with a literal code. Index scan, instant
Production
Through the panel: sequential scan, every row, every time
Seq Scan on tokens
Filter: ((client)::text = (InitPlan 1).col1)
Look at (client)::text. PostgreSQL cast our domain column down to plain text
Once the column is cast, the index on it is useless
Why
current_user is not text. It is type name, and name has collation "C"
Casting does not change that:
select collation for (current_user); -- "C"
select collation for (current_user::text); -- "C"
select collation for (get_client(current_user)); -- "C"
The collation survives the cast. It even survives the function: get_client() returns text, but it inherits "C" from its argument
Our domains use the default collation
So PostgreSQL has a "C" value on one side and a default-collation domain on the other. It unwraps the domain to text to compare them, and the index is gone
The fix
Say the collation out loud:
where t.client = (select get_client(current_user::text collate "default"))
Index Only Scan using tokens_pkey on tokens
Index Cond: (client = (InitPlan 1).col1)
Same with a domain compared directly:
email = current_user::text -> Seq Scan
email = current_user::text collate "default" -> Index Only Scan
Plain text is not safe either
The same "C" hits plain text columns. Our wallet page filters transactions by a text client column
25 ms in testing with a literal, over 60 seconds through the panel. Every customer got a 504
With collate "default": 5.8 ms
Our dates are shown in the customer's timezone. A small function does that, once per row:
select timezone from profiles where username = current_user;
username is plain text, primary key. Same "C", same full table read on every call
The activity log shows 37,000 rows for some accounts. 37,000 table scans per page
With current_user::text collate "default": 1,900 ms down to 190 ms
Keep your domains
Domains are not the problem. current_user is
Anything derived from it carries "C" until you say otherwise
Check yours
Benchmark views as the real role, never with a literal:
begin;
set local role "some_customer_role";
explain analyze select * from my_tokens;
rollback;
Look for (column)::text in a Filter: line. That is your domain being unwrapped
Then check the other side:
select collation for (your_function(current_user));
"C" against a normal index needs collate "default"