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 1kXwCL-0003IG-U1 for pgsql-performance@arkaria.postgresql.org; Thu, 29 Oct 2020 00:53:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kXwCJ-0006a6-QI for pgsql-performance@arkaria.postgresql.org; Thu, 29 Oct 2020 00:53:47 +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 1kXwCJ-0006Zy-J3 for pgsql-performance@lists.postgresql.org; Thu, 29 Oct 2020 00:53:47 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kXwCG-0007Kc-11 for pgsql-performance@lists.postgresql.org; Thu, 29 Oct 2020 00:53:47 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 9ED2D7BA for ; Wed, 28 Oct 2020 20:53:40 -0400 (EDT) Received: from imap4 ([10.202.2.54]) by compute5.internal (MEProxy); Wed, 28 Oct 2020 20:53:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plumdev.com; h= mime-version:message-id:in-reply-to:references:date:from:to :subject:content-type; s=mesmtp; bh=R9jXfBqJ6ciLC5ZJERnIEYySoh9w Yy/KvALwUXDKapA=; b=ABPqjqvyEoC3UDghl8ioKQdL0kgeqfj+dKMWX2MP3AcC WKCkvdvtt00Nyqm+lu76eIlFmjjchGzmALShWM0eHRwXQKhaG3lkmeq/exOoEA1i GR15/K9BtQCcapGFCI76VB0RPcBJpcGROYiFwuc2k8P6obD1cB0hizKuYg4AKnw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=R9jXfB qJ6ciLC5ZJERnIEYySoh9wYy/KvALwUXDKapA=; b=VJeUzGHv6Fb+5BMNXn7l/2 0qNDZnnuLbaS52auIKw/Rth3xBEyEVRRhhRQEKjf+ZHnVM93EeYZl4cKdeVAxZIq eqCsS4xT97vtOj0x0bk6dSIzpYhkagzYUZK0WXJXQkO2CKIJUaCo7+UWgDrvLrB0 9zRenKlRuKEV+k3clHY5XKtTdhInoISF/Q3mcV2wU39ZTBpQ1uXAHIni51icku5J XGqDUoZBPDFl+Gz2ir4IRu2VUMpoZB1TkQfT46MFIdQXmLbgjFmWRvv7deYPjtVf gn6uHH3YLwfIqRCzvH8Lgu6KzZHd93XSuoAeQjvTw/w5dFFIhrnnAYHH/cRIisSA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrledvgdehhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvffutgesthdtre dtreerjeenucfhrhhomhepfdfqlhhivhhivghrucfrohhquhgvthdfuceoohhpohhquhgv thesphhluhhmuggvvhdrtghomheqnecuggftrfgrthhtvghrnhepkedvleejvdejhfetke ejjefhgfeuffdtueevtedtfefhveeftdeiueettedujedvnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepohhpohhquhgvthesphhluhhmuggvvh drtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 894183C00A1; Wed, 28 Oct 2020 20:53:39 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-529-g69105b1-fm-20201021.003-g69105b13 Mime-Version: 1.0 Message-Id: <7416fcaf-bbae-4f10-a790-769430f9e897@www.fastmail.com> In-Reply-To: <1455831.1603927818@sss.pgh.pa.us> References: <59eb4cf7-4c68-448d-a2d8-541464803353@www.fastmail.com> <1455831.1603927818@sss.pgh.pa.us> Date: Wed, 28 Oct 2020 20:53:09 -0400 From: "Olivier Poquet" To: pgsql-performance@lists.postgresql.org Subject: =?UTF-8?Q?Re:_query_plan_using_partial_index_expects_a_much_larger_numbe?= =?UTF-8?Q?r_of_rows_than_is_possible?= Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thanks Tom, That makes perfect sense. I'd already gone the route of materializing the condition but I didn't even realize that generated columns was an option (I'd done the same with triggers instead). So thanks a lot of that too! -- Olivier Poquet opoquet@plumdev.com On Wed, Oct 28, 2020, at 7:30 PM, Tom Lane wrote: > "Olivier Poquet" writes: > > Looking at it in more detail, I found that the planner is assuming that I'll get millions of rows back even when I do a simple query that does an index scan on my partial index: > > We don't look at partial-index predicates when trying to estimate the > selectivity of a WHERE clause. It's not clear to me whether that'd be > a useful thing to do, or whether it could be shoehorned into the system > easily. (One big problem is that while the index size could provide > an upper bound, it's not apparent how to combine that knowledge with > selectivities of unrelated conditions. Also, it's riskier to extrapolate > a current rowcount estimate from stale relpages/reltuples data for an > index than it is for a table, because the index is less likely to scale > up linearly.) > > If this particular query is performance-critical, you might consider > materializing the condition, that is something like > > create table orderitems ( > ... , > committed_unfulfilled bool GENERATED ALWAYS AS > (LEAST(committed, quantity) > fulfilled) STORED > ); > > and then your queries and your partial-index predicate must look > like "WHERE committed_unfulfilled". Having done this, ANALYZE > would gather stats on the values of that column and the WHERE > clauses would be estimated accurately. > > regards, tom lane >