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 1jZOuw-0000es-F7 for pgsql-general@arkaria.postgresql.org; Fri, 15 May 2020 01:13:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jZOuu-0006y7-G5 for pgsql-general@arkaria.postgresql.org; Fri, 15 May 2020 01:13:36 +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 1jZOuu-0006y0-88 for pgsql-general@lists.postgresql.org; Fri, 15 May 2020 01:13:36 +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 1jZOur-0005aH-N8 for pgsql-general@postgresql.org; Fri, 15 May 2020 01:13:35 +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 04F1DUlb020140; Thu, 14 May 2020 21:13:30 -0400 From: Tom Lane To: Vamsi Meduri cc: pgsql-general@postgresql.org Subject: Re: view selection during query rewrite In-reply-to: References: Comments: In-reply-to Vamsi Meduri message dated "Thu, 14 May 2020 17:22:35 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20138.1589505210.1@sss.pgh.pa.us> Date: Thu, 14 May 2020 21:13:30 -0400 Message-ID: <20139.1589505210@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Vamsi Meduri writes: > Suppose I have the following query and a view v1 defined as follows: > *Q1: SELECT * FROM Table1 WHERE Table1.col1 = 5 and Table1.col2 LIKE > '%abc%';* > *create view v1 as select * from Table1 where Table1.col1 = 5;* > An effective way to execute Q1 would be to re-write it as: *select * from > v1 where col2 LIKE '%abc%';* provided that the selection predicates are > highly selective and also if it is cheaper in execution time of the query. > However I see that postgres does not automatically perform query rewriting > using view. I have looked at the plan using explain (analyze) and I see > that Q1 is always executed using the base tables unless I explicitly > mention the view in the query instead of the base table. If you are talking about a regular view, such a transformation would be a complete waste of time, because the end result would be exactly the same (after expending a lot of cycles transforming the query and then reversing it back to the original state during view expansion). If it's a materialized view, then there'd be a potential for savings ... but the other side of that coin is that you'd get stale data, since a matview is not going to be entirely up to date. It's no business of the rewriter (or the query planner) to decide that such a tradeoff is OK. I do recall some discussion of extensions attempting to do such things, but I doubt we'd ever put it in core Postgres. regards, tom lane