PostgresIndexes

Index suffix searches with reverse()

A B-tree index can serve a prefix search like email LIKE 'jane%', but not a suffix search like email LIKE '%@bigcorp.com'. A suffix search reads the whole table.

The trick is to reverse the column. A suffix of email is a prefix of reverse(email), so an index on the reversed value turns the suffix search into a prefix search:

CREATE INDEX users_email_rev
  ON users (reverse(email) text_pattern_ops);

SELECT *
FROM users
WHERE reverse(email) LIKE reverse('%@bigcorp.com');

reverse('%@bigcorp.com') is 'moc.procgib@%', which ends in %, so the index can find it. Postgres computes it once, when it plans the query. The query must use the same expression as the index, reverse(email).

Don’t forget text_pattern_ops

A B-tree index sorts text by the database collation. Most databases use a locale collation such as en_US.UTF-8, which sorts by language rules, not byte by byte. LIKE 'abc%' needs byte order, so it can’t use that index.

text_pattern_ops builds the index in byte order instead.

I tested it on Postgres 18 with an en_US.utf8 database and 200,000 emails, searching for '%@tiny.io'. With CREATE INDEX ON users (reverse(email)), the query still read the whole table, even with enable_seqscan = off. With text_pattern_ops, it used the index:

EXPLAIN (COSTS OFF)
SELECT *
FROM users
WHERE reverse(email) LIKE reverse('%@tiny.io');
Bitmap Heap Scan on users
  Filter: (reverse(email) ~~ 'oi.ynit@%'::text)
  ->  Bitmap Index Scan on users_email_rev
        Index Cond: ((reverse(email) ~>=~ 'oi.ynit@'::text)
                 AND (reverse(email) ~<~ 'oi.ynitA'::text))

References