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 1hO1Ip-0006GU-Ti for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2019 14:42:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hO1Io-0001I9-IL for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2019 14:42:42 +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 1hO1Io-0001Fc-5T for pgsql-hackers@lists.postgresql.org; Tue, 07 May 2019 14:42:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hO1Il-00054F-R2 for pgsql-hackers@postgresql.org; Tue, 07 May 2019 14:42:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x47EgamG027779; Tue, 7 May 2019 10:42:36 -0400 From: Tom Lane To: Tomas Vondra cc: Thomas Munro , Melanie Plageman , hackers Subject: Re: accounting for memory used for BufFile during hash joins In-reply-to: <20190507131742.nixczzyyyuuoig7m@development> References: <20190504003414.bulcbnge3rhwhcsh@development> <20190507031511.lvp6bqn2e5dpsnga@development> <27902.1557199108@sss.pgh.pa.us> <20190507131742.nixczzyyyuuoig7m@development> Comments: In-reply-to Tomas Vondra message dated "Tue, 07 May 2019 15:17:42 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27777.1557240156.1@sss.pgh.pa.us> Date: Tue, 07 May 2019 10:42:36 -0400 Message-ID: <27778.1557240156@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. regards, tom lane