From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tomas Vondra <tomas.vondra@enterprisedb.com>
Cc: David Geier <geidav.pg@gmail.com>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes
Date: Thu, 17 Nov 2022 20:36:50 -0500
Message-ID: <2106238.1668735410@sss.pgh.pa.us> (raw)
In-Reply-To: <2067066.1668731970@sss.pgh.pa.us>
References: <b9846ca0-5f1c-9b26-5881-aad3f42b07f0@gmail.com>
<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>
I wrote:
> Tomas Vondra <tomas.vondra@enterprisedb.com> 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
Attachments:skip-reading-MCVs-when-only-one.patch (text/x-diff, 2.0K) downloaddiff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.cindex 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 = NULL;
bool have_mcvs1 = false;
bool have_mcvs2 = 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 = (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 = (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 = 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 = (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 = get_attstatsslot(&sslot2, vardata2.statsTuple,
STATISTIC_KIND_MCV, InvalidOid,
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: tgl@sss.pgh.pa.us, tomas.vondra@enterprisedb.com, geidav.pg@gmail.com
Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes
In-Reply-To: <2106238.1668735410@sss.pgh.pa.us>
References: <b9846ca0-5f1c-9b26-5881-aad3f42b07f0@gmail.com> <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> <2106238.1668735410@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox