Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kIYFi-0000sG-6T for pgsql-performance@arkaria.postgresql.org; Wed, 16 Sep 2020 14:17:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kIYFh-0002Xv-5S for pgsql-performance@arkaria.postgresql.org; Wed, 16 Sep 2020 14:17:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kIYFg-0002Xo-V2 for pgsql-performance@lists.postgresql.org; Wed, 16 Sep 2020 14:17:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kIYFd-0006hJ-Bz for pgsql-performance@lists.postgresql.org; Wed, 16 Sep 2020 14:17:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 08GEHXGG1502556; Wed, 16 Sep 2020 10:17:33 -0400 From: Tom Lane To: "Gopisetty, Ramesh" cc: "pgsql-performance@lists.postgresql.org" Subject: Re: Performance issue when we use policies for Row Level Security along with functions In-reply-to: References: Comments: In-reply-to "Gopisetty, Ramesh" message dated "Wed, 16 Sep 2020 03:39:08 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1502554.1600265853.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 16 Sep 2020 10:17:33 -0400 Message-ID: <1502555.1600265853@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Gopisetty, Ramesh" writes: > Policy > create policy policy_sel on test FOR SELECT to ram1 USING ( testkey i= n (f_sel_policy_test(testkey)) ); > Going to a Sequential scan instead of index scan. Hence, performance is= sue. > If i replace the policy with stright forward without function then it ch= ooses the index. Not sure how i can implement with the function. > create policy policy_sel on test FOR SELECT to ram1 USING ( testkey in= ('COMMON',current_setting('ctx_ng'||'.'||'ctx_key_fil'))); " testkey in ('COMMON',current_setting('ctx_ng'||'.'||'ctx_key_fil')) " is an indexable condition on testkey, because it compares testkey to a constant (or at least, a value that's fixed for the life of the query). " testkey in (f_sel_policy_test(testkey)) " is not an indexable condition on anything, because there are variables on both sides of the condition. So there's no fixed value that the index can search on. If you intend f_sel_policy_test() to be equivalent to the other condition, why are you passing it an argument it doesn't need? As Luis noted, there's also the problem that an indexable condition can't be volatile. I gather that SYS_CONTEXT ends up being a probe of some GUC setting, which means that marking the function IMMUTABLE would be a lie, but you ought to be able to mark it STABLE. regards, tom lane