Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHCu7-0006SH-R1 for pgsql-general@arkaria.postgresql.org; Tue, 21 Nov 2017 18:04:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHCu6-0000kK-Qc for pgsql-general@arkaria.postgresql.org; Tue, 21 Nov 2017 18:04:14 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eHCu6-0000kA-KV for pgsql-general@lists.postgresql.org; Tue, 21 Nov 2017 18:04:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eHCu2-0003q3-SM for pgsql-general@postgresql.org; Tue, 21 Nov 2017 18:04:14 +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 vALI477Z012077; Tue, 21 Nov 2017 13:04:07 -0500 From: Tom Lane To: Cory Tucker cc: "pgsql-general@postgresql.org" Subject: Re: [GENERAL] Query Using Massive Temp Space In-reply-to: References: <16625.1511199408@sss.pgh.pa.us> Comments: In-reply-to Cory Tucker message dated "Tue, 21 Nov 2017 06:51:07 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12075.1511287447.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 21 Nov 2017 13:04:07 -0500 Message-ID: <12076.1511287447@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Cory Tucker writes: > On Mon, Nov 20, 2017 at 9:36 AM Tom Lane wrote: >> The only thing I can think of offhand that could create temp files far = in >> excess of the actual data volume is if a hash join repeatedly decides t= hat >> it needs to increase the number of hash batches. We have seen that hap= pen >> in cases where an inner relation contains a vast number of identical ha= sh >> key values: they all go into the same bucket, and the executor keeps >> splitting the batch trying to reduce its size, but being all identical >> values it will never be the case that some rows go into a different bat= ch >> than others. There is logic in there to give up splitting when this >> happens, but maybe it's not firing in your case, or maybe somebody brok= e >> it recently :-(. > What was happening is that there is a fairly non-unique "parent_id" valu= e > (66k times) that is creating a very large hashkey that it cannot break i= nto > a smaller chunks -- so, essentially what your guess was, Tom. Perhaps > worth investigating whether that code is still functioning as intended. I tried to duplicate this result, using a test case constructed like this: create table hj(f1 int, filler text); alter table hj alter column filler set storage plain; -- prevent compress= ion insert into hj select x, repeat('xyzzy', 4000/5) from generate_series(1,10= 00000) x; insert into hj select 42, repeat('xyzzy', 4000/5) from generate_series(1,1= 0000) x; vacuum analyze hj; set enable_mergejoin TO 0; set max_parallel_workers_per_gather TO 0; explain analyze select count(h1.filler),count(h2.filler) from hj h1, hj h2 where h1.f1 =3D= h2.f1; This causes a hash join between two datasets a bit under 4GB each, with the largest bucket being circa 40MB. work_mem is at the default 4MB, so that bucket will be large enough to force split attempts. What I see is that the temp disk space rises slowly to ~7.5GB and then declines smoothly to ~40MB, where it stays for awhile before the query finishes. The EXPLAIN ANALYZE report says Aggregate (cost=3D10020068069.47..10020068069.48 rows=3D1 width=3D16) (a= ctual time=3D81812.299..81812.299 rows=3D1 loops=3D1) -> Hash Join (cost=3D10001024835.00..10019362963.81 rows=3D141021131 = width=3D8008) (actual time=3D10227.585..67092.480 rows=3D101020000 loops=3D= 1) Hash Cond: (h1.f1 =3D h2.f1) -> Seq Scan on hj h1 (cost=3D0.00..515100.00 rows=3D1010000 wid= th=3D4008) (actual time=3D0.023..2531.739 rows=3D1010000 loops=3D1) -> Hash (cost=3D515100.00..515100.00 rows=3D1010000 width=3D400= 8) (actual time=3D10197.922..10197.922 rows=3D1010000 loops=3D1) Buckets: 1024 (originally 1024) Batches: 2097152 (original= ly 1024) Memory Usage: 39462kB -> Seq Scan on hj h2 (cost=3D0.00..515100.00 rows=3D10100= 00 width=3D4008) (actual time=3D0.003..2582.198 rows=3D1010000 loops=3D1) Planning time: 0.062 ms Execution time: 81832.867 ms Now, there's definitely something busted here; it should not have gone as far as 2 million batches before giving up on splitting. But that breakage isn't translating into disk space bloat. I haven't traced through the code yet, but I believe what's likely happening is that it's creating new batch files but freeing the old ones, so that the disk space usage remains constant during the fruitless split attempts. So there's some aspect of what you're seeing that this simple case doesn't capture. I wonder if you can build a publishable test case that shows the space bloat you're experiencing. The behavior should only depend on the hash join key values --- since those are apparently account numbers, maybe they're nonsensitive, or you could obscure them without changing the behavior? regards, tom lane