You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
VACUUM can skip heap pages altogether when there's a run of consecutivepages that are all-visible according to the visibility map. This causes itto not update its nonempty_pages count, just as if those pages were empty,which means that at the end we will think they are candidates for deletion.Thus, we may take the table's AccessExclusive lock only to find that nopages are really truncatable. This usually causes no real problems on amaster server, thanks to the lock being acquired only conditionally; but onhot-standby servers, the same lock must be acquired unconditionally whichcan result in unnecessary query cancellations.To improve matters, force examination of the table's last page wheneverwe reach there with a nonempty_pages count that would allow a truncationattempt. If it's not empty, we'll advance nonempty_pages and therebyprevent the truncation attempt.If we are unable to acquire cleanup lock on that page, there's no need toforce it, unless we're doing an anti-wraparound vacuum. We can just checkfor tuples with a shared buffer lock and then give up. (When we are doingan anti-wraparound vacuum, and decide it's okay to skip the page because itcontains no freezable tuples, this patch still improves matters becausenonempty_pages is properly updated, which it was not before.)Since only the last page is special-cased in this way, we might attempt atruncation that will release many fewer pages than the normal heuristicwould suggest; at worst, only one page would be truncated. But that seemsall right, because the situation won't repeat during the next vacuum.The real problem with the old logic is that the useless truncation attempthappens every time we vacuum, so long as the state of the last few dozenpages doesn't change.This is a longstanding deficiency, but since the consequences aren't verysevere in most scenarios, I'm not going to risk a back-patch.Jeff Janes and Tom Lane