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 1kX5up-0002WO-04 for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 17:04:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kX5un-0005d6-U7 for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 17:04:13 +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 1kX5un-0005cz-Ic for pgsql-performance@lists.postgresql.org; Mon, 26 Oct 2020 17:04:13 +0000 Received: from mail-il1-x12b.google.com ([2607:f8b0:4864:20::12b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kX5uk-0002ZR-Eg for pgsql-performance@postgresql.org; Mon, 26 Oct 2020 17:04:13 +0000 Received: by mail-il1-x12b.google.com with SMTP id k6so3743723ilq.2 for ; Mon, 26 Oct 2020 10:04:09 -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=xFUrFvaQQefXp4MIhZTIYnJg354IUc6u6jMYxiKG9DE=; b=lkV2WCkL9WS4tmkwI2KAYK8INvrzDTByMgSEk4g1gV4PyR0/Rgy/tL0WUNqKU1d1Oa KuP0yZggsOyOCocIFsZ10LRTT+hNLRzfRTyo7d/4NlSFe1EceZ5fSJH9iAewz0vvsEPc j+l2WOOjbFR9bdCvT96jZpnkokjg3+ucWxo88VBfLg9sp+bd8Qip8Prpu+K6vfc5o/wd Y9qNrBEI+JaQ1sZCGGCvkL0LC8Y/1QFFituUcgsIF0zEpKTlIOWoTb8uaFtRTSvMdKo0 hQpxYbQFktmjKw1hy3fdWZXU9Qe6PUIf4B3VP6pT2xmC91TPdudK6+pWGgCninAFPFV7 +85g== 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=xFUrFvaQQefXp4MIhZTIYnJg354IUc6u6jMYxiKG9DE=; b=SSr7umQinx0ztgnENPodiA0K9u2I/T1jOyUCwi8on+Jn4ByOUa0Kdpka3eqbUVWfc0 jxsEQjeP+1RsxNbg2i2eWbOosLv/s6KL/btEkoBhSdgAfSbb1SLIJBEcVVyHfkhp1E5e bcn946Af1B9BiOebnT4v8r2ldS5dniDzl+EZPu8PPdk1ipSxxk0BUkGuEV2gt5DjPuGo 5Mm/NIf5riy32eEgRm4gO3z7j9gWl2UEILiq3gFbsFT62kH62NgEoRFl97RH713Yow8K OLoycrHMOZ1l28DZOdk4wLpv7GVUOePQBlCaQNUn2wypHrx8kXJchRBB8mT00K8R5VJp n6Rw== X-Gm-Message-State: AOAM532lNQrN8iXXKcToOe1Fc5beY0+F10ejbRHbZLNolNn4Fj766Y0N sZPWpfkWD9VS1Ac8AmNkFwggepO9qwqGrg== X-Google-Smtp-Source: ABdhPJxVlIZJKDohPDJfc/D6v5ODkl8H5jL0s0Yv8dOkE/glho96hHg6BYshWhUqRu6CbZkDP07dAQ== X-Received: by 2002:a92:db05:: with SMTP id b5mr11483438iln.279.1603731848397; Mon, 26 Oct 2020 10:04:08 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id e204sm5745480iof.16.2020.10.26.10.04.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Oct 2020 10:04:07 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 864CC80077A; Mon, 26 Oct 2020 12:04:05 -0500 (CDT) Date: Mon, 26 Oct 2020 12:04:05 -0500 From: Justin Pryzby To: Philip Semanchuk Cc: pgsql-performance@postgresql.org Subject: Re: Understanding bad estimate (related to FKs?) Message-ID: <20201026170405.GT9241@telsasoft.com> References: <7AC7104E-E233-4CCA-B99E-12197D61A4DD@americanefficient.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7AC7104E-E233-4CCA-B99E-12197D61A4DD@americanefficient.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Oct 26, 2020 at 12:50:38PM -0400, Philip Semanchuk wrote: > I'm trying to understand a bad estimate by the planner, and what I can do about it. The anonymized plan is here: https://explain.depesz.com/s/0MDz What postgres version ? Since 9.6(?) FKs affect estimates. > 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) > > The columns in the first two expressions (five_uniform and whiskey_mike) are NOT NULL, and have foreign key constraints to their respective tables (zulu_five.five_uniform and juliet_india.whiskey_mike). The planner can know in advance that 100% of the rows in the table will satisfy those criteria. > > For the third expression (bravo = 'mike'), Postgres has excellent statistics. The estimated frequency of 'mike' is 2.228%, actual frequency is 2.242%, so Postgres' estimate is only off by a tiny amount (0.014%). > > From what I understand, the planner has all the information it needs to make a very accurate estimate here, but it's off by quite a lot. What information am I failing to give to the planner? > > My second point of confusion is related. There are 564,071 rows in the source table (xray_india, aliased as papa) that satisfy the condition bravo = 'mike'. EXPLAIN reports the actual number of rows returned as 896*1062 ~= 951,552. I understand that the number reported by EXPLAIN is often a bit bigger, but this discrepancy is much larger than I'm expecting. What am I missing here?