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 1lP6Wx-0001zV-9q for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Mar 2021 16:38:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lP6Wv-0000KE-Ks for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Mar 2021 16:38:49 +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 1lP6Wv-0000K6-9L for pgsql-hackers@lists.postgresql.org; Wed, 24 Mar 2021 16:38:49 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lP6Wn-0007N7-Ro for pgsql-hackers@postgresql.org; Wed, 24 Mar 2021 16:38:47 +0000 Received: by mail-ed1-x530.google.com with SMTP id j3so28340592edp.11 for ; Wed, 24 Mar 2021 09:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-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=biKrcfJPgE3lq8Gh1GCGhxXgiJDUb0tknXUS5gexLFM=; b=WpXI46CRipvBQCAcRDEEW/7cWNRY/XVUjLfCsrUMVu/PsuofJltATe8ncosI2jcBGd zNF3RTVPIQihaSmQ+MgUaWUl5BJo/1PZHaOULId/e+PVKgs1JKV53TN/B2d28yQOSprs JTkWE/ZMq846dQjiFXkzpPfWmLfAIwdu8N6e46sSLGquwMX65PvfFvebooInsnj0jep8 nvN5iFpbBR7vS/21kNMiDqr2oj9DyvHayQTiIDWvsj+VYJxqQIODGgQMgmRFSDh8Oza4 QxSGlEkPDsX3PPANyKqZHqKP3GGcKEAndIu6c36evZBg9MbElqRGGirFrerQvp4xR/mt hu9g== 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=biKrcfJPgE3lq8Gh1GCGhxXgiJDUb0tknXUS5gexLFM=; b=l7F/XeBrw5PzPTQiM7Qi028/p36ESGy4a3heX/gLJO+bPUJ934j/kMOWWNqS6rAnNk fb6/sXuh4suQstc7k1FnrSIx93XwKEB5MtEkZ+Vqh6URttiWEYmiwfWv5SpzRs2mojcf gROtr1MQaCcs37s6r6qKnWto+WgqqnP7YGS7lZVrSM1P/DDO+7XvX84X8z+y3DzWclUw RceFQ2OD++Yo/uj6kiSCsqzzvkQkjR61f5KrtTOGFZDwRW9pKQfPrrXM++Ly5Dhwo7iB s0Y3UbgFLSWwGF1+fDvJSpBO9XphyR8Q5Wd/qq7b8bw7LdM4n7x2wNGgw6cf9k9LP+at ihbg== X-Gm-Message-State: AOAM5313LtU6oBTdciR2jxx27VKE0k1UDL9qjr0d6/RPoZhPzUfSJO2f 8y37q0Qf7RiuCAcgA3ZPdeda8f/IwfokCOwR9IM4iXZXf6LelTMC+P0MVeEu+tUpYFhdUJBGKtB Top3aPV+h/NFVd7GbiCVRqfOcSe8NY9v9TGtlUS6J798y5Of3epqH/QmulrZXMytSVlRLrDOFP2 ckXU8rBphm3z/uxrtFmpxmE7qA7IW4JEm3gzlIeSdcBYu/2X3FNA/V8NvGqoRu4axybpzY8d0VX 0TT/GEz+Veh8hjJiC3wNURy3L843Peti4vk6w== X-Google-Smtp-Source: ABdhPJxGcw8PpeEkqkoyKIWuNgwFGKjrbRHlZmtOti86iyNaYHsQLoN3wKoHh/I0jkWDnqB6/P3Jfg== X-Received: by 2002:a05:6402:1d1a:: with SMTP id dg26mr4333321edb.266.1616603919523; Wed, 24 Mar 2021 09:38:39 -0700 (PDT) Received: from [10.137.0.20] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id s6sm1149553ejx.83.2021.03.24.09.38.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Mar 2021 09:38:38 -0700 (PDT) Subject: Re: PoC/WIP: Extended statistics on expressions To: Justin Pryzby Cc: Dean Rasheed , pgsql-hackers@postgresql.org References: <1913c1fe-13da-7250-adf4-befeda397536@enterprisedb.com> <9f6654b1-ca31-f979-0fe8-137683818273@enterprisedb.com> <20210324062446.GA28335@telsasoft.com> From: Tomas Vondra Message-ID: Date: Wed, 24 Mar 2021 17:38:32 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20210324062446.GA28335@telsasoft.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/24/21 7:24 AM, Justin Pryzby wrote: > Most importantly, it looks like this forgets to update catalog documentation > for stxexprs and stxkind='e' > Good catch. > It seems like you're preferring to use pluralized "statistics" in a lot of > places that sound wrong to me. For example: >> Currently the first statistics wins, which seems silly. > I can write more separately, but I think this is resolved and clarified if you > write "statistics object" and not just "statistics". > OK "statistics object" seems better and more consistent. >> + Name of schema containing table > > I don't know about the nearby descriptions, but this one sounds too much like a > "schema-containing" table. Say "Name of the schema which contains the table" ? > I think the current spelling is OK / consistent with the other catalogs. >> + Name of table > > Say "name of table on which the extended statistics are defined" > I've used "Name of table the statistics object is defined on". >> + Name of extended statistics > > "Name of the extended statistic object" > >> + Owner of the extended statistics > > ..object > OK >> + Expression the extended statistics is defined on > > I think it should say "the extended statistic", or "the extended statistics > object". Maybe "..on which the extended statistic is defined" > OK >> + of random access to the disk. (This expression is null if the expression >> + data type does not have a < operator.) > > expression's data type > OK >> + much-too-small row count estimate in the first two queries. Moreover, the > > maybe say "dramatically underestimates the rowcount" > I've changed this to "... results in a significant underestimate of row count". >> + planner has no information about relationship between the expressions, so it > > the relationship > OK >> + assumes the two WHERE and GROUP BY >> + conditions are independent, and multiplies their selectivities together to >> + arrive at a much-too-high group count estimate in the aggregate query. > > severe overestimate ? > OK >> + This is further exacerbated by the lack of accurate statistics for the >> + expressions, forcing the planner to use default ndistinct estimate for the > > use *a default > OK >> + expression derived from ndistinct for the column. With such statistics, the >> + planner recognizes that the conditions are correlated and arrives at much >> + more accurate estimates. > > are correlated comma > OK >> + if (type->lt_opr == InvalidOid) > > These could be !OidIsValid > Maybe, but it's like this already. I'll leave this alone and then fix/backpatch separately. >> + * expressions. It's either expensive or very easy to defeat for >> + * determined used, and there's no risk if we allow such statistics (the >> + * statistics is useless, but harmless). > > I think it's meant to say "for a determined user" ? > Right. >> + * If there are no simply-referenced columns, give the statistics an auto >> + * dependency on the whole table. In most cases, this will be redundant, >> + * but it might not be if the statistics expressions contain no Vars >> + * (which might seem strange but possible). >> + */ >> + if (!nattnums) >> + { >> + ObjectAddressSet(parentobject, RelationRelationId, relid); >> + recordDependencyOn(&myself, &parentobject, DEPENDENCY_AUTO); >> + } > > Can this be unconditional ? > What would be the benefit? This behavior copied from index_create, so I'd prefer keeping it the same for consistency reason. Presumably it's like that for some reason (a bit of cargo cult programming, I know). >> + * Translate the array of indexs to regular attnums for the dependency (we > > sp: indexes > OK >> + * Not found a matching expression, so we can simply skip > > Found no matching expr > OK >> + /* if found a matching, */ > > matching .. > Matching dependency. >> +examine_attribute(Node *expr) > > Maybe you should rename this to something distinct ? So it's easy to add a > breakpoint there, for example. > What would be a better name? It's not difficult to add a breakpoint using line number, for example. >> + stats->anl_context = CurrentMemoryContext; /* XXX should be using >> + * something else? */ > >> + bool nulls[Natts_pg_statistic]; > ... >> + * Construct a new pg_statistic tuple >> + */ >> + for (i = 0; i < Natts_pg_statistic; ++i) >> + { >> + nulls[i] = false; >> + } > > Shouldn't you just write nulls[Natts_pg_statistic] = {false}; > or at least: memset(nulls, 0, sizeof(nulls)); > Maybe, but it's a copy of what update_attstats() does, so I prefer keeping it the same. >> + * We don't store collations used to build the statistics, but >> + * we can use the collation for the attribute itself, as >> + * stored in varcollid. We do reset the statistics after a >> + * type change (including collation change), so this is OK. We >> + * may need to relax this after allowing extended statistics >> + * on expressions. > > This text should be updated or removed ? > Yeah, the last sentence is obsolete. Updated. >> @@ -2705,7 +2705,108 @@ describeOneTableDetails(const char *schemaname, >> } >> >> /* print any extended statistics */ >> - if (pset.sversion >= 100000) >> + if (pset.sversion >= 140000) >> + { >> + printfPQExpBuffer(&buf, >> + "SELECT oid, " >> + "stxrelid::pg_catalog.regclass, " >> + "stxnamespace::pg_catalog.regnamespace AS nsp, " >> + "stxname,\n" >> + "pg_get_statisticsobjdef_columns(oid) AS columns,\n" >> + " 'd' = any(stxkind) AS ndist_enabled,\n" >> + " 'f' = any(stxkind) AS deps_enabled,\n" >> + " 'm' = any(stxkind) AS mcv_enabled,\n"); >> + >> + if (pset.sversion >= 130000) >> + appendPQExpBufferStr(&buf, " stxstattarget\n"); >> + else >> + appendPQExpBufferStr(&buf, " -1 AS stxstattarget\n"); > > >= 130000 is fully determined by >= 14000 :) > Ah, right. >> + * type of the opclass, which is not interesting for our purposes. (Note: >> + * if we did anything with non-expression index columns, we'd need to > > index is wrong ? > Fixed > I mentioned a bunch of other references to "index" and "predicate" which are > still around: > Whooops, sorry. Fixed. I'll post a cleaned-up version of the patch addressing Dean's review comments too. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company