pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jurrie Overgoor <postgresql-mailinglist@jurr.org>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: How to debug a connection that's "active" but hanging?
Date: Mon, 12 Jul 2021 17:21:07 -0400
Message-ID: <3164615.1626124867@sss.pgh.pa.us> (raw)
In-Reply-To: <1b783c37-0712-8b21-f1e3-3ac1f8c4f096@jurr.org>
References: <e15b9bf5-4d1f-5abd-5e71-78894bea4916@jurr.org>
	<CAM+6J97mNj4gFnC=wYpsy5L+igEE8gwifVhaoXm-BE8BYX2PPQ@mail.gmail.com>
	<eb266d47-dd5c-3622-aa5a-fd45ed71d9de@jurr.org>
	<2985639.1626111990@sss.pgh.pa.us>
	<CAM+6J96Leay_mMqvP6kg6sqrD2KZrWYOmGkThictEz2PK=HhcA@mail.gmail.com>
	<1b783c37-0712-8b21-f1e3-3ac1f8c4f096@jurr.org>

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





view thread (8+ messages)  latest in thread

Message-ID: <3164615.1626124867@sss.pgh.pa.us>
Permalink:  ../3164615.1626124867@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3164615.1626124867@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, postgresql-mailinglist@jurr.org
  Subject: Re: How to debug a connection that's "active" but hanging?
  In-Reply-To: <3164615.1626124867@sss.pgh.pa.us>
  References: <e15b9bf5-4d1f-5abd-5e71-78894bea4916@jurr.org> <CAM+6J97mNj4gFnC=wYpsy5L+igEE8gwifVhaoXm-BE8BYX2PPQ@mail.gmail.com> <eb266d47-dd5c-3622-aa5a-fd45ed71d9de@jurr.org> <2985639.1626111990@sss.pgh.pa.us> <CAM+6J96Leay_mMqvP6kg6sqrD2KZrWYOmGkThictEz2PK=HhcA@mail.gmail.com> <1b783c37-0712-8b21-f1e3-3ac1f8c4f096@jurr.org> <3164615.1626124867@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox