pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: Benedict Holland <benedict.m.holland@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Update blocking a select count(*)?
Date: Fri, 15 Jun 2012 14:32:10 -0500
Message-ID: <4FDB476A02000025000484F4@gw.wicourts.gov> (raw)
In-Reply-To: <CAD+mzowzWWea80Vthj_xNuiDOZzyqLpDZpjMgMSY-fHL-ewsCg@mail.gmail.com>
References: <CAD+mzoxfm8Wa5hrekeakG_TYNd_dsTR3W3DPZEV90chuzh0N_A@mail.gmail.com>
	<CAEYLb_WqEecnjr5o0DoNh15kNF_FXGkErVKw7fCGM5xxNLmrvQ@mail.gmail.com>
	<4FDB3C0202000025000484E4@gw.wicourts.gov>
	<CAD+mzozTqB8YiFQsgxU5mMqVo01wxVE0yiwoMHD_uSfSApKt7Q@mail.gmail.com>
	<CAD+mzoykMCtGaAKN9jOyH=nDF1E=EORcs4DSAbbKwvt4jJEXsQ@mail.gmail.com>
	<4FDB40B702000025000484EE@gw.wicourts.gov>
	<CAD+mzowzWWea80Vthj_xNuiDOZzyqLpDZpjMgMSY-fHL-ewsCg@mail.gmail.com>

Benedict Holland <benedict.m.holland@gmail.com> wrote:

> Yes. I needed to do a full vacuum. Again, the database is very
> large. I batch inserted quite a lot of data and then modified that
> data. The vacuum isn't blocking anything. It was blocking other
> tables (as expected) but continues to run and clean. My tables in
> general are around 10GB, each update seems to nearly double the
> size of it so I required a full vacuum.
 
I was trying to suggest techniques which would prevent that bloat
and make the VACUUM FULL unnecessary.  But, now that I've had a
chance to format the attachment into a readable format, I agree that
it isn't part of the problem.  The iceberg in this case is the ALTER
TABLE, which is competing with two other queries.
 
> The blocked statements are the select count(*) and the alter
> table.
 
OK.
 
> Both are blocked on the update table command.
 
Not directly.  The lock held by the UPDATE would *not* block the
SELECT; but it *does* block the ALTER TABLE command, which can't
share the table while it changes the structure of the table.  The
SELECT is blocked behind the ALTER TABLE.
 
> The alter table command SHOULD be blocked and that is fine.
 
I'm glad we're on the same page there.
 
> The select count(*) should never be blocked as that is the whole
> point of running an MVCC operation at least to my understanding. I
> can even accept the use case that the select should block with an
> Alter Table operation if data is retrieved from the table, but a
> select count(*) only returns the number of rows and should be
> table space independent.
 
In PostgreSQL SELECT count(*) must scan the table to see which rows
are visible to the executing database transaction.  Without that, it
can't give a completely accurate count from a transactional
perspective.  If you can settle for a non-transactional
approximation, select the reltuples value from the pg_class row for
the table.
 
> I also don't understand why a select count(*) requires an
> AccessShareLock. I don't understand why a select should lock
> anything at all.
 
So that the table isn't dropped or truncated while the count is
scanning the table.
 
-Kevin



view thread (18+ messages)  latest in thread

Message-ID: <4FDB476A02000025000484F4@gw.wicourts.gov>
Permalink:  ../4FDB476A02000025000484F4@gw.wicourts.gov/
Also on:    postgresql.org/message-id/4FDB476A02000025000484F4@gw.wicourts.gov

 · 

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-performance@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov, benedict.m.holland@gmail.com
  Subject: Re: Update blocking a select count(*)?
  In-Reply-To: <4FDB476A02000025000484F4@gw.wicourts.gov>
  References: <CAD+mzoxfm8Wa5hrekeakG_TYNd_dsTR3W3DPZEV90chuzh0N_A@mail.gmail.com> <CAEYLb_WqEecnjr5o0DoNh15kNF_FXGkErVKw7fCGM5xxNLmrvQ@mail.gmail.com> <4FDB3C0202000025000484E4@gw.wicourts.gov> <CAD+mzozTqB8YiFQsgxU5mMqVo01wxVE0yiwoMHD_uSfSApKt7Q@mail.gmail.com> <CAD+mzoykMCtGaAKN9jOyH=nDF1E=EORcs4DSAbbKwvt4jJEXsQ@mail.gmail.com> <4FDB40B702000025000484EE@gw.wicourts.gov> <CAD+mzowzWWea80Vthj_xNuiDOZzyqLpDZpjMgMSY-fHL-ewsCg@mail.gmail.com> <4FDB476A02000025000484F4@gw.wicourts.gov>

* 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