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 1ovqJP-0004sf-Ce for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Nov 2022 01:36:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ovqJM-0000C8-EP for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Nov 2022 01:36:56 +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 1ovqJM-00007X-4E for pgsql-hackers@lists.postgresql.org; Fri, 18 Nov 2022 01:36:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ovqJJ-000090-Qt for pgsql-hackers@lists.postgresql.org; Fri, 18 Nov 2022 01:36:55 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2AI1ao0b2106239; Thu, 17 Nov 2022 20:36:50 -0500 From: Tom Lane To: Tomas Vondra cc: David Geier , PostgreSQL Developers Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes In-reply-to: <2067066.1668731970@sss.pgh.pa.us> References: <522124.1668179795@sss.pgh.pa.us> <16acb727-24e8-6453-1711-aae339802966@gmail.com> <2f30ba9c-b707-55ae-0afd-68eb6a8a7708@enterprisedb.com> <2067066.1668731970@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 17 Nov 2022 19:39:30 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2106038.1668735268.0@sss.pgh.pa.us> Date: Thu, 17 Nov 2022 20:36:50 -0500 Message-ID: <2106238.1668735410@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2106038.1668735268.1@sss.pgh.pa.us> I wrote: > Tomas Vondra writes: >> Or perhaps what if we have a function that quickly determines if the >> attribute has MCV, without loading it? I'd bet the expensive part of >> get_attstatslot() is the deconstruct_array(). >> We could have a function that only does the first small loop over slots, >> and returns true/false if we have a slot of the requested stakind. > Yeah, I like this idea. Actually, looking at get_attstatslot, I realize it was already designed to do that -- just pass zero for flags. So we could do it as attached. We could make some consequent simplifications by only retaining one "have_mcvs" flag, but I'm inclined to leave the rest of the code as-is. We would not get much gain from that, and it would make this harder to undo if there ever is a reason to consider just one set of MCVs. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="skip-reading-MCVs-when-only-one.patch"; charset="us-ascii" Content-ID: <2106038.1668735268.2@sss.pgh.pa.us> Content-Description: skip-reading-MCVs-when-only-one.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/self= uncs.c index d597b7e81f..e0aeaa6909 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -2261,6 +2261,7 @@ eqjoinsel(PG_FUNCTION_ARGS) Form_pg_statistic stats2 =3D NULL; bool have_mcvs1 =3D false; bool have_mcvs2 =3D false; + bool get_mcv_stats; bool join_is_reversed; RelOptInfo *inner_rel; = @@ -2275,11 +2276,25 @@ eqjoinsel(PG_FUNCTION_ARGS) memset(&sslot1, 0, sizeof(sslot1)); memset(&sslot2, 0, sizeof(sslot2)); = + /* + * There is no use in fetching one side's MCVs if we lack MCVs for the + * other side, so do a quick check to verify that both stats exist. + */ + get_mcv_stats =3D (HeapTupleIsValid(vardata1.statsTuple) && + HeapTupleIsValid(vardata2.statsTuple) && + get_attstatsslot(&sslot1, vardata1.statsTuple, + STATISTIC_KIND_MCV, InvalidOid, + 0) && + get_attstatsslot(&sslot2, vardata2.statsTuple, + STATISTIC_KIND_MCV, InvalidOid, + 0)); + if (HeapTupleIsValid(vardata1.statsTuple)) { /* note we allow use of nullfrac regardless of security check */ stats1 =3D (Form_pg_statistic) GETSTRUCT(vardata1.statsTuple); - if (statistic_proc_security_check(&vardata1, opfuncoid)) + if (get_mcv_stats && + statistic_proc_security_check(&vardata1, opfuncoid)) have_mcvs1 =3D get_attstatsslot(&sslot1, vardata1.statsTuple, STATISTIC_KIND_MCV, InvalidOid, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS); @@ -2289,7 +2304,8 @@ eqjoinsel(PG_FUNCTION_ARGS) { /* note we allow use of nullfrac regardless of security check */ stats2 =3D (Form_pg_statistic) GETSTRUCT(vardata2.statsTuple); - if (statistic_proc_security_check(&vardata2, opfuncoid)) + if (get_mcv_stats && + statistic_proc_security_check(&vardata2, opfuncoid)) have_mcvs2 =3D get_attstatsslot(&sslot2, vardata2.statsTuple, STATISTIC_KIND_MCV, InvalidOid, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS); ------- =_aaaaaaaaaa0--