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 1qAffC-0003oD-Un for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jun 2023 23:49:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qAff9-0005E8-UN for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jun 2023 23:48:59 +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 1qAff9-0005Dy-J6 for pgsql-performance@lists.postgresql.org; Sat, 17 Jun 2023 23:48:59 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qAff5-0038RX-Ux for pgsql-performance@lists.postgresql.org; Sat, 17 Jun 2023 23:48:58 +0000 Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-9864d03e838so325935166b.2 for ; Sat, 17 Jun 2023 16:48:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1687045734; x=1689637734; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=Xg15uPSOG492WV670fjOVJOkU+BFtfYjC9a9uKIO7W0=; b=h12skobYugZF99IK22U06WlGkd9r2gOPS5woziriKfzSLESOefx2JTO35+xqFtkVmO BiSz6reox992UlNV0C9zJ155nJJfvWP9vtR86vKCe6fticCTbeNW8mElo6W7BqferaXG gUR67mtVaTeVyogstYmB063v5X6eLlsMrKiVruvZ/7E/ljy77tu/YhiFNG23g2Jbq0Q9 5qrKL4lRfI/wjBEpOe8fRgGq5iG0uG9snmZC0ZVgVf3clRZ4BID7ddlxYANNEyfMLg59 E5bvuUrEoOm24Nht4FSFSXYfJwA/E7F/9JOxTHv+mXGs+gj91o+w5yFAf6vBAMZ9LKMV 1Qmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687045734; x=1689637734; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Xg15uPSOG492WV670fjOVJOkU+BFtfYjC9a9uKIO7W0=; b=IiogYSoo5xmRVl20jqvuB9NoE3rTyJ/7YG3+Vu1Bgnhaniw3l6Egw44kRdnSNe/5Hs XMsXUbHInQkBmmU0B22Lsv9yd4xYCzUAbd1qne/41vMXoQxb+Pt0Is4Hx537w+swDVov wFQKqy7doUVPYSpRNwaQtinh2zSWwmwx6ke3dDFR2UqNYYexu6MDm2Nz26f8oMMrQjLd 5KHytleLnrcs+20xrYMwPpLtj7m53eRuOYm0hwCKyiCr+vLVCp1EeG+/lHET2wWl4Hr/ yWiU6C9guodB3jBS+5M0mKZmEdH+Y+I75t4T48e0YMGpPDDC6Lr5973NMFs9mNeE39EC i+hQ== X-Gm-Message-State: AC+VfDxGq4xcsjPZSscwRUTaS5v3gDF0D4qxlkbLowV1Ej9JBjVxTIko aKwzzfn+V7063U7PLKVG0kwL9A== X-Google-Smtp-Source: ACHHUZ750Is82oTeRQcI4EwH4/7nSeApTQi5OGoq8FUvE0WczQVc06MQh7WYKp+AOgrNd+oJei3q6A== X-Received: by 2002:a17:907:9623:b0:988:6e75:9808 with SMTP id gb35-20020a170907962300b009886e759808mr576347ejc.70.1687045734431; Sat, 17 Jun 2023 16:48:54 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id ke3-20020a17090798e300b00977cf84c42dsm12501276ejc.43.2023.06.17.16.48.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Jun 2023 16:48:53 -0700 (PDT) Message-ID: <07f2d1b3-c159-dee9-a059-1f71027fe8cd@enterprisedb.com> Date: Sun, 18 Jun 2023 01:48:51 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Subject: Re: Merge David and Goliath tables efficiently To: nicolas paris , pgsql-performance@lists.postgresql.org References: <28172511-8297-4E6D-9DD0-421B5CF5240A@riseup.net> <516e9f9f56314f3d812615b7e6aadfa0bf45e418.camel@riseup.net> Content-Language: en-US From: Tomas Vondra In-Reply-To: <516e9f9f56314f3d812615b7e6aadfa0bf45e418.camel@riseup.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/17/23 23:42, nicolas paris wrote: >>> My interpretation reading the query plan is: well sized small >>> batches of upserts leverage the indexes while the regular join >>> choose the sequential scan, including sorting and hashing which >>> takes forever time and resources including disk. >> >> You may be right, but it's hard to tell without seeing the query >> plan. > > Here are part of both plans: > I don't understand why you're sharing just a part of the plan and not the whole thing, ideally including the actual update ... Giving us the info in small pieces just means we need to guess and speculate. > Bad case (strategy 2.1): > > -> Merge Left Join (cost=530202629.03..255884257913.32 > rows=17023331531230 width=579) > Merge Cond: (david.list_id = ca.list_id) > -> Sort (cost=2019172.91..2024398.82 rows=2090361 width=569) > Sort Key: david.list_id > -> Append (cost=0.00..192152.41 rows=2090361 width=569) > -> Seq Scan on david_0 david_1 (cost=0.00..1812.52 > rows=20852 width=569) > -> Seq Scan on david_1 david_2 (cost=0.00..1800.09 > rows=20709 width=569) > -> Seq Scan on david_2 david_3 (cost=0.00..1794.44 > rows=20644 width=569) > Well, I kinda doubt you have 17023331531230 rows (not even physically possible with 2TB disk), so that's immediately suspicious. I'd bet the UPDATE ... FROM ... is missing a condition or something like that, which results in a cartesian product. > Good case (strategy 3): > > Merge on goliath_23 ca (cost=2139.75..11077.17 rows=0 width=0) > -> Nested Loop Left Join (cost=2139.75..11077.17 rows=1000 > width=575) > -> Limit (cost=2139.19..2495.67 rows=1000 width=569) > -> Index Scan using david_23_list_id_account_id_idx on > david_23 (cost=0.29..6794.16 rows=19058 width=569) > -> Index Scan using goliath_23_list_id_account_id_idx on > goliath_23 ca (cost=0.56..8.56 rows=1 width=14) > Index Cond: (list_id = david_23.list_id) > >> >> Sounds very much like you'd benefit from tuning some cost parameters >> to >> make the index scan look cheaper. >> Not sure what 'batched indexed join' would be, but it very much >> sounds >> like a nested loop with an index scan. > > Agreed, a 2M nested loop over index scan would likely work as well. > Would tuning the costs param could lead to get such large nested loop ? > It should be, but maybe let's see if there are other problems in the query itself. If it's generating a cartesian product, it's pointless to tune parameters. >> What PostgreSQL version are you using, what hardware? Did you tune it >> in >> any way, or is everything just default? > > It is pg 15.3, on 2 cores / 8GO / 2TO ssds, with defaults cloud > provider parameters (RDS). > I assume 2TO is 2TB? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company