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 1kX64f-0002ny-K5 for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 17:14:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kX64c-00005x-4h for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 17:14:22 +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 1kX64b-00005q-L5 for pgsql-performance@lists.postgresql.org; Mon, 26 Oct 2020 17:14:21 +0000 Received: from mail-qk1-x72e.google.com ([2607:f8b0:4864:20::72e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kX64U-00074Z-S9 for pgsql-performance@postgresql.org; Mon, 26 Oct 2020 17:14:20 +0000 Received: by mail-qk1-x72e.google.com with SMTP id a23so9043567qkg.13 for ; Mon, 26 Oct 2020 10:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=americanefficient-com.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=KOdufTWqZgFOhKl7BqIfJt0h8lzlmc6Oc9LkBYviCWI=; b=MMqxonzEOul/fKqSdhNltK4fRUNeB1iowpWIKqFhSpgv2RS+M1rZ+GRDCgVH5KMxTA WPChFOtqN+J3kCdDoRXqsATURrh3+/AkRkvZVdeZzrmEGfJVF6+KH2ZIN4+8pbgRuJWp WaMfXtsQ0Qbd2PENQRRlviFJRL5IYeRX6SJm69iAYE+FeLjZUUa5g8eZZhAV49b+Pvte b2F2uafI8suGSqTc1ACEN2uyIJMnPUb6iQkvhNMj/QF4LdCDDZENqYy3qqK9yL3Bw612 uP1079BoinzELZ5ILo2/TkEcpRK7q8uPV1+2OeD9tldjB8xZHFexrS+GATT7n9t9t0Hm s5+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=KOdufTWqZgFOhKl7BqIfJt0h8lzlmc6Oc9LkBYviCWI=; b=Sp93pK8pPgOUsnsO/eFPwzRJA0gLgBx2vIQ0IpW/atuO4I3LAJwIJRM/+0PzXn5jIF ksUrY5cfS7DIp4L23d5ZpLanv7SjvY3QoTilE8he1u4JjeqYWKZsJngJEaF0MiBS+QPT zMTjgQmszKwxa1+bHU5e5ORGBR17wPhQrtEs3g0quiWRB/8Zi1C7NkozrnxlfaCNLoRd b4Dvk915zpSBUznmumaLZw1eKV1Pst9IPereiiKmEbWO8j4zPoakKkdCx7yBqsa2kxFg Zs/iYKI/AM0L67SUv64MdqwbTjDx05W71BmFzsYoQYLKhPZHMUCr1BjOVI/aYf8mxnbz OqfA== X-Gm-Message-State: AOAM532E9efqssY3Z9LBxaeL9S9VuwCG4vmxHTG7ZYtW0XYqpVJzXktb KtgsUXNEJYzWVpoirGjuNgqEXg== X-Google-Smtp-Source: ABdhPJwrT0UWAhj2QKSE2Few14l8AAB0UDe3htYjdiW/XzyGHxaCQTjBducq+vx9HSyAbxgA+vcdMg== X-Received: by 2002:ae9:e702:: with SMTP id m2mr15897115qka.387.1603732453912; Mon, 26 Oct 2020 10:14:13 -0700 (PDT) Received: from [10.0.1.7] (user-10lff1p.cable.mindspring.com. [65.87.188.57]) by smtp.gmail.com with ESMTPSA id j2sm7100752qtp.27.2020.10.26.10.14.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Oct 2020 10:14:13 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: Understanding bad estimate (related to FKs?) From: Philip Semanchuk In-Reply-To: <20201026170405.GT9241@telsasoft.com> Date: Mon, 26 Oct 2020 13:14:12 -0400 Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <7AC7104E-E233-4CCA-B99E-12197D61A4DD@americanefficient.com> <20201026170405.GT9241@telsasoft.com> To: Justin Pryzby X-Mailer: Apple Mail (2.3608.120.23.2.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Oct 26, 2020, at 1:04 PM, Justin Pryzby = wrote: >=20 > 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 >=20 > What postgres version ? > Since 9.6(?) FKs affect estimates. We=E2=80=99re using 11.6 (under AWS Aurora). >=20 >> 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. >>=20 >> The first is Postgres' estimate. The condition for this index scan = contains three expressions -- >>=20 >> (five_uniform =3D zulu_five.five_uniform) AND >> (whiskey_mike =3D juliet_india.whiskey_mike) AND >> (bravo =3D 'mike'::text) >>=20 >> 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. >>=20 >> For the third expression (bravo =3D '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%). >>=20 >> =46rom 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? >>=20 >> 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 =3D 'mike'. EXPLAIN reports the actual number of rows = returned as 896*1062 ~=3D 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?