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 1nLDQu-0004TH-9y for pgsql-hackers@arkaria.postgresql.org; Sat, 19 Feb 2022 00:17:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nLDQt-0003X0-5s for pgsql-hackers@arkaria.postgresql.org; Sat, 19 Feb 2022 00:17:03 +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 1nLDQs-0003Wr-Gm for pgsql-hackers@lists.postgresql.org; Sat, 19 Feb 2022 00:17:02 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nLDQo-0001GQ-QJ for pgsql-hackers@postgresql.org; Sat, 19 Feb 2022 00:17:01 +0000 Received: by mail-ed1-x531.google.com with SMTP id x5so18083610edd.11 for ; Fri, 18 Feb 2022 16:16:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=wS7zB6pbMWbY+lKuju2NBKLGMA5btKV6bOJa0bY+7nQ=; b=ZOGbynvp/kLH+OmmSr5wggsw975Zwyc39xDQn7bIvK7IUo9jM5ld9ILCle4r4owiU3 ioCqjexJHO0MkNuO5K9ZzKimEW4XsfGxIslaqiWLPZyR4zbnRQCRmDYp+bApGD/i2wvH WNSue8Zup0OBLoClqNsYMuuGVaPxT+QzvyNw7YeR3qZqY/Nip1yCpULPjpKuyz9LQtft jw3dyySAIwbDHyhyJkfIh8l0Ozw/nKIvpAddTLbETWd8Ug9wdA/Ih97/ii0RU/fMcSfE au45KajdptpqS6clAwll42unfXc+OzrxoGczfOSt2AY9j+EHa9JThPM2KSiGp+tfhDfc 6Kvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=wS7zB6pbMWbY+lKuju2NBKLGMA5btKV6bOJa0bY+7nQ=; b=EFQ/ExZS07+1826w5dWVu4OqvCmos1vomUW1reLvAQdxV5EbiJTz1d/ASv7nOVH7Uv BY3hkwoRou8aWttu5o4VliPxlcWSDtQVEz+Zxr9GM9NuNAhbbhSRRInGD/QsssKiBjrX FrejEimvXpgw93kF76OyuC9hetTxeuxBjksmxpOQMlxl8TGgfg7JrUwZvPL8Ot2g4pTv JR3fkT6ulWgyrosYFkCyS1DGhIpX+xeS+oj/w8mHlbyM4TDxBtLSxOTZgGBGW37u8vaf yslJrCMsCZc/NvAqncJqNA35CniN5pRj4wVm99UR6nD2ro2X9PuBbXCqIicuK9GIFtrG jGnA== X-Gm-Message-State: AOAM53344bKh9XZvSjd5rvNyUGaKGDTmEquigD4qXQitjEw3yebjKFjV KFauU6CzSEwXzj3aMPpsdoZ67y/eLF4mo/j8fi4Lz6gH3eSTnogfAgVQOJrrEFVyl3JL1FeqmD2 3k2dG0LgLR8OrzbKZ/5so1jyvGjiINX85sdJaeExSG23sX7tIeEox4VTpnGPUUYRLCL3thefpmw wpVOlxC/hYG/OXhoS9/TJsYnvZa1jfBJHOYzeY X-Google-Smtp-Source: ABdhPJylxG3nOt4ZTU8WAspuBF9w27MkcAvRVNcRaAe5gs2Jz//VKPoq1WROq0lFcPZwuqOr7tWSBQ== X-Received: by 2002:aa7:d2da:0:b0:410:b9f1:ff35 with SMTP id k26-20020aa7d2da000000b00410b9f1ff35mr10868086edr.217.1645229816626; Fri, 18 Feb 2022 16:16:56 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id 5sm2839209ejl.32.2022.02.18.16.16.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Feb 2022 16:16:56 -0800 (PST) Message-ID: Date: Sat, 19 Feb 2022 01:16:53 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Condition pushdown: why (=) is pushed down into join, but BETWEEN or >= is not? Content-Language: en-US To: Robert Haas Cc: Andy Fan , David Rowley , Tom Lane , Dmitry Astapov , PostgreSQL-development References: <1099349.1620834846@sss.pgh.pa.us> <1727507.1620948117@sss.pgh.pa.us> <260dd267-922e-2b98-fb46-6956d6e65fae@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 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 2/17/22 23:16, Robert Haas wrote: > On Thu, Feb 17, 2022 at 4:17 PM Tomas Vondra > wrote: >> IMHO the whole problem is we're unable to estimate the join clause as a >> conditional probability, i.e. >> >> P(A.x = B.x | (A.x < 42) & (B.x < 42)) >> >> so maybe instead of trying to generate additional RelOptInfo items we >> should think about improving that. The extra RelOptInfos don't really >> solve this, because even if you decide to join A|x<42 to B|x<42 it does >> nothing to improve the join clause estimate. > > I guess I hadn't considered that angle. I think the extra RelOptInfos > (or whatever) actually do solve a problem, because enforcing a > high-selectivity join qual against both sides is potentially quite > wasteful, and you need some way to decide whether to do it on one > side, the other, or both. But it's also true that I was wrong to > assume independence ... and if we could avoid assuming that, then the > join selectivity would work itself out without any of the machinery > that I just proposed. > True. We kinda already have this issue for the equality clauses, and having paths with the condition pushed down (or not) seems like a natural approach. >> It actually deals with a more general form of this case, because the >> clauses don't need to reference the same attribute - so for example this >> would work too, assuming there is extended stats object on the columns >> on each side: >> >> P(A.c = B.d | (A.e < 42) & (B.f < 42)) > > That'd be cool. > Yeah, but the patch implementing this still needs more work. >> Not sure. In my experience queries with both a join clause and other >> clauses referencing the same attribute are pretty rare. But I agree if >> we can do the expensive stuff only when actually needed, with no cost in >> the 99.999% other cases, I don't see why not. Of course, code complexity >> is a cost too. > > Right. I mean, we could have a planner GUC to control whether the > optimization is used even in cases where we see that it's possible. > But Tom keeps arguing that it is possible in many queries and would > benefit few queries, and I'm not seeing why that should be so. I think > it's likely to benefit many of the queries to which it applies. > Maybe. Although the example I linked some time ago shows a pretty dramatic improvement, due to picking merge join + index scan, and not realizing we'll have to skip a lot of data. But that's just one anecdotal example. Anyway, I think the best way to deal with these (perfectly legitimate) concerns is to show how expensive it is for queries not not having such join/restriction clauses, with the cost being close to 0. And then for queries with such clauses but not benefiting from the change (a bit like a worst case). regards [1] https://www.postgresql.org/message-id/CA%2B1Wm9U_sP9237f7OH7O%3D-UTab71DWOO4Qc-vnC78DfsJQBCwQ%40mail.gmail.com -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company