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

Commit c242baa

Browse files
Consider triggering VACUUM failsafe during scan.
The wraparound failsafe mechanism added by commit 1e55e7d handled the one-pass strategy case (i.e. the "table has no indexes" case) by adding a dedicated failsafe check. This made up for the fact that the usual one-pass checks inside lazy_vacuum_all_indexes() cannot ever be reached during a one-pass strategy VACUUM. This approach failed to account for two-pass VACUUMs that opt out of index vacuuming up-front. The INDEX_CLEANUP off case in the only case that works like that. Fix this by performing a failsafe check every 4GB during the first scan of the heap, regardless of the details of the VACUUM. This eliminates the special case, and will make the failsafe trigger more reliably. Author: Peter Geoghegan <pg@bowt.ie> Reported-By: Andres Freund <andres@anarazel.de> Reviewed-By: Masahiko Sawada <sawada.mshk@gmail.com> Discussion: https://postgr.es/m/20210424002921.pb3t7h6frupdqnkp@alap3.anarazel.de
1 parent 713a431 commit c242baa

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/backend/access/heap/vacuumlazy.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@
110110
#define BYPASS_THRESHOLD_PAGES 0.02 /* i.e. 2% of rel_pages */
111111

112112
/*
113-
* When a table is small (i.e. smaller than this), save cycles by avoiding
114-
* repeated failsafe checks
113+
* Perform a failsafe check every 4GB during the heap scan, approximately
115114
*/
116-
#define FAILSAFE_MIN_PAGES \
115+
#define FAILSAFE_EVERY_PAGES \
117116
((BlockNumber) (((uint64) 4 * 1024 * 1024 * 1024) / BLCKSZ))
118117

119118
/*
@@ -890,6 +889,7 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
890889
BlockNumber nblocks,
891890
blkno,
892891
next_unskippable_block,
892+
next_failsafe_block,
893893
next_fsm_block_to_vacuum;
894894
PGRUsage ru0;
895895
Buffer vmbuffer = InvalidBuffer;
@@ -919,6 +919,7 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
919919

920920
nblocks = RelationGetNumberOfBlocks(vacrel->rel);
921921
next_unskippable_block = 0;
922+
next_failsafe_block = 0;
922923
next_fsm_block_to_vacuum = 0;
923924
vacrel->rel_pages = nblocks;
924925
vacrel->scanned_pages = 0;
@@ -1130,6 +1131,20 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
11301131

11311132
vacuum_delay_point();
11321133

1134+
/*
1135+
* Regularly check if wraparound failsafe should trigger.
1136+
*
1137+
* There is a similar check inside lazy_vacuum_all_indexes(), but
1138+
* relfrozenxid might start to look dangerously old before we reach
1139+
* that point. This check also provides failsafe coverage for the
1140+
* one-pass strategy case.
1141+
*/
1142+
if (blkno - next_failsafe_block >= FAILSAFE_EVERY_PAGES)
1143+
{
1144+
lazy_check_wraparound_failsafe(vacrel);
1145+
next_failsafe_block = blkno;
1146+
}
1147+
11331148
/*
11341149
* Consider if we definitely have enough space to process TIDs on page
11351150
* already. If we are close to overrunning the available space for
@@ -1375,17 +1390,12 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
13751390
* Periodically perform FSM vacuuming to make newly-freed
13761391
* space visible on upper FSM pages. Note we have not yet
13771392
* performed FSM processing for blkno.
1378-
*
1379-
* Call lazy_check_wraparound_failsafe() here, too, since we
1380-
* also don't want to do that too frequently, or too
1381-
* infrequently.
13821393
*/
13831394
if (blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES)
13841395
{
13851396
FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
13861397
blkno);
13871398
next_fsm_block_to_vacuum = blkno;
1388-
lazy_check_wraparound_failsafe(vacrel);
13891399
}
13901400

13911401
/*
@@ -2558,7 +2568,6 @@ lazy_check_needs_freeze(Buffer buf, bool *hastup, LVRelState *vacrel)
25582568
/*
25592569
* Trigger the failsafe to avoid wraparound failure when vacrel table has a
25602570
* relfrozenxid and/or relminmxid that is dangerously far in the past.
2561-
*
25622571
* Triggering the failsafe makes the ongoing VACUUM bypass any further index
25632572
* vacuuming and heap vacuuming. Truncating the heap is also bypassed.
25642573
*
@@ -2567,24 +2576,10 @@ lazy_check_needs_freeze(Buffer buf, bool *hastup, LVRelState *vacrel)
25672576
* that it started out with.
25682577
*
25692578
* Returns true when failsafe has been triggered.
2570-
*
2571-
* Caller is expected to call here before and after vacuuming each index in
2572-
* the case of two-pass VACUUM, or every VACUUM_FSM_EVERY_PAGES blocks in the
2573-
* case of no-indexes/one-pass VACUUM.
2574-
*
2575-
* There is also a precheck before the first pass over the heap begins, which
2576-
* is helpful when the failsafe initially triggers during a non-aggressive
2577-
* VACUUM -- the automatic aggressive vacuum to prevent wraparound that
2578-
* follows can independently trigger the failsafe right away.
25792579
*/
25802580
static bool
25812581
lazy_check_wraparound_failsafe(LVRelState *vacrel)
25822582
{
2583-
/* Avoid calling vacuum_xid_failsafe_check() very frequently */
2584-
if (vacrel->num_index_scans == 0 &&
2585-
vacrel->rel_pages <= FAILSAFE_MIN_PAGES)
2586-
return false;
2587-
25882583
/* Don't warn more than once per VACUUM */
25892584
if (vacrel->do_failsafe)
25902585
return true;
@@ -2600,7 +2595,7 @@ lazy_check_wraparound_failsafe(LVRelState *vacrel)
26002595
vacrel->do_failsafe = true;
26012596

26022597
ereport(WARNING,
2603-
(errmsg("abandoned index vacuuming of table \"%s.%s.%s\" as a failsafe after %d index scans",
2598+
(errmsg("bypassing nonessential maintenance of table \"%s.%s.%s\" as a failsafe after %d index scans",
26042599
get_database_name(MyDatabaseId),
26052600
vacrel->relnamespace,
26062601
vacrel->relname,

0 commit comments

Comments
 (0)