Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qt9UX-00B3Ns-Pv for pgsql-performance@arkaria.postgresql.org; Wed, 18 Oct 2023 16:33:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qt9UV-006HXU-QG for pgsql-performance@arkaria.postgresql.org; Wed, 18 Oct 2023 16:33:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qt7D5-004MY8-5y for pgsql-performance@lists.postgresql.org; Wed, 18 Oct 2023 14:07:43 +0000 Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qt7D1-001NHe-Uc for pgsql-performance@postgresql.org; Wed, 18 Oct 2023 14:07:43 +0000 Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2c50305c5c4so84757361fa.1 for ; Wed, 18 Oct 2023 07:07:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stiltsoft.com; s=google; t=1697638060; x=1698242860; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=HEaplEbIXu5CzfG/OWkn/5TL+VEBoXOy6/9GGa/tPU4=; b=FI/EtElFJLwnn7qqI1Epe0q2gB5+QrPAxMopMYJHOqWtg9Wp2F9Xg7dJGo3/fBWH12 q9WMQd26IqbCmhPjxZnsTuOGtRMVIa0TyX0GSCudfAcYHoRyxpNGbY/usoBMpKFFJ/QG yVzCY9mmWnuLDqsAwAGBkmgtkDVnV09cAylHM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697638060; x=1698242860; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HEaplEbIXu5CzfG/OWkn/5TL+VEBoXOy6/9GGa/tPU4=; b=kz6Hj+COVWHEWbKERWA1heMp9/rhoXvbBRGGcw7Rl1pQizvvg+C8oizhUIJ6T2KxUj nYJwPevYE9LcRim/VuC9Kgw3F4VAk2MxguJp97vQHa+uFOPZ/UFrQsz0GYdmixi58Hsl uvGHCOtZPzmhfrekzA675OvLDolaETkfhHhv1R/xzxrzf3QHT4oUyCrhpMFOF5xUDqFB YmV4D0im6G2X4NptBMowahqXJvPr8p/bKK3evr/+nb/MdXQVRAHKx+YNNmGL3eUaPMrw gdRNB1D+m0u3UaHNMZJF0qQoPNXvd6AY9dAj1sw5fBUyYsfnwdMIr8MLxuCQuJu9OJCL QwUw== X-Gm-Message-State: AOJu0YxGoW7rRKnX5lIewPm+RCcXpWi7T+kfkyLeYuVv4yxUQMQJTjb2 HLGgdArLgEi4HiVAyenaY9REN3dJf+YSe2ch9haetGP2 X-Google-Smtp-Source: AGHT+IEjscATVM4BUJbyHK6XidBDcUifsqibF6p2nTuX7aeB+1lDT9zQue4VWKqMKBxPt/Tz++qK/A== X-Received: by 2002:a05:651c:544:b0:2c5:1915:9824 with SMTP id q4-20020a05651c054400b002c519159824mr4598105ljp.9.1697638059806; Wed, 18 Oct 2023 07:07:39 -0700 (PDT) Received: from ?IPV6:2a02:a31d:8540:a280:38a:ec48:fa5a:d99e? ([2a02:a31d:8540:a280:38a:ec48:fa5a:d99e]) by smtp.gmail.com with ESMTPSA id e34-20020a5d5962000000b003143c9beeaesm2198178wri.44.2023.10.18.07.07.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Oct 2023 07:07:39 -0700 (PDT) Message-ID: Date: Wed, 18 Oct 2023 16:07:38 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Postgres 15 SELECT query doesn't use index under RLS Content-Language: en-US To: Tom Lane Cc: pgsql-performance@postgresql.org References: <5c1179bb-240b-4c1c-b4b3-2a24868e44bc@stiltsoft.com> <1570249.1697228785@sss.pgh.pa.us> From: Alexander Okulovich In-Reply-To: <1570249.1697228785@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Tom, > If in fact you were getting decent performance from an indexscan plan > before, the only explanation I can think of is that the repo_ids you > are querying for are correlated with the tenant_id, so that the RLS > filter doesn't eliminate very many rows from the index result. The > planner wouldn't realize that by default, but if you create extended > statistics on repo_id and tenant_id then it might do better. Still, > you probably want the extra index. Do you have any idea how to measure that correlation? > You haven't shown any evidence suggesting that. My suggestion is based on following backward reasoning. We used the product with the default settings. The requests are simple. We didn't change the hardware (actually, we use even more performant hardware because of that issue) and DDL. I've checked the request on old and new databases. Requests that rely on this index execute more than 10 times longer. Planner indeed used Index Scan before, but now it doesn't. So, from my perspective, the only reason we experience that is database logic change. I think we could probably try to reproduce the issue on different Postgres versions and find the specific version that causes this. > Adding tenant_id is going to bloat your indexes quite a bit, > so I wouldn't do that except in cases where you've demonstrated > it's important. Any recommendations from the Postgres team on how to use the indexes under RLS would help a lot here, but I didn't find them. Kind regards, Alexander On 13.10.2023 22:26, Tom Lane wrote: > Alexander Okulovich writes: >> 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 >> index on the "repo_id" column when requesting under user limited with >> RLS. On prod, it happens when there are more than 316 IDs in the IN part >> of the query, on stage - 3. If we execute the request from Superuser, >> the planner always uses the "repo_id" index. > The superuser bypasses the RLS policy. When that's enforced, the > query can no longer use an index-only scan (because it needs to fetch > tenant_id too). Moreover, it may be that only a small fraction of the > rows fetched via the index will satisfy the RLS condition. So the > estimated cost of an indexscan query could be high enough to persuade > the planner that a seqscan is a better idea. > >> Luckily, we can easily reproduce this on our stage database (which is >> smaller). If we add a multicolumn "repo_id, tenant_id" index, the >> planner uses it (Index Only Scan) with any IN params count under RLS. > Yeah, that would be the obvious way to ameliorate both problems. > > If in fact you were getting decent performance from an indexscan plan > before, the only explanation I can think of is that the repo_ids you > are querying for are correlated with the tenant_id, so that the RLS > filter doesn't eliminate very many rows from the index result. The > planner wouldn't realize that by default, but if you create extended > statistics on repo_id and tenant_id then it might do better. Still, > you probably want the extra index. > >> Could you please clarify if this is a Postgres bug or not? > You haven't shown any evidence suggesting that. > >> Should we >> include the "tenant_id" column in all our indexes to make them work >> under RLS? > Adding tenant_id is going to bloat your indexes quite a bit, > so I wouldn't do that except in cases where you've demonstrated > it's important. > > regards, tom lane