Sample Header Ad - 728x90

Postgres 15 doesn't use index under RLS

1 vote
0 answers
186 views
Recently, we upgraded the AWS RDS instance from Postgres 12.14 to 15.4 and noticed extremely high disk consumption on the following query execution:
select (exists (select 1 as "one" from "public"."indexed_commit" where 
"public"."indexed_commit"."repo_id" in (964992,964994,964999, ...);
For some reason, the query planner starts using Seq Scan instead of the "indexed-commit-repo-idx" index when requesting under user limited with RLS. It happens when there are more than 316 IDs in the IN part of the query. If we execute the request from Superuser, the planner always uses the index (Index Only Scan). Query plans: [316 ids under RLS limited user](https://explain.depesz.com/s/X7Iq) [392 ids under RLS limited user](https://explain.depesz.com/s/lbkX) [392 ids under Superuser](https://explain.depesz.com/s/uKSG) What we've already tried: 1. If we add a multicolumn "repo_id, tenant_id" index, the planner uses it (Index Only Scan) with any IN params count under RLS. 2. REINDEX CONCURRENT on the "indexed-commit-repo-idx" index didn't help. 3. Planner uses the index if we add
enable_seqscan = off
before the query execution. **So the questions are**: - Why did the planner stop using "indexed-commit-repo-idx" index? Is it a Postgres bug? - Should we add "tenant_id" to our indexes to force the query planner to use them under RLS? Postgres version
PostgreSQL 15.4 on aarch64-unknown-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-6), 64-bit
Schema
\d indexed_commit
                         Table "public.indexed_commit"
     Column     |            Type             | Collation | Nullable | 
Default
---------------+-----------------------------+-----------+----------+---------
  id            | bigint                      |           | not null |
  commit_hash   | character varying(40)       |           | not null |
  parent_hash   | text                        | |          |
  created_ts    | timestamp without time zone |           | not null |
  repo_id       | bigint                      |           | not null |
  lines_added   | bigint                      | |          |
  lines_removed | bigint                      | |          |
  tenant_id     | uuid                        |           | not null |
  author_id     | uuid                        |           | not null |
Indexes:
     "indexed-commit-repo-idx" btree (repo_id)
     "indexed_commit_commit_hash_repo_id_key" UNIQUE CONSTRAINT, btree 
(commit_hash, repo_id) REPLICA IDENTITY
     "indexed_commit_repo_id_without_loc_idx" btree (repo_id) WHERE 
lines_added IS NULL OR lines_removed IS NULL
Policies:
     POLICY "commit_isolation_policy"
       USING ((tenant_id = 
(current_setting('app.current_tenant_id'::text))::uuid))
Table Metadata
SELECT relname, relpages, reltuples, relallvisible, relkind, relnatts, 
relhassubclass, reloptions, pg_table_size(oid) FROM pg_class WHERE 
relname='indexed_commit';
     relname     | relpages |  reltuples   | relallvisible | relkind | 
relnatts | relhassubclass | reloptions | pg_table_size
----------------+----------+--------------+---------------+---------+----------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------+---------------
  indexed_commit | 18170522 | 7.451964e+08 |      18104744 | r |        
9 | f              | 
{autovacuum_vacuum_scale_factor=0,autovacuum_analyze_scale_factor=0,autovacuum_vacuum_threshold=200000,autovacuum_analyze_threshold=100000} 
|  148903337984
Asked by Alexander Ok. (11 rep)
Nov 3, 2023, 10:28 AM
Last activity: Nov 3, 2023, 10:32 AM