Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bVRdX-0006Tr-T0 for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Aug 2016 23:01:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bVRdX-0001wX-FO for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Aug 2016 23:01:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bVRdW-0001wI-RI for pgsql-hackers@postgresql.org; Thu, 04 Aug 2016 23:01:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bVRdT-0006q3-PI for pgsql-hackers@postgresql.org; Thu, 04 Aug 2016 23:01:10 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u74N16NL018488; Thu, 4 Aug 2016 19:01:06 -0400 From: Tom Lane To: pgsql-hackers@postgresql.org cc: Andreas Joseph Krogh Subject: Re: Bogus ANALYZE results for an otherwise-unique column with many nulls In-reply-to: <16143.1470350371@sss.pgh.pa.us> References: <16143.1470350371@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 04 Aug 2016 18:39:31 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <18434.1470351650.0@sss.pgh.pa.us> Date: Thu, 04 Aug 2016 19:01:06 -0400 Message-ID: <18487.1470351666@sss.pgh.pa.us> X-Pg-Spam-Score: -3.1 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <18434.1470351650.1@sss.pgh.pa.us> I wrote: > Looking around, there are a couple of places outside commands/analyze.c > that are making the same mistake, so this patch isn't complete, but it > illustrates what needs to be done. Here's a more complete patch. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="analyze-unique-with-nulls-2.patch"; charset="us-ascii" Content-ID: <18434.1470351650.2@sss.pgh.pa.us> Content-Description: analyze-unique-with-nulls-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 5fcedd7..9ac7122 100644 *** a/src/backend/commands/analyze.c --- b/src/backend/commands/analyze.c *************** compute_distinct_stats(VacAttrStatsP sta *** 2049,2056 **** =20=20 if (nmultiple =3D=3D 0) { ! /* If we found no repeated values, assume it's a unique column */ ! stats->stadistinct =3D -1.0; } else if (track_cnt < track_max && toowide_cnt =3D=3D 0 && nmultiple =3D=3D track_cnt) --- 2049,2059 ---- =20=20 if (nmultiple =3D=3D 0) { ! /* ! * If we found no repeated non-null values, assume it's a unique ! * column; but be sure to discount for any nulls we found. ! */ ! stats->stadistinct =3D -1.0 * (1.0 - stats->stanullfrac); } else if (track_cnt < track_max && toowide_cnt =3D=3D 0 && nmultiple =3D=3D track_cnt) *************** compute_scalar_stats(VacAttrStatsP stats *** 2426,2433 **** =20=20 if (nmultiple =3D=3D 0) { ! /* If we found no repeated values, assume it's a unique column */ ! stats->stadistinct =3D -1.0; } else if (toowide_cnt =3D=3D 0 && nmultiple =3D=3D ndistinct) { --- 2429,2439 ---- =20=20 if (nmultiple =3D=3D 0) { ! /* ! * If we found no repeated non-null values, assume it's a unique ! * column; but be sure to discount for any nulls we found. ! */ ! stats->stadistinct =3D -1.0 * (1.0 - stats->stanullfrac); } else if (toowide_cnt =3D=3D 0 && nmultiple =3D=3D ndistinct) { *************** compute_scalar_stats(VacAttrStatsP stats *** 2753,2759 **** else stats->stawidth =3D stats->attrtype->typlen; /* Assume all too-wide values are distinct, so it's a unique column */ ! stats->stadistinct =3D -1.0; } else if (null_cnt > 0) { --- 2759,2765 ---- else stats->stawidth =3D stats->attrtype->typlen; /* Assume all too-wide values are distinct, so it's a unique column */ ! stats->stadistinct =3D -1.0 * (1.0 - stats->stanullfrac); } else if (null_cnt > 0) { diff --git a/src/backend/tsearch/ts_typanalyze.c b/src/backend/tsearch/ts_t= ypanalyze.c index 0f851ea..817453c 100644 *** a/src/backend/tsearch/ts_typanalyze.c --- b/src/backend/tsearch/ts_typanalyze.c *************** compute_tsvector_stats(VacAttrStats *sta *** 295,301 **** stats->stawidth =3D total_width / (double) nonnull_cnt; =20=20 /* Assume it's a unique column (see notes above) */ ! stats->stadistinct =3D -1.0; =20=20 /* * Construct an array of the interesting hashtable items, that is, --- 295,301 ---- stats->stawidth =3D total_width / (double) nonnull_cnt; =20=20 /* Assume it's a unique column (see notes above) */ ! stats->stadistinct =3D -1.0 * (1.0 - stats->stanullfrac); =20=20 /* * Construct an array of the interesting hashtable items, that is, diff --git a/src/backend/utils/adt/rangetypes_typanalyze.c b/src/backend/ut= ils/adt/rangetypes_typanalyze.c index fcb71d3..56504fc 100644 *** a/src/backend/utils/adt/rangetypes_typanalyze.c --- b/src/backend/utils/adt/rangetypes_typanalyze.c *************** compute_range_stats(VacAttrStats *stats, *** 203,209 **** /* Do the simple null-frac and width stats */ stats->stanullfrac =3D (double) null_cnt / (double) samplerows; stats->stawidth =3D total_width / (double) non_null_cnt; ! stats->stadistinct =3D -1.0; =20=20 /* Must copy the target values into anl_context */ old_cxt =3D MemoryContextSwitchTo(stats->anl_context); --- 203,211 ---- /* Do the simple null-frac and width stats */ stats->stanullfrac =3D (double) null_cnt / (double) samplerows; stats->stawidth =3D total_width / (double) non_null_cnt; !=20 ! /* Estimate that non-null values are unique */ ! stats->stadistinct =3D -1.0 * (1.0 - stats->stanullfrac); =20=20 /* Must copy the target values into anl_context */ old_cxt =3D MemoryContextSwitchTo(stats->anl_context); diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfu= ncs.c index cc2a9a1..56943f2 100644 *** a/src/backend/utils/adt/selfuncs.c --- b/src/backend/utils/adt/selfuncs.c *************** double *** 4738,4743 **** --- 4738,4744 ---- get_variable_numdistinct(VariableStatData *vardata, bool *isdefault) { double stadistinct; + double stanullfrac =3D 0.0; double ntuples; =20=20 *isdefault =3D false; *************** get_variable_numdistinct(VariableStatDat *** 4745,4751 **** /* * Determine the stadistinct value to use. There are cases where we can * get an estimate even without a pg_statistic entry, or can get a better ! * value than is in pg_statistic. */ if (HeapTupleIsValid(vardata->statsTuple)) { --- 4746,4753 ---- /* * Determine the stadistinct value to use. There are cases where we can * get an estimate even without a pg_statistic entry, or can get a better ! * value than is in pg_statistic. Grab stanullfrac too if we can find it ! * (otherwise, assume no nulls, for lack of any better idea). */ if (HeapTupleIsValid(vardata->statsTuple)) { *************** get_variable_numdistinct(VariableStatDat *** 4754,4759 **** --- 4756,4762 ---- =20=20 stats =3D (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); stadistinct =3D stats->stadistinct; + stanullfrac =3D stats->stanullfrac; } else if (vardata->vartype =3D=3D BOOLOID) { *************** get_variable_numdistinct(VariableStatDat *** 4777,4783 **** { case ObjectIdAttributeNumber: case SelfItemPointerAttributeNumber: ! stadistinct =3D -1.0; /* unique */ break; case TableOidAttributeNumber: stadistinct =3D 1.0; /* only 1 value */ --- 4780,4786 ---- { case ObjectIdAttributeNumber: case SelfItemPointerAttributeNumber: ! stadistinct =3D -1.0; /* unique (and all non null) */ break; case TableOidAttributeNumber: stadistinct =3D 1.0; /* only 1 value */ *************** get_variable_numdistinct(VariableStatDat *** 4799,4808 **** * If there is a unique index or DISTINCT clause for the variable, assume * it is unique no matter what pg_statistic says; the statistics could be * out of date, or we might have found a partial unique index that proves ! * the var is unique for this query. */ if (vardata->isunique) ! stadistinct =3D -1.0; =20=20 /* * If we had an absolute estimate, use that. --- 4802,4812 ---- * If there is a unique index or DISTINCT clause for the variable, assume * it is unique no matter what pg_statistic says; the statistics could be * out of date, or we might have found a partial unique index that proves ! * the var is unique for this query. However, we'd better still believe ! * the null-fraction statistic. */ if (vardata->isunique) ! stadistinct =3D -1.0 * (1.0 - stanullfrac); =20=20 /* * If we had an absolute estimate, use that. ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--