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 1lvd67-0004VF-TQ for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jun 2021 09:53:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lvd66-0004HF-HA for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jun 2021 09:53:34 +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 1lvd66-0004Gd-5F for pgsql-hackers@lists.postgresql.org; Tue, 22 Jun 2021 09:53:34 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lvd63-00047P-0S for pgsql-hackers@lists.postgresql.org; Tue, 22 Jun 2021 09:53:33 +0000 Received: by mail-ed1-x529.google.com with SMTP id m14so6108382edp.9 for ; Tue, 22 Jun 2021 02:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=dDo0yjMAjPLyNba/TtQy9JkyydWaundfvugiZ1WqMqc=; b=jOSISlwKwMlYVtqNFT2z0JCsI0jPOeDZv708GwmI9btkSPFCmIFW/u/23hp7AadC0H aYqWozbKzdkhcAQfiBuO9gUZL8cezFmrAtus9PGq3ELpQ5TDefJsoob/QM1JxlZMqs00 j7EX+goGWAo5iRHr6NeaTiuyfQMvYnPD+ZGPmDDkvs+4ilGUx6GMxRTAa5Xh4LuQ6+x3 /dlW+koyfvMR43m3bumxPlnn5IrheIxDZP/He3d/C58Yhx7Ibvz/tKMSWL8tIuLD8sdj FP9bTX/uLVCEk9h1M/Wgf1PNbYdfLGNykhe2Iyk0aI3x7+lTzArja1txQePrO8iY/GcE 3gqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=dDo0yjMAjPLyNba/TtQy9JkyydWaundfvugiZ1WqMqc=; b=s1fs1WeWliaII/zYE2y9Wshk4wFInHdXNpfksSZO4bQq8Ls0ENzh0ahr1QRI/3TSaV lEd9yF59sAnHqdLHr0rp08MU1CJqx1rZMQBKpO8Mt4VzEdvib7WuA/mj6DwC50S7mLaT OvQqhzx3I/LtKBD0WPskT/N0qIpa4bZUKM+wehzGgRI5J2DdRogeWbAdu22aOMYPwvaK p5ZYVcgD5MMNuVQybkz3Vth2hVxoCb0S/BuUpqFfUwRkLNgBFDJ+KExRKswJf3hImJL4 aVvABniItSCwT0zXbqUNVd5dlnpDxn9ufgtBAeQ6zq5VNWEqUS17qstO91s6C5rDzx+1 Kinw== X-Gm-Message-State: AOAM531ZC0Fhpsl7YFuK9sxJ5wfJhOX+FJNaAe3vSibB7AkgKx0lrHFD ViwfSxAyCM7bvizyOLLGTnGz55TRiNSiISLKy8PqFyJdTsH8n7CxC6t8jakPAAQVycHNuxSreH5 yWpC53YhpYbTJZkNGouzd0vUZS9giqXI6srvqXDD6mnJn67jxLNvtA9fv6JDACfrViR4eHgaE36 GKKSaCAdoVSXGf/uSKHDljB9ZtNRM44/ANpS77XFjeffhV26HBgk1jDP6FM3akPj8zX9wcRGEsN PwCD9ozddmNrCzDxknpx3F+mSQa3QyDMLV7Yz8gY7zycjneYQWAAAsI X-Google-Smtp-Source: ABdhPJwAMe/5dUO1Tabz6FQifmYj/iQtCpxZAsPCS7KG1mDovoYT6pj3eR1ZhdnMBKkPgVF/GfCXgQ== X-Received: by 2002:a05:6402:d69:: with SMTP id ec41mr3836826edb.19.1624355608544; Tue, 22 Jun 2021 02:53:28 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id da28sm7059322edb.0.2021.06.22.02.53.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Jun 2021 02:53:28 -0700 (PDT) Subject: Re: disfavoring unparameterized nested loops To: Peter Geoghegan , Bruce Momjian Cc: Tom Lane , Robert Haas , David Rowley , PostgreSQL Hackers References: <1648206.1624290948@sss.pgh.pa.us> <1650873.1624294359@sss.pgh.pa.us> <20210621235119.GE22121@momjian.us> From: Tomas Vondra Message-ID: Date: Tue, 22 Jun 2021 11:53:26 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.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 6/22/21 2:25 AM, Peter Geoghegan wrote: > On Mon, Jun 21, 2021 at 4:51 PM Bruce Momjian wrote: >> There were a lot of interesting ideas in this thread and I want to >> analyze some of them. First, there is the common assumption (not >> stated) that over-estimating by 5% and underestimating by 5% cause the >> same harm, which is clearly false. If I go to a restaurant and estimate >> the bill to be 5% higher or %5 lower, assuming I have sufficient funds, >> under or over estimating is probably fine. If I am driving and >> under-estimate the traction of my tires, I am probably fine, but if I >> over-estimate their traction by 5%, I might crash. > > My favorite analogy is the home insurance one: > > It might make sense to buy home insurance because losing one's home > (say through fire) is a loss that usually just cannot be tolerated -- > you are literally ruined. We can debate how likely it is to happen, > but in the end it's not so unlikely that it can't be ruled out. At the > same time I may be completely unwilling to buy insurance for personal > electronic devices. I can afford to replace all of them if I truly > have to. And the chances of all of them breaking or being stolen on > the same day is remote (unless my home burns down!). If I drop my cell > phone and crack the screen, I'll be annoyed, but it's certainly not > the end of the world. > > This behavior will make perfect sense to most people. But it doesn't > scale up or down. I have quite a few electronic devices, but only a > single home, so technically I'm taking risks way more often than I am > playing it safe here. Am I risk tolerant when it comes to insurance? > Conservative? > > I myself don't think that it is sensible to apply either term here. > It's easier to just look at the specifics. A home is a pretty > important thing to almost everybody; we can afford to treat it as a > special case. > >> If that is accurate, I think the big question is how common are cases >> where the outer side can't be proven to have zero or one row and nested >> loops are enough of a win to risk its greater sensitivity to >> misestimation. If it is uncommon, seems we could just code the >> optimizer to use hash joins in those cases without a user-visible knob, >> beyond the knob that already turns off nested loop joins. > > I think it's possible that Robert's proposal will lead to very > slightly slower plans in the vast majority of cases that are affected, > while still being a very good idea. Why should insurance be 100% free, > though? Maybe it can be in some cases where we get lucky, but why > should that be the starting point? It just has to be very cheap > relative to what we do today for us to come out ahead, certainly, but > that seems quite possible in at least this case. > Yeah, I like the insurance analogy - it gets to the crux of the problem, because insurance is pretty much exactly about managing risk. But making everything slower will be a hard sell, because wast majority of workloads already running on Postgres don't have this issue at all, so for them it's not worth the expense. Following the insurance analogy, selling tornado insurance in Europe is mostly pointless. Insurance is also about personal preference / risk tolerance. Maybe I'm fine with accepting risk that my house burns down, or whatever ... And the lack of data also plays role - the insurance company will ask for higher rates when it does not have enough accurate data about the phenomenon, or when there's a lot of unknowns. Maybe this would allow some basic measure of uncertainty, based on the number and type of restrictions, joins, etc. The more restrictions we have, the less certain the estimates are. Some conditions are estimated less accurately, and using default estimates makes it much less accurate. So maybe some fairly rough measure of uncertainty might work, and the user might specify how much risk it's willing to tolerate. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company