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 1nP380-0004yG-8o for pgsql-performance@arkaria.postgresql.org; Tue, 01 Mar 2022 14:05:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nP37z-0000Rg-0p for pgsql-performance@arkaria.postgresql.org; Tue, 01 Mar 2022 14:05:23 +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 1nP37y-0000RW-BC for pgsql-performance@lists.postgresql.org; Tue, 01 Mar 2022 14:05:22 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nP37t-00083h-ND for pgsql-performance@postgresql.org; Tue, 01 Mar 2022 14:05:21 +0000 Received: by mail-wr1-x430.google.com with SMTP id r10so20724346wrp.3 for ; Tue, 01 Mar 2022 06:05:17 -0800 (PST) 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=vvAv6q1bed8ZLdee4uhDWl5SqM39thYyYTFkKvs+Sg0=; b=AEvAoxVuA5cKdNBBvDCsZ51iw+mwNNPLnldl6IXhV+7K4HpRwc0QjeO9315QoI6hDr a/hmS8OfCXemsdQ24WyNKkw17R1VCgOlcxEUeA4XQnxx2XZ4cLeiNlB6RlF3qX9lCtvl LDbYgAeW8jqfk+/jckj9AQD7jAR3Ovnqk8/OQERGpQ1JKcObJegGtjjYSlQHcxVA7lZe XTEIMn2sBWvmAO9Etnb7ZlMRDgVTEI+IJ/XV1SyGQmCShNvl3ZFVEZCX/AVoopvfJyFf fOihaFTZ+CITM+78hzYH3Jet6aNk/9YPiN3i0kDJQZMTlea5r/pj0pBWlLL97pBiohPA ZU5g== 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=vvAv6q1bed8ZLdee4uhDWl5SqM39thYyYTFkKvs+Sg0=; b=bgSfHcW0yO1RT3SkaBZEuHNr0xf2e/oAb+ilKxuzcm1DkUPJ2wf+tJiutjxsMTvpDw UnlVyQHuuNVz/4os/KQNyCwKERnpxHzc+fRnxqUXCaNDSprzi55C/Le5zj2OeKiXsb1r xPHymXBg6OA41UDnqQC51w84y2jDav78Kh62gSRsr81qxXOP88KhwM/6zdbmm+kC89tM SxL1Lf5n6LUrcJQbLCzGMfnTw7DcvcVFpOZamnQXog6xbsPv1GBvBxjsU4SWQ5Jta+Mh nPoxQm53sIL/FbgMRIj6M4GlXLNhByXn4ef0sHUAqCU+hfoM/WxA7tdYpQwSvZQW4C8w yRTQ== X-Gm-Message-State: AOAM532z/s5Ix73b2z9P3n0L/MeF7o+Xmdtfm5+l7eMJEE8noKh8GqVB oceSHZJZa5zBtblHv7rVaJTsbPUiMTQj6LkDc40et8ioLQ5ShthmzJ+k7XDnczlsir8EjbPbI9O iwW6lgJadQBQgs1Hjyze4Llq2r0CaFeezWJKAlvXjHxn/d+q9FkfbFHerCs00sWyWuc2rSDIpUW zdVIzy+3433x4MHoaFYRHTo3jeVs3C6+JKK9DFF6NtWnS/wFaump6oYbE= X-Google-Smtp-Source: ABdhPJziwAEkoMqCiCe+9WE0LwvJ4v6nJxdAaRc0m0lUr0yQHB1/NUGkn9lmlMitl8oJ8fgQ2h+FQg== X-Received: by 2002:a05:6000:50a:b0:1e3:5af:153e with SMTP id a10-20020a056000050a00b001e305af153emr19601127wrf.385.1646143516266; Tue, 01 Mar 2022 06:05:16 -0800 (PST) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id i12-20020adfe48c000000b001ef6eab4c81sm13382536wrm.96.2022.03.01.06.05.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Mar 2022 06:05:15 -0800 (PST) Message-ID: <01028050-efd3-06af-6ffd-57722acb9051@enterprisedb.com> Date: Tue, 1 Mar 2022 15:05:12 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Never Ending query in PostgreSQL Content-Language: en-US To: Jeff Janes , "Kumar, Mukesh" Cc: "pgsql-performance@postgresql.org" References: 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 2/27/22 18:20, Jeff Janes wrote: > > On Sun, Feb 27, 2022 at 7:09 AM Kumar, Mukesh > wrote: > > Hi Team,  > > Can you please help in tunning the attached query as , i am trying > to run this query and it runs for several hours and it did not give > any output. > > > Several hours is not all that long.  Without an EXPLAIN ANALYZE, we > could easily spend several hours scratching our heads and still get > nowhere.  So unless having this running cripples the rest of your > system, please queue up another one and let it go longer.  But first, do > an ANALYZE (and preferably a VACUUM ANALYZE) on all the tables.  If you > have a test db which is a recent clone of production, you could do it > there so as not to slow down production.  The problem is that the row > estimates must be way off (otherwise, it shouldn't take long) and if > that is the case, we can't use the plan to decide much of anything, > since we don't trust it. > I'd bet Jeff is right and poor estimates are the root cause. The pattern with a cascade of "nested loop" in the explain is fairly typical. This is likely due to the complex join conditions and correlation. > In parallel you could start evicting table joins from the query to > simplify it until it gets to the point where it will run, so you can > then see the actual row counts.  To do that it does help if you know > what the intent of the query is (or for that matter, the text of the > query--you attached the plan twice). > Right, simplify the query. Or maybe do it the other way around - start with the simplest query (the inner-most part of the explain) and add joins one by one (by following the explains) until it suddenly starts being much slower. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company