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 1kYGj5-0004Ei-1I for pgsql-performance@arkaria.postgresql.org; Thu, 29 Oct 2020 22:48:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kYGj3-00058M-9j for pgsql-performance@arkaria.postgresql.org; Thu, 29 Oct 2020 22:48:57 +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 1kYGj3-00058E-24 for pgsql-performance@lists.postgresql.org; Thu, 29 Oct 2020 22:48:57 +0000 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kYGiv-0003NG-V3 for pgsql-performance@postgresql.org; Thu, 29 Oct 2020 22:48:55 +0000 Received: by mail-ed1-x544.google.com with SMTP id w25so4746609edx.2 for ; Thu, 29 Oct 2020 15:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=onmsbt7fu9zkAmq9JUGL3gD8ZzHv3Nvu9RH7/dtEOyE=; b=Gs82dZJqxrNHGRLT3wHkRJx9tOvTZvOmTXhYHR3P5b3688XyRLU/rlxOqkpiCQT5Kb Bdcevntzmcka7wOYctzdTf5EAWJVEpc9ZdH97zimOxq++UooKxxSjDxYfzXctWQsUQwT Arh/KDCMFbZgs+VTKN0g//3e34gHk9gdRYdkbiphm1RkUVyB5TdR13y+uPach6+Ho2HX 0VfVE4PXxxXOlKD4EG2d1McOvKK+WCENlpIxSB4XIzDUzzaqR7d2ioXX1JCWz/uk6vek HUjQv7of0iCbAMq2rjkqErzHP4nFHaqHVRAJH+V0u8eTK1oYPmQM3bIRE6taH3gT6+yc Kriw== 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:content-transfer-encoding :in-reply-to; bh=onmsbt7fu9zkAmq9JUGL3gD8ZzHv3Nvu9RH7/dtEOyE=; b=VKgrVAxGSbtNZLX9mzHYX7sgV+9s3FNddEStNIaDHNf70BC8XhRNUEcN/AW0Apncfi PsoVdpm395El7Nv6mU4UuCnvqpSpqPY3x8RfC72pi8NxERvb8BY8Z7xyq9npG+CN556p NODA2rHTpFo+X8YcM/Sos2F4XNsGgF3ps4gj4L4amo2WtthmOxV8iH8poyKcpNdJeK71 +RO/sVR3dpYSjXthOaeBV+OfZsf/sD6e+LCAyJqO77f+cf1L7oNTUd7vS0Ce2wEacFEK 7k0IY/QhjV4lEjRGZpWKOVR9NaabBkfUy+aAKuhVKJ1sOTk/JIdyH/ukpTM6G80WsMvA T65w== X-Gm-Message-State: AOAM532nCol8x/fFUU7MOqrPKB5X01R4MnTwRZwNJNzdmp187M+syrFX QnLkQ/LvW2PJE9zbHd1aF89fTw== X-Google-Smtp-Source: ABdhPJxJDhBqWOTIO3RPbQ1LPg2VUSURH4Ert4Gxn7U3HAt49O8Gd+tnsjx2sqChicRVMW9T8DqQIA== X-Received: by 2002:aa7:da4d:: with SMTP id w13mr6272777eds.266.1604011728465; Thu, 29 Oct 2020 15:48:48 -0700 (PDT) Received: from localhost (ip-86-49-245-76.net.upcbroadband.cz. [86.49.245.76]) by smtp.gmail.com with ESMTPSA id c5sm2274370edx.58.2020.10.29.15.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 15:48:47 -0700 (PDT) Date: Thu, 29 Oct 2020 23:48:45 +0100 From: Tomas Vondra To: Philip Semanchuk Cc: Justin Pryzby , Michael Lewis , postgres performance list Subject: Re: Understanding bad estimate (related to FKs?) Message-ID: <20201029224845.ptteqhzq65bh6jsh@development> References: <7AC7104E-E233-4CCA-B99E-12197D61A4DD@americanefficient.com> <20201026170405.GT9241@telsasoft.com> <20201029011343.GO5380@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Oct 29, 2020 at 11:25:48AM -0400, Philip Semanchuk wrote: > > >> On Oct 28, 2020, at 9:13 PM, Justin Pryzby >> wrote: >> >> On Mon, Oct 26, 2020 at 11:20:01AM -0600, Michael Lewis wrote: >>> On Mon, Oct 26, 2020 at 11:14 AM Philip Semanchuk >>> wrote: >>> >>>>>> The item I'm focused on is node 23. The estimate is for 7 rows, >>>>>> actual >>>> is 896 (multiplied by 1062 loops). I'm confused about two things in >>>> this node. >>>>>> >>>>>> The first is Postgres' estimate. The condition for this index >>>>>> scan >>>> contains three expressions -- >>>>>> >>>>>> (five_uniform = zulu_five.five_uniform) AND (whiskey_mike = >>>>>> juliet_india.whiskey_mike) AND (bravo = 'mike'::text) >>>> >>> >>> Are the columns correlated? >> >> I guess it shouldn't matter, since the FKs should remove all but one >> of the conditions. > >Yes, I had the same expectation. I thought Postgres would calculate the >selectivity as 1.0 * 1.0 * whatever estimate it has for the frequency >of ‘mike’, but since the frequency estimate is very accurate but the >planner’s estimate is not, there’s something else going on. > Well, this is quite a bit more complicated, I'm afraid :-( The clauses include parameters passed from the nodes above the index scan. So even if we had extended stats on the table, we couldn't use them as that requires (Var op Const) conditions. So this likely ends up with a product of estimates for each clause, and even then we can't use any particular value so we probably end up with something like 1/ndistinct or something like that. So if the values actually passed to the index scan are more common and/or if the columns are somehow correlated, it's not surprising we end up with an overestimate. >> Maybe you saw this other thread, which I tentatively think also >> affects your case (equijoin with nonjoin condition) >> https://www.postgresql.org/message-id/AM6PR02MB5287A0ADD936C1FA80973E72AB190%40AM6PR02MB5287.eurprd02.prod.outlook.com > >Yes, thank you, I read that thread with interest. I tried your clever >trick using BETWEEN, but it didn’t change the plan. Does that suggest >there’s some other cause for the planner’s poor estimate? > I don't think that's related - to hit that bug, there would have to be implied conditions pushed-down to the scan level. And there's nothing like that in this case. FWIW I don't think this has anything to do with join cardinality estimation - at least not for the node 23. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services