Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1j9HX0-0005td-18 for pgsql-general@arkaria.postgresql.org; Wed, 04 Mar 2020 00:04:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j9HWy-0004yO-Ck for pgsql-general@arkaria.postgresql.org; Wed, 04 Mar 2020 00:04:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j9HWy-0004xK-3R for pgsql-general@lists.postgresql.org; Wed, 04 Mar 2020 00:04:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j9HWq-0006sJ-C2 for pgsql-general@lists.postgresql.org; Wed, 04 Mar 2020 00:04:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 02404eYA011493; Tue, 3 Mar 2020 19:04:40 -0500 From: Tom Lane To: Alastair McKinley cc: "pgsql-general@lists.postgresql.org" Subject: Re: Poor plan choice with partial unique indexes on jsonb column and simple RLS policy (with test script) In-reply-to: References: Comments: In-reply-to Alastair McKinley message dated "Tue, 03 Mar 2020 20:42:43 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11491.1583280280.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 03 Mar 2020 19:04:40 -0500 Message-ID: <11492.1583280280@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alastair McKinley writes: > I have recently encountered a strange poor query plan choice after imple= menting RLS. > My table has a number of partial indexes on a jsonb column and the query= went from low number of milliseconds to several seconds as the planner ch= ose a different index. > Simply stated, in the jsonb column case, "using ( (select true) )" inste= ad of "using (true)" produces a bad plan, illustrated below: If the planner isn't sure you have access to all rows in the table, that disables some of its ability to estimate where-clause selectivity. In particular it can't run "leaky" where-clauses against all values in the table's statistics entries to see how many pass, because a nefarious user could use that to glean info about what's in the table. Eyeing your test query, it looks like the issue is that jsonb "->" isn't leakproof, so that clause falls back to a default selectivity estimate, and you get a bad plan as a result. regards, tom lane