Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 02c9386

Browse files
Fix nbtree cleanup-only VACUUM stats inaccuracies.
Logic for counting heap TIDs from posting list tuples (added by commit 0d861bb) was faulty. It didn't count any TIDs/index tuples in the event of no callback being set. This meant that we incorrectly counted no index tuples in clean-up only VACUUMs, which could lead to pg_class.reltuples being spuriously set to 0 in affected indexes. To fix, go back to counting items from the page in cases where there is no callback. This approach isn't very accurate, but it works well enough in practice while avoiding the expense of accessing every index tuple during cleanup-only VACUUMs. Author: Peter Geoghegan <pg@bowt.ie> Reported-By: Jehan-Guillaume de Rorthais <jgdr@dalibo.com> https://postgr.es/m/20201023174451.69e358f1@firost Backpatch: 13-, where nbtree deduplication was introduced
1 parent 82d4a2a commit 02c9386

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/backend/access/nbtree/nbtree.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,12 @@ btvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
926926
* double-counting some index tuples, so disbelieve any total that exceeds
927927
* the underlying heap's count ... if we know that accurately. Otherwise
928928
* this might just make matters worse.
929+
*
930+
* Posting list tuples are another source of inaccuracy. Cleanup-only
931+
* btvacuumscan calls assume that the number of index tuples can be used
932+
* as num_index_tuples, even though num_index_tuples is supposed to
933+
* represent the number of TIDs in the index. This naive approach can
934+
* underestimate the number of tuples in the index.
929935
*/
930936
if (!info->estimated_count)
931937
{
@@ -1389,11 +1395,18 @@ btvacuumpage(BTVacState *vstate, BlockNumber scanblkno)
13891395
* separate live tuples). We don't delete when backtracking, though,
13901396
* since that would require teaching _bt_pagedel() about backtracking
13911397
* (doesn't seem worth adding more complexity to deal with that).
1398+
*
1399+
* We don't count the number of live TIDs during cleanup-only calls to
1400+
* btvacuumscan (i.e. when callback is not set). We count the number
1401+
* of index tuples directly instead. This avoids the expense of
1402+
* directly examining all of the tuples on each page.
13921403
*/
13931404
if (minoff > maxoff)
13941405
attempt_pagedel = (blkno == scanblkno);
1395-
else
1406+
else if (callback)
13961407
stats->num_index_tuples += nhtidslive;
1408+
else
1409+
stats->num_index_tuples += maxoff - minoff + 1;
13971410

13981411
Assert(!attempt_pagedel || nhtidslive == 0);
13991412
}

0 commit comments

Comments
 (0)