Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pHWSz-0000Qj-Qs for pgsql-general@arkaria.postgresql.org; Mon, 16 Jan 2023 20:52:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pHWSy-0005a0-QL for pgsql-general@arkaria.postgresql.org; Mon, 16 Jan 2023 20:52:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pHUna-0002tt-BH for pgsql-general@lists.postgresql.org; Mon, 16 Jan 2023 19:05:38 +0000 Received: from mout.gmx.net ([212.227.17.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pHUnX-0002hs-Ez for pgsql-general@lists.postgresql.org; Mon, 16 Jan 2023 19:05:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1673895932; bh=GMP54O6wJgTrVuiUebb2jK7A/Bu0Zq5eJNtuKwmTELI=; h=X-UI-Sender-Class:Date:From:To:cc:Subject:In-Reply-To:References; b=nCVaZi6NNRD7/avzUd7psV6W6YGiQ+SvQ77aOmuEThXE0CNFqfulx8RbD5A4XApwL JiNg9gPO6EjVRkoRNZc/PN8er+5AIm9HuGX+XVUhEHHChTyAkLw8Sm1XrnHR1xEYar WLUZ4/ZHEBTjQfjwch6sAKP6e5tGPh7RRil1n7lbzXXSEJZusZOCyPmNBMG5lBR/2l gfSE1mxJdq5Kw9X2V5MPzya29H/eLR0TwA/HlfLrj6haagHIBrkIC2UZSnifavbjKn EOfTkrcDTTD/dvHa/90j15VyCfwTFvKI9z4U8QBUtFCbWJ8In078iU/0l45pYzpcWx EkM29zqZDRD9A== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.9.70.36] ([185.55.107.82]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MNsw4-1p6XTw02x6-00OGfM; Mon, 16 Jan 2023 20:05:32 +0100 Date: Mon, 16 Jan 2023 20:05:30 +0100 (CET) From: Dimitrios Apostolou To: Tom Lane cc: David Rowley , pgsql-general@lists.postgresql.org Subject: Re: Why is a hash join preferred when it does not fit in work_mem In-Reply-To: <1007582.1673713044@sss.pgh.pa.us> Message-ID: <78211cab-8f6e-52e8-8d06-26e7097c43bd@gmx.net> References: <7ee75988-8408-36f8-3024-93dbf45b6f19@gmx.net> <1007582.1673713044@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-Provags-ID: V03:K1:NQBeuKUB8bABxNimAYawBWKUv2QQkoE0uzR9p2Pnwti3wKLdgl8 639wM5VGZdsOzTX7blMFvRLG/3jkiHelTGb5Te6S0kS4X37hJ4TUQ/TwNhaf8pEuVm1H6OE tZ8Ik6NeMSBtbKP6HflNKxiBqKH7tWhJuhhbzdG38JIueiM7CIeWzQLj2XSd9JN9d5iE0JI RDdTPT3R6P3IzmJ7yrLgg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:qce+Fw4CkpA=;EEows1G4FcXsFUyUjBprEnaAFdv RAYUA5qkCzNGDNhXVMPX0pYUQGclV2xTvHfVH3DgvDsJJHe46pilUmtxZoe0x88Y4up41lf2C bpXC2y37XhTs12pzW3obkfSShTriJeVJwkJ3k20Deq+/AxfhI0q9PZZ1t2LmV/t3tz2mqgS6k EgtUQiil+pGFu61a2TjbO2DqDaWDIYVaKyilwHi7zh5B1fmhk2odHiVMC39iEHgIWy7ROhARZ rZ16Nk1v2ttmQPNNqBAtP5KMcfjJM9AaAnRW6CQNYKDqRKyUe1lc+VFlqRVPZUNrnBg164pgq rwIcYIZgPL0FVsVutbNYxzeemgSashw0O5uUDnilJD7xvLVkNdhpSvudnLtOjgXjv+eYLCRQF XGkW0SoubF2tm9N5HHhxt+XxPNkfc66mgPhOuhLxMT3cGWFHT3TQcm8nqQbLXLZIGT+82QyWH GEQMnSmILPszRqeN2IV1TjoBUb1ghpB5baLI4hOE1SxZz3DF+r7EsOwr4rjnd4u9pFBLhB3bm Ehl4mBtzvjMzGYrGiRGu2QY/awQSl3QjiA3dntSAp1upRWSgQv0MVYzVVLGDie5EPgS1XsNdl ONEQNqCK9Af1Mm0J+K6Zsx8DPBcJGhVUawm1tLRizuKW9XVAm+w5Tm4RD1XtqWM/dqZZM95DN aqavECfUBMSQKrG3RUhqAHc1DjS5GdbVgO15y3xajQg0DdwLrBn3fidMVlIB0HV59z46kvga4 A4++Ww2Dl1MhkJw8V8/1E/s75jWckOVhxU5DkG3NbIK/u/iHKFgK1HjZhilts7IXgUR8TfW+S 81DNLNA7DjCEDjF4R/zhwEIVzTPqTE6NBJmDQhhiQaeLdcMZmsbQ46rrcKojQXbgVu9lAX9AT jGPPe8ilsGhxZbEn33quhGVmrxh18UNcd32vqk2aBeIipdABYvIPaUDdB Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 14 Jan 2023, Tom Lane wrote: > Dimitrios Apostolou writes: >> Please correct me if I'm wrong, as I'm a newcomer to PostgreSQL, but he= re >> is how I understand things according to posts I've read, and classical >> algorithms: > >> + The Hash Join is fastest when one side fits in work_mem. Then on one >> hand you have a hash table lookup (amortized O(1)) and on the other >> hand, if the table has M rows that that do not fit in memory, you ha= ve >> sequential reads from the disk (given low fragmentation of the table= or >> index files): For every line you read from the disk, you lookup the= key >> in the hash table. > >> If the hash table does not fit in RAM then the cost becomes prohibit= ive. >> Every lookup is a random access possibly hitting the disk. The total >> cost should be random_page_cost * M. > > That would be true of a simple hash join, but Postgres uses batched > hash joins: we split up the hash key space into subsets, where hopefully > each subset includes few enough inner-side rows to fit into work_mem. > While this can go wrong given pathological distribution of the inner-sid= e > keys, it does mean that the join can perform well even when the inner > side is much larger than work_mem. So it's not the case that the planne= r > will simply disregard hash joins beyond work_mem. It will apply a cost > penalty for the predicted batching overhead; Thanks for this, I found a page [1] that describes the hash join and now I understand a bit more. [1] https://www.interdb.jp/pg/pgsql03.html I'm not sure whether the key distribution is pathological in my case. The join condition is: Hash Cond: (tasks_mm_workitems.workitem_n =3D workitem_ids.workitem_n) and workitem_ids.workitem_n is an integer GENERATED AS IDENTITY and PUBLIC KEY. The TABLE workitem_ids har 1.7M rows, and the other table has 3.7M rows. None of them fit in workmem. In my (simplified and pathological) case of work_mem =3D=3D 1MB, the hash = join does 512 batches (Buckets: 4,096 Batches: 512 Memory Usage: 759kB). I'm not sure which hash-merge strategy is followed, but based on that document, it should be the "hybrid hash join with skew". I don't quite follow the I/O requirements of this algorithm, yet. :-) > but that can still come out > cheaper than merge join, because the sorting needed for merge is general= ly > also far from cheap. I was under the impression that on-disk merge-sort is a relatively cheap (logN) operation, regarding random I/O. > >> So I would expect an increased random_page_cost to benefit the Merge Jo= in >> algorithm. And since my setup involves spinning disks, it does makes se= nse >> to increase it. > > What is probably really happening is that random_page_cost affects the > estimated cost of performing the sort using an index scan instead of > a bespoke sort step. AFAIR, cost_sort doesn't consider random_page_cost > at all, and neither does cost_hashjoin. On the last EXPLAIN I posted for the forced merge-join, I see that it uses an index-scan on the "small" table. It makes sense since the join happens on the primary key of the table. On the large table it does not use an index scan, because an index doesn't exist for that column. It sorts the 3.7M rows of the table (and FWIW that table only has two integer columns). If I understood correctly what you meant with "performing the sort using an index scan". The problem I see is that the estimated cost of the sort operation is 609,372.91..618,630.40. It's already way above the whole hash-join cost (121,222.68..257,633.01). However the real timings are very different. Actual time for Sort is 4,602.569..5,414.072 ms while for the whole hash join it is 145,641.295..349,682.387 ms. Am I missing some configuration knobs to put some sense to the planner? Thanks, Dimitris