Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dGtuA-0002zH-4Q for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Jun 2017 21:14:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dGtu9-0005Ku-LD for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Jun 2017 21:14:45 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dGtu8-0005KP-SE for pgsql-bugs@postgresql.org; Fri, 02 Jun 2017 21:14:45 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dGtu5-000057-Tu for pgsql-bugs@postgresql.org; Fri, 02 Jun 2017 21:14:43 +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 v52LEdmm025541; Fri, 2 Jun 2017 17:14:40 -0400 From: Tom Lane To: marko@joh.to cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14676: neqsel is NULL dumb In-reply-to: <4698.1496334173@sss.pgh.pa.us> References: <20170529153847.4275.95416@wrigleys.postgresql.org> <4698.1496334173@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 01 Jun 2017 12:22:53 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <25521.1496438043.0@sss.pgh.pa.us> Date: Fri, 02 Jun 2017 17:14:39 -0400 Message-ID: <25540.1496438079@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25521.1496438043.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable I wrote: > marko@joh.to writes: >> This seems to be because neqsel() doesn't take at all into account that = both >> operators will exclude NULL rows, and does a simple 1.0 - eqsel(). > Yeah, that's clearly broken. A localized fix would be to re-fetch the > nullfrac statistic and subtract it off, but that seems pretty inefficient. > I'd be inclined to refactor things so that eqsel() and neqsel() call a > common routine that takes a "bool negate" argument, about like the way the > patternsel() functions work, and then the common routine could handle > the nullfrac correctly for both cases. Here's a proposed patch for that. I also fixed patternsel, but elected not to mess with neqjoinsel; partly because I think joining on inequality is so rare as to not be worth sweating over, and partly because I wasn't too sure what's the appropriate correction, especially for semijoins. Although this is clearly a bug fix, I'm leaning towards committing it only in HEAD; given the lack of previous field complaints, I fear back-patching might yield more complaints about destabilizing plans than kudos for fixing things. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-neqsel-and-patternsel-for-nulls.patch"; charset="us-ascii" Content-ID: <25521.1496438043.2@sss.pgh.pa.us> Content-Description: fix-neqsel-and-patternsel-for-nulls.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfu= ncs.c index 6e491bb..52d0e48 100644 *** a/src/backend/utils/adt/selfuncs.c --- b/src/backend/utils/adt/selfuncs.c *************** *** 154,165 **** get_relation_stats_hook_type get_relation_stats_hook =3D NULL; get_index_stats_hook_type get_index_stats_hook =3D NULL; =20=20 static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, ! bool varonleft); static double var_eq_non_const(VariableStatData *vardata, Oid operator, Node *other, ! bool varonleft); static double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, FmgrInfo *opproc, bool isgt, --- 154,166 ---- get_relation_stats_hook_type get_relation_stats_hook =3D NULL; get_index_stats_hook_type get_index_stats_hook =3D NULL; =20=20 + static double eqsel_internal(PG_FUNCTION_ARGS, bool negate); static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, ! bool varonleft, bool negate); static double var_eq_non_const(VariableStatData *vardata, Oid operator, Node *other, ! bool varonleft, bool negate); static double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, FmgrInfo *opproc, bool isgt, *************** static List *add_predicate_to_quals(Inde *** 227,232 **** --- 228,242 ---- Datum eqsel(PG_FUNCTION_ARGS) { + PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, false)); + } +=20 + /* + * Common code for eqsel() and neqsel() + */ + static double + eqsel_internal(PG_FUNCTION_ARGS, bool negate) + { PlannerInfo *root =3D (PlannerInfo *) PG_GETARG_POINTER(0); Oid operator =3D PG_GETARG_OID(1); List *args =3D (List *) PG_GETARG_POINTER(2); *************** eqsel(PG_FUNCTION_ARGS) *** 237,248 **** double selec; =20=20 /* * If expression is not variable =3D something or something =3D variable= , then * punt and return a default estimate. */ if (!get_restriction_variable(root, args, varRelid, &vardata, &other, &varonleft)) ! PG_RETURN_FLOAT8(DEFAULT_EQ_SEL); =20=20 /* * We can do a lot better if the something is a constant. (Note: the --- 247,272 ---- double selec; =20=20 /* + * When asked about <>, we do the estimation using the corresponding =3D + * operator, then convert to <> via "1.0 - eq_selectivity - nullfrac". + */ + if (negate) + { + operator =3D get_negator(operator); + if (!OidIsValid(operator)) + { + /* Use default selectivity (should we raise an error instead?) */ + return 1.0 - DEFAULT_EQ_SEL; + } + } +=20 + /* * If expression is not variable =3D something or something =3D variable= , then * punt and return a default estimate. */ if (!get_restriction_variable(root, args, varRelid, &vardata, &other, &varonleft)) ! return negate ? (1.0 - DEFAULT_EQ_SEL) : DEFAULT_EQ_SEL; =20=20 /* * We can do a lot better if the something is a constant. (Note: the *************** eqsel(PG_FUNCTION_ARGS) *** 253,266 **** selec =3D var_eq_const(&vardata, operator, ((Const *) other)->constvalue, ((Const *) other)->constisnull, ! varonleft); else selec =3D var_eq_non_const(&vardata, operator, other, ! varonleft); =20=20 ReleaseVariableStats(vardata); =20=20 ! PG_RETURN_FLOAT8((float8) selec); } =20=20 /* --- 277,290 ---- selec =3D var_eq_const(&vardata, operator, ((Const *) other)->constvalue, ((Const *) other)->constisnull, ! varonleft, negate); else selec =3D var_eq_non_const(&vardata, operator, other, ! varonleft, negate); =20=20 ReleaseVariableStats(vardata); =20=20 ! return selec; } =20=20 /* *************** eqsel(PG_FUNCTION_ARGS) *** 271,290 **** static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, ! bool varonleft) { double selec; bool isdefault; Oid opfuncoid; =20=20 /* * If the constant is NULL, assume operator is strict and return zero, i= e, ! * operator will never return TRUE. */ if (constisnull) return 0.0; =20=20 /* * If we matched the var to a unique index or DISTINCT clause, assume * there is exactly one match regardless of anything else. (This is * slightly bogus, since the index or clause's equality operator might be --- 295,327 ---- static double var_eq_const(VariableStatData *vardata, Oid operator, Datum constval, bool constisnull, ! bool varonleft, bool negate) { double selec; + double nullfrac =3D 0.0; bool isdefault; Oid opfuncoid; =20=20 /* * If the constant is NULL, assume operator is strict and return zero, i= e, ! * operator will never return TRUE. (It's zero even for a negator op.) */ if (constisnull) return 0.0; =20=20 /* + * Grab the nullfrac for use below. Note we allow use of nullfrac + * regardless of security check. + */ + if (HeapTupleIsValid(vardata->statsTuple)) + { + Form_pg_statistic stats; +=20 + stats =3D (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); + nullfrac =3D stats->stanullfrac; + } +=20 + /* * If we matched the var to a unique index or DISTINCT clause, assume * there is exactly one match regardless of anything else. (This is * slightly bogus, since the index or clause's equality operator might be *************** var_eq_const(VariableStatData *vardata,=20 *** 292,302 **** * ignoring the information.) */ if (vardata->isunique && vardata->rel && vardata->rel->tuples >=3D 1.0) ! return 1.0 / vardata->rel->tuples; !=20 ! if (HeapTupleIsValid(vardata->statsTuple) && ! statistic_proc_security_check(vardata, ! (opfuncoid =3D get_opcode(operator)))) { Form_pg_statistic stats; AttStatsSlot sslot; --- 329,340 ---- * ignoring the information.) */ if (vardata->isunique && vardata->rel && vardata->rel->tuples >=3D 1.0) ! { ! selec =3D 1.0 / vardata->rel->tuples; ! } ! else if (HeapTupleIsValid(vardata->statsTuple) && ! statistic_proc_security_check(vardata, ! (opfuncoid =3D get_opcode(operator)))) { Form_pg_statistic stats; AttStatsSlot sslot; *************** var_eq_const(VariableStatData *vardata,=20 *** 363,369 **** =20=20 for (i =3D 0; i < sslot.nnumbers; i++) sumcommon +=3D sslot.numbers[i]; ! selec =3D 1.0 - sumcommon - stats->stanullfrac; CLAMP_PROBABILITY(selec); =20=20 /* --- 401,407 ---- =20=20 for (i =3D 0; i < sslot.nnumbers; i++) sumcommon +=3D sslot.numbers[i]; ! selec =3D 1.0 - sumcommon - nullfrac; CLAMP_PROBABILITY(selec); =20=20 /* *************** var_eq_const(VariableStatData *vardata,=20 *** 396,401 **** --- 434,443 ---- selec =3D 1.0 / get_variable_numdistinct(vardata, &isdefault); } =20=20 + /* now adjust if we wanted <> rather than =3D */ + if (negate) + selec =3D 1.0 - selec - nullfrac; +=20 /* result should be in range, but make sure... */ CLAMP_PROBABILITY(selec); =20=20 *************** var_eq_const(VariableStatData *vardata,=20 *** 408,419 **** static double var_eq_non_const(VariableStatData *vardata, Oid operator, Node *other, ! bool varonleft) { double selec; bool isdefault; =20=20 /* * If we matched the var to a unique index or DISTINCT clause, assume * there is exactly one match regardless of anything else. (This is * slightly bogus, since the index or clause's equality operator might be --- 450,473 ---- static double var_eq_non_const(VariableStatData *vardata, Oid operator, Node *other, ! bool varonleft, bool negate) { double selec; + double nullfrac =3D 0.0; bool isdefault; =20=20 /* + * Grab the nullfrac for use below. + */ + if (HeapTupleIsValid(vardata->statsTuple)) + { + Form_pg_statistic stats; +=20 + stats =3D (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); + nullfrac =3D stats->stanullfrac; + } +=20 + /* * If we matched the var to a unique index or DISTINCT clause, assume * there is exactly one match regardless of anything else. (This is * slightly bogus, since the index or clause's equality operator might be *************** var_eq_non_const(VariableStatData *varda *** 421,429 **** * ignoring the information.) */ if (vardata->isunique && vardata->rel && vardata->rel->tuples >=3D 1.0) ! return 1.0 / vardata->rel->tuples; !=20 ! if (HeapTupleIsValid(vardata->statsTuple)) { Form_pg_statistic stats; double ndistinct; --- 475,484 ---- * ignoring the information.) */ if (vardata->isunique && vardata->rel && vardata->rel->tuples >=3D 1.0) ! { ! selec =3D 1.0 / vardata->rel->tuples; ! } ! else if (HeapTupleIsValid(vardata->statsTuple)) { Form_pg_statistic stats; double ndistinct; *************** var_eq_non_const(VariableStatData *varda *** 441,447 **** * values, regardless of their frequency in the table. Is that a good * idea?) */ ! selec =3D 1.0 - stats->stanullfrac; ndistinct =3D get_variable_numdistinct(vardata, &isdefault); if (ndistinct > 1) selec /=3D ndistinct; --- 496,502 ---- * values, regardless of their frequency in the table. Is that a good * idea?) */ ! selec =3D 1.0 - nullfrac; ndistinct =3D get_variable_numdistinct(vardata, &isdefault); if (ndistinct > 1) selec /=3D ndistinct; *************** var_eq_non_const(VariableStatData *varda *** 469,474 **** --- 524,533 ---- selec =3D 1.0 / get_variable_numdistinct(vardata, &isdefault); } =20=20 + /* now adjust if we wanted <> rather than =3D */ + if (negate) + selec =3D 1.0 - selec - nullfrac; +=20 /* result should be in range, but make sure... */ CLAMP_PROBABILITY(selec); =20=20 *************** var_eq_non_const(VariableStatData *varda *** 485,517 **** Datum neqsel(PG_FUNCTION_ARGS) { ! PlannerInfo *root =3D (PlannerInfo *) PG_GETARG_POINTER(0); ! Oid operator =3D PG_GETARG_OID(1); ! List *args =3D (List *) PG_GETARG_POINTER(2); ! int varRelid =3D PG_GETARG_INT32(3); ! Oid eqop; ! float8 result; !=20 ! /* ! * We want 1 - eqsel() where the equality operator is the one associated ! * with this !=3D operator, that is, its negator. ! */ ! eqop =3D get_negator(operator); ! if (eqop) ! { ! result =3D DatumGetFloat8(DirectFunctionCall4(eqsel, ! PointerGetDatum(root), ! ObjectIdGetDatum(eqop), ! PointerGetDatum(args), ! Int32GetDatum(varRelid))); ! } ! else ! { ! /* Use default selectivity (should we raise an error instead?) */ ! result =3D DEFAULT_EQ_SEL; ! } ! result =3D 1.0 - result; ! PG_RETURN_FLOAT8(result); } =20=20 /* --- 544,550 ---- Datum neqsel(PG_FUNCTION_ARGS) { ! PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, true)); } =20=20 /* *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1114,1119 **** --- 1147,1153 ---- Const *patt; Const *prefix =3D NULL; Selectivity rest_selec =3D 0; + double nullfrac =3D 0.0; double result; =20=20 /* *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1203,1208 **** --- 1237,1253 ---- } =20=20 /* + * Grab the nullfrac for use below. + */ + if (HeapTupleIsValid(vardata.statsTuple)) + { + Form_pg_statistic stats; +=20 + stats =3D (Form_pg_statistic) GETSTRUCT(vardata.statsTuple); + nullfrac =3D stats->stanullfrac; + } +=20 + /* * Pull out any fixed prefix implied by the pattern, and estimate the * fractional selectivity of the remainder of the pattern. Unlike many = of * the other functions in this file, we use the pattern operator's actual *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1252,1258 **** if (eqopr =3D=3D InvalidOid) elog(ERROR, "no =3D operator for opfamily %u", opfamily); result =3D var_eq_const(&vardata, eqopr, prefix->constvalue, ! false, true); } else { --- 1297,1303 ---- if (eqopr =3D=3D InvalidOid) elog(ERROR, "no =3D operator for opfamily %u", opfamily); result =3D var_eq_const(&vardata, eqopr, prefix->constvalue, ! false, true, false); } else { *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1275,1282 **** Selectivity selec; int hist_size; FmgrInfo opproc; ! double nullfrac, ! mcv_selec, sumcommon; =20=20 /* Try to use the histogram entries to get selectivity */ --- 1320,1326 ---- Selectivity selec; int hist_size; FmgrInfo opproc; ! double mcv_selec, sumcommon; =20=20 /* Try to use the histogram entries to get selectivity */ *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1328,1338 **** mcv_selec =3D mcv_selectivity(&vardata, &opproc, constval, true, &sumcommon); =20=20 - if (HeapTupleIsValid(vardata.statsTuple)) - nullfrac =3D ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stan= ullfrac; - else - nullfrac =3D 0.0; -=20 /* * Now merge the results from the MCV and histogram calculations, * realizing that the histogram covers only the non-null values that --- 1372,1377 ---- *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1340,1351 **** */ selec *=3D 1.0 - nullfrac - sumcommon; selec +=3D mcv_selec; -=20 - /* result should be in range, but make sure... */ - CLAMP_PROBABILITY(selec); result =3D selec; } =20=20 if (prefix) { pfree(DatumGetPointer(prefix->constvalue)); --- 1379,1394 ---- */ selec *=3D 1.0 - nullfrac - sumcommon; selec +=3D mcv_selec; result =3D selec; } =20=20 + /* now adjust if we wanted not-match rather than match */ + if (negate) + result =3D 1.0 - result - nullfrac; +=20 + /* result should be in range, but make sure... */ + CLAMP_PROBABILITY(result); +=20 if (prefix) { pfree(DatumGetPointer(prefix->constvalue)); *************** patternsel(PG_FUNCTION_ARGS, Pattern_Typ *** 1354,1360 **** =20=20 ReleaseVariableStats(vardata); =20=20 ! return negate ? (1.0 - result) : result; } =20=20 /* --- 1397,1403 ---- =20=20 ReleaseVariableStats(vardata); =20=20 ! return result; } =20=20 /* *************** boolvarsel(PlannerInfo *root, Node *arg, *** 1451,1457 **** * compute the selectivity as if that is what we have. */ selec =3D var_eq_const(&vardata, BooleanEqualOperator, ! BoolGetDatum(true), false, true); } else if (is_funcclause(arg)) { --- 1494,1500 ---- * compute the selectivity as if that is what we have. */ selec =3D var_eq_const(&vardata, BooleanEqualOperator, ! BoolGetDatum(true), false, true, false); } else if (is_funcclause(arg)) { *************** prefix_selectivity(PlannerInfo *root, Va *** 5788,5794 **** if (cmpopr =3D=3D InvalidOid) elog(ERROR, "no =3D operator for opfamily %u", opfamily); eq_sel =3D var_eq_const(vardata, cmpopr, prefixcon->constvalue, ! false, true); =20=20 prefixsel =3D Max(prefixsel, eq_sel); =20=20 --- 5831,5837 ---- if (cmpopr =3D=3D InvalidOid) elog(ERROR, "no =3D operator for opfamily %u", opfamily); eq_sel =3D var_eq_const(vardata, cmpopr, prefixcon->constvalue, ! false, true, false); =20=20 prefixsel =3D Max(prefixsel, eq_sel); =20=20 ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--