stawidth inconsistency with all NULL columns

From: Joe Conway <mail(at)joeconway(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: stawidth inconsistency with all NULL columns
Date: 2019-05-21 19:48:37
Message-ID: bacc32db-3776-b7b9-d5f4-622e67653bf7@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Consider:

CREATE TABLE testwid
(
txtnotnull text,
txtnull text,
int8notnull int8,
int8null int8
);
INSERT INTO testwid
SELECT 'a' || g.i,
NULL,
g.i,
NULL
FROM generate_series(1,10000) AS g(i);
ANALYZE testwid;
SELECT attname, avg_width FROM pg_stats WHERE tablename = 'testwid';
attname | avg_width
-------------+-----------
txtnotnull | 5
txtnull | 0
int8notnull | 8
int8null | 8
(4 rows)

I see in analyze.c
8<-----------------
/* We can only compute average width if we found some non-null values.*/
if (nonnull_cnt > 0)

[snip]

else if (null_cnt > 0)
{
/* We found only nulls; assume the column is entirely null */
stats->stats_valid = true;
stats->stanullfrac = 1.0;
if (is_varwidth)
stats->stawidth = 0; /* "unknown" */
else
stats->stawidth = stats->attrtype->typlen;
stats->stadistinct = 0.0; /* "unknown" */
}
8<-----------------

So apparently intentional, but seems gratuitously inconsistent. Could
this cause any actual inconsistent behaviors? In any case that first
comment does not reflect the code.

Joe
--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2019-05-21 19:55:05 Re: stawidth inconsistency with all NULL columns
Previous Message Bruce Momjian 2019-05-21 19:47:34 Re: PG 12 draft release notes