Re: How to debug a connection that's "active" but hanging?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jurrie Overgoor <postgresql-mailinglist(at)jurr(dot)org>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: How to debug a connection that's "active" but hanging?
Date: 2021-07-12 21:21:07
Message-ID: 3164615.1626124867@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jurrie Overgoor <postgresql-mailinglist(at)jurr(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Luca Ferrari 2021-07-13 07:07:09 pg_wal lifecycle
Previous Message Jurrie Overgoor 2021-07-12 20:56:53 Re: How to debug a connection that's "active" but hanging?