Skip to content

Commit 48e1291

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 c732c3f commit 48e1291

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,12 @@ btvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
933933
* double-counting some index tuples, so disbelieve any total that exceeds
934934
* the underlying heap's count ... if we know that accurately. Otherwise
935935
* this might just make matters worse.
936+
*
937+
* Posting list tuples are another source of inaccuracy. Cleanup-only
938+
* btvacuumscan calls assume that the number of index tuples can be used
939+
* as num_index_tuples, even though num_index_tuples is supposed to
940+
* represent the number of TIDs in the index. This naive approach can
941+
* underestimate the number of tuples in the index.
936942
*/
937943
if (!info->estimated_count)
938944
{
@@ -1394,11 +1400,18 @@ btvacuumpage(BTVacState *vstate, BlockNumber scanblkno)
13941400
* separate live tuples). We don't delete when backtracking, though,
13951401
* since that would require teaching _bt_pagedel() about backtracking
13961402
* (doesn't seem worth adding more complexity to deal with that).
1403+
*
1404+
* We don't count the number of live TIDs during cleanup-only calls to
1405+
* btvacuumscan (i.e. when callback is not set). We count the number
1406+
* of index tuples directly instead. This avoids the expense of
1407+
* directly examining all of the tuples on each page.
13971408
*/
13981409
if (minoff > maxoff)
13991410
attempt_pagedel = (blkno == scanblkno);
1400-
else
1411+
else if (callback)
14011412
stats->num_index_tuples += nhtidslive;
1413+
else
1414+
stats->num_index_tuples += maxoff - minoff + 1;
14021415

14031416
Assert(!attempt_pagedel || nhtidslive == 0);
14041417
}

0 commit comments

Comments
 (0)