Over the past week, I’ve been working on implementing text search in a NestJS application using PostgreSQL. The challenge is that the app supports multiple tenants, all sharing the same tables, so we use Row-Level Security (RLS) to isolate data between tenants.
I want to perform simple text searches using LIKE and ILIKE, but the catch is: the data is stored case-sensitive in the database, and I want the searches to be case-insensitive.I also would like to extend this in the future to do some more complex searching.
At first, I used GIN indexes because I read they are well-suited for text search in PostgreSQL.
When I tested queries as a user without Row-Level Security (RLS), the indexes were used as expected. However, when I ran the same queries as a user with RLS enabled, the GIN indexes were ignored.
After some research I learned that, due to “leakproof” requirements in PostgreSQL RLS, GIN indexes cannot be used in queries because they are not leakproof.
Now I’m wondering: is it even possible to do efficient text search while using RLS? And what are the recommended approaches to handle this scenario?
LEAKPROOFis unrelated to indexes, so I don't think that's the reason. You have to analyze the execution withEXPLAIN (ANALYZE, BUFFERS).