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 1qAWEu-0004yS-Qo for pgsql-hackers@arkaria.postgresql.org; Sat, 17 Jun 2023 13:45:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qAWEt-0007PY-MR for pgsql-hackers@arkaria.postgresql.org; Sat, 17 Jun 2023 13:45:15 +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 1qAWEt-0007PP-8e for pgsql-hackers@lists.postgresql.org; Sat, 17 Jun 2023 13:45:15 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qAWEp-002iDd-K7 for pgsql-hackers@postgresql.org; Sat, 17 Jun 2023 13:45:13 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-987341238aeso134075566b.3 for ; Sat, 17 Jun 2023 06:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1687009509; x=1689601509; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=nuK79X+lYhoJOuAA1clN64Jnllvgpx7eJyFlLD+Kggc=; b=MsfiBTbt1T80gE01GdgCLvXyjboTlgJm8XJvoVRZl53Q2CYrgpcAG/I3sWunoX09eT kmG9EIc+uH4dDRSsvOw3RmdYsQ0jHm1p3Nyva2SdDFVbKKH3EAobCkbXLE+119UJPsX8 FsvYPePYDc+m6IfWQr4363c9yYDri//Fm+1PXHRnymvgBhqqS5rhn2DE31XU2qSO/hQ0 trENiH6XwwBOHpgtQr6MynzSSYzMbTz/LmbvqN7sjPvIvT+vWOJ5z8Y8xkJhpcPfzQDI R9BTiiN2nnwfSTuh5vB89l8ufCWB6v8rKFfqnrDEC7HEfWThW8Srm6w82+6BcVzTlyw7 DrZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687009509; x=1689601509; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=nuK79X+lYhoJOuAA1clN64Jnllvgpx7eJyFlLD+Kggc=; b=U24dGp8CVDeK7LJeAjMQEKVAd6JNyuKA3fJG7VZ1ph+TcURNTlnVJoV4AatK0P3eBY YRxFceqhku10DJ3cEmWyfcNuu9zoJZeCR/D2Zl79Wgc7+ve1hxRDdR0TaQPB7dZuLG8C q2lYjwmfkp2Arn8uOL+o1Zxmj3/izoejnM/cBowZn46hlew9lqiKhdCj3eSKMi8/SOdK ol9yb6R7941klcpnh5Dts9X+8500aCE/SJRpucjyX3iMN8yi44/+F9NLGjkmNfKV4fgB hJ5bxgza/IBAM8KHZwlbOQEbFMvp6mixSGK+jU9N10E5Y5J2tBOGrhs5s+DmCwCWMweZ N7Jw== X-Gm-Message-State: AC+VfDyRA6OSiuKWuPve5/1YrWCViFm6+FTx6lxMao5tz2n/zAJ9fX4S TXM7HhyE2+erhrrV9wFkpCDW1CzCm8Oa034BKQQ= X-Google-Smtp-Source: ACHHUZ6Ej3dqO34ToI3ILZa4cvszEVvS2ztnJDX4c57gEWLv6KI3bxMtWkGQbfeEn+GPXaQBrscRiw== X-Received: by 2002:a17:907:2d14:b0:96f:e7cf:5004 with SMTP id gs20-20020a1709072d1400b0096fe7cf5004mr4923640ejc.73.1687009509055; Sat, 17 Jun 2023 06:45:09 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id i3-20020a170906850300b0098646c12d44sm2086721ejx.206.2023.06.17.06.45.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Jun 2023 06:45:08 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------7kxtMUWLVbAchXCgNufiHd1u" Message-ID: <753270d7-a303-d1bb-1291-da717bc615a1@enterprisedb.com> Date: Sat, 17 Jun 2023 15:45:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Subject: Re: Incorrect estimation of HashJoin rows resulted from inaccurate small table statistics Content-Language: en-US To: Quan Zongliang , Tom Lane Cc: pgsql-hackers References: <83d056c0-c3eb-6ef7-bf3a-7e461fcde74d@yeah.net> <2463029.1686955597@sss.pgh.pa.us> <4660403b-8df9-2b44-2214-26b21f35b539@yeah.net> From: Tomas Vondra In-Reply-To: <4660403b-8df9-2b44-2214-26b21f35b539@yeah.net> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------7kxtMUWLVbAchXCgNufiHd1u Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 6/17/23 02:02, Quan Zongliang wrote: > > > On 2023/6/17 06:46, Tom Lane wrote: >> Quan Zongliang writes: >>> Perhaps we should discard this (dups cnt > 1) restriction? >> >> That's not going to happen on the basis of one test case that you >> haven't even shown us.  The implications of doing it are very unclear. >> In particular, I seem to recall that there are bits of logic that >> depend on the assumption that MCV entries always represent more than >> one row.  The nmultiple calculation Tomas referred to may be failing >> because of that, but I'm worried about there being other places. >> I don't recall any logic that'd outright fail with MCVs containing single-row groups, and I haven't noticed anything obvious in analyze.c during a cursory search. Maybe the paper analyze_mcv_list builds on makes some assumptions? Not sure. However, compute_distinct_stats() doesn't seem to have such protection against single-row MCV groups, so if that's wrong we kinda already have the issue I think (admittedly, compute_distinct_stats is much less used than compute_scalar_stats). > > The statistics for the other table look like this: > stadistinct | 6 > stanumbers1 | {0.50096667,0.49736667,0.0012} > stavalues1  | {v22,v23,v5} > > The value that appears twice in the small table (v1 and v2) does not > appear here. The stadistinct's true value is 18 instead of 6 (three > values in the small table do not appear here). > > When calculating the selectivity: > if (nd2 > sslot2->nvalues) >   totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2->nvalues); > > totalsel1 = 0 > nd2 = 21 > sslot2->nvalues = 2 > unmatchfreq1 = 0.99990002016420476 > otherfreq2 = 0.82608695328235626 > > result: totalsel1 = 0.043473913749706022 > rows = 0.043473913749706022 * 23 * 2,000,000 = 1999800 > Attached is a script reproducing this. I think the fundamental issue here is that the most common element of the large table - v22 (~50%) is not in the tiny one at all. IIRC the join estimation assumes the domain of one table is a subset of the other. The values 22 / 23 violate that assumption, unfortunately. Including all values into the small MCV fix this because then otherfreq1 = 0.0 and that simply eliminates the impact of stuff that didn't have a match between the two MCV lists. Which mitigates the violated assumption. But once the small table gets too large for the MCV, this won't work that well - it probably helps a bit, as it makes otherfreq1 smaller. Which doesn't mean it's useless, but it's likely a rare combination that a table is (and remains) smaller than MCV, and the large table contains values without a match in the smaller one (think foreign keys). > >> Basically, you're proposing a rather fundamental change in the rules >> by which Postgres has gathered statistics for decades.  You need to >> bring some pretty substantial evidence to support that.  The burden >> of proof is on you, not on the status quo. >> Right. It's a good example of a "quick hack" fixing one particular case, without considering the consequences on other cases too much. Good as a starting point, but plenty of legwork to do. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------7kxtMUWLVbAchXCgNufiHd1u Content-Type: application/sql; name="script.sql" Content-Disposition: attachment; filename="script.sql" Content-Transfer-Encoding: base64 Y3JlYXRlIHRhYmxlIHQxIChhIGludCk7Cmluc2VydCBpbnRvIHQxIHNlbGVjdCBpIGZyb20g Z2VuZXJhdGVfc2VyaWVzKDEsMjEpIHMoaSk7Cmluc2VydCBpbnRvIHQxIHZhbHVlcyAoMSks ICgyKTsKYW5hbHl6ZSB0MTsKCnNlbGVjdCBtb3N0X2NvbW1vbl92YWxzLCBtb3N0X2NvbW1v bl9mcmVxcyBmcm9tIHBnX3N0YXRzIHdoZXJlIHRhYmxlbmFtZSA9ICd0MSc7CgpjcmVhdGUg dGFibGUgdDIgKGEgaW50KTsKaW5zZXJ0IGludG8gdDIgc2VsZWN0IDIyIGZyb20gZ2VuZXJh dGVfc2VyaWVzKDEsMTAwMDAwKSBzKGkpOwppbnNlcnQgaW50byB0MiBzZWxlY3QgMjMgZnJv bSBnZW5lcmF0ZV9zZXJpZXMoMSwxMDAwMDApIHMoaSk7Cmluc2VydCBpbnRvIHQyIHNlbGVj dCA1IGZyb20gZ2VuZXJhdGVfc2VyaWVzKDEsMTAwMCkgcyhpKTsKYW5hbHl6ZSB0MjsKCnNl bGVjdCBtb3N0X2NvbW1vbl92YWxzLCBtb3N0X2NvbW1vbl9mcmVxcyBmcm9tIHBnX3N0YXRz IHdoZXJlIHRhYmxlbmFtZSA9ICd0Mic7CgpleHBsYWluIGFuYWx5emUgc2VsZWN0ICogZnJv bSB0MSBqb2luIHQyIHVzaW5nIChhKTsK --------------7kxtMUWLVbAchXCgNufiHd1u--