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 1m2tq1-00052J-89 for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 11:11:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m2tq0-0005m8-8Q for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 11:11:00 +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 1m2tpz-0005ly-VG for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 11:11:00 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m2tpw-0006wc-T6 for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 11:10:59 +0000 Received: from Egors-MacBook-Pro.local (unknown [46.188.123.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 601C021C936F; Mon, 12 Jul 2021 14:10:54 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1626088254; bh=AGq6ep+Fs+FIcNbhe8RJM5/CkKl0IAO7OJ7PgqJ/3UA=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=dPdnhxNLyMSSdOIH0cSS8APyNmt4+MqTEJcQW3hAEhsctorM0xn7GHycxuUihIMSX HRGrBoEAOwOhFvi1glHdZjtxeHVj5X9451Q0ofeanq4ubfmiValPgNB2kVUS6nWlXQ AuNK50Gdwhw5OkqdMn2eC+jpqu+im9WXgtKPpslg= Subject: Re: pg_stats and range statistics To: Soumyadeep Chakraborty , Tomas Vondra Cc: PostgreSQL Hackers References: <55c8ecd4-57d0-5c63-6fe3-1b81e8caea9d@enterprisedb.com> From: Egor Rogov Message-ID: Date: Mon, 12 Jul 2021 14:10:53 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, thanks for the review and corrections. On 11.07.2021 21:54, Soumyadeep Chakraborty wrote: > Hello, > > This should have been added with [1]. > > Excerpt from the documentation: > "pg_stats is also designed to present the information in a more readable > format than the underlying catalog — at the cost that its schema must > be extended whenever new slot types are defined for pg_statistic." [2] > > So, I added a reminder in pg_statistic.h. Good point. > Attached is v2 of this patch with some cosmetic changes. I wonder why "TODO: catalog version bump"? This patch doesn't change catalog structure, or I miss something? > Renamed the columns a > bit and updated the docs to be a bit more descriptive. > (range_length_empty_frac -> empty_range_frac, range_bounds_histogram -> > range_bounds_histograms) I intended to make the same prefix ("range_") for all columns concerned with range types, although I'm fine with the proposed naming. > One question: > > We do have the option of representing the histogram of lower bounds separately > from the histogram of upper bounds, as two separate view columns. Don't know if > there is much utility though and there is a fair bit of added complexity: see > below. Thoughts? I thought about it too, and decided not to transform the underlying data structure. As far as I can see, pg_stats never employed such transformations. For example, STATISTIC_KIND_DECHIST is an array containing the histogram followed by the average in its last element. It is shown in pg_stats.elem_count_histogram as is, although it arguably may be splitted into two fields. All in all, I believe pg_stats's job is to "unpack" stavalues and stanumbers into meaningful fields, and not to try to go deeper than that. > > My attempts via SQL (unnest -> lower|upper -> array_agg) were futile given > unnest does not play nice with anyarray. For instance: > > select unnest(stavalues1) from pg_statistic; > ERROR: cannot determine element type of "anyarray" argument > > Maybe the only option is to write a UDF pg_get_{lower|upper}_bounds_histogram > which can do something similar to what calc_hist_selectivity does: > > /* > * Convert histogram of ranges into histograms of its lower and upper > * bounds. > */ > nhist = hslot.nvalues; > hist_lower = (RangeBound *) palloc(sizeof(RangeBound) * nhist); > hist_upper = (RangeBound *) palloc(sizeof(RangeBound) * nhist); > for (i = 0; i < nhist; i++) > { > bool empty; > > range_deserialize(rng_typcache, DatumGetRangeTypeP(hslot.values[i]), > &hist_lower[i], &hist_upper[i], &empty); > /* The histogram should not contain any empty ranges */ > if (empty) > elog(ERROR, "bounds histogram contains an empty range"); > } > > This is looking good and ready. > > [1] https://github.com/postgres/postgres/commit/918eee0c497c88260a2e107318843c9b1947bc6f > [2] https://www.postgresql.org/docs/devel/view-pg-stats.html > > Regards, > Soumyadeep (VMware)