Re: Proposal: COUNT(*) (and related) speedup

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joshua Yanovski <pythonesque(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Proposal: COUNT(*) (and related) speedup
Date: 2014-04-04 17:19:39
Message-ID: 20929.1396631979@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Joshua Yanovski <pythonesque(at)gmail(dot)com> writes:
>> But worse, what happens if a count(*)
>> is in progress? It might or might not have scanned this page already,
>> and there's no way to get the right answer in both cases. Counter
>> updates done by VACUUM would have a similar race-condition problem.

> I don't think the first part really matters. If the page was visible
> when COUNT(*) started and then got dirtied by some other transaction,
> any changes by the second transaction shouldn't alter the actual count
> in our transaction anyway, so whether we scan the page needlessly or
> not seems beside the point.

The question is not whether you scan a page "needlessly" or not, it's
whether you double-count the tuples on it. I don't think it's possible to
be sure that when you add the central counter value into your local sum,
you are neither double-counting nor omitting pages whose all-visible state
changed while you were scanning the table.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2014-04-04 17:33:59 Another thought about search_path semantics
Previous Message Tom Lane 2014-04-04 17:14:38 Re: Using indices for UNION.