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 1nXMbd-0002TC-CP for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Mar 2022 12:30:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nXMbb-0001gz-T2 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Mar 2022 12:30:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXM4H-00037L-62 for pgsql-bugs@lists.postgresql.org; Thu, 24 Mar 2022 11:55:53 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nXM4E-0004bW-2P for pgsql-bugs@lists.postgresql.org; Thu, 24 Mar 2022 11:55:51 +0000 Received: by mail-ed1-x52e.google.com with SMTP id y10so5321558edv.7 for ; Thu, 24 Mar 2022 04:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=M0OCzPO6FN+kQkTmxjeyQrOlWcQFUu+591RK/+tVZng=; b=axL0qFXloK4pPfXVskSwPLSrzPqDtqdyjzds5z8M9Eo8whsLBJcNDRdvpercQ0WdC4 hcVtv7YvNokxyTDxcCQ+01jlv5Bc9oPDcKrFWLV5phdIiCzKxy75MnF+tViYKPjBZj5z xotnNzRqlNiEj/CfEJBD7JccYbdNdbccpa6DZsh6gsSQ07QNb9I4x+cnAx7WGVAgMzQV aZUIjyOhVUOrB4RjaeToWIdQQkrnpRnd1XGV9Mvwi2m6A6BJtXtfQVeU5THEPuMfroVp eoCA6/UOYpjja6QkViWT81XSm2oPdaQpkyMosn5gwv7zSvHYUNPEk6AwZsWbWWH6cf+2 2U7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=M0OCzPO6FN+kQkTmxjeyQrOlWcQFUu+591RK/+tVZng=; b=GRbpDtpfXg47BShPyoZ/nR6NWdVZVENx8bCtlmIDlnSITgygHk0kjvzzyQ0w0ZbdNB mvImI7o1i+PEirFm07w0D8M4DwKfukXXfPm+CqIQFWJsO30NBq3dh5z8QHgm3FMt5HGS gIhfofCx3dGKOH8qtSyBxwv8d2XkNf5Q/TPp3OLbujTx7GUBDInYvgXupNVIFua0OfyA j97Qsw9W2xPA4M2xt/o0+NcdJpxbUbzFnj5adomUJCsQ7izDzTnohTpm+/qXgMiiI/pi TnU03NbfGaXoPtsrVzU8tQpoUY/WeXUTesfunga3NwHQ0sYL+6WhZ4+K/xiFmnu5Z+PX RHIw== X-Gm-Message-State: AOAM531wD/vmCjMdogZD8ju9wAY3Vx4WtScSuPc06xah9jJaHq/MLxrL Sqi1jAfpl333D4sw3E1RdnWKgCKYIKciJpBfxEEg3cClk+hfF+Kd7dUT81vEJSyOdGYh1bG+7fR /H0EmdJ0TMbw1TCFwHKzhRiRfq2/2aQ8/wZe0JLLoP/F3MGrYiGYvH9oIpNZc33fkitRu4Ekhli 07L+1o1cbJCTnjJadZmde0QFw1+V66B5OTwOBGtxmCpuLxaEZsQnAx X-Google-Smtp-Source: ABdhPJxyrGleqPX1dExgpAbx13GQ1oUHFq4+vjb6q9AMxWoIbR8REf8nRzPYlDyof+wkB5kZ5wiZ/g== X-Received: by 2002:aa7:cac4:0:b0:418:85b7:adec with SMTP id l4-20020aa7cac4000000b0041885b7adecmr6256701edt.231.1648122941097; Thu, 24 Mar 2022 04:55:41 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id d4-20020a1709067a0400b006d6e3ca9f71sm1034605ejo.198.2022.03.24.04.55.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Mar 2022 04:55:40 -0700 (PDT) Message-ID: <6e31a8c8-f74b-e4b3-f5d1-dce68e68abbf@enterprisedb.com> Date: Thu, 24 Mar 2022 12:55:41 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: Performance issue post upgrade on Version 13 - Incorrect Estimation Cost choosing Hash Aggregate-Nested Left Loop Join Content-Language: en-US To: Prajna Shetty , Tom Lane , "pgsql-bugs@lists.postgresql.org" , "pgsql-performance@postgresql.org" Cc: Beenu Sharma , Pranabesh Saha , Surya Susai References: <1872582.1647871148@sss.pgh.pa.us> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/22/22 13:57, Prajna Shetty wrote: > ++ _pgsql-performance@postgresql.org_ > >   > Hello Team, >   > There is change in query plan in 12.4 version and Version 13 resulting > in performance slowness post upgrade. >   > > * In 12.4 version, Sort Operation Group Aggregate is selected which > results to Merge Join. Query takes ~5 seconds. > * In 13.5 version, optimizer wrongly estimates and due to new Disk > Based Hash Aggregate feature, it prefers Hash Aggregate instead of > Sort Operation which finally blocks merge-join and chooses Nested > Loop Left Join. Query takes ~5 minutes. > >   > *_NOTE: _*Disabling Hash Aggregate on instance level forces optimizer to > choose merge operation but such instance level modification is not > possible in terms of Application Functionality. >   > This performance issue is on all over most of queries. Attached one of > the query and its plan in both version for reference in case that helps > for recreating the issue. >   It's impossible to comment those other queries, but chances are the root cause is the same. > Version 13 query plan has lower estimated cost than that of 12.4 which > implies 13.5 planner thought it found a better plan, but it is running > slower and actual cost show more. >   > 12.4 Version: > "Merge Right Join  (cost=*202198.78..295729.10* rows=1 width=8) (actual > time=1399.727..*5224.574* rows=296 loops=1)" >   > 13.5 version:- > "Nested Loop Left Join  (cost=*196360.90..287890.45* rows=1 width=8) > (actual time=3209.577..*371300.693* rows=296 loops=1)" >   This is not a costing issue, the problem is that we expect 1 row and calculate the cost for that, but then get 296. And unfortunately a nested loop degrades much faster than a merge join. I'm not sure why exactly 12.4 picked a merge join, chances are the costing formular changed a bit somewhere. But as I said, the problem is in bogus row cardinality estimates - 12.4 is simply lucky. The problem most likely stems from this part: -> GroupAggregate (cost=0.43..85743.24 rows=1830 width=72) (actual time=1.621..3452.034 rows=282179 loops=3) Group Key: student_class_detail.aamc_id Filter: (max((student_class_detail.class_level_cd)::text) = '4'::text) Rows Removed by Filter: 76060 -> Index Scan using uk_student_class_detail_aamcid_classlevelcd on student_class_detail (cost=0.43..74747.61 rows=1284079 width=6) (actual time=1.570..2723.014 rows=1272390 loops=3) Filter: (class_level_start_dt IS NOT NULL) Rows Removed by Filter: 160402 The filter is bound to be misestimated, and the error then snowballs. Try replacing this part with a temporary table (with pre-aggregated results) - you can run analyze on it, etc. I'd bet that'll make the issue go away. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company