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 1lRdx5-0000uG-Of for pgsql-committers@arkaria.postgresql.org; Wed, 31 Mar 2021 16:44:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRdx4-00087c-7Z for pgsql-committers@arkaria.postgresql.org; Wed, 31 Mar 2021 16:44:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRdx3-00087V-NK for pgsql-committers@lists.postgresql.org; Wed, 31 Mar 2021 16:44:18 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lRdx0-0000UD-Iv for pgsql-committers@lists.postgresql.org; Wed, 31 Mar 2021 16:44:16 +0000 Received: by mail-ej1-x62b.google.com with SMTP id r12so31049561ejr.5 for ; Wed, 31 Mar 2021 09:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=VK44c1PTexVlj9EH6oQLpxK8YQw84O4oSWvarAj/AtI=; b=x1RN9mkgUNGEEE/8zFv5HkMF0XD0+FumuzoP0gjFHYCVxlOCyUwxL6YBuG5IYhFrDI DJZlVs544qR65piydAXzhxezqVnkrJkDqrZiEvtvnoEWwf1uY1/rjlnHSWxwFMWngr/Y QdJvuFRWgZVk3bBN9qh90C/0Ijp+asXfNFtprrM0srWUq0KCjm3RFbof8dS9ddADQp5F FkzBzGoj49rXyzwv1RoYXdBE7uOS8heHcHSCA6ELBfc0hbXSNksIJ64N5HBLxQkvR1g/ uUXdJFi8kuqFvosAwUrhzZUF4wI+i7aWVR1PJs5xtuT0na68pc4aZ9zehFx97cFq4rV4 BsaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=VK44c1PTexVlj9EH6oQLpxK8YQw84O4oSWvarAj/AtI=; b=qmWllrB/x35tXmP+MBcSsBac93h58yyATEHUud6Qc8Ri9mMoLnScMKdJMUaZArRE6t qmcDv7IjhW6j49f+HjATDarNpLyLoVvcFNaKOmOHCJk28U5v57Fj+9nxyQvPLol82xe/ tVN6pk8I4SQIauty0JfS9psYCgX2qIFM1NeEB/jTu5mH6B3sYY0mPEU0pgIQr0g6CYsQ reNfDtS4Uqx9kVfbaN0EJlTHQN/0DWnUyipUeADCz/OmAt4eyWJ0VpCht6grTOSJxfYk VOG5nsp/Rc9VpLrtjVqncAIMVOTMUi9AC2gkO6zsAjjOkI9PArovNJS/Ndx+k5XV8Ajy q0EQ== X-Gm-Message-State: AOAM531i1QEMzvD9P1tYgXVjQxaM6ke5pVg8Fn/L4hDUfy6/yLhSWUuQ ApLzDILLspMBi1yIM3iAevJgUDThqJ7ncLJYFZXYwGi/EkDYCvuy5ga/bol9XSQCKSObopGDlUW XMlS4C+Uu5M50CKjhLTKAy7RfpabkTI6pvupIC7lQUR/wpnQwejSNNF1uvz4mnpWIfQJSwClJgV kDGvU9iEwY9pQL/mSL/+WwlqebaQBTjiNpSlg4Dio/Ha2j2kBbAI6UqFTB1QoCj2vRsOaXnIvr8 b1yRk17ti4tIxkZxNzV1vTBrITDwIfcw/o/8CLLb4y1mYOLNPi2nUHvRJpY1g== X-Google-Smtp-Source: ABdhPJyMyc1EFbBFmAloaNnlJ+eAk3b2BTAVUJMUkEyOT8Pa0OjJWFivpBE5Xend64VNnm2bN+Kz8A== X-Received: by 2002:a17:906:5248:: with SMTP id y8mr4476767ejm.150.1617209052305; Wed, 31 Mar 2021 09:44:12 -0700 (PDT) Received: from [10.137.0.20] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id c15sm1534824ejm.52.2021.03.31.09.44.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 31 Mar 2021 09:44:11 -0700 (PDT) Subject: Re: pgsql: Extended statistics on expressions To: pgsql-committers@lists.postgresql.org References: From: Tomas Vondra Message-ID: Date: Wed, 31 Mar 2021 18:44:09 +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: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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 On 3/31/21 10:05 AM, David Rowley wrote: > Hi Tomas, > > I'm debugging a crash after running sqllancer on current master. The > first bad commit seems to be this one. > > The crash stack trace is: > > Program received signal SIGSEGV, Segmentation fault. > pg_detoast_datum_packed (datum=0x5) at fmgr.c:1759 > 1759 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum)) > (gdb) bt > #0 pg_detoast_datum_packed (datum=0x5) at fmgr.c:1759 > #1 0x000055e460c99da2 in textlike (fcinfo=0x7fff1bbc39e0) at like.c:287 > #2 0x000055e460d44dde in FunctionCall2Coll > (flinfo=flinfo@entry=0x7fff1bbc3ae0, collation=, > arg1=, arg2=) at fmgr.c:1163 > #3 0x000055e460bd6dad in mcv_get_match_bitmap (clauses= out>, keys=0x55e4614627e8, exprs=0x0, mcvlist=, > is_or=, root=) at mcv.c:1706 > #4 0x000055e460bd965d in mcv_clauselist_selectivity > (root=root@entry=0x55e46145fca0, stat=stat@entry=0x55e461462790, > clauses=clauses@entry=0x55e46144cf08, varRelid=varRelid@entry=0, > jointype=jointype@entry=JOIN_INNER, > sjinfo=sjinfo@entry=0x0, rel=0x55e461461f18, > basesel=0x7fff1bbc3c88, totalsel=0x7fff1bbc3c90) at mcv.c:2043 > #5 0x000055e460bd6364 in statext_mcv_clauselist_selectivity > (is_or=, estimatedclauses=, > rel=, sjinfo=, jointype=, > varRelid=, > clauses=, root=) at extended_stats.c:1916 > #6 statext_clauselist_selectivity (root=root@entry=0x55e46145fca0, > clauses=clauses@entry=0x55e461462ad0, varRelid=varRelid@entry=0, > jointype=jointype@entry=JOIN_INNER, sjinfo=sjinfo@entry=0x0, > rel=0x55e461461f18, > estimatedclauses=0x7fff1bbc3d28, is_or=false) at extended_stats.c:1948 > #7 0x000055e460b0e973 in clauselist_selectivity_ext > (root=root@entry=0x55e46145fca0, clauses=0x55e461462ad0, > varRelid=varRelid@entry=0, jointype=jointype@entry=JOIN_INNER, > sjinfo=0x0, use_extended_stats=true) at clausesel.c:155 > #8 0x000055e460b0e346 in clause_selectivity_ext > (root=root@entry=0x55e46145fca0, clause=0x55e46144c8f8, > varRelid=varRelid@entry=0, jointype=jointype@entry=JOIN_INNER, > sjinfo=sjinfo@entry=0x0, > use_extended_stats=use_extended_stats@entry=true) at clausesel.c:838 > #9 0x000055e460b0e1a4 in clauselist_selectivity_or > (use_extended_stats=, sjinfo=, > jointype=JOIN_INNER, varRelid=0, clauses=0x55e461462cc0, > root=0x55e46145fca0) at clausesel.c:414 > #10 clause_selectivity_ext (root=0x55e46145fca0, clause= out>, varRelid=0, jointype=JOIN_INNER, sjinfo=, > use_extended_stats=use_extended_stats@entry=true) at clausesel.c:851 > #11 0x000055e460b0e863 in clauselist_selectivity_ext > (root=root@entry=0x55e46145fca0, clauses=0x55e46144cd28, > varRelid=varRelid@entry=0, jointype=jointype@entry=JOIN_INNER, > sjinfo=sjinfo@entry=0x0, > use_extended_stats=use_extended_stats@entry=true) at > ../../../../src/include/nodes/pg_list.h:260 > #12 0x000055e460b0eacf in clauselist_selectivity > (root=root@entry=0x55e46145fca0, clauses=, > varRelid=varRelid@entry=0, jointype=jointype@entry=JOIN_INNER, > sjinfo=sjinfo@entry=0x0) at clausesel.c:108 > #13 0x000055e460b16992 in set_baserel_size_estimates > (root=root@entry=0x55e46145fca0, rel=rel@entry=0x55e461461f18) at > costsize.c:4753 > #14 0x000055e460b0b971 in set_plain_rel_size (rte=, > rel=0x55e461461f18, root=0x55e46145fca0) at allpaths.c:583 > #15 set_rel_size (root=0x55e46145fca0, rel=0x55e461461f18, rti=1, > rte=0x55e4614220a8) at allpaths.c:412 > #16 0x000055e460b0d8c0 in set_base_rel_sizes (root=) at > allpaths.c:323 > > I'm struggling a bit to reproduce this consistently. > > If you do: > > psql -c "CREATE ROLE sqlancer;" postgres > created test; > psql -f database0.sql test > > then run the following set of commands: > > insert into t1 values(1234,false),(5678,true); > analyze; > delete from t1; > SELECT t1.c0 FROM ONLY t1 WHERE > ((((((((upper('%|1j]<#^j\???u,???D'))LIKE((((('h???x')||(178227136)))||((('(-2073106655,58629343]'::int4range)*('(-1903439243,-1774128827)'::int4range)))))))AND((((t1.c0) > IN (-91026522, 0.6000845835151706))OR(t1.c1)))))OR(t1.c1)))OR(CAST(t1.c1 > AS BOOLEAN))); > > It seems to be a corrupt Datum in the 2nd argument to textlike(). > > (gdb) frame 3 > #3 0x000055e460bd6dad in mcv_get_match_bitmap (clauses= out>, keys=0x55e461480148, exprs=0x0, mcvlist=, > is_or=, root=) at mcv.c:1706 > 1706 match = DatumGetBool(FunctionCall2Coll(&opproc, > (gdb) p i > $7 = 0 > (gdb) p idx > $8 = 2 > > I've not studied the code in great detail, but per above, idx == 2 and > you're doing item->values[idx]. Isn't the index of 2 out of bounds of > the most_common_vals column below? > > test1=# select most_common_vals from pg_stats_ext; > most_common_vals > --------------------- > {{1234,f},{5678,t}} > (1 row) > > The code that sets idx to 2 (mcv_match_expression()) looks a bit > weird. These stats don't have any exprs and the "expr" past to > mcv_match_expression is an OpExpr. The function just goes and sets > idx = bms_num_members(keys); then does 0 loops due to the empty > "exprs" and returns 2. The Assert() does not help catch not finding > anything since it checks idx >= bms_num_members(keys), which is 2. > > I'm not quite sure how this is all meant to work. Are you able to look further? > 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 ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company