Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0XfL-0006eY-5n for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 19:20:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0XfK-0005Aq-9j for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 19:20:22 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0XcN-0008QA-3E for pgsql-hackers@lists.postgresql.org; Mon, 26 Mar 2018 19:17:19 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0XcJ-0006zO-O9 for pgsql-hackers@postgresql.org; Mon, 26 Mar 2018 19:17:17 +0000 Received: by mail-wm0-x243.google.com with SMTP id l9so17511179wmh.2 for ; Mon, 26 Mar 2018 12:17:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=VmTtBXMvyRQwJSc7L7fDjYuiA/4N8iI+J2eidnrLelk=; b=MFiaz+pxI7xkGHlhgDs/rm+n3zrL4JV7x6C92oCbTmLi3I10qcNqq10cljBV5GAGQl 9KVGlGloh2Ttwowu0nW8lTZ+tql5CdeNZONQ4ENXcoISp0FieV3cLlKS3Zl8gF+mrYOL 0cbJ0uyUGyZ4e8enNrqdTSuTkxf89E5zqePg3jpEse6Tt3lWx6I7cRAiu2sbq2B1gzEy Pvax0UXZ4sxIH9LUWz3n5MvD1+KwVmB6OMXlhDHZC17FqQ/yNOa2dKQot14eH5qHU7j7 P6jRU9NJqWtfy2GLQ5Vtb+5EdnjP5wKg3j/mSNXTgGVrx3Z0W1GpnyGq1jlj3g+vBGe4 dDyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=VmTtBXMvyRQwJSc7L7fDjYuiA/4N8iI+J2eidnrLelk=; b=WCwAdB57p4sjo8NIKEKnYIefHm24MhEczQGUMrSqTlCXjJ8jHNsYP9glCmP3g7DxaX rbLLZKaQhonkxwG31vB8u0kh6saDT2yjb0XDvILSvRmTsdfe36JaCld+kxIXvkIjVxkd TxPIx4e7nQIvco1vN+1gABW64uEvPkA7VPIg16h1Ol5SflXNyHVeDoArt1rTV7eQJd6l gJTtjDUCeoziQwDWayGJUUitNOaSkVHGg4cG6ExKagJCpUYEnayIsfcnH8TuqR5In1jN e8BORapOwBKC1+UQw68MFEFSdGOaWsPEMfz6vSD5Ag5jAHfphn89s8JwxNxqNxzsR7RY j76A== X-Gm-Message-State: AElRT7FYMb2Nleg0ZTznQraQrSCnyIjqohULfSbaff7S2fAcs2BFgtx7 zwFbllJ+djw+gzk5eOmtdrJG10VIayJUfJF59wibmsN3ut2wBfzTz31jMRMzYG8QGNyXDk5MjUy kUtAsvGc+gtqTVPCDZq6Ol9ZTfcLQyQeqhHjPOiqcOOHKtCaYutH3vOOYIWqFNg99pxHZ5x4YXF Dl53EK/eGb X-Google-Smtp-Source: AG47ELsBW9RVK2dPvS3VtQqhdxZZ2X9yLgnCziWmhn6AkruoNfo3MlYn71Z/AdxHg0/7Fws5sSvoIA== X-Received: by 10.28.179.133 with SMTP id c127mr12334245wmf.24.1522091833596; Mon, 26 Mar 2018 12:17:13 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id l1sm14837059wmh.25.2018.03.26.12.17.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Mar 2018 12:17:12 -0700 (PDT) Subject: Re: [HACKERS] PATCH: multivariate histograms and MCV lists To: Dean Rasheed Cc: Alvaro Herrera , Andres Freund , Thomas Munro , Mark Dilger , Adrien Nayrat , Pg Hackers References: <20180310171906.gd6vyz6hnou2n3h7@alvherre.pgsql> <850c3b6b-2641-496c-4efc-500c720f7260@2ndquadrant.com> From: Tomas Vondra Message-ID: <66ba9c45-3b07-e5b6-fd11-15e75f6ee8e1@2ndquadrant.com> Date: Mon, 26 Mar 2018 21:17:09 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/26/2018 09:01 PM, Dean Rasheed wrote: > On 18 March 2018 at 23:57, Tomas Vondra wrote: >> Attached is an updated version of the patch series, addressing issues >> pointed out by Alvaro. > > I've just been reading the new code in > statext_clauselist_selectivity() and mcv_clauselist_selectivity(), and > I'm having a hard time convincing myself that it's correct. > > This code in statext_clauselist_selectivity() looks a bit odd: > > /* > * Evaluate the MCV selectivity. See if we got a full match and the > * minimal selectivity. > */ > if (stat->kind == STATS_EXT_MCV) > s1 = mcv_clauselist_selectivity(root, stat, clauses, varRelid, > jointype, sjinfo, rel, > &fullmatch, &mcv_lowsel); > > /* > * If we got a full equality match on the MCV list, we're done (and the > * estimate is likely pretty good). > */ > if (fullmatch && (s1 > 0.0)) > return s1; > > /* > * If it's a full match (equalities on all columns) but we haven't found > * it in the MCV, then we limit the selectivity by frequency of the last > * MCV item. Otherwise reset it to 1.0. > */ > if (!fullmatch) > mcv_lowsel = 1.0; > > return Min(s1, mcv_lowsel); > > So if fullmatch is true and s1 is greater than 0, it will return s1. > If fullmatch is true and s1 equals 0, it will return Min(s1, > mcv_lowsel) which will also be s1. If fullmatch is false, mcv_lowsel > will be set to 1 and it will return Min(s1, mcv_lowsel) which will > also be s1. So it always just returns s1, no? Maybe there's no point > in computing fullmatch. > Hmmm, I think you're right. It probably got broken in the last rebase, when I moved a bunch of code from the histogram part to the MCV one. I'll take a look. > Also, wouldn't mcv_lowsel potentially be a significant overestimate > anyway? Perhaps 1 minus the sum of the MCV frequencies might be > closer, but even that ought to take into account the number of > distinct values remaining, although that information may not always be > available. > That is definitely true. 1 minus the sum of the MCV frequencies, and I suppose we might even improve that if we had some ndistinct estimate on those columns to compute an average. > Also, just above that, in statext_clauselist_selectivity(), it > computes the list stat_clauses, then doesn't appear to use it > anywhere. I think that would have been the appropriate thing to pass > to mcv_clauselist_selectivity(). Otherwise, passing unrelated clauses > into mcv_clauselist_selectivity() will cause it to fail to find any > matches and then underestimate. > Will check. > I've also come across a few incorrect/out-of-date comments: > > /* > * mcv_clauselist_selectivity > * Return the estimated selectivity of the given clauses using MCV list > * statistics, or 1.0 if no useful MCV list statistic exists. > */ > > -- I can't see any code path that returns 1.0 if there are no MCV > stats. The last part of that comment is probably more appropriate to > statext_clauselist_selectivity() > > > /* > * mcv_update_match_bitmap > * [snip] > * The function returns the number of items currently marked as 'match', and > * ... > > -- it doesn't seem to return the number of items marked as 'match'. > > Then inside that function, this comment is wrong (copied from the > preceding comment): > > /* AND clauses assume nothing matches, initially */ > memset(bool_matches, STATS_MATCH_FULL, sizeof(char) * > mcvlist->nitems); > > Still reading... > > Regards, > Dean > Yeah, sorry about that - I forgot to fix those comments after removing the match counting to simplify the patches. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services