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 1jWN0l-0004ms-5J for pgsql-performance@arkaria.postgresql.org; Wed, 06 May 2020 16:35:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jWN0j-0004kC-Re for pgsql-performance@arkaria.postgresql.org; Wed, 06 May 2020 16:35:05 +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 1jWN0j-0004j6-GO for pgsql-performance@lists.postgresql.org; Wed, 06 May 2020 16:35:05 +0000 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jWN0g-00066R-2l for pgsql-performance@lists.postgresql.org; Wed, 06 May 2020 16:35:04 +0000 Received: by mail-qt1-x841.google.com with SMTP id l18so2037762qtp.0 for ; Wed, 06 May 2020 09:35:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=1H4kGkC0BxslSjO7kgBFdlQ3oVjUZ/73nTMZwWAV0jQ=; b=A9Z9GWgPGr+2sv5DEh6AxrA7RvFr205ltYx9d9ReGwjA2Dl79nHGOuCAu6g/B8muip 4i8/DWpaxdQHcL2p7rzo0kdYwskxB2YrbeYiNTNmY8/AwuEX54inobGDXb2DKfvbc02u lhjLrbjjnRWE9MbtA8NslDsks+16DC6A8ctTKagsxDV5arfatxJmKqSQwE5nm+q1TV9k y4HioTASz3IRF5Os6/j+6k5Dtxem8161vDgwnD5ufpaNS/WREPO5nYKTimT7yGuHPoDQ vNs0YwS11k/3tbBuTI17bAQD/hcUoHA4KD1OeXDPaTok3Iw4IM8FQgm+blR8oNC3t53P NinA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=1H4kGkC0BxslSjO7kgBFdlQ3oVjUZ/73nTMZwWAV0jQ=; b=lQTr0OslBFhKJ+n4hJKTJFRP+8nR39vRQtLZl15fYJcribsRrNFLjSsRPL5Ci9ce+n NZMObofQo8TVRLXxQEJNhEMZK4ThMR1l8m9RXg8nmi5NFPsflFMomyZmlcVa96oY4M+O 7rMnyKeiTW8KRldS1PdzdzO8qlTRoUQd75SWmIGKurTwzCATIPxUHrrKhhcKBIHrwEgt 8gUToofcVEKYy1ycvcmrOEWtNXk67qMWKJY1DnJKzgS4DC8OEfAzqHtkXN8dsXTXC1B7 z76bTBsAOFn+6tqYlqBgcGJBwjCYhlZMjL1FHx//rY+lOLUfAaNkCeq9whKHcELUT4xv r92Q== X-Gm-Message-State: AGi0PuYeS+tBGAPvmQjXIdG9FfE1I5Z9KtU2a1xrpe8h6n8uTnSJ9CyT OhU+gLNZ9rYGpZzUSrnK/whIyw== X-Google-Smtp-Source: APiQypJb3mVaKQXuHHf299LQcJajFNnPwWHRecaNtkJd2f1ToeFNvKn1XwSA8aRku9/zp0TIJqHpuQ== X-Received: by 2002:aed:2766:: with SMTP id n93mr9151677qtd.130.1588782900450; Wed, 06 May 2020 09:35:00 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id g6sm1920285qtc.52.2020.05.06.09.34.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 May 2020 09:34:59 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 78A74800A79; Wed, 6 May 2020 11:34:57 -0500 (CDT) Date: Wed, 6 May 2020 11:34:57 -0500 From: Justin Pryzby To: Steve Pritchard Cc: pgsql-performance@lists.postgresql.org Subject: Re: Inaccurate Rows estimate for "Bitmap And" causes Planner to choose wrong join Message-ID: <20200506163457.GM28974@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, May 06, 2020 at 05:19:48PM +0100, Steve Pritchard wrote: > Version: Postgres 9.6.3 production system (but also tested on Postgres 12) > > For my query the Planner is sometimes choosing an execution plan that uses > "Bitmap And" (depending on the parameters): > > The Planner then carries this estimate of "1 row" through the rest of the > query (which is quite complex), and then makes poor choices about joins. > e.g. uses "Nested Loop Left Join" because it's only expecting one row, > whereas in practice it has to do 15636 loops which is very slow. > Note that in cases where the Planner selects a single Index Scan for this > query (with different parameters), the Planner makes an accurate estimate > of the number of rows and then makes sensible selections of joins (i.e. > quick). > i.e. the issue seems to be with the "Bitmap And". > > I don't have an index with both user_id & loc_id, as this is one of several > different combinations that can arise (it would require quite a few indexes > to cover all the possible combinations). However if I did have such an > index, the planner would presumably be able to use the statistics for > user_id and loc_id to estimate the number of rows. > > So why can't it make an accurate estimate of the rows with a "Bitmap And" & > " Bitmap Heap Scan"? (as above) It probably *has* statistics for user_id and loc_id, but doesn't have stats for (user_id,loc_id). Presumbly the conditions are partially redundant, so loc_id => user_id (strictly implies or just correlated) or the other way around. In pg10+ you can use "CREATE STATISTICS (dependencies)" to improve that. https://www.postgresql.org/docs/devel/sql-createstatistics.html Otherwise you can use the "CREATE TYPE / CREATE INDEX" trick Tomas described here: https://www.postgresql.org/message-id/20190424003633.ruvhbv5ro3fawo67%40development -- Justin