From: Tomas Vondra <tomas.vondra@enterprisedb.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Extended statistics on expressions
Date: Thu, 1 Apr 2021 00:07:43 +0200
Message-ID: <c61cfe32-251f-e9cb-1be1-0e15218cda17@enterprisedb.com> (raw)
In-Reply-To: <eae26b31-2d9b-1293-a3b0-a8be4820bea3@enterprisedb.com>
References: <20210331170828.GA26795@alvherre.pgsql>
<eae26b31-2d9b-1293-a3b0-a8be4820bea3@enterprisedb.com>
On 3/31/21 7:54 PM, Tomas Vondra wrote:
> On 3/31/21 7:08 PM, Alvaro Herrera wrote:
>> On 2021-Mar-31, Tomas Vondra wrote:
>>
>>> Thanks for the report, I'll take a look. You're right this seems like an
>>> out-of-bounds access, but mcv_match_expression is only expected to be
>>> run on expressions we know are in the statistics (because we pick the
>>> statistics like that). Clearly, that does not happen here, not sure why.
>>>
>>> It's quite weird that we end up running textlike(), when the statistics
>>> is on (double precision, boolean) columns ...
>>
>> Uninitialized values somewhere? Maybe valgrind would help.
>>
>
> Unlikely, I've ran it through valgrind repeatedly, including right
> before commit (both on x86_64 and arm).
>
> FWIW I'm unable to reproduce it, so not sure what's going on. David,
> what configure option are you using? Anything special?
>
>
> It's a bit strange, because statext_mcv_clauselist_selectivity should
> only estimate "matching" clauses on the statistics. So how come this
> estimates such a complex expression using textlike(), when neither of
> those columns is text?
>
> It'd be interesting to know what's happening in the code after
>
> stat = choose_best_statistics(...);
>
> i.e. what clauses it considers "compatible" with the statistics and why.
> In fact, I wouldn't have expected the statistics to be used at all.
>
OK, I managed to reproduce/trigger the issue. The simplest query that
triggers the issue for me is this:
SELECT t1.c0 FROM ONLY t1 WHERE
(
upper('x') LIKE ('x'||('[0,1]'::int4range))
AND
(t1.c0 IN (0, 1) OR t1.c1)
)
I think the code matching clauses to the statistics gets a bit confused
when processing the AND clause. It extracts 2 attnums for the OR part,
but the first part should be "incompatible" with the statistics. But
after picking the statistics to apply, it gets confused and includes the
first expression (the whole LIKE clause) as compatible too.
The attached patch fixes this for me. David, can you check if this
resolves the issue for you?
I don't feel like I want to push a fix at midnight, and I'd like to
think about maybe making this part of the code a bit clearer tomorrow.
It's not very comprehensible, I'm afraid.
regards
--
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Attachments:ext-stats-rowley-fix.patch (text/x-patch, 1.2K) downloaddiff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.cindex 8c75690fce..3567526c85 100644--- a/src/backend/statistics/extended_stats.c+++ b/src/backend/statistics/extended_stats.c@@ -1768,6 +1768,7 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli
* estimate.
*/
if (!bms_is_member(listidx, *estimatedclauses) &&
+ (list_attnums[listidx] != NULL || list_exprs[listidx] != NULL) &&
bms_is_subset(list_attnums[listidx], stat->keys) &&
stat_covers_expressions(stat, list_exprs[listidx], NULL))
{
diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.cindex 2a00fb4848..7629e2b251 100644--- a/src/backend/statistics/mcv.c+++ b/src/backend/statistics/mcv.c@@ -1654,6 +1654,8 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses,
/* match the attribute/expression to a dimension of the statistic */
idx = mcv_match_expression(clause_expr, keys, exprs, &collid);
+ Assert((idx >= 0) && (idx < bms_num_members(keys) + list_length(exprs)));+
/*
* Walk through the MCV items and evaluate the current clause. We
* can skip items that were already ruled out, and terminate if
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-committers@postgresql.org
Cc: tomas.vondra@enterprisedb.com, alvherre@alvh.no-ip.org
Subject: Re: pgsql: Extended statistics on expressions
In-Reply-To: <c61cfe32-251f-e9cb-1be1-0e15218cda17@enterprisedb.com>
References: <20210331170828.GA26795@alvherre.pgsql> <eae26b31-2d9b-1293-a3b0-a8be4820bea3@enterprisedb.com> <c61cfe32-251f-e9cb-1be1-0e15218cda17@enterprisedb.com>
* 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