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 1hO1j5-0007SU-4G for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2019 15:09:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hO1j2-0002lU-5j for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2019 15:09:48 +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 1hO1j1-0002lN-TH for pgsql-hackers@lists.postgresql.org; Tue, 07 May 2019 15:09:47 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hO1ix-0001FV-EG for pgsql-hackers@postgresql.org; Tue, 07 May 2019 15:09:47 +0000 Received: by mail-wm1-x344.google.com with SMTP id b10so21006282wmj.4 for ; Tue, 07 May 2019 08:09:42 -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=/uzMyuCRf71yM59ZiHArsLJHg/AWORLkMCkdMJLzhq8=; b=pIA521qDCbTm5rHAivp8vgVZJ5YTD0kTLq5Yabgy5vJzoE15mCr3PCQsMykoWWaUza cu+guLO8GXzSDJWL7t6wMpq5beJ+JYIwuzOp8Kee9vLgs0S+xmJgc0Quc3vztptOBpmY OIExlKsn8/Bu1ql65m3gbGZigwivil5Bc2wYNOYZrPNrFozFA6Bm6m3lx5KNLEgHRo6f EXgK3eyaP38DT/YxVdm6qUKDQGgc0EP4jD9CARpAplQ3W/HLHS/DdBceGdl4Cg1nRc35 vgyePktIQmglmJCt/rEJAJ1Li1B4Nc0FCwFp1HU56s6YiHcYGVpQkjwFp0MJua/7Ps85 N1WA== 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=/uzMyuCRf71yM59ZiHArsLJHg/AWORLkMCkdMJLzhq8=; b=eRIRp6TWkfk+YWl32zSaOvjU3uFRc3eYZsUiCneVA3kyAYPb+aeseycu8RguIyexsF 6WWYm43lSVnFOfTiAVpoKFi+xG5v8VcUlZAjNla2F3Opf170cjs86pRe8IXvznluQ5Bb m1N/84ap68Gybc8IEnuRQdJqmT3qeUwdC7HYNH6zUPLpXq6cXb2Gpi+Ig7kH1Jc5sgDq 97cbjuArICfVLtqDuUmAmNWrXwQ3ALgXrModSAcYGWSKFOl4003A0kVnaN7dy3vILKte QND2w96ZhM7lO5B1LYef8RZkvb7hDAvR5YS8JbuyAaFuRcDOwB0UCKFP1N82NQcZdEn7 R47A== X-Gm-Message-State: APjAAAUVIAklZrbnsmYtoUZgs9jyTBJtAKUQVYEsG5cuwUgsb4X7Fnhd mEDK9cXQxNwidgFkeYCNapY/lQ== X-Google-Smtp-Source: APXvYqzMUBYluzAjxk86B8PrPwvRlu23uY2g7KPmpiaTcxWYUWirb8CUrGnxQsKB/hmfBEEMx9SyXg== X-Received: by 2002:a1c:9609:: with SMTP id y9mr19913080wmd.27.1557241780067; Tue, 07 May 2019 08:09:40 -0700 (PDT) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id u11sm24146361wrg.35.2019.05.07.08.09.32 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 07 May 2019 08:09:33 -0700 (PDT) Date: Tue, 7 May 2019 17:09:31 +0200 From: Tomas Vondra To: Tom Lane Cc: Thomas Munro , Melanie Plageman , hackers Subject: Re: accounting for memory used for BufFile during hash joins Message-ID: <20190507150931.g74bhqqzb6y6ivxj@development> References: <20190504003414.bulcbnge3rhwhcsh@development> <20190507031511.lvp6bqn2e5dpsnga@development> <27902.1557199108@sss.pgh.pa.us> <20190507131742.nixczzyyyuuoig7m@development> <27778.1557240156@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <27778.1557240156@sss.pgh.pa.us> User-Agent: NeoMutt/20180716-1444-295967-dirty List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, May 07, 2019 at 10:42:36AM -0400, Tom Lane wrote: >Tomas Vondra writes: >> On Mon, May 06, 2019 at 11:18:28PM -0400, Tom Lane wrote: >>> Tomas Vondra writes: >>>> Do we actually check how many duplicates are there during planning? > >>> Certainly that's part of the planner's cost estimates ... but it's >>> only as good as the planner's statistical knowledge. > >> I'm looking at the code, and the only place where I see code dealing with >> MCVs (probably the best place for info about duplicate values) is >> estimate_hash_bucketsize in final_cost_hashjoin. > >What I'm thinking of is this bit in final_cost_hashjoin: > > /* > * If the bucket holding the inner MCV would exceed work_mem, we don't > * want to hash unless there is really no other alternative, so apply > * disable_cost. (The executor normally copes with excessive memory usage > * by splitting batches, but obviously it cannot separate equal values > * that way, so it will be unable to drive the batch size below work_mem > * when this is true.) > */ > if (relation_byte_size(clamp_row_est(inner_path_rows * innermcvfreq), > inner_path->pathtarget->width) > > (work_mem * 1024L)) > startup_cost += disable_cost; > >It's certainly likely that that logic needs improvement in view of this >discussion --- I was just pushing back on the claim that we weren't >considering the issue at all. > Ah, this code is new in 11, and I was looking at code from 10 for some reason. I don't think we can do much better than this, except perhaps falling back to (1/ndistinct) when there's no MCV available. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services