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 1s9syD-005mLO-K4 for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 20:53:59 +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 1s9syD-003dL8-5R for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 20:53:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9syC-003dL0-Qs for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 20:53:56 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9syA-000GAC-7r for pgsql-general@postgresql.org; Wed, 22 May 2024 20:53:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 44MKrr0f014388; Wed, 22 May 2024 16:53:53 -0400 From: Tom Lane To: "Dirschel, Steve" cc: "pgsql-general@postgresql.org" Subject: Re: Hash join and picking which result set to build the hash table with. In-reply-to: References: <202405221100.fy66dsew2f52@alvherre.pgsql> <6806.1716408332@sss.pgh.pa.us> Comments: In-reply-to "Dirschel, Steve" message dated "Wed, 22 May 2024 20:42:39 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14386.1716411233.1@sss.pgh.pa.us> Date: Wed, 22 May 2024 16:53:53 -0400 Message-ID: <14387.1716411233@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Dirschel, Steve" writes: > The query and execution plan are shown below. My question is > related to the result set the optimizer is choosing to build the > hash table from. My understanding is for a hash join you want to > build the hash table out of the smaller result set. That's *a* consideration, but not the only one. We also consider whether the hash key has a flat distribution; if it is too skewed, we might find specific hash chains getting too long. > When running some tests I forgot to create the PK on table > docloc_test. When the PK was not on the table the optimizer decided > to create the hash table off the 1000 rows from collection. But as > soon as I put the PK on that table it then decides to use > docloc_test to build the hash table. I think that the presence of a unique index overrides the statistics (or the lack of any) so that the planner knows that the column is unique and thus safe to use as a hash key. Now, it should have known that anyway, unless maybe this is a freshly-built table that auto-analyze hasn't gotten to yet? regards, tom lane