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.89) (envelope-from ) id 1hvrA9-0001gY-9J for pgsql-performance@arkaria.postgresql.org; Thu, 08 Aug 2019 22:45:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvrA7-0004M7-Sk for pgsql-performance@arkaria.postgresql.org; Thu, 08 Aug 2019 22:45:35 +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 1hvrA7-0004F1-Ge for pgsql-performance@lists.postgresql.org; Thu, 08 Aug 2019 22:45:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvr9j-0006I6-CB for pgsql-performance@lists.postgresql.org; Thu, 08 Aug 2019 22:45: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 x78Mj7K6027101; Thu, 8 Aug 2019 18:45:07 -0400 From: Tom Lane To: "Thomas Rosenstein" cc: pgsql-performance@lists.postgresql.org Subject: Re: Postgres not using correct indices for views. In-reply-to: References: <31061.1565280312@sss.pgh.pa.us> Comments: In-reply-to "Thomas Rosenstein" message dated "Thu, 08 Aug 2019 22:04:31 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27099.1565304307.1@sss.pgh.pa.us> Date: Thu, 08 Aug 2019 18:45:07 -0400 Message-ID: <27100.1565304307@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Thomas Rosenstein" writes: > The planner estimates the correct row counts, but still does the wrong > planning. Hm, I'm not exactly convinced. You show > Wrong: > -> Hash Join (cost=359555.11..1849150.95 rows=1496816 > width=1508) (actual time=1081.081..24251.466 rows=543231 loops=1) > Hash Cond: (p.customer_id = l.customer_id) > Join Filter: ((p.date - '3 days'::interval day) <= > l.duedate) > Rows Removed by Join Filter: 596120 > Correct: > -> Nested Loop (cost=0.87..2961441.25 rows=515233 width=1471) The join size estimate seems a lot closer to being correct in the second case, which could lend support to the idea that statistics aren't being applied in the first case. However ... it sort of looks like the planner didn't even consider the second plan shape in the "wrong" case. If it had, then even if it costed it 3X more than it did in the "right" case, the second plan would still have won out by orders of magnitude. So there's something else going on. Can you show the actual query and table and view definitions? regards, tom lane