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 1jLgU6-0005W6-Jv for pgsql-performance@arkaria.postgresql.org; Tue, 07 Apr 2020 05:09:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jLgU5-00044I-Hy for pgsql-performance@arkaria.postgresql.org; Tue, 07 Apr 2020 05:09:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLgU5-00044B-Ab for pgsql-performance@lists.postgresql.org; Tue, 07 Apr 2020 05:09:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLgU2-0007LD-UO for pgsql-performance@postgresql.org; Tue, 07 Apr 2020 05:09:12 +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 037599Rh026257; Tue, 7 Apr 2020 01:09:09 -0400 From: Tom Lane To: Justin Pryzby cc: Rick Vincent , pgsql-performance@postgresql.org, Manoj Kumar , Herve Aubert Subject: Re: Postgres not using index on views In-reply-to: <20200407045929.GJ2228@telsasoft.com> References: <20200407045929.GJ2228@telsasoft.com> Comments: In-reply-to Justin Pryzby message dated "Mon, 06 Apr 2020 23:59:29 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <26255.1586236149.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 07 Apr 2020 01:09:09 -0400 Message-ID: <26256.1586236149@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Justin Pryzby writes: > On Mon, Apr 06, 2020 at 02:19:59PM +0000, Rick Vincent wrote: >> The following query takes an extremely long time for only 180 rows, and= what this means is that we would have to index anything appearing in the = where clause for every table in order to use views because the views seem = not to consider the select clause. Why is that and does anyone know a way= around this? > Is there a reason why you don't store the extracted value in its own col= umn ? The planner seems to be quite well aware that the slower query is going to be slower, since the estimated costs are much higher. Since it's not choosing to optimize into a faster form, I wonder whether it's constrained by semantic requirements. In particular, I'm suspicious that some of those functions you have in the view are marked "volatile", preventing them from being optimized away. Beyond that guess, though, there's really not enough info here to say. The info we usually ask for to debug slow-query problems is explained at https://wiki.postgresql.org/wiki/Slow_Query_Questions regards, tom lane