Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iX57d-0004tO-G1 for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Nov 2019 15:08:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iX57a-0005Mv-UJ for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Nov 2019 15:08:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iX57a-0005Mo-HV for pgsql-bugs@lists.postgresql.org; Tue, 19 Nov 2019 15:08:50 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iX57T-0003vA-07 for pgsql-bugs@lists.postgresql.org; Tue, 19 Nov 2019 15:08:49 +0000 Received: by mail-wm1-x343.google.com with SMTP id t26so4062899wmi.4 for ; Tue, 19 Nov 2019 07:08:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=H+kbkYKPjEQtouS8Ow3pI2OIeKzJ5p6LkE6x84AkhNA=; b=qrmB1u6uNa7/Mlh/BDgiTsJQ/NBEaPD+fp3u7dG6CaV7NqZNgMHBh2YfR4BjgrGdTY e4iCwawWxEmOVynFfAOV8oIqrSly29uYK3KrfosyHincpgBWh9KrSZ1bm3QgNI6V1OOp 79b91zewo/6lO5L/bcg3OtrdzdGVTVvYblN/w3zBKT6VyGS+WUp9o2E4UcEH6pdSlqZb F/7K0dUR9Yt/1dz2bioEW2q+hbNu0Y93mrhSNjHlLIlrmfb/yKX1BFlkh3xIgh//DVT9 BWbs+g6LF4/HXwDwiTxH7qVBtRVwmi7XxhQtvZp9XmIYS1eBN6s07lEqkAKco9mcGftA Fu/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=H+kbkYKPjEQtouS8Ow3pI2OIeKzJ5p6LkE6x84AkhNA=; b=TMjt3PGyHa6F1CsH+hE6FHWk7PzR/4VK9IZoZl/Kv/qsMlPNHeKbAvGFzbYrxEG+gf 7L5fKEB8BpouUorFBrbsU1Q5P2kezI+eBhTKrF+AsYPhCctI0ADgClhIL2Pj19KQSwOD 9xOxG1ifaI5BsXneNZ+Sw1kJOABxV3LQhho7T6Yt30kxdegr2pnOjtMSenbjiRfP0Uhy ip7K0mGrUcFE+yD9HmK7xC1LrLRK/B67SHj42Wm9L42+wOB8BXj75DRcbgjkUgWzbEBM FekyCiaADLZR9cR3Ml8cm7yLl4bBMs6zsmJLqKvZ4uKycxUFbduqFIk7LJlJusmLX+nU lrrw== X-Gm-Message-State: APjAAAUehwkkatMdM78RlVtBvA6x2UM2vkKLP7xpP8cYgPpf5HkC7JEz 6Cn672NQ6gCKwJ39igX6WoknJw== X-Google-Smtp-Source: APXvYqxy59sbOxYf3vQViD1t+JNbNXTWZCTlTXifXPdE0X5T4ogdcIZtG6bG6SdjIkkhuiVwd3Yqog== X-Received: by 2002:a1c:7701:: with SMTP id t1mr6653409wmi.113.1574176121793; Tue, 19 Nov 2019 07:08:41 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id l10sm31274194wrg.90.2019.11.19.07.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 07:08:40 -0800 (PST) Date: Tue, 19 Nov 2019 16:08:40 +0100 From: Tomas Vondra To: Daniel Gustafsson Cc: Dmitry Dolgov <9erthalion6@gmail.com>, Manuel Rigger , PostgreSQL mailing lists Subject: Re: Failed assertion clauses != NIL Message-ID: <20191119150840.nmezhsf2tpip7osh@development> References: <20191119133839.5qj5zia4pkd3zldt@localhost> <520C6E76-53A5-4EF2-A855-8054582221A5@yesql.se> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <520C6E76-53A5-4EF2-A855-8054582221A5@yesql.se> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Nov 19, 2019 at 02:45:54PM +0100, Daniel Gustafsson wrote: >> On 19 Nov 2019, at 14:38, Dmitry Dolgov <9erthalion6@gmail.com> wrote: >> >>> On Tue, Nov 19, 2019 at 01:50:51PM +0100, Manuel Rigger wrote: >>> >>> when building PostgreSQL with -enable-cassert, executing the following >>> statements result in an assertion error: >>> >>> CREATE TABLE t0(c0 boolean, c1 boolean, c2 boolean); >>> INSERT INTO t0 VALUES(FALSE, FALSE, FALSE); >>> CREATE STATISTICS s0 ON c0, c2 FROM t0; >>> ANALYZE; >>> SELECT * FROM t0 WHERE t0.c2 OR t0.c1 OR t0.c0; >> >> Yes, I can reproduce it too. mcv_get_match_bitmap expects that >> stat_clauses will not be empty, but looks like in this situation >> stat_clauses is indeed NIL. clauselist_selectivity_simple right before >> actually doesn't insist on stat_clauses being non empty, probably it's >> just too strict assert. > >I might be missing something, but if the clause list is NIL, wouldn't it better >to exit earlier from statext_mcv_clauselist_selectivity rather than relax the >Assertion since we will get a 1.0 estimate either way? > Hmmm, this is actually a thinko in how we match stats to clauses. We simply extract attnums from Vars in each clause, and then pick the statistic matching at least two of those attnums (and we pick the one matching the most attnums, but that does not matter here). And then we go and pick all the clauses covered by the statistic, assuming that we'll get some matching clauses. Unfortunately, that fails here because it's essentially just a single OR clause. And it references attributes that are not covered by the statistic. So we get clauses_attnums = {1,2,3}, we pick the statistic which however only covers {1,3}, and then we fail because the clause is c0 OR c1 OD c2 which is not actually covered by the statistic, because of c1. Kaboom! Yes, adding the condition to statext_mcv_clauselist_selectivity() would make this go away, and it's about the simplest solution. Ideally, we'd be able to improve the statistics matching to recognize it has to match all three attributes to match the clause, which in this case would mean the OR clause is passed to clause_selectivity, and we do some magic with extended statistics there. I'll see how complex / backpatchable that would be. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services