Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uBYDd-003H1Y-UF for pgsql-bugs@arkaria.postgresql.org; Sun, 04 May 2025 12:13:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uBYDb-000Qjz-KD for pgsql-bugs@arkaria.postgresql.org; Sun, 04 May 2025 12:13:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uBYDb-000Qjq-1T for pgsql-bugs@lists.postgresql.org; Sun, 04 May 2025 12:13:15 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uBYDY-0006RG-0p for pgsql-bugs@lists.postgresql.org; Sun, 04 May 2025 12:13:13 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-441ab63a415so34884195e9.3 for ; Sun, 04 May 2025 05:13:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746360791; x=1746965591; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=QuiIEtAYsBrI5AcN6/elyPtLgEP2RDnw8M7xT5DGI0g=; b=RvUBzt1q+UmC9pvwQwWgU9PlRyByxDuqP5nsG1NPNTogFjdTvAWJWAuP25F+99aV7T g0xyqKAAfqSHsAmv/26n9KXRa3PadLN1n/JfZWQNmdEGWxJF4FQXyOP1Z8rkw242gf7/ J48RLFjY5bja7TzpLqx4kcggnmYI0gQ7eP5UPp50oWCG1khra5cbVebkd2JHeph4eK9S 1DYjoxzaswCVnLTd1Qbr+7ogB8h8EcPjneVQzTu1+r2uj6JL/aueIj07TGHnL2IXw9Sn iHBE2xINUHMZYKe2Lxpro12aSqt30AhaxIzHt9hHE+ywBKfiykRiqOD9vlFVlQpEa1zA eG4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746360791; x=1746965591; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QuiIEtAYsBrI5AcN6/elyPtLgEP2RDnw8M7xT5DGI0g=; b=bj4ejbVOrkCkvPmNeoKx9PJB+eoZpVo8idG4sS/NuzNNL5KMETnSydLapAe3HJLqBd f6nEhseGWR092TEzQEVKE0qCE4raR5vZnEAkry4UxYyU6lFPxmcY1dVyTfzF3HJqB9/w PLlM18C/pnHPLnpf2uMn4Cki9yoAofMz/94SulDXYhnFpmp/i+/qkNEAi5UwvXGab0oQ aoa66XQeeIaBvwgQhqwM9/wf9gh9oySrxBNvZl9y0MHyNXXngGoyvOXDnHADTIkjZvgA 71BFDN/y6JJanIj0v6rnzvk4hU1p4UMKU3PA04ES7TLZmLVLvQxYGRmc1ITOAHXsccEH Zo7g== X-Gm-Message-State: AOJu0YwReEU+mBUYBgXUFE+ge8AnotH6Nz3IQph4nXXCBHzODHKxjTKp YqO/wAepg8V+7zGtVEOGwuff6YGOnQ0EzUmWVIejQHZT9opxos8Q X-Gm-Gg: ASbGnctw/W3q6ACsJCm3yXgz9tL3HcR7hx51GL8/7tvhq+ovFtILP1APg6eh3Rqxczx gUJeCZhpYzMk19qhnAcsWY6nBzuE4BVCKtUwoXH2pd8BvRPl+CDVUWGogat/wvkLA4vHDUn9kst dZN3o3MwY/pim5cQk/ZHGrgEdlQVTnG8tCJ9vS/R9u9qjFh7YzPOXkxk9hrfcG5TX7S7uGthuLw LcoJFBafiwfvoXMHtGW/EgRKmz+y7sjRSVsttVHwvik+xKDsXoJ43OaIVgD70sOCe0AuyU+q6cQ d+HSlt0NdrvxDHnjoPY9MitM6C0jwpivOkAuLG8E5qK2VrYNJ0ir28ZXeqZAtpmwufPyb5qMfwe KL0ytEBqYBdY= X-Google-Smtp-Source: AGHT+IG2lUb+EOrQU9fepCjQOYNJiUX5VgRcUsBB9g8hfnkVXhv0qjL8ymqzhzbdSCNDxLHjWTux8A== X-Received: by 2002:a05:600c:524a:b0:43d:412e:8a81 with SMTP id 5b1f17b1804b1-441c1d72c41mr36880185e9.28.1746360790473; Sun, 04 May 2025 05:13:10 -0700 (PDT) Received: from [192.168.0.211] (84.123.234.224.dyn.user.ono.com. [84.123.234.224]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a099ae0cc9sm7211523f8f.9.2025.05.04.05.13.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 04 May 2025 05:13:10 -0700 (PDT) Message-ID: Date: Sun, 4 May 2025 14:13:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #18909: Query creates millions of temporary files and stalls To: Sergey Koposov , "tgl@sss.pgh.pa.us" Cc: "pgsql-bugs@lists.postgresql.org" , "a.rybakina" , Tomas Vondra , melanieplageman@gmail.com References: <18909-e5e1b702c9441b8a@postgresql.org> <2051617.1746223978@sss.pgh.pa.us> <5e98fba645682c40bf5f88ef858963ade9681956.camel@ed.ac.uk> <2177960.1746289626@sss.pgh.pa.us> <80bab58be182e80e73b5d5f71664ede9ce58f957.camel@ed.ac.uk> <8fc51cd7-115a-425a-828e-c99defb5afef@gmail.com> <0ce957be37434cfca4820d1e89a70cb2b456d3db.camel@ed.ac.uk> <2216372.1746309806@sss.pgh.pa.us> <2221240.1746312453@sss.pgh.pa.us> <9691212b-86f0-4476-940e-2e4ba0dd0cf9@gmail.com> <0a2ccb925ec37c93a143359de6381392779fdde4.camel@ed.ac.uk> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: <0a2ccb925ec37c93a143359de6381392779fdde4.camel@ed.ac.uk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/5/2025 13:45, Sergey Koposov wrote: > On Sun, 2025-05-04 at 09:52 +0200, Andrei Lepikhov wrote: >> On 4/5/2025 01:05, Sergey Koposov wrote: >>> The only thing I can add is that panstarrs1bestneighbour is ordered on disk by source_id. I don't know if that can be relevant. >> Seems promising. May you show us how much NULLs generates underlying >> JOIN operator. I guess, in your example the query should be close to the >> following: >> >> SELECT count(*) FROM disk_sample1 d >> LEFT JOIN gaia_edr3_aux.panstarrs1bestneighbour AS gaps1 >> ON (gaps1.original_ext_source_id = d.objid) >> WHERE gaps1.original_ext_source_id IS NULL; >> >> And just to be sure, show us the number of NULLs that may be generated >> by alternative LEFT JOIN: >> >> SELECT count(*) FROM gaia_edr3_aux.panstarrs1bestneighbour AS gaps1 >> LEFT JOIN gaia_edr3.gaia_source AS g >> ON (g.source_id = gaps1.source_id) >> WHERE g.source_id IS NULL; >> >> Also, show please the current value of the GUC hash_mem_multiplier. >> > > Here: > > wsdb=> > SELECT count(*) FROM disk_sample1 d > LEFT JOIN gaia_edr3_aux.panstarrs1bestneighbour AS gaps1 > ON (gaps1.original_ext_source_id = d.objid) > WHERE gaps1.original_ext_source_id IS NULL; > count > ---------- > 37635427 > (1 row) > > wsdb=> > SELECT count(*) FROM gaia_edr3_aux.panstarrs1bestneighbour AS gaps1 > LEFT JOIN gaia_edr3.gaia_source AS g > ON (g.source_id = gaps1.source_id) > WHERE g.source_id IS NULL; > count > ------- > 0 > (1 row) > > wsdb=> show hash_mem_multiplier ; > hash_mem_multiplier > --------------------- > 2 > (1 row) > > Sergey > The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336. Is e buidheann carthannais a th’ ann an Oilthigh Dhùn Èideann, clàraichte an Alba, àireamh clàraidh SC005336. Hmm. That means the bucket size to store tuples with NULL values should be at least 2.103GB (I think it will borrow more). So, my main conjecture is the extreme data skew issue - when we have said Postgres should use less memory than a single non-divisible batch may fit in. To solve it, we have at least two ongoing projects in this area. Project [1] is dedicated to resolving this issue using 'slices' by matching the too-massive batch to the corresponding batch on the other side of the join in small portions. Project [2] should allow the optimiser to take into account NULLs generated by the nullable side of the join, which will give more chances to avoid skewed HashJoin. Both these projects are not guaranteed to be committed even in PG 19. What's more, there is little chance they will be back-patched into v.15. So, if your problem is exactly with too many nulls, I would say you need a workaround: increment hash_mem_multiplier in case you are sure you may calculate the top boundary for the number of such nulls in this query. Or turn off hash join in this query at all. As for fixing the bug quickly (I have at least one report on this issue right now, too), I think we may invent two or three steps of (local) hash_mem_multiplier incrementation in case a massive not-breaking-down batch is detected. And definitely, it make sense for me to add an error message which will signal people what to do if they stuck into this problem. [1] https://www.postgresql.org/message-id/flat/7bed6c08-72a0-4ab9-a79c-e01fcdd0940f%40vondra.me [2] https://www.postgresql.org/message-id/flat/52b55b53-420a-722c-adbd-706922fc059b%40enterprisedb.com -- regards, Andrei Lepikhov