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.96) (envelope-from ) id 1vxgIM-00H296-1s for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 07:05:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vxgIK-00Aw7I-3B for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 07:05:21 +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.96) (envelope-from ) id 1vxgIK-00Aw7A-0E for pgsql-hackers@lists.postgresql.org; Wed, 04 Mar 2026 07:05:21 +0000 Received: from fhigh-b8-smtp.messagingengine.com ([202.12.124.159]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vxgII-00000000KZK-25Tl for pgsql-hackers@lists.postgresql.org; Wed, 04 Mar 2026 07:05:19 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id 76C837A01B1; Wed, 4 Mar 2026 02:05:17 -0500 (EST) Received: from phl-imap-03 ([10.202.2.93]) by phl-compute-05.internal (MEProxy); Wed, 04 Mar 2026 02:05:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1772607917; x=1772694317; bh=BVY+L99fbaF0qo2TybLSmVnp877VvqIHOMjuX/X04DI=; b= m/XvazXRA0BZKc9lsPBO2lbKGbDGRIQhpV6YbV4kE5Dm/v2mz4e64VCABVJYL0mX hPR4DhAyOQ9CeIS/JeWMYlNDwoJjF5nvYHWuBcuOm0NHFL1zJGTM6DAz8E7YUm23 2jgz22YNz195fGx0YjCy5P/7GPvb+97cbu1wwQBZtg/b7gcF0NyEOunh0fRm8+M+ 2UxP3EkCNy3SAVg43iT29JndVFjq0ebOpwPl29QF4o8pjf404atOlPJOlJeecC0w DuAQHeYA6Z09p+Q5dBXletG77Rv6lXB8YDxPKgtNd4PKr2b8YZ3V31AKZBWTU60d juiMxQ4zgc/BqzqWEVTitw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1772607917; x= 1772694317; bh=BVY+L99fbaF0qo2TybLSmVnp877VvqIHOMjuX/X04DI=; b=G n12QUnA7XqJCGM+AgNYcLuehUdcpgWYZF8efnyKrXjvL3zTYzuhUSa7Whfgufu0X xEwyTlhz6QFWa/LCCiShQ98bZlvhNyZbDZDKS4i21T5B9RZqZiKdgbDk+hJK2oMV dUNmuFUwFneW8dQWCuDZYHkvLMrJYDkdar7xVl3K1U1hknRNsZxdr8ZhPYCQwN52 rfLLNMFZz1Qi6DT2O4/TY0h7vMQdMrjVumi/DNB/d9CtgOiobrnnY1jGDHuQ/6E7 WQTHMuYZVbyhZomLYLhnTUclMM4JiQ47wpHDIqAdry2yERDwec0OpJy7LVvoO5Rp 62Nwt+qlAp9cxXUwf6HpQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddviedvkeefucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvvefkjghfufgtgfesthejre dtredttdenucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestgho mhhpihhlvghrrdhorhhgqeenucggtffrrghtthgvrhhnpeefheelffegvdelfeevteejtd ekteefleeihfdtudelkeeijedtvefgueevieekieenucevlhhushhtvghrufhiiigvpedt necurfgrrhgrmhepmhgrihhlfhhrohhmpehjohgvlhestghomhhpihhlvghrrdhorhhgpd hnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehlihdr vghvrghnrdgthhgrohesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrg gtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopeht ghhlsehsshhsrdhpghhhrdhprgdruhhs X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 061B418E006C; Wed, 4 Mar 2026 02:05:17 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: Ak2UFnfuIevg Date: Wed, 04 Mar 2026 08:04:38 +0100 From: "Joel Jacobson" To: "Tom Lane" , "Chao Li" Cc: pgsql-hackers@lists.postgresql.org Message-Id: <3ccec807-d2ff-4150-9585-74cc52926646@app.fastmail.com> In-Reply-To: <1290278.1772571496@sss.pgh.pa.us> References: <3061845.1746486714@sss.pgh.pa.us> <496221.1748882849@sss.pgh.pa.us> <175507656113.993.1381684440543440253.pgcf@coridan.postgresql.org> <544D7C83-CECE-44E7-B5D7-530E9318D231@gmail.com> <616751.1755276726@sss.pgh.pa.us> <5C5EE031-F086-4353-A17A-DA563CD24DDD@gmail.com> <463296.1755553046@sss.pgh.pa.us> <659B2B36-C0FE-4642-921D-E7120E838891@gmail.com> <52163.1755988628@sss.pgh.pa.us> <1290278.1772571496@sss.pgh.pa.us> Subject: Re: Improve hash join's handling of tuples with null join keys Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Mar 3, 2026, at 21:58, Tom Lane wrote: > I wrote: >> Bug #19030 [1] seems to be a fresh report of the problem this patch >> aims to solve. While answering that, I realized that the v2 patch >> causes null-keyed inner rows to not be included in EXPLAIN ANALYZE's >> report of the number of rows output by the Hash node. Now on the >> one hand, what it's reporting is an accurate reflection of the >> number of rows in the hash table, which perhaps is useful. On the >> other hand, it's almost surely going to confuse users, and it's >> different from the number we produced before. Should we try to >> preserve the old behavior here? (I've not looked at what code >> changes would be needed for that.) > > I got around to looking at that finally. It's not terribly difficult > to fix, but while figuring out which counters were used for what, > I noticed a pre-existing bug: when ExecHashRemoveNextSkewBucket moves > tuples into the main hash table from the skew hash table, it fails to > adjust hashtable->skewTuples, meaning that subsequent executions of > ExecHashTableInsert will have the wrong idea of how many tuples are in > the main table. The error is probably not very large because the > skew table is not supposed to be big relative to the main table, > but still, it's wrong. So I tried to clean that up here. > > 0001 attached is the same patch as before (brought up to HEAD, but > only line numbers change). 0002 is the new code to fix these > tuple-counting issues. > > regards, tom lane I've tested v3-0001 and v3-0002 and can confirm the bug introduced in v3-0001 is fixed in v3-0002: % cat explain-analyze-problem.sql CREATE TABLE ea_hash (id int); INSERT INTO ea_hash SELECT g FROM generate_series(1, 10) g; INSERT INTO ea_hash SELECT NULL FROM generate_series(1, 90); ANALYZE ea_hash; CREATE TABLE ea_probe (id int); INSERT INTO ea_probe SELECT (g % 10) + 1 FROM generate_series(1, 10000) g; ANALYZE ea_probe; SET enable_nestloop = off; SET enable_mergejoin = off; EXPLAIN (COSTS OFF, ANALYZE, TIMING OFF, BUFFERS OFF, SUMMARY OFF) SELECT count(*) FROM ea_probe FULL OUTER JOIN ea_hash ON ea_probe.id = ea_hash.id; EXPLAIN (COSTS OFF, ANALYZE, TIMING OFF, BUFFERS OFF, SUMMARY OFF) SELECT count(*) FROM ea_probe RIGHT OUTER JOIN ea_hash ON ea_probe.id = ea_hash.id; % git diff --no-index master.out v3-0001.out diff --git a/master.out b/v3-0001.out index 1e05e7e39a6..54210c49757 100644 --- a/master.out +++ b/v3-0001.out @@ -17,8 +17,8 @@ SET -> Hash Full Join (actual rows=10090.00 loops=1) Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) - -> Hash (actual rows=100.00 loops=1) - Buckets: 1024 Batches: 1 Memory Usage: 12kB + -> Hash (actual rows=10.00 loops=1) + Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) @@ -29,8 +29,8 @@ SET -> Hash Right Join (actual rows=10090.00 loops=1) Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) - -> Hash (actual rows=100.00 loops=1) - Buckets: 1024 Batches: 1 Memory Usage: 12kB + -> Hash (actual rows=10.00 loops=1) + Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) % git diff --no-index v3-0001.out v3-0002.out diff --git a/v3-0001.out b/v3-0002.out index 54210c49757..17dfe335b0b 100644 --- a/v3-0001.out +++ b/v3-0002.out @@ -17,7 +17,7 @@ SET -> Hash Full Join (actual rows=10090.00 loops=1) Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) - -> Hash (actual rows=10.00 loops=1) + -> Hash (actual rows=100.00 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) @@ -29,7 +29,7 @@ SET -> Hash Right Join (actual rows=10090.00 loops=1) Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) - -> Hash (actual rows=10.00 loops=1) + -> Hash (actual rows=100.00 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) % git diff --no-index master.out v3-0002.out diff --git a/master.out b/v3-0002.out index 1e05e7e39a6..17dfe335b0b 100644 --- a/master.out +++ b/v3-0002.out @@ -18,7 +18,7 @@ SET Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) -> Hash (actual rows=100.00 loops=1) - Buckets: 1024 Batches: 1 Memory Usage: 12kB + Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) @@ -30,7 +30,7 @@ SET Hash Cond: (ea_probe.id = ea_hash.id) -> Seq Scan on ea_probe (actual rows=10000.00 loops=1) -> Hash (actual rows=100.00 loops=1) - Buckets: 1024 Batches: 1 Memory Usage: 12kB + Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on ea_hash (actual rows=100.00 loops=1) (7 rows) /Joel