Re: How to debug a connection that's "active" but hanging? - Mailing list pgsql-general

From Tom Lane
Subject Re: How to debug a connection that's "active" but hanging?
Date
Msg-id 3164615.1626124867@sss.pgh.pa.us
Whole thread Raw
In response to Re: How to debug a connection that's "active" but hanging?  (Jurrie Overgoor <postgresql-mailinglist@jurr.org>)
Responses Re: How to debug a connection that's "active" but hanging?  (Jurrie Overgoor <postgresql-mailinglist@jurr.org>)
List pgsql-general
Jurrie Overgoor <postgresql-mailinglist@jurr.org> 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



pgsql-general by date:

Previous
From: Jurrie Overgoor
Date:
Subject: Re: How to debug a connection that's "active" but hanging?
Next
From: Luca Ferrari
Date:
Subject: pg_wal lifecycle