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 1hmOs4-00032e-Bu for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jul 2019 20:43:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hmOs3-0006xC-3y for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jul 2019 20:43:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hmOs2-0006x3-Ii for pgsql-hackers@lists.postgresql.org; Sat, 13 Jul 2019 20:43:50 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hmOry-0006sQ-UF for pgsql-hackers@lists.postgresql.org; Sat, 13 Jul 2019 20:43:49 +0000 Received: by mail-wr1-x444.google.com with SMTP id r1so13180770wrl.7 for ; Sat, 13 Jul 2019 13:43:46 -0700 (PDT) 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:user-agent; bh=nn8YoxTv/kX/mJLR1TWFD8ElcnE7v8ra2FaQhK5YQVY=; b=gpCsYBGDImZrEEty9Z9UGJLFeOXUi5UfNYShxA9kyf16Rfw8UcVhqzdC8HXvgvqLl+ 9NvmvgRR6NicP+oL3C+OQWdxCzSrXDCJh+hAZHLvG5+15xheiP/DA9cNaz+5TiWCx5Px PSQ3SiT84pjjqI588k/O4A4IeGwt6wNIwXd8vFa/GXgbEDJZ9r/ZI/Vj78KJ3aoQmQ7J ionJNiHmfPIjrwRCY2Sl+Qq9FXdiaLGRzY983z6TTfjWzDcvDUyCFpK7/m+zAY66Jo2c iMAd8Fk0zKfSG4+FecPWr9cXvD2+KBdtHSrL78j0NMcqzxjlcxJTlbr3GR1Pm9evkGb4 qJGA== 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:user-agent; bh=nn8YoxTv/kX/mJLR1TWFD8ElcnE7v8ra2FaQhK5YQVY=; b=hiERZqfK3lF6cC47dgxSWle36uFp06KLwMHd61Xvvd/M1gxfxnPam4eVEdww3iufgs D15OZuWyaFFROUsSdrrpVAv17UCXJkgsM6AZ9tMtdelQo/V2mwYsdO6F7VtbnpYwFyT+ k/IZGFPc94XXN6UpdJ020D336LMWcLjleD4xQKcuqrGLxaXWckA6rzoASsZLT/jT3wYw UDdlFD5JZIdyMh7dujr8hbJnjVWkWO+rX2Eqpzp+azlUiCIuhfZzBUayYvuq7PzGKBZc wPdTSJPTaBtgSUwWiJa4TLkRN53kW/3VOrN8jjAok3MP6UHtKdh8Zy3ujxNefe5KTMJH aoEg== X-Gm-Message-State: APjAAAUEJpSheTtsZdOsJH/5gdKF9yU/soO1eGux8xXESsYjlUo07das iC7gUVE9gf0052k6hGBSjZCR3Q== X-Google-Smtp-Source: APXvYqyYzkknXXgaJ77VezahCbrZCZXuBwm/ZAz2WO18o3UmDQjm0ccfSeZwGha7jTdww2Mp0rZINw== X-Received: by 2002:adf:8028:: with SMTP id 37mr18679749wrk.106.1563050624710; Sat, 13 Jul 2019 13:43:44 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id w67sm14932381wma.24.2019.07.13.13.43.43 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 13 Jul 2019 13:43:43 -0700 (PDT) Date: Sat, 13 Jul 2019 22:43:42 +0200 From: Tomas Vondra To: Tom Lane Cc: Andreas Seltenreich , pgsql-hackers@lists.postgresql.org, Tomas Vondra Subject: Re: [sqlsmith] Crash in mcv_get_match_bitmap Message-ID: <20190713204342.pysg4odqj2j6gn3o@development> References: <8736jdhbhc.fsf@ansel.ydns.eu> <15097.1562792274@sss.pgh.pa.us> <20190710212609.5mrl7hltbozoxpq7@development> <17042.1562795124@sss.pgh.pa.us> <20190710215904.ncaqlswrylliisv3@development> <19586.1562798896@sss.pgh.pa.us> <20190711150822.z7cirna2fjtrfcnu@development> <20190713001137.7saj5g46qpdump4r@development> <19208.1563032395@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <19208.1563032395@sss.pgh.pa.us> User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Jul 13, 2019 at 11:39:55AM -0400, Tom Lane wrote: >Tomas Vondra writes: >> On Thu, Jul 11, 2019 at 05:08:22PM +0200, Tomas Vondra wrote: >>> On Wed, Jul 10, 2019 at 06:48:16PM -0400, Tom Lane wrote: >>>> The right way to determine operator semantics is to look to see >>>> whether they are in a btree opclass. That's what the rest of the >>>> planner does, and there is no good reason for the mcv code to >>>> do it some other way. > >>> Hmmm, but that will mean we're unable to estimate operators that are not >>> part of a btree opclass. Which is a bit annoying, because that would also >>> affect equalities (and thus functional dependencies), in which case the >>> type may easily have just hash opclass or something. > >After thinking about this more, I may have been analogizing to the wrong >code. It's necessary to use opclass properties when we're reasoning about >operators in a way that *must* be correct, for instance to conclude that >a partition can be pruned from a query. But this code is just doing >selectivity estimation, so the correctness standards are a lot lower. >In particular I see that the long-established range-query-detection >code in clauselist_selectivity is looking for operators that have >F_SCALARLTSEL etc. as restriction estimators (in fact I'm guessing you >lifted parts of the mcv code from that, cause it looks pretty similar). >So if we've gotten away with that so far over there, there's probably >no reason not to do likewise here. > >I am a little troubled by the point I made about operators possibly >wanting to have a more-specific estimator than scalarltsel, but that >seems like an issue to solve some other time; and if we do change that >logic then clauselist_selectivity needs to change too. > >> Here are WIP patches addressing two of the issues: > >> 1) determining operator semantics by matching them to btree opclasses > >Per above, I'm sort of inclined to drop this, unless you feel better >about doing it like this than the existing way. > OK. TBH I don't have a very strong opinion on this - I always disliked how we rely on the estimator OIDs in this code, and relying on btree opclasses seems somewhat more principled. But I'm not sure I understand all the implications of such change (and I have some concerns about it too, per my last message), so I'd revisit that in PG13. >> 2) deconstructing OpExpr into Var/Const nodes > >deconstruct_opexpr is still failing to verify that the Var is a Var. >I'd try something like > > leftop = linitial(expr->args); > while (IsA(leftop, RelabelType)) > leftop = ((RelabelType *) leftop)->arg; > // and similarly for rightop > if (IsA(leftop, Var) && IsA(rightop, Const)) > // return appropriate results > else if (IsA(leftop, Const) && IsA(rightop, Var)) > // return appropriate results > else > // fail > Ah, right. The RelabelType might be on top of something that's not Var. >Also, I think deconstruct_opexpr is far too generic a name for what >this is actually doing. It'd be okay as a static function name >perhaps, but not if you're going to expose it globally. > I agree. I can't quite make it static, because it's used from multiple places, but I'll move it to extended_stats_internal.h (and I'll see if I can think of a better name too). >> a) I don't think unary-argument OpExpr are an issue, because this is >> checked when determining which clauses are compatible (and the code only >> allows the case with 2 arguments). > >OK. > >> b) Const with constisnull=true - I'm not yet sure what to do about this. >> The easiest fix would be to deem those clauses incompatible, but that >> seems a bit too harsh. The right thing is probably passing the NULL to >> the operator proc (but that means we can't use FunctionCall). > >No, because most of the functions in question are strict and will just >crash on null inputs. Perhaps you could just deem that cases involving >a null Const don't match what you're looking for. > Makes sense, I'll do that. >> Now, looking at this code, I wonder if using DEFAULT_COLLATION_OID when >> calling the operator is the right thing. We're using type->typcollation >> when building the stats, so maybe we should do the same thing here. > >Yeah, I was wondering that too. But really you should be using the >column's collation not the type's default collation. See commit >5e0928005. > OK, thanks. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services