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 1m33Ma-0003oM-Io for pgsql-general@arkaria.postgresql.org; Mon, 12 Jul 2021 21:21:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m33MZ-0007Iu-3x for pgsql-general@arkaria.postgresql.org; Mon, 12 Jul 2021 21:21:15 +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 1m33MY-0007Ig-Qi for pgsql-general@lists.postgresql.org; Mon, 12 Jul 2021 21:21:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m33MW-0003G3-Mb for pgsql-general@lists.postgresql.org; Mon, 12 Jul 2021 21:21:13 +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 16CLL76n3164616; Mon, 12 Jul 2021 17:21:07 -0400 From: Tom Lane To: Jurrie Overgoor cc: pgsql-general@lists.postgresql.org Subject: Re: How to debug a connection that's "active" but hanging? In-reply-to: <1b783c37-0712-8b21-f1e3-3ac1f8c4f096@jurr.org> References: <2985639.1626111990@sss.pgh.pa.us> <1b783c37-0712-8b21-f1e3-3ac1f8c4f096@jurr.org> Comments: In-reply-to Jurrie Overgoor message dated "Mon, 12 Jul 2021 22:56:53 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3164614.1626124867.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Mon, 12 Jul 2021 17:21:07 -0400 Message-ID: <3164615.1626124867@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jurrie Overgoor writes: > Then I tried this. The query plans are indeed not consistent. > Most of the time the first line of the query plan is: Unique  > (cost=4892.35..4892.35 rows=1 width=64) [1] > I have seen other costs: 5818.30, 6350.85 and 6514.73. They all complete > correctly. [2], [3], [4] > The plan that leaves the query hanging in the 'active' state starts > with: Unique  (cost=241.81..241.82 rows=1 width=64) [5] > That's clearly much lower than the rest. So I suspect the planner making > a 'wrong' guess there, causing a bad plan, and a long time to execute. Yeah, evidently. > Now, where to go from here? The most likely bet here is that you're populating a table and then running a query on it before autovacuum has had a chance to catch up with what you did. Then the planner is working with obsolete stats or none at all, and it guesses wrong about what to do. The standard fix is to issue a manual ANALYZE on the table between the data-load and querying steps of your application. > Is this considered a bug in PostgreSQL, or > am I misusing the database engine by doing DROP DATABASE and CREATE > DATABASE over and over again? It's not a bug. I suppose in a perfect world the stats would automatically be up to date all the time, but in the real world it seems like the cost of that would be exorbitant. regards, tom lane