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 1pwqve-0005KO-4c for pgsql-hackers@arkaria.postgresql.org; Wed, 10 May 2023 21:00:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pwqvc-0000gu-Mv for pgsql-hackers@arkaria.postgresql.org; Wed, 10 May 2023 21:00:52 +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 1pwqvc-0000gk-8d for pgsql-hackers@lists.postgresql.org; Wed, 10 May 2023 21:00:52 +0000 Received: from mail1.dalibo.net ([51.159.93.128] helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pwqvR-001qcm-Tr for pgsql-hackers@postgresql.org; Wed, 10 May 2023 21:00:51 +0000 Received: from karst (larco.ioguix.net [78.202.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id 3EAE51FBD6; Wed, 10 May 2023 23:00:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1683752432; bh=85rQkp/tfgdQ/c9r6o8d+LFfdW9j6o1o+jmtGms039E=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=fAux3wx2hJa7oM1VxAeUEubRcQLvvGjylSduoVhRRWrwwXqToXZ3nEj/2yzBMxXtV u9buV8hJLqWVzIEkYNaocH5rQ+pzNJHBr43rV38QlnCHItTbZocgW8PT+k0hl2EmuW mVzm6bPc5R6FCx8Pe2toxPaR1yNLqqUaAhOT769k= Date: Wed, 10 May 2023 23:00:31 +0200 From: Jehan-Guillaume de Rorthais To: Thomas Munro Cc: pgsql-hackers Subject: Re: Unlinking Parallel Hash Join inner batch files sooner Message-ID: <20230510230031.4d710fd1@karst> In-Reply-To: References: Organization: Dalibo MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, Thanks for working on this! On Wed, 10 May 2023 15:11:20 +1200 Thomas Munro wrote: > One complaint about PHJ is that it can, in rare cases, use a > surprising amount of temporary disk space where non-parallel HJ would > not. When it decides that it needs to double the number of batches to > try to fit each inner batch into memory, and then again and again > depending on your level of bad luck, it leaves behind all the earlier > generations of inner batch files to be cleaned up at the end of the > query. That's stupid. Here's a patch to unlink them sooner, as a > small improvement. This patch can indeed save a decent amount of temporary disk space. Considering its complexity is (currently?) quite low, it worth it. > The reason I didn't do this earlier is that sharedtuplestore.c > continues the pre-existing tradition where each parallel process > counts what it writes against its own temp_file_limit. At the time I > thought I'd need to have one process unlink all the files, but if a > process were to unlink files that it didn't create, that accounting > system would break. Without some new kind of shared temp_file_limit > mechanism that doesn't currently exist, per-process counters could go > negative, creating free money. In the attached patch, I realised > something that I'd missed before: there is a safe point for each > backend to unlink just the files that it created, and there is no way > for a process that created files not to reach that point. Indeed. For what it worth, from my new and non-experienced understanding of the parallel mechanism, waiting for all workers to reach WAIT_EVENT_HASH_GROW_BATCHES_REPARTITION, after re-dispatching old batches in new ones, seems like a safe place to instruct each workers to clean their old temp files. > Here's an example query that tries 8, 16 and then 32 batches on my > machine, because reltuples is clobbered with a bogus value. Nice! Regards,