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 1lRj0G-0000gk-I4 for pgsql-committers@arkaria.postgresql.org; Wed, 31 Mar 2021 22:07:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRj0D-0006hY-Nl for pgsql-committers@arkaria.postgresql.org; Wed, 31 Mar 2021 22:07:53 +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 1lRj0D-0006hR-Aj for pgsql-committers@lists.postgresql.org; Wed, 31 Mar 2021 22:07:53 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lRj09-0006kI-Co for pgsql-committers@lists.postgresql.org; Wed, 31 Mar 2021 22:07:52 +0000 Received: by mail-ej1-x631.google.com with SMTP id w3so32369115ejc.4 for ; Wed, 31 Mar 2021 15:07:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=GxzoSvEOoYn05ibfr0nOQRutlb0bxRWZ+VH1C/B3q9A=; b=hsRbgKphhuk/j1dLald9YdIYqvRVplWTRqHFSpJYSQevDK+ksoAcszvfuyGLprJatV 5XfAqFvWbngc6CfQ5Ks58k5rkXhIFgJ9qYQQe8+c8A3mga8L/VnbQn+KV2IVtG0W2/wC JbDuAKENf4kr6ZOFFrpknzcPrhS5QJgmkEultJr7dTEgot7/yjNa3B7N2qpROmcyokOD h+J80p+RBU42X8r8WiJo0yjJ2sbvRYABp2rfcxF5Jge9RPccTzJOWNc1WqnWpJNdklrO yen0abRnKZbC34408OnVRmofu4iJVay70tvuAznsD5p4MVvuZu5LnYq4gYFOIxs5DcRh zavA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=GxzoSvEOoYn05ibfr0nOQRutlb0bxRWZ+VH1C/B3q9A=; b=CJommaEFBbaqUhYZP6pOko9HoD6z6ecSgCDJN+BBir39JwcEMI/0o1tT2hD1N3gUQf xgPwqt1jCNDW5vv2OW0rARma5n4k10VYrBrLJ6xOYQI2EbqCT7IXTqP2H7o8oG3BKBXV HpKbCStZ6rse76ZK8sVazlPQR4n3TjB9y1Zh+Qdm1FbaMaAjTA42I6yTo1D7dRYFjUrl 1eC3jU8mUZpQ4lrfcuqhcFGSsBbhnVcxfrDz/ujVciHEmeRrEEnfAMLTB6MJMCOHS/dw VfGcTWjqQGWez/8LQauCCOKCq6sdgu8H6YmPQCsuW0nlxxjR3oxZJgnvhmil1HTSskWO vZXA== X-Gm-Message-State: AOAM531B/iZvKOUvtsEP6fggvx6uq5h41v+sugD+jB2FWhw3lLdICoI2 RuLoeDvPjxJ/SS/VidZ4KrcyaqAkxeRbKl86EQXFeka+wO4HRSo+7cnyOFdxiUDGjgnGVIDMPAP WriycbjYd/7xFmKC2VTuSjSX/lNAKMq+pxWibmR3PN2zKJpT9AFF328U9vfUOfcb/mQqDp4ZbVW 8I3owthgoTzkbntNHXYchh5zE7vjQCBc5LN5vAIqM1VuZ4lJF3UwzLTVuxURNaSWoqzy5c5cKqA lVBA+/gSsjWLUjd3RSY4OzYdwAR8UWsSusmeGZ329aD2bVpFCUB8ScZ3xqaJA== X-Google-Smtp-Source: ABdhPJwagkPJ9pMx9zgFbrXKukrwN6iPU0vntE964tuVakmEikwDaN4d10EEpDTWzU5TeygVpQuIpQ== X-Received: by 2002:a17:906:c405:: with SMTP id u5mr5958977ejz.341.1617228466840; Wed, 31 Mar 2021 15:07:46 -0700 (PDT) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id c10sm2394549edt.64.2021.03.31.15.07.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 31 Mar 2021 15:07:45 -0700 (PDT) Subject: Re: pgsql: Extended statistics on expressions From: Tomas Vondra To: Alvaro Herrera Cc: pgsql-committers@lists.postgresql.org References: <20210331170828.GA26795@alvherre.pgsql> Message-ID: Date: Thu, 1 Apr 2021 00:07:43 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------6048EA3895E1BBFBDF4527EA" Content-Language: en-US X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------6048EA3895E1BBFBDF4527EA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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 --------------6048EA3895E1BBFBDF4527EA Content-Type: text/x-patch; charset=UTF-8; name="ext-stats-rowley-fix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ext-stats-rowley-fix.patch" diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 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.c index 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 --------------6048EA3895E1BBFBDF4527EA--