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 1lOwwq-0004HP-O0 for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Mar 2021 06:24:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lOwwp-00046a-GQ for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Mar 2021 06:24:55 +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 1lOwwp-00046R-5W for pgsql-hackers@lists.postgresql.org; Wed, 24 Mar 2021 06:24:55 +0000 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lOwwk-0005iv-7x for pgsql-hackers@postgresql.org; Wed, 24 Mar 2021 06:24:54 +0000 Received: by mail-il1-x132.google.com with SMTP id d10so13633887ils.5 for ; Tue, 23 Mar 2021 23:24:50 -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=JWbuSTl9opRL2+P3/1FHxtRhTAaKSj86tsRcLE1ogLM=; b=CFqYUU7JTrt5GonoxZ9dmfDIpbFHbR2ykBHVXpN6wBUAKl0YZAd2mev6oFHregGjmC 6+KH9/4nvSISSrKPVEIPN1XnYwORPcIUxz01d/3a1JnW70DR2OoLYW0V0ZLinuVXAkfx UIuxYygHnI5qW64afkSZiqOZpaBvNyX9cM4PYSAjUy40c2qnJGaFOsD21dDknWsghmqF kSwVn5jRa4x4LHmQng/PFKEXs/wrqURMkAtb52jEB5aJCGaojdrsXV19cePLj69VvcYE RY6Lq+NFPpr2K1rh7R34oxptsH3nF16AcSS1lAdX4jtezWEaXvgtoh6jQ5WsFskaeeO6 UuJg== 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=JWbuSTl9opRL2+P3/1FHxtRhTAaKSj86tsRcLE1ogLM=; b=cglHU8aYFSiQmm5V6NddwcMXDt7fh8O3/L4OB/VecqvD/nqtGQpMBQWcFjYDxCPD3C WuNOCyeaGfnzvAkX3IzTOwQLjQVqCWGwF1N4qsQbkvJsICywurIrAlHG09VAEUdhCDQy ILeYl6kZP4Zw1kx4fKcxgerNkEuyq+V1tMJcmdACoGHAOClBiDswxWEW9/nD3E9On24s GKXu+4ygYfFQrRMS/vf3JEveXFMtwfWjxNmGjEJ5XAw+HpMj2+5OKKgdYQabT9Z45n0p e70GtSsvDF4EI2fIWp71QH9J68/pdvtFFdF6ebGXi5615JtnfDCblauHjbw+v589mJof j/9Q== X-Gm-Message-State: AOAM532FM8HgFRcbPkcSVvO9urOg3xSe5maaVP55VBmu5zTn/XN4bHZd +8NXdr3k/dcNV3Ug7KM2yj4hHXMbJJgQuA== X-Google-Smtp-Source: ABdhPJy6+kniIxaRe5Sv5gFLGeHF+N+bSVPGV1uWAoly5f2JdYEhj4RC0KIuMHdFIC/nI/zJcbCVPQ== X-Received: by 2002:a05:6e02:1688:: with SMTP id f8mr1449777ila.110.1616567088638; Tue, 23 Mar 2021 23:24:48 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id s5sm641399ild.13.2021.03.23.23.24.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Mar 2021 23:24:48 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 288B7800897; Wed, 24 Mar 2021 01:24:47 -0500 (CDT) Date: Wed, 24 Mar 2021 01:24:47 -0500 From: Justin Pryzby To: Tomas Vondra Cc: Dean Rasheed , pgsql-hackers@postgresql.org Subject: Re: PoC/WIP: Extended statistics on expressions Message-ID: <20210324062446.GA28335@telsasoft.com> References: <1913c1fe-13da-7250-adf4-befeda397536@enterprisedb.com> <9f6654b1-ca31-f979-0fe8-137683818273@enterprisedb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9f6654b1-ca31-f979-0fe8-137683818273@enterprisedb.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Most importantly, it looks like this forgets to update catalog documentation for stxexprs and stxkind='e' 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". > + 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" ? > + Name of table Say "name of table on which the extended statistics are defined" > + Name of extended statistics "Name of the extended statistic object" > + Owner of the extended statistics ..object > + 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" > + of random access to the disk. (This expression is null if the expression > + data type does not have a < operator.) expression's data type > + much-too-small row count estimate in the first two queries. Moreover, the maybe say "dramatically underestimates the rowcount" > + planner has no information about relationship between the expressions, so it the relationship > + 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 ? > + 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 > + 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 > + if (type->lt_opr == InvalidOid) These could be !OidIsValid > + * 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" ? > + * 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 ? > + * Translate the array of indexs to regular attnums for the dependency (we sp: indexes > + * Not found a matching expression, so we can simply skip Found no matching expr > + /* if found a matching, */ matching .. > +examine_attribute(Node *expr) Maybe you should rename this to something distinct ? So it's easy to add a breakpoint there, 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)); > + * 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 ? > @@ -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 :) > + * 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 ? I mentioned a bunch of other references to "index" and "predicate" which are still around: On Thu, Jan 07, 2021 at 08:35:37PM -0600, Justin Pryzby wrote: > There's some remaining copy/paste stuff from index expressions: > > errmsg("statistics expressions and predicates can refer only to the table being indexed"))); > left behind by evaluating the predicate or index expressions. > Set up for predicate or expression evaluation > Need an EState for evaluation of index expressions and > partial-index predicates. Create it in the per-index context to be > Fetch function for analyzing index expressions.