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.98.2) (envelope-from ) id 1x8OmJ-00000001Awz-2e8I for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Sep 2026 21:08:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x8OmH-00000007A2m-3FsB for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Sep 2026 21:08:49 +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.98.2) (envelope-from ) id 1x8OmH-00000007A2d-1qPB for pgsql-bugs@lists.postgresql.org; Sun, 20 Sep 2026 21:08:49 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x8OmE-00000000OAR-3DqI for pgsql-bugs@lists.postgresql.org; Sun, 20 Sep 2026 21:08:48 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 68KL8hhU552620; Sun, 20 Sep 2026 17:08:43 -0400 From: Tom Lane To: Alexandre Felipe cc: yanarnold5@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19708: Hash Join becomes about 300x slower with higher work_mem In-reply-to: References: <19708-bca71f8de0d45605@postgresql.org> Comments: In-reply-to Alexandre Felipe message dated "Sun, 20 Sep 2026 21:05:17 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <552618.1789938523.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 20 Sep 2026 17:08:43 -0400 Message-ID: <552619.1789938523@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alexandre Felipe writes: > Your query grows with the 4th power of the number of rows, and the table > statistics show 2910 rows for that table. Actually there aren't any statistics. Rather than trust the observed fact that the table is of size zero, the planner assumes it's 10 pages, and then 2910 rows is what could be expected to fit with zero-column rows. (The alternative of trusting the table to be empty is not better: it leads to planning failures in the other direction where we make a plan for trivial amounts of data and then it runs forever because there's more data than the planner thought.) > So the plan estimates 211 quadrillion rows, Yeah. Specifically, the CTE is estimated to produce 2910*2910*768 rows, and then the planner thinks it's dealing with a darn big hash join, so it instructs the executor to set up for that: -> Hash (cost=3D130070016.00..130070016.00 rows=3D6503500800 width=3D= 4) (actual time=3D5.853..5.853 rows=3D768.00 loops=3D1) Buckets: 4194304 Batches: 4096 Memory Usage: 32768kB It's the overhead of setting up and tearing down all those batches that is making the query take so long. (If you don't suppress the timing figures, you'll see that that overhead is charged to the Hash Join node not the Hash node, which is a bit of an implementation artifact.) If we actually did have that much data to contend with, of course the setup overhead would be negligible, but with a trivial amount of actual data it dominates the runtime. Reducing work_mem reduces this overhead by constraining how much memory the executor is allowed to allocate --- but that would be a pretty bad idea if there actually were a lot of rows to join. > If you run an analyse here you get an accurate estimate of the number ro= ws > in the table. Indeed. So I think this is an uninteresting contrived case. regards, tom lane