Re: Postgres 15 SELECT query doesn't use index under RLS

Alexander Okulovich <aokulovich@stiltsoft.com>

From: Alexander Okulovich <aokulovich@stiltsoft.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-performance@postgresql.org
Date: 2023-10-31T16:01:29Z
Lists: pgsql-performance
Hi Tom,

> Can you force it in either direction with "set enable_seqscan = off"
> (resp. "set enable_indexscan = off")?  If so, how do the estimated
> costs compare for the two plan shapes?
Here are the results from the prod instance:

seqscan off <https://explain.depesz.com/s/9AWx>

indexscan_off <https://explain.depesz.com/s/mTU2>

Just noticed that the WHEN clause differs from the initial one (392 ids 
under RLS). Probably, this is why the execution time isn't so 
catastrophic. Please let me know if this matters, and I'll rerun this 
with the initial request.

Speaking of the stage vs local Docker Postgres instance, the execution 
time on stage is so short (0.1 ms with seq scan, 0.195 with index scan) 
that we probably should not consider them. But I'll execute the requests 
if it's necessary.

> Maybe your prod installation has a bloated index, and that's driving
> up the estimated cost enough to steer the planner away from it.
We tried to make REINDEX CONCURRENTLY on a prod copy, but the planner 
still used Seq Scan instead of Index Scan afterward.

Kind regards,

Alexander