| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* vacuumlazy.c |
| 4 |
* Concurrent ("lazy") vacuuming. |
| 5 |
* |
| 6 |
* Heap relations are vacuumed in three main phases. In phase I, vacuum scans |
| 7 |
* relation pages, pruning and freezing tuples and saving dead tuples' TIDs in |
| 8 |
* a TID store. If that TID store fills up or vacuum finishes scanning the |
| 9 |
* relation, it progresses to phase II: index vacuuming. Index vacuuming |
| 10 |
* deletes the dead index entries referenced in the TID store. In phase III, |
| 11 |
* vacuum scans the blocks of the relation referred to by the TIDs in the TID |
| 12 |
* store and reaps the corresponding dead items, freeing that space for future |
| 13 |
* tuples. |
| 14 |
* |
| 15 |
* If there are no indexes or index scanning is disabled, phase II may be |
| 16 |
* skipped. If phase I identified very few dead index entries or if vacuum's |
| 17 |
* failsafe mechanism has triggered (to avoid transaction ID wraparound), |
| 18 |
* vacuum may skip phases II and III. |
| 19 |
* |
| 20 |
* If the TID store fills up in phase I, vacuum suspends phase I and proceeds |
| 21 |
* to phases II and III, cleaning up the dead tuples referenced in the current |
| 22 |
* TID store. This empties the TID store, allowing vacuum to resume phase I. |
| 23 |
* |
| 24 |
* In a way, the phases are more like states in a state machine, but they have |
| 25 |
* been referred to colloquially as phases for so long that they are referred |
| 26 |
* to as such here. |
| 27 |
* |
| 28 |
* Manually invoked VACUUMs may scan indexes during phase II in parallel. For |
| 29 |
* more information on this, see the comment at the top of vacuumparallel.c. |
| 30 |
* |
| 31 |
* In between phases, vacuum updates the freespace map (every |
| 32 |
* VACUUM_FSM_EVERY_PAGES). |
| 33 |
* |
| 34 |
* After completing all three phases, vacuum may truncate the relation if it |
| 35 |
* has emptied pages at the end. Finally, vacuum updates relation statistics |
| 36 |
* in pg_class and the cumulative statistics subsystem. |
| 37 |
* |
| 38 |
* Relation Scanning: |
| 39 |
* |
| 40 |
* Vacuum scans the heap relation, starting at the beginning and progressing |
| 41 |
* to the end, skipping pages as permitted by their visibility status, vacuum |
| 42 |
* options, and various other requirements. |
| 43 |
* |
| 44 |
* Vacuums are either aggressive or normal. Aggressive vacuums must scan every |
| 45 |
* unfrozen tuple in order to advance relfrozenxid and avoid transaction ID |
| 46 |
* wraparound. Normal vacuums may scan otherwise skippable pages for one of |
| 47 |
* two reasons: |
| 48 |
* |
| 49 |
* When page skipping is not disabled, a normal vacuum may scan pages that are |
| 50 |
* marked all-visible (and even all-frozen) in the visibility map if the range |
| 51 |
* of skippable pages is below SKIP_PAGES_THRESHOLD. This is primarily for the |
| 52 |
* benefit of kernel readahead (see comment in heap_vac_scan_next_block()). |
| 53 |
* |
| 54 |
* A normal vacuum may also scan skippable pages in an effort to freeze them |
| 55 |
* and decrease the backlog of all-visible but not all-frozen pages that have |
| 56 |
* to be processed by the next aggressive vacuum. These are referred to as |
| 57 |
* eagerly scanned pages. Pages scanned due to SKIP_PAGES_THRESHOLD do not |
| 58 |
* count as eagerly scanned pages. |
| 59 |
* |
| 60 |
* Eagerly scanned pages that are set all-frozen in the VM are successful |
| 61 |
* eager freezes and those not set all-frozen in the VM are failed eager |
| 62 |
* freezes. |
| 63 |
* |
| 64 |
* Because we want to amortize the overhead of freezing pages over multiple |
| 65 |
* vacuums, normal vacuums cap the number of successful eager freezes to |
| 66 |
* MAX_EAGER_FREEZE_SUCCESS_RATE of the number of all-visible but not |
| 67 |
* all-frozen pages at the beginning of the vacuum. Since eagerly frozen pages |
| 68 |
* may be unfrozen before the next aggressive vacuum, capping the number of |
| 69 |
* successful eager freezes also caps the downside of eager freezing: |
| 70 |
* potentially wasted work. |
| 71 |
* |
| 72 |
* Once the success cap has been hit, eager scanning is disabled for the |
| 73 |
* remainder of the vacuum of the relation. |
| 74 |
* |
| 75 |
* Success is capped globally because we don't want to limit our successes if |
| 76 |
* old data happens to be concentrated in a particular part of the table. This |
| 77 |
* is especially likely to happen for append-mostly workloads where the oldest |
| 78 |
* data is at the beginning of the unfrozen portion of the relation. |
| 79 |
* |
| 80 |
* On the assumption that different regions of the table are likely to contain |
| 81 |
* similarly aged data, normal vacuums use a localized eager freeze failure |
| 82 |
* cap. The failure count is reset for each region of the table -- comprised |
| 83 |
* of EAGER_SCAN_REGION_SIZE blocks. In each region, we tolerate |
| 84 |
* vacuum_max_eager_freeze_failure_rate of EAGER_SCAN_REGION_SIZE failures |
| 85 |
* before suspending eager scanning until the end of the region. |
| 86 |
* vacuum_max_eager_freeze_failure_rate is configurable both globally and per |
| 87 |
* table. |
| 88 |
* |
| 89 |
* Aggressive vacuums must examine every unfrozen tuple and thus are not |
| 90 |
* subject to any of the limits imposed by the eager scanning algorithm. |
| 91 |
* |
| 92 |
* Once vacuum has decided to scan a given block, it must read the block and |
| 93 |
* obtain a cleanup lock to prune tuples on the page. A non-aggressive vacuum |
| 94 |
* may choose to skip pruning and freezing if it cannot acquire a cleanup lock |
| 95 |
* on the buffer right away. In this case, it may miss cleaning up dead tuples |
| 96 |
* and their associated index entries (though it is free to reap any existing |
| 97 |
* dead items on the page). |
| 98 |
* |
| 99 |
* After pruning and freezing, pages that are newly all-visible and all-frozen |
| 100 |
* are marked as such in the visibility map. |
| 101 |
* |
| 102 |
* Dead TID Storage: |
| 103 |
* |
| 104 |
* The major space usage for vacuuming is storage for the dead tuple IDs that |
| 105 |
* are to be removed from indexes. We want to ensure we can vacuum even the |
| 106 |
* very largest relations with finite memory space usage. To do that, we set |
| 107 |
* upper bounds on the memory that can be used for keeping track of dead TIDs |
| 108 |
* at once. |
| 109 |
* |
| 110 |
* We are willing to use at most maintenance_work_mem (or perhaps |
| 111 |
* autovacuum_work_mem) memory space to keep track of dead TIDs. If the |
| 112 |
* TID store is full, we must call lazy_vacuum to vacuum indexes (and to vacuum |
| 113 |
* the pages that we've pruned). This frees up the memory space dedicated to |
| 114 |
* store dead TIDs. |
| 115 |
* |
| 116 |
* In practice VACUUM will often complete its initial pass over the target |
| 117 |
* heap relation without ever running out of space to store TIDs. This means |
| 118 |
* that there only needs to be one call to lazy_vacuum, after the initial pass |
| 119 |
* completes. |
| 120 |
* |
| 121 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 122 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 123 |
* |
| 124 |
* |
| 125 |
* IDENTIFICATION |
| 126 |
* src/backend/access/heap/vacuumlazy.c |
| 127 |
* |
| 128 |
*------------------------------------------------------------------------- |
| 129 |
*/ |
| 130 |
#include "postgres.h" |
| 131 |
|
| 132 |
#include <math.h> |
| 133 |
|
| 134 |
#include "access/genam.h" |
| 135 |
#include "access/heapam.h" |
| 136 |
#include "access/htup_details.h" |
| 137 |
#include "access/multixact.h" |
| 138 |
#include "access/tidstore.h" |
| 139 |
#include "access/transam.h" |
| 140 |
#include "access/visibilitymap.h" |
| 141 |
#include "access/xloginsert.h" |
| 142 |
#include "catalog/storage.h" |
| 143 |
#include "commands/dbcommands.h" |
| 144 |
#include "commands/progress.h" |
| 145 |
#include "commands/vacuum.h" |
| 146 |
#include "common/int.h" |
| 147 |
#include "common/pg_prng.h" |
| 148 |
#include "executor/instrument.h" |
| 149 |
#include "miscadmin.h" |
| 150 |
#include "pgstat.h" |
| 151 |
#include "portability/instr_time.h" |
| 152 |
#include "postmaster/autovacuum.h" |
| 153 |
#include "storage/bufmgr.h" |
| 154 |
#include "storage/freespace.h" |
| 155 |
#include "storage/lmgr.h" |
| 156 |
#include "storage/read_stream.h" |
| 157 |
#include "utils/lsyscache.h" |
| 158 |
#include "utils/pg_rusage.h" |
| 159 |
#include "utils/timestamp.h" |
| 160 |
|
| 161 |
|
| 162 |
/* |
| 163 |
* Space/time tradeoff parameters: do these need to be user-tunable? |
| 164 |
* |
| 165 |
* To consider truncating the relation, we want there to be at least |
| 166 |
* REL_TRUNCATE_MINIMUM or (relsize / REL_TRUNCATE_FRACTION) (whichever |
| 167 |
* is less) potentially-freeable pages. |
| 168 |
*/ |
| 169 |
#define REL_TRUNCATE_MINIMUM 1000 |
| 170 |
#define REL_TRUNCATE_FRACTION 16 |
| 171 |
|
| 172 |
/* |
| 173 |
* Timing parameters for truncate locking heuristics. |
| 174 |
* |
| 175 |
* These were not exposed as user tunable GUC values because it didn't seem |
| 176 |
* that the potential for improvement was great enough to merit the cost of |
| 177 |
* supporting them. |
| 178 |
*/ |
| 179 |
#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL 20 /* ms */ |
| 180 |
#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL 50 /* ms */ |
| 181 |
#define VACUUM_TRUNCATE_LOCK_TIMEOUT 5000 /* ms */ |
| 182 |
|
| 183 |
/* |
| 184 |
* Threshold that controls whether we bypass index vacuuming and heap |
| 185 |
* vacuuming as an optimization |
| 186 |
*/ |
| 187 |
#define BYPASS_THRESHOLD_PAGES 0.02 /* i.e. 2% of rel_pages */ |
| 188 |
|
| 189 |
/* |
| 190 |
* Perform a failsafe check each time we scan another 4GB of pages. |
| 191 |
* (Note that this is deliberately kept to a power-of-two, usually 2^19.) |
| 192 |
*/ |
| 193 |
#define FAILSAFE_EVERY_PAGES \ |
| 194 |
((BlockNumber) (((uint64) 4 * 1024 * 1024 * 1024) / BLCKSZ)) |
| 195 |
|
| 196 |
/* |
| 197 |
* When a table has no indexes, vacuum the FSM after every 8GB, approximately |
| 198 |
* (it won't be exact because we only vacuum FSM after processing a heap page |
| 199 |
* that has some removable tuples). When there are indexes, this is ignored, |
| 200 |
* and we vacuum FSM after each index/heap cleaning pass. |
| 201 |
*/ |
| 202 |
#define VACUUM_FSM_EVERY_PAGES \ |
| 203 |
((BlockNumber) (((uint64) 8 * 1024 * 1024 * 1024) / BLCKSZ)) |
| 204 |
|
| 205 |
/* |
| 206 |
* Before we consider skipping a page that's marked as clean in |
| 207 |
* visibility map, we must've seen at least this many clean pages. |
| 208 |
*/ |
| 209 |
#define SKIP_PAGES_THRESHOLD ((BlockNumber) 32) |
| 210 |
|
| 211 |
/* |
| 212 |
* Size of the prefetch window for lazy vacuum backwards truncation scan. |
| 213 |
* Needs to be a power of 2. |
| 214 |
*/ |
| 215 |
#define PREFETCH_SIZE ((BlockNumber) 32) |
| 216 |
|
| 217 |
/* |
| 218 |
* Macro to check if we are in a parallel vacuum. If true, we are in the |
| 219 |
* parallel mode and the DSM segment is initialized. |
| 220 |
*/ |
| 221 |
#define ParallelVacuumIsActive(vacrel) ((vacrel)->pvs != NULL) |
| 222 |
|
| 223 |
/* Phases of vacuum during which we report error context. */ |
| 224 |
typedef enum |
| 225 |
{ |
| 226 |
VACUUM_ERRCB_PHASE_UNKNOWN, |
| 227 |
VACUUM_ERRCB_PHASE_SCAN_HEAP, |
| 228 |
VACUUM_ERRCB_PHASE_VACUUM_INDEX, |
| 229 |
VACUUM_ERRCB_PHASE_VACUUM_HEAP, |
| 230 |
VACUUM_ERRCB_PHASE_INDEX_CLEANUP, |
| 231 |
VACUUM_ERRCB_PHASE_TRUNCATE, |
| 232 |
} VacErrPhase; |
| 233 |
|
| 234 |
/* |
| 235 |
* An eager scan of a page that is set all-frozen in the VM is considered |
| 236 |
* "successful". To spread out freezing overhead across multiple normal |
| 237 |
* vacuums, we limit the number of successful eager page freezes. The maximum |
| 238 |
* number of eager page freezes is calculated as a ratio of the all-visible |
| 239 |
* but not all-frozen pages at the beginning of the vacuum. |
| 240 |
*/ |
| 241 |
#define MAX_EAGER_FREEZE_SUCCESS_RATE 0.2 |
| 242 |
|
| 243 |
/* |
| 244 |
* On the assumption that different regions of the table tend to have |
| 245 |
* similarly aged data, once vacuum fails to freeze |
| 246 |
* vacuum_max_eager_freeze_failure_rate of the blocks in a region of size |
| 247 |
* EAGER_SCAN_REGION_SIZE, it suspends eager scanning until it has progressed |
| 248 |
* to another region of the table with potentially older data. |
| 249 |
*/ |
| 250 |
#define EAGER_SCAN_REGION_SIZE 4096 |
| 251 |
|
| 252 |
/* |
| 253 |
* heap_vac_scan_next_block() sets these flags to communicate information |
| 254 |
* about the block it read to the caller. |
| 255 |
*/ |
| 256 |
#define VAC_BLK_WAS_EAGER_SCANNED (1 << 0) |
| 257 |
#define VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM (1 << 1) |
| 258 |
|
| 259 |
typedef struct LVRelState |
| 260 |
{ |
| 261 |
/* Target heap relation and its indexes */ |
| 262 |
Relation rel; |
| 263 |
Relation *indrels; |
| 264 |
int nindexes; |
| 265 |
|
| 266 |
/* Buffer access strategy and parallel vacuum state */ |
| 267 |
BufferAccessStrategy bstrategy; |
| 268 |
ParallelVacuumState *pvs; |
| 269 |
|
| 270 |
/* Aggressive VACUUM? (must set relfrozenxid >= FreezeLimit) */ |
| 271 |
bool aggressive; |
| 272 |
/* Use visibility map to skip? (disabled by DISABLE_PAGE_SKIPPING) */ |
| 273 |
bool skipwithvm; |
| 274 |
/* Consider index vacuuming bypass optimization? */ |
| 275 |
bool consider_bypass_optimization; |
| 276 |
|
| 277 |
/* Doing index vacuuming, index cleanup, rel truncation? */ |
| 278 |
bool do_index_vacuuming; |
| 279 |
bool do_index_cleanup; |
| 280 |
bool do_rel_truncate; |
| 281 |
|
| 282 |
/* VACUUM operation's cutoffs for freezing and pruning */ |
| 283 |
struct VacuumCutoffs cutoffs; |
| 284 |
GlobalVisState *vistest; |
| 285 |
/* Tracks oldest extant XID/MXID for setting relfrozenxid/relminmxid */ |
| 286 |
TransactionId NewRelfrozenXid; |
| 287 |
MultiXactId NewRelminMxid; |
| 288 |
bool skippedallvis; |
| 289 |
|
| 290 |
/* Error reporting state */ |
| 291 |
char *dbname; |
| 292 |
char *relnamespace; |
| 293 |
char *relname; |
| 294 |
char *indname; /* Current index name */ |
| 295 |
BlockNumber blkno; /* used only for heap operations */ |
| 296 |
OffsetNumber offnum; /* used only for heap operations */ |
| 297 |
VacErrPhase phase; |
| 298 |
bool verbose; /* VACUUM VERBOSE? */ |
| 299 |
|
| 300 |
/* |
| 301 |
* dead_items stores TIDs whose index tuples are deleted by index |
| 302 |
* vacuuming. Each TID points to an LP_DEAD line pointer from a heap page |
| 303 |
* that has been processed by lazy_scan_prune. Also needed by |
| 304 |
* lazy_vacuum_heap_rel, which marks the same LP_DEAD line pointers as |
| 305 |
* LP_UNUSED during second heap pass. |
| 306 |
* |
| 307 |
* Both dead_items and dead_items_info are allocated in shared memory in |
| 308 |
* parallel vacuum cases. |
| 309 |
*/ |
| 310 |
TidStore *dead_items; /* TIDs whose index tuples we'll delete */ |
| 311 |
VacDeadItemsInfo *dead_items_info; |
| 312 |
|
| 313 |
BlockNumber rel_pages; /* total number of pages */ |
| 314 |
BlockNumber scanned_pages; /* # pages examined (not skipped via VM) */ |
| 315 |
|
| 316 |
/* |
| 317 |
* Count of all-visible blocks eagerly scanned (for logging only). This |
| 318 |
* does not include skippable blocks scanned due to SKIP_PAGES_THRESHOLD. |
| 319 |
*/ |
| 320 |
BlockNumber eager_scanned_pages; |
| 321 |
|
| 322 |
BlockNumber removed_pages; /* # pages removed by relation truncation */ |
| 323 |
BlockNumber new_frozen_tuple_pages; /* # pages with newly frozen tuples */ |
| 324 |
|
| 325 |
/* # pages newly set all-visible in the VM */ |
| 326 |
BlockNumber vm_new_visible_pages; |
| 327 |
|
| 328 |
/* |
| 329 |
* # pages newly set all-visible and all-frozen in the VM. This is a |
| 330 |
* subset of vm_new_visible_pages. That is, vm_new_visible_pages includes |
| 331 |
* all pages set all-visible, but vm_new_visible_frozen_pages includes |
| 332 |
* only those which were also set all-frozen. |
| 333 |
*/ |
| 334 |
BlockNumber vm_new_visible_frozen_pages; |
| 335 |
|
| 336 |
/* # all-visible pages newly set all-frozen in the VM */ |
| 337 |
BlockNumber vm_new_frozen_pages; |
| 338 |
|
| 339 |
BlockNumber lpdead_item_pages; /* # pages with LP_DEAD items */ |
| 340 |
BlockNumber missed_dead_pages; /* # pages with missed dead tuples */ |
| 341 |
BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */ |
| 342 |
|
| 343 |
/* Statistics output by us, for table */ |
| 344 |
double new_rel_tuples; /* new estimated total # of tuples */ |
| 345 |
double new_live_tuples; /* new estimated total # of live tuples */ |
| 346 |
/* Statistics output by index AMs */ |
| 347 |
IndexBulkDeleteResult **indstats; |
| 348 |
|
| 349 |
/* Instrumentation counters */ |
| 350 |
int num_index_scans; |
| 351 |
/* Counters that follow are only for scanned_pages */ |
| 352 |
int64 tuples_deleted; /* # deleted from table */ |
| 353 |
int64 tuples_frozen; /* # newly frozen */ |
| 354 |
int64 lpdead_items; /* # deleted from indexes */ |
| 355 |
int64 live_tuples; /* # live tuples remaining */ |
| 356 |
int64 recently_dead_tuples; /* # dead, but not yet removable */ |
| 357 |
int64 missed_dead_tuples; /* # removable, but not removed */ |
| 358 |
|
| 359 |
/* State maintained by heap_vac_scan_next_block() */ |
| 360 |
BlockNumber current_block; /* last block returned */ |
| 361 |
BlockNumber next_unskippable_block; /* next unskippable block */ |
| 362 |
bool next_unskippable_allvis; /* its visibility status */ |
| 363 |
bool next_unskippable_eager_scanned; /* if it was eagerly scanned */ |
| 364 |
Buffer next_unskippable_vmbuffer; /* buffer containing its VM bit */ |
| 365 |
|
| 366 |
/* State related to managing eager scanning of all-visible pages */ |
| 367 |
|
| 368 |
/* |
| 369 |
* A normal vacuum that has failed to freeze too many eagerly scanned |
| 370 |
* blocks in a region suspends eager scanning. |
| 371 |
* next_eager_scan_region_start is the block number of the first block |
| 372 |
* eligible for resumed eager scanning. |
| 373 |
* |
| 374 |
* When eager scanning is permanently disabled, either initially |
| 375 |
* (including for aggressive vacuum) or due to hitting the success cap, |
| 376 |
* this is set to InvalidBlockNumber. |
| 377 |
*/ |
| 378 |
BlockNumber next_eager_scan_region_start; |
| 379 |
|
| 380 |
/* |
| 381 |
* The remaining number of blocks a normal vacuum will consider eager |
| 382 |
* scanning when it is successful. When eager scanning is enabled, this is |
| 383 |
* initialized to MAX_EAGER_FREEZE_SUCCESS_RATE of the total number of |
| 384 |
* all-visible but not all-frozen pages. For each eager freeze success, |
| 385 |
* this is decremented. Once it hits 0, eager scanning is permanently |
| 386 |
* disabled. It is initialized to 0 if eager scanning starts out disabled |
| 387 |
* (including for aggressive vacuum). |
| 388 |
*/ |
| 389 |
BlockNumber eager_scan_remaining_successes; |
| 390 |
|
| 391 |
/* |
| 392 |
* The maximum number of blocks which may be eagerly scanned and not |
| 393 |
* frozen before eager scanning is temporarily suspended. This is |
| 394 |
* configurable both globally, via the |
| 395 |
* vacuum_max_eager_freeze_failure_rate GUC, and per table, with a table |
| 396 |
* storage parameter of the same name. It is calculated as |
| 397 |
* vacuum_max_eager_freeze_failure_rate of EAGER_SCAN_REGION_SIZE blocks. |
| 398 |
* It is 0 when eager scanning is disabled. |
| 399 |
*/ |
| 400 |
BlockNumber eager_scan_max_fails_per_region; |
| 401 |
|
| 402 |
/* |
| 403 |
* The number of eagerly scanned blocks vacuum failed to freeze (due to |
| 404 |
* age) in the current eager scan region. Vacuum resets it to |
| 405 |
* eager_scan_max_fails_per_region each time it enters a new region of the |
| 406 |
* relation. If eager_scan_remaining_fails hits 0, eager scanning is |
| 407 |
* suspended until the next region. It is also 0 if eager scanning has |
| 408 |
* been permanently disabled. |
| 409 |
*/ |
| 410 |
BlockNumber eager_scan_remaining_fails; |
| 411 |
} LVRelState; |
| 412 |
|
| 413 |
|
| 414 |
/* Struct for saving and restoring vacuum error information. */ |
| 415 |
typedef struct LVSavedErrInfo |
| 416 |
{ |
| 417 |
BlockNumber blkno; |
| 418 |
OffsetNumber offnum; |
| 419 |
VacErrPhase phase; |
| 420 |
} LVSavedErrInfo; |
| 421 |
|
| 422 |
|
| 423 |
/* non-export function prototypes */ |
| 424 |
static void lazy_scan_heap(LVRelState *vacrel); |
| 425 |
static void heap_vacuum_eager_scan_setup(LVRelState *vacrel, |
| 426 |
VacuumParams *params); |
| 427 |
static BlockNumber heap_vac_scan_next_block(ReadStream *stream, |
| 428 |
void *callback_private_data, |
| 429 |
void *per_buffer_data); |
| 430 |
static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis); |
| 431 |
static bool lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, |
| 432 |
BlockNumber blkno, Page page, |
| 433 |
bool sharelock, Buffer vmbuffer); |
| 434 |
static int lazy_scan_prune(LVRelState *vacrel, Buffer buf, |
| 435 |
BlockNumber blkno, Page page, |
| 436 |
Buffer vmbuffer, bool all_visible_according_to_vm, |
| 437 |
bool *has_lpdead_items, bool *vm_page_frozen); |
| 438 |
static bool lazy_scan_noprune(LVRelState *vacrel, Buffer buf, |
| 439 |
BlockNumber blkno, Page page, |
| 440 |
bool *has_lpdead_items); |
| 441 |
static void lazy_vacuum(LVRelState *vacrel); |
| 442 |
static bool lazy_vacuum_all_indexes(LVRelState *vacrel); |
| 443 |
static void lazy_vacuum_heap_rel(LVRelState *vacrel); |
| 444 |
static void lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, |
| 445 |
Buffer buffer, OffsetNumber *deadoffsets, |
| 446 |
int num_offsets, Buffer vmbuffer); |
| 447 |
static bool lazy_check_wraparound_failsafe(LVRelState *vacrel); |
| 448 |
static void lazy_cleanup_all_indexes(LVRelState *vacrel); |
| 449 |
static IndexBulkDeleteResult *lazy_vacuum_one_index(Relation indrel, |
| 450 |
IndexBulkDeleteResult *istat, |
| 451 |
double reltuples, |
| 452 |
LVRelState *vacrel); |
| 453 |
static IndexBulkDeleteResult *lazy_cleanup_one_index(Relation indrel, |
| 454 |
IndexBulkDeleteResult *istat, |
| 455 |
double reltuples, |
| 456 |
bool estimated_count, |
| 457 |
LVRelState *vacrel); |
| 458 |
static bool should_attempt_truncation(LVRelState *vacrel); |
| 459 |
static void lazy_truncate_heap(LVRelState *vacrel); |
| 460 |
static BlockNumber count_nondeletable_pages(LVRelState *vacrel, |
| 461 |
bool *lock_waiter_detected); |
| 462 |
static void dead_items_alloc(LVRelState *vacrel, int nworkers); |
| 463 |
static void dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets, |
| 464 |
int num_offsets); |
| 465 |
static void dead_items_reset(LVRelState *vacrel); |
| 466 |
static void dead_items_cleanup(LVRelState *vacrel); |
| 467 |
static bool heap_page_is_all_visible(LVRelState *vacrel, Buffer buf, |
| 468 |
TransactionId *visibility_cutoff_xid, bool *all_frozen); |
| 469 |
static void update_relstats_all_indexes(LVRelState *vacrel); |
| 470 |
static void vacuum_error_callback(void *arg); |
| 471 |
static void update_vacuum_error_info(LVRelState *vacrel, |
| 472 |
LVSavedErrInfo *saved_vacrel, |
| 473 |
int phase, BlockNumber blkno, |
| 474 |
OffsetNumber offnum); |
| 475 |
static void restore_vacuum_error_info(LVRelState *vacrel, |
| 476 |
const LVSavedErrInfo *saved_vacrel); |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
/* |
| 481 |
* Helper to set up the eager scanning state for vacuuming a single relation. |
| 482 |
* Initializes the eager scan management related members of the LVRelState. |
| 483 |
* |
| 484 |
* Caller provides whether or not an aggressive vacuum is required due to |
| 485 |
* vacuum options or for relfrozenxid/relminmxid advancement. |
| 486 |
*/ |
| 487 |
static void |
| 488 |
heap_vacuum_eager_scan_setup(LVRelState *vacrel, VacuumParams *params) |
| 489 |
{ |
| 490 |
uint32 randseed; |
| 491 |
BlockNumber allvisible; |
| 492 |
BlockNumber allfrozen; |
| 493 |
float first_region_ratio; |
| 494 |
bool oldest_unfrozen_before_cutoff = false; |
| 495 |
|
| 496 |
/* |
| 497 |
* Initialize eager scan management fields to their disabled values. |
| 498 |
* Aggressive vacuums, normal vacuums of small tables, and normal vacuums |
| 499 |
* of tables without sufficiently old tuples disable eager scanning. |
| 500 |
*/ |
| 501 |
vacrel->next_eager_scan_region_start = InvalidBlockNumber; |
| 502 |
vacrel->eager_scan_max_fails_per_region = 0; |
| 503 |
vacrel->eager_scan_remaining_fails = 0; |
| 504 |
vacrel->eager_scan_remaining_successes = 0; |
| 505 |
|
| 506 |
/* If eager scanning is explicitly disabled, just return. */ |
| 507 |
if (params->max_eager_freeze_failure_rate == 0) |
| 508 |
return; |
| 509 |
|
| 510 |
/* |
| 511 |
* The caller will have determined whether or not an aggressive vacuum is |
| 512 |
* required by either the vacuum parameters or the relative age of the |
| 513 |
* oldest unfrozen transaction IDs. An aggressive vacuum must scan every |
| 514 |
* all-visible page to safely advance the relfrozenxid and/or relminmxid, |
| 515 |
* so scans of all-visible pages are not considered eager. |
| 516 |
*/ |
| 517 |
if (vacrel->aggressive) |
| 518 |
return; |
| 519 |
|
| 520 |
/* |
| 521 |
* Aggressively vacuuming a small relation shouldn't take long, so it |
| 522 |
* isn't worth amortizing. We use two times the region size as the size |
| 523 |
* cutoff because the eager scan start block is a random spot somewhere in |
| 524 |
* the first region, making the second region the first to be eager |
| 525 |
* scanned normally. |
| 526 |
*/ |
| 527 |
if (vacrel->rel_pages < 2 * EAGER_SCAN_REGION_SIZE) |
| 528 |
return; |
| 529 |
|
| 530 |
/* |
| 531 |
* We only want to enable eager scanning if we are likely to be able to |
| 532 |
* freeze some of the pages in the relation. |
| 533 |
* |
| 534 |
* Tuples with XIDs older than OldestXmin or MXIDs older than OldestMxact |
| 535 |
* are technically freezable, but we won't freeze them unless the criteria |
| 536 |
* for opportunistic freezing is met. Only tuples with XIDs/MXIDs older |
| 537 |
* than the FreezeLimit/MultiXactCutoff are frozen in the common case. |
| 538 |
* |
| 539 |
* So, as a heuristic, we wait until the FreezeLimit has advanced past the |
| 540 |
* relfrozenxid or the MultiXactCutoff has advanced past the relminmxid to |
| 541 |
* enable eager scanning. |
| 542 |
*/ |
| 543 |
if (TransactionIdIsNormal(vacrel->cutoffs.relfrozenxid) && |
| 544 |
TransactionIdPrecedes(vacrel->cutoffs.relfrozenxid, |
| 545 |
vacrel->cutoffs.FreezeLimit)) |
| 546 |
oldest_unfrozen_before_cutoff = true; |
| 547 |
|
| 548 |
if (!oldest_unfrozen_before_cutoff && |
| 549 |
MultiXactIdIsValid(vacrel->cutoffs.relminmxid) && |
| 550 |
MultiXactIdPrecedes(vacrel->cutoffs.relminmxid, |
| 551 |
vacrel->cutoffs.MultiXactCutoff)) |
| 552 |
oldest_unfrozen_before_cutoff = true; |
| 553 |
|
| 554 |
if (!oldest_unfrozen_before_cutoff) |
| 555 |
return; |
| 556 |
|
| 557 |
/* We have met the criteria to eagerly scan some pages. */ |
| 558 |
|
| 559 |
/* |
| 560 |
* Our success cap is MAX_EAGER_FREEZE_SUCCESS_RATE of the number of |
| 561 |
* all-visible but not all-frozen blocks in the relation. |
| 562 |
*/ |
| 563 |
visibilitymap_count(vacrel->rel, &allvisible, &allfrozen); |
| 564 |
|
| 565 |
vacrel->eager_scan_remaining_successes = |
| 566 |
(BlockNumber) (MAX_EAGER_FREEZE_SUCCESS_RATE * |
| 567 |
(allvisible - allfrozen)); |
| 568 |
|
| 569 |
/* If every all-visible page is frozen, eager scanning is disabled. */ |
| 570 |
if (vacrel->eager_scan_remaining_successes == 0) |
| 571 |
return; |
| 572 |
|
| 573 |
/* |
| 574 |
* Now calculate the bounds of the first eager scan region. Its end block |
| 575 |
* will be a random spot somewhere in the first EAGER_SCAN_REGION_SIZE |
| 576 |
* blocks. This affects the bounds of all subsequent regions and avoids |
| 577 |
* eager scanning and failing to freeze the same blocks each vacuum of the |
| 578 |
* relation. |
| 579 |
*/ |
| 580 |
randseed = pg_prng_uint32(&pg_global_prng_state); |
| 581 |
|
| 582 |
vacrel->next_eager_scan_region_start = randseed % EAGER_SCAN_REGION_SIZE; |
| 583 |
|
| 584 |
Assert(params->max_eager_freeze_failure_rate > 0 && |
| 585 |
params->max_eager_freeze_failure_rate <= 1); |
| 586 |
|
| 587 |
vacrel->eager_scan_max_fails_per_region = |
| 588 |
params->max_eager_freeze_failure_rate * |
| 589 |
EAGER_SCAN_REGION_SIZE; |
| 590 |
|
| 591 |
/* |
| 592 |
* The first region will be smaller than subsequent regions. As such, |
| 593 |
* adjust the eager freeze failures tolerated for this region. |
| 594 |
*/ |
| 595 |
first_region_ratio = 1 - (float) vacrel->next_eager_scan_region_start / |
| 596 |
EAGER_SCAN_REGION_SIZE; |
| 597 |
|
| 598 |
vacrel->eager_scan_remaining_fails = |
| 599 |
vacrel->eager_scan_max_fails_per_region * |
| 600 |
first_region_ratio; |
| 601 |
} |
| 602 |
|
| 603 |
/* |
| 604 |
* heap_vacuum_rel() -- perform VACUUM for one heap relation |
| 605 |
* |
| 606 |
* This routine sets things up for and then calls lazy_scan_heap, where |
| 607 |
* almost all work actually takes place. Finalizes everything after call |
| 608 |
* returns by managing relation truncation and updating rel's pg_class |
| 609 |
* entry. (Also updates pg_class entries for any indexes that need it.) |
| 610 |
* |
| 611 |
* At entry, we have already established a transaction and opened |
| 612 |
* and locked the relation. |
| 613 |
*/ |
| 614 |
void |
| 615 |
heap_vacuum_rel(Relation rel, VacuumParams *params, |
| 616 |
BufferAccessStrategy bstrategy) |
| 617 |
{ |
| 618 |
LVRelState *vacrel; |
| 619 |
bool verbose, |
| 620 |
instrument, |
| 621 |
skipwithvm, |
| 622 |
frozenxid_updated, |
| 623 |
minmulti_updated; |
| 624 |
BlockNumber orig_rel_pages, |
| 625 |
new_rel_pages, |
| 626 |
new_rel_allvisible, |
| 627 |
new_rel_allfrozen; |
| 628 |
PGRUsage ru0; |
| 629 |
TimestampTz starttime = 0; |
| 630 |
PgStat_Counter startreadtime = 0, |
| 631 |
startwritetime = 0; |
| 632 |
WalUsage startwalusage = pgWalUsage; |
| 633 |
BufferUsage startbufferusage = pgBufferUsage; |
| 634 |
ErrorContextCallback errcallback; |
| 635 |
char **indnames = NULL; |
| 636 |
|
| 637 |
verbose = (params->options & VACOPT_VERBOSE) != 0; |
| 638 |
instrument = (verbose || (AmAutoVacuumWorkerProcess() && |
| 639 |
params->log_min_duration >= 0)); |
| 640 |
if (instrument) |
| 641 |
{ |
| 642 |
pg_rusage_init(&ru0); |
| 643 |
if (track_io_timing) |
| 644 |
{ |
| 645 |
startreadtime = pgStatBlockReadTime; |
| 646 |
startwritetime = pgStatBlockWriteTime; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
/* Used for instrumentation and stats report */ |
| 651 |
starttime = GetCurrentTimestamp(); |
| 652 |
|
| 653 |
pgstat_progress_start_command(PROGRESS_COMMAND_VACUUM, |
| 654 |
RelationGetRelid(rel)); |
| 655 |
|
| 656 |
/* |
| 657 |
* Setup error traceback support for ereport() first. The idea is to set |
| 658 |
* up an error context callback to display additional information on any |
| 659 |
* error during a vacuum. During different phases of vacuum, we update |
| 660 |
* the state so that the error context callback always display current |
| 661 |
* information. |
| 662 |
* |
| 663 |
* Copy the names of heap rel into local memory for error reporting |
| 664 |
* purposes, too. It isn't always safe to assume that we can get the name |
| 665 |
* of each rel. It's convenient for code in lazy_scan_heap to always use |
| 666 |
* these temp copies. |
| 667 |
*/ |
| 668 |
vacrel = (LVRelState *) palloc0(sizeof(LVRelState)); |
| 669 |
vacrel->dbname = get_database_name(MyDatabaseId); |
| 670 |
vacrel->relnamespace = get_namespace_name(RelationGetNamespace(rel)); |
| 671 |
vacrel->relname = pstrdup(RelationGetRelationName(rel)); |
| 672 |
vacrel->indname = NULL; |
| 673 |
vacrel->phase = VACUUM_ERRCB_PHASE_UNKNOWN; |
| 674 |
vacrel->verbose = verbose; |
| 675 |
errcallback.callback = vacuum_error_callback; |
| 676 |
errcallback.arg = vacrel; |
| 677 |
errcallback.previous = error_context_stack; |
| 678 |
error_context_stack = &errcallback; |
| 679 |
|
| 680 |
/* Set up high level stuff about rel and its indexes */ |
| 681 |
vacrel->rel = rel; |
| 682 |
vac_open_indexes(vacrel->rel, RowExclusiveLock, &vacrel->nindexes, |
| 683 |
&vacrel->indrels); |
| 684 |
vacrel->bstrategy = bstrategy; |
| 685 |
if (instrument && vacrel->nindexes > 0) |
| 686 |
{ |
| 687 |
/* Copy index names used by instrumentation (not error reporting) */ |
| 688 |
indnames = palloc(sizeof(char *) * vacrel->nindexes); |
| 689 |
for (int i = 0; i < vacrel->nindexes; i++) |
| 690 |
indnames[i] = pstrdup(RelationGetRelationName(vacrel->indrels[i])); |
| 691 |
} |
| 692 |
|
| 693 |
/* |
| 694 |
* The index_cleanup param either disables index vacuuming and cleanup or |
| 695 |
* forces it to go ahead when we would otherwise apply the index bypass |
| 696 |
* optimization. The default is 'auto', which leaves the final decision |
| 697 |
* up to lazy_vacuum(). |
| 698 |
* |
| 699 |
* The truncate param allows user to avoid attempting relation truncation, |
| 700 |
* though it can't force truncation to happen. |
| 701 |
*/ |
| 702 |
Assert(params->index_cleanup != VACOPTVALUE_UNSPECIFIED); |
| 703 |
Assert(params->truncate != VACOPTVALUE_UNSPECIFIED && |
| 704 |
params->truncate != VACOPTVALUE_AUTO); |
| 705 |
|
| 706 |
/* |
| 707 |
* While VacuumFailSafeActive is reset to false before calling this, we |
| 708 |
* still need to reset it here due to recursive calls. |
| 709 |
*/ |
| 710 |
VacuumFailsafeActive = false; |
| 711 |
vacrel->consider_bypass_optimization = true; |
| 712 |
vacrel->do_index_vacuuming = true; |
| 713 |
vacrel->do_index_cleanup = true; |
| 714 |
vacrel->do_rel_truncate = (params->truncate != VACOPTVALUE_DISABLED); |
| 715 |
if (params->index_cleanup == VACOPTVALUE_DISABLED) |
| 716 |
{ |
| 717 |
/* Force disable index vacuuming up-front */ |
| 718 |
vacrel->do_index_vacuuming = false; |
| 719 |
vacrel->do_index_cleanup = false; |
| 720 |
} |
| 721 |
else if (params->index_cleanup == VACOPTVALUE_ENABLED) |
| 722 |
{ |
| 723 |
/* Force index vacuuming. Note that failsafe can still bypass. */ |
| 724 |
vacrel->consider_bypass_optimization = false; |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
/* Default/auto, make all decisions dynamically */ |
| 729 |
Assert(params->index_cleanup == VACOPTVALUE_AUTO); |
| 730 |
} |
| 731 |
|
| 732 |
/* Initialize page counters explicitly (be tidy) */ |
| 733 |
vacrel->scanned_pages = 0; |
| 734 |
vacrel->eager_scanned_pages = 0; |
| 735 |
vacrel->removed_pages = 0; |
| 736 |
vacrel->new_frozen_tuple_pages = 0; |
| 737 |
vacrel->lpdead_item_pages = 0; |
| 738 |
vacrel->missed_dead_pages = 0; |
| 739 |
vacrel->nonempty_pages = 0; |
| 740 |
/* dead_items_alloc allocates vacrel->dead_items later on */ |
| 741 |
|
| 742 |
/* Allocate/initialize output statistics state */ |
| 743 |
vacrel->new_rel_tuples = 0; |
| 744 |
vacrel->new_live_tuples = 0; |
| 745 |
vacrel->indstats = (IndexBulkDeleteResult **) |
| 746 |
palloc0(vacrel->nindexes * sizeof(IndexBulkDeleteResult *)); |
| 747 |
|
| 748 |
/* Initialize remaining counters (be tidy) */ |
| 749 |
vacrel->num_index_scans = 0; |
| 750 |
vacrel->tuples_deleted = 0; |
| 751 |
vacrel->tuples_frozen = 0; |
| 752 |
vacrel->lpdead_items = 0; |
| 753 |
vacrel->live_tuples = 0; |
| 754 |
vacrel->recently_dead_tuples = 0; |
| 755 |
vacrel->missed_dead_tuples = 0; |
| 756 |
|
| 757 |
vacrel->vm_new_visible_pages = 0; |
| 758 |
vacrel->vm_new_visible_frozen_pages = 0; |
| 759 |
vacrel->vm_new_frozen_pages = 0; |
| 760 |
|
| 761 |
/* |
| 762 |
* Get cutoffs that determine which deleted tuples are considered DEAD, |
| 763 |
* not just RECENTLY_DEAD, and which XIDs/MXIDs to freeze. Then determine |
| 764 |
* the extent of the blocks that we'll scan in lazy_scan_heap. It has to |
| 765 |
* happen in this order to ensure that the OldestXmin cutoff field works |
| 766 |
* as an upper bound on the XIDs stored in the pages we'll actually scan |
| 767 |
* (NewRelfrozenXid tracking must never be allowed to miss unfrozen XIDs). |
| 768 |
* |
| 769 |
* Next acquire vistest, a related cutoff that's used in pruning. We use |
| 770 |
* vistest in combination with OldestXmin to ensure that |
| 771 |
* heap_page_prune_and_freeze() always removes any deleted tuple whose |
| 772 |
* xmax is < OldestXmin. lazy_scan_prune must never become confused about |
| 773 |
* whether a tuple should be frozen or removed. (In the future we might |
| 774 |
* want to teach lazy_scan_prune to recompute vistest from time to time, |
| 775 |
* to increase the number of dead tuples it can prune away.) |
| 776 |
*/ |
| 777 |
vacrel->aggressive = vacuum_get_cutoffs(rel, params, &vacrel->cutoffs); |
| 778 |
vacrel->rel_pages = orig_rel_pages = RelationGetNumberOfBlocks(rel); |
| 779 |
vacrel->vistest = GlobalVisTestFor(rel); |
| 780 |
|
| 781 |
/* Initialize state used to track oldest extant XID/MXID */ |
| 782 |
vacrel->NewRelfrozenXid = vacrel->cutoffs.OldestXmin; |
| 783 |
vacrel->NewRelminMxid = vacrel->cutoffs.OldestMxact; |
| 784 |
|
| 785 |
/* |
| 786 |
* Initialize state related to tracking all-visible page skipping. This is |
| 787 |
* very important to determine whether or not it is safe to advance the |
| 788 |
* relfrozenxid/relminmxid. |
| 789 |
*/ |
| 790 |
vacrel->skippedallvis = false; |
| 791 |
skipwithvm = true; |
| 792 |
if (params->options & VACOPT_DISABLE_PAGE_SKIPPING) |
| 793 |
{ |
| 794 |
/* |
| 795 |
* Force aggressive mode, and disable skipping blocks using the |
| 796 |
* visibility map (even those set all-frozen) |
| 797 |
*/ |
| 798 |
vacrel->aggressive = true; |
| 799 |
skipwithvm = false; |
| 800 |
} |
| 801 |
|
| 802 |
vacrel->skipwithvm = skipwithvm; |
| 803 |
|
| 804 |
/* |
| 805 |
* Set up eager scan tracking state. This must happen after determining |
| 806 |
* whether or not the vacuum must be aggressive, because only normal |
| 807 |
* vacuums use the eager scan algorithm. |
| 808 |
*/ |
| 809 |
heap_vacuum_eager_scan_setup(vacrel, params); |
| 810 |
|
| 811 |
if (verbose) |
| 812 |
{ |
| 813 |
if (vacrel->aggressive) |
| 814 |
ereport(INFO, |
| 815 |
(errmsg("aggressively vacuuming \"%s.%s.%s\"", |
| 816 |
vacrel->dbname, vacrel->relnamespace, |
| 817 |
vacrel->relname))); |
| 818 |
else |
| 819 |
ereport(INFO, |
| 820 |
(errmsg("vacuuming \"%s.%s.%s\"", |
| 821 |
vacrel->dbname, vacrel->relnamespace, |
| 822 |
vacrel->relname))); |
| 823 |
} |
| 824 |
|
| 825 |
/* |
| 826 |
* Allocate dead_items memory using dead_items_alloc. This handles |
| 827 |
* parallel VACUUM initialization as part of allocating shared memory |
| 828 |
* space used for dead_items. (But do a failsafe precheck first, to |
| 829 |
* ensure that parallel VACUUM won't be attempted at all when relfrozenxid |
| 830 |
* is already dangerously old.) |
| 831 |
*/ |
| 832 |
lazy_check_wraparound_failsafe(vacrel); |
| 833 |
dead_items_alloc(vacrel, params->nworkers); |
| 834 |
|
| 835 |
/* |
| 836 |
* Call lazy_scan_heap to perform all required heap pruning, index |
| 837 |
* vacuuming, and heap vacuuming (plus related processing) |
| 838 |
*/ |
| 839 |
lazy_scan_heap(vacrel); |
| 840 |
|
| 841 |
/* |
| 842 |
* Free resources managed by dead_items_alloc. This ends parallel mode in |
| 843 |
* passing when necessary. |
| 844 |
*/ |
| 845 |
dead_items_cleanup(vacrel); |
| 846 |
Assert(!IsInParallelMode()); |
| 847 |
|
| 848 |
/* |
| 849 |
* Update pg_class entries for each of rel's indexes where appropriate. |
| 850 |
* |
| 851 |
* Unlike the later update to rel's pg_class entry, this is not critical. |
| 852 |
* Maintains relpages/reltuples statistics used by the planner only. |
| 853 |
*/ |
| 854 |
if (vacrel->do_index_cleanup) |
| 855 |
update_relstats_all_indexes(vacrel); |
| 856 |
|
| 857 |
/* Done with rel's indexes */ |
| 858 |
vac_close_indexes(vacrel->nindexes, vacrel->indrels, NoLock); |
| 859 |
|
| 860 |
/* Optionally truncate rel */ |
| 861 |
if (should_attempt_truncation(vacrel)) |
| 862 |
lazy_truncate_heap(vacrel); |
| 863 |
|
| 864 |
/* Pop the error context stack */ |
| 865 |
error_context_stack = errcallback.previous; |
| 866 |
|
| 867 |
/* Report that we are now doing final cleanup */ |
| 868 |
pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, |
| 869 |
PROGRESS_VACUUM_PHASE_FINAL_CLEANUP); |
| 870 |
|
| 871 |
/* |
| 872 |
* Prepare to update rel's pg_class entry. |
| 873 |
* |
| 874 |
* Aggressive VACUUMs must always be able to advance relfrozenxid to a |
| 875 |
* value >= FreezeLimit, and relminmxid to a value >= MultiXactCutoff. |
| 876 |
* Non-aggressive VACUUMs may advance them by any amount, or not at all. |
| 877 |
*/ |
| 878 |
Assert(vacrel->NewRelfrozenXid == vacrel->cutoffs.OldestXmin || |
| 879 |
TransactionIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.FreezeLimit : |
| 880 |
vacrel->cutoffs.relfrozenxid, |
| 881 |
vacrel->NewRelfrozenXid)); |
| 882 |
Assert(vacrel->NewRelminMxid == vacrel->cutoffs.OldestMxact || |
| 883 |
MultiXactIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.MultiXactCutoff : |
| 884 |
vacrel->cutoffs.relminmxid, |
| 885 |
vacrel->NewRelminMxid)); |
| 886 |
if (vacrel->skippedallvis) |
| 887 |
{ |
| 888 |
/* |
| 889 |
* Must keep original relfrozenxid in a non-aggressive VACUUM that |
| 890 |
* chose to skip an all-visible page range. The state that tracks new |
| 891 |
* values will have missed unfrozen XIDs from the pages we skipped. |
| 892 |
*/ |
| 893 |
Assert(!vacrel->aggressive); |
| 894 |
vacrel->NewRelfrozenXid = InvalidTransactionId; |
| 895 |
vacrel->NewRelminMxid = InvalidMultiXactId; |
| 896 |
} |
| 897 |
|
| 898 |
/* |
| 899 |
* For safety, clamp relallvisible to be not more than what we're setting |
| 900 |
* pg_class.relpages to |
| 901 |
*/ |
| 902 |
new_rel_pages = vacrel->rel_pages; /* After possible rel truncation */ |
| 903 |
visibilitymap_count(rel, &new_rel_allvisible, &new_rel_allfrozen); |
| 904 |
if (new_rel_allvisible > new_rel_pages) |
| 905 |
new_rel_allvisible = new_rel_pages; |
| 906 |
|
| 907 |
/* |
| 908 |
* An all-frozen block _must_ be all-visible. As such, clamp the count of |
| 909 |
* all-frozen blocks to the count of all-visible blocks. This matches the |
| 910 |
* clamping of relallvisible above. |
| 911 |
*/ |
| 912 |
if (new_rel_allfrozen > new_rel_allvisible) |
| 913 |
new_rel_allfrozen = new_rel_allvisible; |
| 914 |
|
| 915 |
/* |
| 916 |
* Now actually update rel's pg_class entry. |
| 917 |
* |
| 918 |
* In principle new_live_tuples could be -1 indicating that we (still) |
| 919 |
* don't know the tuple count. In practice that can't happen, since we |
| 920 |
* scan every page that isn't skipped using the visibility map. |
| 921 |
*/ |
| 922 |
vac_update_relstats(rel, new_rel_pages, vacrel->new_live_tuples, |
| 923 |
new_rel_allvisible, new_rel_allfrozen, |
| 924 |
vacrel->nindexes > 0, |
| 925 |
vacrel->NewRelfrozenXid, vacrel->NewRelminMxid, |
| 926 |
&frozenxid_updated, &minmulti_updated, false); |
| 927 |
|
| 928 |
/* |
| 929 |
* Report results to the cumulative stats system, too. |
| 930 |
* |
| 931 |
* Deliberately avoid telling the stats system about LP_DEAD items that |
| 932 |
* remain in the table due to VACUUM bypassing index and heap vacuuming. |
| 933 |
* ANALYZE will consider the remaining LP_DEAD items to be dead "tuples". |
| 934 |
* It seems like a good idea to err on the side of not vacuuming again too |
| 935 |
* soon in cases where the failsafe prevented significant amounts of heap |
| 936 |
* vacuuming. |
| 937 |
*/ |
| 938 |
pgstat_report_vacuum(RelationGetRelid(rel), |
| 939 |
rel->rd_rel->relisshared, |
| 940 |
Max(vacrel->new_live_tuples, 0), |
| 941 |
vacrel->recently_dead_tuples + |
| 942 |
vacrel->missed_dead_tuples, |
| 943 |
starttime); |
| 944 |
pgstat_progress_end_command(); |
| 945 |
|
| 946 |
if (instrument) |
| 947 |
{ |
| 948 |
TimestampTz endtime = GetCurrentTimestamp(); |
| 949 |
|
| 950 |
if (verbose || params->log_min_duration == 0 || |
| 951 |
TimestampDifferenceExceeds(starttime, endtime, |
| 952 |
params->log_min_duration)) |
| 953 |
{ |
| 954 |
long secs_dur; |
| 955 |
int usecs_dur; |
| 956 |
WalUsage walusage; |
| 957 |
BufferUsage bufferusage; |
| 958 |
StringInfoData buf; |
| 959 |
char *msgfmt; |
| 960 |
int32 diff; |
| 961 |
double read_rate = 0, |
| 962 |
write_rate = 0; |
| 963 |
int64 total_blks_hit; |
| 964 |
int64 total_blks_read; |
| 965 |
int64 total_blks_dirtied; |
| 966 |
|
| 967 |
TimestampDifference(starttime, endtime, &secs_dur, &usecs_dur); |
| 968 |
memset(&walusage, 0, sizeof(WalUsage)); |
| 969 |
WalUsageAccumDiff(&walusage, &pgWalUsage, &startwalusage); |
| 970 |
memset(&bufferusage, 0, sizeof(BufferUsage)); |
| 971 |
BufferUsageAccumDiff(&bufferusage, &pgBufferUsage, &startbufferusage); |
| 972 |
|
| 973 |
total_blks_hit = bufferusage.shared_blks_hit + |
| 974 |
bufferusage.local_blks_hit; |
| 975 |
total_blks_read = bufferusage.shared_blks_read + |
| 976 |
bufferusage.local_blks_read; |
| 977 |
total_blks_dirtied = bufferusage.shared_blks_dirtied + |
| 978 |
bufferusage.local_blks_dirtied; |
| 979 |
|
| 980 |
initStringInfo(&buf); |
| 981 |
if (verbose) |
| 982 |
{ |
| 983 |
/* |
| 984 |
* Aggressiveness already reported earlier, in dedicated |
| 985 |
* VACUUM VERBOSE ereport |
| 986 |
*/ |
| 987 |
Assert(!params->is_wraparound); |
| 988 |
msgfmt = _("finished vacuuming \"%s.%s.%s\": index scans: %d\n"); |
| 989 |
} |
| 990 |
else if (params->is_wraparound) |
| 991 |
{ |
| 992 |
/* |
| 993 |
* While it's possible for a VACUUM to be both is_wraparound |
| 994 |
* and !aggressive, that's just a corner-case -- is_wraparound |
| 995 |
* implies aggressive. Produce distinct output for the corner |
| 996 |
* case all the same, just in case. |
| 997 |
*/ |
| 998 |
if (vacrel->aggressive) |
| 999 |
msgfmt = _("automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n"); |
| 1000 |
else |
| 1001 |
msgfmt = _("automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n"); |
| 1002 |
} |
| 1003 |
else |
| 1004 |
{ |
| 1005 |
if (vacrel->aggressive) |
| 1006 |
msgfmt = _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n"); |
| 1007 |
else |
| 1008 |
msgfmt = _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"); |
| 1009 |
} |
| 1010 |
appendStringInfo(&buf, msgfmt, |
| 1011 |
vacrel->dbname, |
| 1012 |
vacrel->relnamespace, |
| 1013 |
vacrel->relname, |
| 1014 |
vacrel->num_index_scans); |
| 1015 |
appendStringInfo(&buf, _("pages: %u removed, %u remain, %u scanned (%.2f%% of total), %u eagerly scanned\n"), |
| 1016 |
vacrel->removed_pages, |
| 1017 |
new_rel_pages, |
| 1018 |
vacrel->scanned_pages, |
| 1019 |
orig_rel_pages == 0 ? 100.0 : |
| 1020 |
100.0 * vacrel->scanned_pages / |
| 1021 |
orig_rel_pages, |
| 1022 |
vacrel->eager_scanned_pages); |
| 1023 |
appendStringInfo(&buf, |
| 1024 |
_("tuples: %" PRId64 " removed, %" PRId64 " remain, %" PRId64 " are dead but not yet removable\n"), |
| 1025 |
vacrel->tuples_deleted, |
| 1026 |
(int64) vacrel->new_rel_tuples, |
| 1027 |
vacrel->recently_dead_tuples); |
| 1028 |
if (vacrel->missed_dead_tuples > 0) |
| 1029 |
appendStringInfo(&buf, |
| 1030 |
_("tuples missed: %" PRId64 " dead from %u pages not removed due to cleanup lock contention\n"), |
| 1031 |
vacrel->missed_dead_tuples, |
| 1032 |
vacrel->missed_dead_pages); |
| 1033 |
diff = (int32) (ReadNextTransactionId() - |
| 1034 |
vacrel->cutoffs.OldestXmin); |
| 1035 |
appendStringInfo(&buf, |
| 1036 |
_("removable cutoff: %u, which was %d XIDs old when operation ended\n"), |
| 1037 |
vacrel->cutoffs.OldestXmin, diff); |
| 1038 |
if (frozenxid_updated) |
| 1039 |
{ |
| 1040 |
diff = (int32) (vacrel->NewRelfrozenXid - |
| 1041 |
vacrel->cutoffs.relfrozenxid); |
| 1042 |
appendStringInfo(&buf, |
| 1043 |
_("new relfrozenxid: %u, which is %d XIDs ahead of previous value\n"), |
| 1044 |
vacrel->NewRelfrozenXid, diff); |
| 1045 |
} |
| 1046 |
if (minmulti_updated) |
| 1047 |
{ |
| 1048 |
diff = (int32) (vacrel->NewRelminMxid - |
| 1049 |
vacrel->cutoffs.relminmxid); |
| 1050 |
appendStringInfo(&buf, |
| 1051 |
_("new relminmxid: %u, which is %d MXIDs ahead of previous value\n"), |
| 1052 |
vacrel->NewRelminMxid, diff); |
| 1053 |
} |
| 1054 |
appendStringInfo(&buf, _("frozen: %u pages from table (%.2f%% of total) had %" PRId64 " tuples frozen\n"), |
| 1055 |
vacrel->new_frozen_tuple_pages, |
| 1056 |
orig_rel_pages == 0 ? 100.0 : |
| 1057 |
100.0 * vacrel->new_frozen_tuple_pages / |
| 1058 |
orig_rel_pages, |
| 1059 |
vacrel->tuples_frozen); |
| 1060 |
|
| 1061 |
appendStringInfo(&buf, |
| 1062 |
_("visibility map: %u pages set all-visible, %u pages set all-frozen (%u were all-visible)\n"), |
| 1063 |
vacrel->vm_new_visible_pages, |
| 1064 |
vacrel->vm_new_visible_frozen_pages + |
| 1065 |
vacrel->vm_new_frozen_pages, |
| 1066 |
vacrel->vm_new_frozen_pages); |
| 1067 |
if (vacrel->do_index_vacuuming) |
| 1068 |
{ |
| 1069 |
if (vacrel->nindexes == 0 || vacrel->num_index_scans == 0) |
| 1070 |
appendStringInfoString(&buf, _("index scan not needed: ")); |
| 1071 |
else |
| 1072 |
appendStringInfoString(&buf, _("index scan needed: ")); |
| 1073 |
|
| 1074 |
msgfmt = _("%u pages from table (%.2f%% of total) had %" PRId64 " dead item identifiers removed\n"); |
| 1075 |
} |
| 1076 |
else |
| 1077 |
{ |
| 1078 |
if (!VacuumFailsafeActive) |
| 1079 |
appendStringInfoString(&buf, _("index scan bypassed: ")); |
| 1080 |
else |
| 1081 |
appendStringInfoString(&buf, _("index scan bypassed by failsafe: ")); |
| 1082 |
|
| 1083 |
msgfmt = _("%u pages from table (%.2f%% of total) have %" PRId64 " dead item identifiers\n"); |
| 1084 |
} |
| 1085 |
appendStringInfo(&buf, msgfmt, |
| 1086 |
vacrel->lpdead_item_pages, |
| 1087 |
orig_rel_pages == 0 ? 100.0 : |
| 1088 |
100.0 * vacrel->lpdead_item_pages / orig_rel_pages, |
| 1089 |
vacrel->lpdead_items); |
| 1090 |
for (int i = 0; i < vacrel->nindexes; i++) |
| 1091 |
{ |
| 1092 |
IndexBulkDeleteResult *istat = vacrel->indstats[i]; |
| 1093 |
|
| 1094 |
if (!istat) |
| 1095 |
continue; |
| 1096 |
|
| 1097 |
appendStringInfo(&buf, |
| 1098 |
_("index \"%s\": pages: %u in total, %u newly deleted, %u currently deleted, %u reusable\n"), |
| 1099 |
indnames[i], |
| 1100 |
istat->num_pages, |
| 1101 |
istat->pages_newly_deleted, |
| 1102 |
istat->pages_deleted, |
| 1103 |
istat->pages_free); |
| 1104 |
} |
| 1105 |
if (track_cost_delay_timing) |
| 1106 |
{ |
| 1107 |
/* |
| 1108 |
* We bypass the changecount mechanism because this value is |
| 1109 |
* only updated by the calling process. We also rely on the |
| 1110 |
* above call to pgstat_progress_end_command() to not clear |
| 1111 |
* the st_progress_param array. |
| 1112 |
*/ |
| 1113 |
appendStringInfo(&buf, _("delay time: %.3f ms\n"), |
| 1114 |
(double) MyBEEntry->st_progress_param[PROGRESS_VACUUM_DELAY_TIME] / 1000000.0); |
| 1115 |
} |
| 1116 |
if (track_io_timing) |
| 1117 |
{ |
| 1118 |
double read_ms = (double) (pgStatBlockReadTime - startreadtime) / 1000; |
| 1119 |
double write_ms = (double) (pgStatBlockWriteTime - startwritetime) / 1000; |
| 1120 |
|
| 1121 |
appendStringInfo(&buf, _("I/O timings: read: %.3f ms, write: %.3f ms\n"), |
| 1122 |
read_ms, write_ms); |
| 1123 |
} |
| 1124 |
if (secs_dur > 0 || usecs_dur > 0) |
| 1125 |
{ |
| 1126 |
read_rate = (double) BLCKSZ * total_blks_read / |
| 1127 |
(1024 * 1024) / (secs_dur + usecs_dur / 1000000.0); |
| 1128 |
write_rate = (double) BLCKSZ * total_blks_dirtied / |
| 1129 |
(1024 * 1024) / (secs_dur + usecs_dur / 1000000.0); |
| 1130 |
} |
| 1131 |
appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"), |
| 1132 |
read_rate, write_rate); |
| 1133 |
appendStringInfo(&buf, |
| 1134 |
_("buffer usage: %" PRId64 " hits, %" PRId64 " reads, %" PRId64 " dirtied\n"), |
| 1135 |
total_blks_hit, |
| 1136 |
total_blks_read, |
| 1137 |
total_blks_dirtied); |
| 1138 |
appendStringInfo(&buf, |
| 1139 |
_("WAL usage: %" PRId64 " records, %" PRId64 " full page images, %" PRIu64 " bytes, %" PRId64 " buffers full\n"), |
| 1140 |
walusage.wal_records, |
| 1141 |
walusage.wal_fpi, |
| 1142 |
walusage.wal_bytes, |
| 1143 |
walusage.wal_buffers_full); |
| 1144 |
appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0)); |
| 1145 |
|
| 1146 |
ereport(verbose ? INFO : LOG, |
| 1147 |
(errmsg_internal("%s", buf.data))); |
| 1148 |
pfree(buf.data); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
/* Cleanup index statistics and index names */ |
| 1153 |
for (int i = 0; i < vacrel->nindexes; i++) |
| 1154 |
{ |
| 1155 |
if (vacrel->indstats[i]) |
| 1156 |
pfree(vacrel->indstats[i]); |
| 1157 |
|
| 1158 |
if (instrument) |
| 1159 |
pfree(indnames[i]); |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
/* |
| 1164 |
* lazy_scan_heap() -- workhorse function for VACUUM |
| 1165 |
* |
| 1166 |
* This routine prunes each page in the heap, and considers the need to |
| 1167 |
* freeze remaining tuples with storage (not including pages that can be |
| 1168 |
* skipped using the visibility map). Also performs related maintenance |
| 1169 |
* of the FSM and visibility map. These steps all take place during an |
| 1170 |
* initial pass over the target heap relation. |
| 1171 |
* |
| 1172 |
* Also invokes lazy_vacuum_all_indexes to vacuum indexes, which largely |
| 1173 |
* consists of deleting index tuples that point to LP_DEAD items left in |
| 1174 |
* heap pages following pruning. Earlier initial pass over the heap will |
| 1175 |
* have collected the TIDs whose index tuples need to be removed. |
| 1176 |
* |
| 1177 |
* Finally, invokes lazy_vacuum_heap_rel to vacuum heap pages, which |
| 1178 |
* largely consists of marking LP_DEAD items (from vacrel->dead_items) |
| 1179 |
* as LP_UNUSED. This has to happen in a second, final pass over the |
| 1180 |
* heap, to preserve a basic invariant that all index AMs rely on: no |
| 1181 |
* extant index tuple can ever be allowed to contain a TID that points to |
| 1182 |
* an LP_UNUSED line pointer in the heap. We must disallow premature |
| 1183 |
* recycling of line pointers to avoid index scans that get confused |
| 1184 |
* about which TID points to which tuple immediately after recycling. |
| 1185 |
* (Actually, this isn't a concern when target heap relation happens to |
| 1186 |
* have no indexes, which allows us to safely apply the one-pass strategy |
| 1187 |
* as an optimization). |
| 1188 |
* |
| 1189 |
* In practice we often have enough space to fit all TIDs, and so won't |
| 1190 |
* need to call lazy_vacuum more than once, after our initial pass over |
| 1191 |
* the heap has totally finished. Otherwise things are slightly more |
| 1192 |
* complicated: our "initial pass" over the heap applies only to those |
| 1193 |
* pages that were pruned before we needed to call lazy_vacuum, and our |
| 1194 |
* "final pass" over the heap only vacuums these same heap pages. |
| 1195 |
* However, we process indexes in full every time lazy_vacuum is called, |
| 1196 |
* which makes index processing very inefficient when memory is in short |
| 1197 |
* supply. |
| 1198 |
*/ |
| 1199 |
static void |
| 1200 |
lazy_scan_heap(LVRelState *vacrel) |
| 1201 |
{ |
| 1202 |
ReadStream *stream; |
| 1203 |
BlockNumber rel_pages = vacrel->rel_pages, |
| 1204 |
blkno = 0, |
| 1205 |
next_fsm_block_to_vacuum = 0; |
| 1206 |
BlockNumber orig_eager_scan_success_limit = |
| 1207 |
vacrel->eager_scan_remaining_successes; /* for logging */ |
| 1208 |
Buffer vmbuffer = InvalidBuffer; |
| 1209 |
bool strategy_cleared = false; |
| 1210 |
const int initprog_index[] = { |
| 1211 |
PROGRESS_VACUUM_PHASE, |
| 1212 |
PROGRESS_VACUUM_TOTAL_HEAP_BLKS, |
| 1213 |
PROGRESS_VACUUM_MAX_DEAD_TUPLE_BYTES |
| 1214 |
}; |
| 1215 |
int64 initprog_val[3]; |
| 1216 |
|
| 1217 |
/* Report that we're scanning the heap, advertising total # of blocks */ |
| 1218 |
initprog_val[0] = PROGRESS_VACUUM_PHASE_SCAN_HEAP; |
| 1219 |
initprog_val[1] = rel_pages; |
| 1220 |
initprog_val[2] = vacrel->dead_items_info->max_bytes; |
| 1221 |
pgstat_progress_update_multi_param(3, initprog_index, initprog_val); |
| 1222 |
|
| 1223 |
/* Initialize for the first heap_vac_scan_next_block() call */ |
| 1224 |
vacrel->current_block = InvalidBlockNumber; |
| 1225 |
vacrel->next_unskippable_block = InvalidBlockNumber; |
| 1226 |
vacrel->next_unskippable_allvis = false; |
| 1227 |
vacrel->next_unskippable_eager_scanned = false; |
| 1228 |
vacrel->next_unskippable_vmbuffer = InvalidBuffer; |
| 1229 |
|
| 1230 |
/* |
| 1231 |
* Set up the read stream for vacuum's first pass through the heap. |
| 1232 |
* |
| 1233 |
* This could be made safe for READ_STREAM_USE_BATCHING, but only with |
| 1234 |
* explicit work in heap_vac_scan_next_block. |
| 1235 |
*/ |
| 1236 |
stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE, |
| 1237 |
vacrel->bstrategy, |
| 1238 |
vacrel->rel, |
| 1239 |
MAIN_FORKNUM, |
| 1240 |
heap_vac_scan_next_block, |
| 1241 |
vacrel, |
| 1242 |
sizeof(uint8)); |
| 1243 |
|
| 1244 |
while (true) |
| 1245 |
{ |
| 1246 |
Buffer buf; |
| 1247 |
Page page; |
| 1248 |
uint8 blk_info = 0; |
| 1249 |
int ndeleted = 0; |
| 1250 |
bool has_lpdead_items; |
| 1251 |
void *per_buffer_data = NULL; |
| 1252 |
bool vm_page_frozen = false; |
| 1253 |
bool got_cleanup_lock = false; |
| 1254 |
|
| 1255 |
vacuum_delay_point(false); |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* Regularly check if wraparound failsafe should trigger. |
| 1259 |
* |
| 1260 |
* There is a similar check inside lazy_vacuum_all_indexes(), but |
| 1261 |
* relfrozenxid might start to look dangerously old before we reach |
| 1262 |
* that point. This check also provides failsafe coverage for the |
| 1263 |
* one-pass strategy, and the two-pass strategy with the index_cleanup |
| 1264 |
* param set to 'off'. |
| 1265 |
*/ |
| 1266 |
if (vacrel->scanned_pages > 0 && |
| 1267 |
vacrel->scanned_pages % FAILSAFE_EVERY_PAGES == 0) |
| 1268 |
lazy_check_wraparound_failsafe(vacrel); |
| 1269 |
|
| 1270 |
/* |
| 1271 |
* Consider if we definitely have enough space to process TIDs on page |
| 1272 |
* already. If we are close to overrunning the available space for |
| 1273 |
* dead_items TIDs, pause and do a cycle of vacuuming before we tackle |
| 1274 |
* this page. However, let's force at least one page-worth of tuples |
| 1275 |
* to be stored as to ensure we do at least some work when the memory |
| 1276 |
* configured is so low that we run out before storing anything. |
| 1277 |
*/ |
| 1278 |
if (vacrel->dead_items_info->num_items > 0 && |
| 1279 |
TidStoreMemoryUsage(vacrel->dead_items) > vacrel->dead_items_info->max_bytes) |
| 1280 |
{ |
| 1281 |
/* |
| 1282 |
* Before beginning index vacuuming, we release any pin we may |
| 1283 |
* hold on the visibility map page. This isn't necessary for |
| 1284 |
* correctness, but we do it anyway to avoid holding the pin |
| 1285 |
* across a lengthy, unrelated operation. |
| 1286 |
*/ |
| 1287 |
if (BufferIsValid(vmbuffer)) |
| 1288 |
{ |
| 1289 |
ReleaseBuffer(vmbuffer); |
| 1290 |
vmbuffer = InvalidBuffer; |
| 1291 |
} |
| 1292 |
|
| 1293 |
/* Perform a round of index and heap vacuuming */ |
| 1294 |
vacrel->consider_bypass_optimization = false; |
| 1295 |
lazy_vacuum(vacrel); |
| 1296 |
|
| 1297 |
/* |
| 1298 |
* Vacuum the Free Space Map to make newly-freed space visible on |
| 1299 |
* upper-level FSM pages. Note that blkno is the previously |
| 1300 |
* processed block. |
| 1301 |
*/ |
| 1302 |
FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, |
| 1303 |
blkno + 1); |
| 1304 |
next_fsm_block_to_vacuum = blkno; |
| 1305 |
|
| 1306 |
/* Report that we are once again scanning the heap */ |
| 1307 |
pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, |
| 1308 |
PROGRESS_VACUUM_PHASE_SCAN_HEAP); |
| 1309 |
} |
| 1310 |
|
| 1311 |
/* |
| 1312 |
* If the wraparound failsafe has engaged -- either via the check |
| 1313 |
* above or during index vacuuming invoked from this loop -- stop |
| 1314 |
* using the buffer access strategy so that the rest of the vacuum may |
| 1315 |
* use all of shared buffers. Failsafe mode stays engaged once |
| 1316 |
* triggered, so we only need to do this once. |
| 1317 |
*/ |
| 1318 |
if (unlikely(VacuumFailsafeActive) && !strategy_cleared) |
| 1319 |
{ |
| 1320 |
read_stream_clear_strategy(stream); |
| 1321 |
strategy_cleared = true; |
| 1322 |
} |
| 1323 |
|
| 1324 |
buf = read_stream_next_buffer(stream, &per_buffer_data); |
| 1325 |
|
| 1326 |
/* The relation is exhausted. */ |
| 1327 |
if (!BufferIsValid(buf)) |
| 1328 |
break; |
| 1329 |
|
| 1330 |
blk_info = *((uint8 *) per_buffer_data); |
| 1331 |
CheckBufferIsPinnedOnce(buf); |
| 1332 |
page = BufferGetPage(buf); |
| 1333 |
blkno = BufferGetBlockNumber(buf); |
| 1334 |
|
| 1335 |
vacrel->scanned_pages++; |
| 1336 |
if (blk_info & VAC_BLK_WAS_EAGER_SCANNED) |
| 1337 |
vacrel->eager_scanned_pages++; |
| 1338 |
|
| 1339 |
/* Report as block scanned, update error traceback information */ |
| 1340 |
pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno); |
| 1341 |
update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_SCAN_HEAP, |
| 1342 |
blkno, InvalidOffsetNumber); |
| 1343 |
|
| 1344 |
/* |
| 1345 |
* Pin the visibility map page in case we need to mark the page |
| 1346 |
* all-visible. In most cases this will be very cheap, because we'll |
| 1347 |
* already have the correct page pinned anyway. |
| 1348 |
*/ |
| 1349 |
visibilitymap_pin(vacrel->rel, blkno, &vmbuffer); |
| 1350 |
|
| 1351 |
/* |
| 1352 |
* We need a buffer cleanup lock to prune HOT chains and defragment |
| 1353 |
* the page in lazy_scan_prune. But when it's not possible to acquire |
| 1354 |
* a cleanup lock right away, we may be able to settle for reduced |
| 1355 |
* processing using lazy_scan_noprune. |
| 1356 |
*/ |
| 1357 |
got_cleanup_lock = ConditionalLockBufferForCleanup(buf); |
| 1358 |
|
| 1359 |
if (!got_cleanup_lock) |
| 1360 |
LockBuffer(buf, BUFFER_LOCK_SHARE); |
| 1361 |
|
| 1362 |
/* Check for new or empty pages before lazy_scan_[no]prune call */ |
| 1363 |
if (lazy_scan_new_or_empty(vacrel, buf, blkno, page, !got_cleanup_lock, |
| 1364 |
vmbuffer)) |
| 1365 |
{ |
| 1366 |
/* Processed as new/empty page (lock and pin released) */ |
| 1367 |
continue; |
| 1368 |
} |
| 1369 |
|
| 1370 |
/* |
| 1371 |
* If we didn't get the cleanup lock, we can still collect LP_DEAD |
| 1372 |
* items in the dead_items area for later vacuuming, count live and |
| 1373 |
* recently dead tuples for vacuum logging, and determine if this |
| 1374 |
* block could later be truncated. If we encounter any xid/mxids that |
| 1375 |
* require advancing the relfrozenxid/relminxid, we'll have to wait |
| 1376 |
* for a cleanup lock and call lazy_scan_prune(). |
| 1377 |
*/ |
| 1378 |
if (!got_cleanup_lock && |
| 1379 |
!lazy_scan_noprune(vacrel, buf, blkno, page, &has_lpdead_items)) |
| 1380 |
{ |
| 1381 |
/* |
| 1382 |
* lazy_scan_noprune could not do all required processing. Wait |
| 1383 |
* for a cleanup lock, and call lazy_scan_prune in the usual way. |
| 1384 |
*/ |
| 1385 |
Assert(vacrel->aggressive); |
| 1386 |
LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
| 1387 |
LockBufferForCleanup(buf); |
| 1388 |
got_cleanup_lock = true; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* If we have a cleanup lock, we must now prune, freeze, and count |
| 1393 |
* tuples. We may have acquired the cleanup lock originally, or we may |
| 1394 |
* have gone back and acquired it after lazy_scan_noprune() returned |
| 1395 |
* false. Either way, the page hasn't been processed yet. |
| 1396 |
* |
| 1397 |
* Like lazy_scan_noprune(), lazy_scan_prune() will count |
| 1398 |
* recently_dead_tuples and live tuples for vacuum logging, determine |
| 1399 |
* if the block can later be truncated, and accumulate the details of |
| 1400 |
* remaining LP_DEAD line pointers on the page into dead_items. These |
| 1401 |
* dead items include those pruned by lazy_scan_prune() as well as |
| 1402 |
* line pointers previously marked LP_DEAD. |
| 1403 |
*/ |
| 1404 |
if (got_cleanup_lock) |
| 1405 |
ndeleted = lazy_scan_prune(vacrel, buf, blkno, page, |
| 1406 |
vmbuffer, |
| 1407 |
blk_info & VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM, |
| 1408 |
&has_lpdead_items, &vm_page_frozen); |
| 1409 |
|
| 1410 |
/* |
| 1411 |
* Count an eagerly scanned page as a failure or a success. |
| 1412 |
* |
| 1413 |
* Only lazy_scan_prune() freezes pages, so if we didn't get the |
| 1414 |
* cleanup lock, we won't have frozen the page. However, we only count |
| 1415 |
* pages that were too new to require freezing as eager freeze |
| 1416 |
* failures. |
| 1417 |
* |
| 1418 |
* We could gather more information from lazy_scan_noprune() about |
| 1419 |
* whether or not there were tuples with XIDs or MXIDs older than the |
| 1420 |
* FreezeLimit or MultiXactCutoff. However, for simplicity, we simply |
| 1421 |
* exclude pages skipped due to cleanup lock contention from eager |
| 1422 |
* freeze algorithm caps. |
| 1423 |
*/ |
| 1424 |
if (got_cleanup_lock && |
| 1425 |
(blk_info & VAC_BLK_WAS_EAGER_SCANNED)) |
| 1426 |
{ |
| 1427 |
/* Aggressive vacuums do not eager scan. */ |
| 1428 |
Assert(!vacrel->aggressive); |
| 1429 |
|
| 1430 |
if (vm_page_frozen) |
| 1431 |
{ |
| 1432 |
if (vacrel->eager_scan_remaining_successes > 0) |
| 1433 |
vacrel->eager_scan_remaining_successes--; |
| 1434 |
|
| 1435 |
if (vacrel->eager_scan_remaining_successes == 0) |
| 1436 |
{ |
| 1437 |
/* |
| 1438 |
* Report only once that we disabled eager scanning. We |
| 1439 |
* may eagerly read ahead blocks in excess of the success |
| 1440 |
* or failure caps before attempting to freeze them, so we |
| 1441 |
* could reach here even after disabling additional eager |
| 1442 |
* scanning. |
| 1443 |
*/ |
| 1444 |
if (vacrel->eager_scan_max_fails_per_region > 0) |
| 1445 |
ereport(vacrel->verbose ? INFO : DEBUG2, |
| 1446 |
(errmsg("disabling eager scanning after freezing %u eagerly scanned blocks of relation \"%s.%s.%s\"", |
| 1447 |
orig_eager_scan_success_limit, |
| 1448 |
vacrel->dbname, vacrel->relnamespace, |
| 1449 |
vacrel->relname))); |
| 1450 |
|
| 1451 |
/* |
| 1452 |
* If we hit our success cap, permanently disable eager |
| 1453 |
* scanning by setting the other eager scan management |
| 1454 |
* fields to their disabled values. |
| 1455 |
*/ |
| 1456 |
vacrel->eager_scan_remaining_fails = 0; |
| 1457 |
vacrel->next_eager_scan_region_start = InvalidBlockNumber; |
| 1458 |
vacrel->eager_scan_max_fails_per_region = 0; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
else if (vacrel->eager_scan_remaining_fails > 0) |
| 1462 |
vacrel->eager_scan_remaining_fails--; |
| 1463 |
} |
| 1464 |
|
| 1465 |
/* |
| 1466 |
* Now drop the buffer lock and, potentially, update the FSM. |
| 1467 |
* |
| 1468 |
* Our goal is to update the freespace map the last time we touch the |
| 1469 |
* page. If we'll process a block in the second pass, we may free up |
| 1470 |
* additional space on the page, so it is better to update the FSM |
| 1471 |
* after the second pass. If the relation has no indexes, or if index |
| 1472 |
* vacuuming is disabled, there will be no second heap pass; if this |
| 1473 |
* particular page has no dead items, the second heap pass will not |
| 1474 |
* touch this page. So, in those cases, update the FSM now. |
| 1475 |
* |
| 1476 |
* Note: In corner cases, it's possible to miss updating the FSM |
| 1477 |
* entirely. If index vacuuming is currently enabled, we'll skip the |
| 1478 |
* FSM update now. But if failsafe mode is later activated, or there |
| 1479 |
* are so few dead tuples that index vacuuming is bypassed, there will |
| 1480 |
* also be no opportunity to update the FSM later, because we'll never |
| 1481 |
* revisit this page. Since updating the FSM is desirable but not |
| 1482 |
* absolutely required, that's OK. |
| 1483 |
*/ |
| 1484 |
if (vacrel->nindexes == 0 |
| 1485 |
|| !vacrel->do_index_vacuuming |
| 1486 |
|| !has_lpdead_items) |
| 1487 |
{ |
| 1488 |
Size freespace = PageGetHeapFreeSpace(page); |
| 1489 |
|
| 1490 |
UnlockReleaseBuffer(buf); |
| 1491 |
RecordPageWithFreeSpace(vacrel->rel, blkno, freespace); |
| 1492 |
|
| 1493 |
/* |
| 1494 |
* Periodically perform FSM vacuuming to make newly-freed space |
| 1495 |
* visible on upper FSM pages. This is done after vacuuming if the |
| 1496 |
* table has indexes. There will only be newly-freed space if we |
| 1497 |
* held the cleanup lock and lazy_scan_prune() was called. |
| 1498 |
*/ |
| 1499 |
if (got_cleanup_lock && vacrel->nindexes == 0 && ndeleted > 0 && |
| 1500 |
blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES) |
| 1501 |
{ |
| 1502 |
FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, |
| 1503 |
blkno); |
| 1504 |
next_fsm_block_to_vacuum = blkno; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
else |
| 1508 |
UnlockReleaseBuffer(buf); |
| 1509 |
} |
| 1510 |
|
| 1511 |
vacrel->blkno = InvalidBlockNumber; |
| 1512 |
if (BufferIsValid(vmbuffer)) |
| 1513 |
ReleaseBuffer(vmbuffer); |
| 1514 |
|
| 1515 |
/* |
| 1516 |
* Report that everything is now scanned. We never skip scanning the last |
| 1517 |
* block in the relation, so we can pass rel_pages here. |
| 1518 |
*/ |
| 1519 |
pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, |
| 1520 |
rel_pages); |
| 1521 |
|
| 1522 |
/* now we can compute the new value for pg_class.reltuples */ |
| 1523 |
vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages, |
| 1524 |
vacrel->scanned_pages, |
| 1525 |
vacrel->live_tuples); |
| 1526 |
|
| 1527 |
/* |
| 1528 |
* Also compute the total number of surviving heap entries. In the |
| 1529 |
* (unlikely) scenario that new_live_tuples is -1, take it as zero. |
| 1530 |
*/ |
| 1531 |
vacrel->new_rel_tuples = |
| 1532 |
Max(vacrel->new_live_tuples, 0) + vacrel->recently_dead_tuples + |
| 1533 |
vacrel->missed_dead_tuples; |
| 1534 |
|
| 1535 |
read_stream_end(stream); |
| 1536 |
|
| 1537 |
/* |
| 1538 |
* Do index vacuuming (call each index's ambulkdelete routine), then do |
| 1539 |
* related heap vacuuming |
| 1540 |
*/ |
| 1541 |
if (vacrel->dead_items_info->num_items > 0) |
| 1542 |
lazy_vacuum(vacrel); |
| 1543 |
|
| 1544 |
/* |
| 1545 |
* Vacuum the remainder of the Free Space Map. We must do this whether or |
| 1546 |
* not there were indexes, and whether or not we bypassed index vacuuming. |
| 1547 |
* We can pass rel_pages here because we never skip scanning the last |
| 1548 |
* block of the relation. |
| 1549 |
*/ |
| 1550 |
if (rel_pages > next_fsm_block_to_vacuum) |
| 1551 |
FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, rel_pages); |
| 1552 |
|
| 1553 |
/* report all blocks vacuumed */ |
| 1554 |
pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, rel_pages); |
| 1555 |
|
| 1556 |
/* Do final index cleanup (call each index's amvacuumcleanup routine) */ |
| 1557 |
if (vacrel->nindexes > 0 && vacrel->do_index_cleanup) |
| 1558 |
lazy_cleanup_all_indexes(vacrel); |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* |
| 1562 |
* heap_vac_scan_next_block() -- read stream callback to get the next block |
| 1563 |
* for vacuum to process |
| 1564 |
* |
| 1565 |
* Every time lazy_scan_heap() needs a new block to process during its first |
| 1566 |
* phase, it invokes read_stream_next_buffer() with a stream set up to call |
| 1567 |
* heap_vac_scan_next_block() to get the next block. |
| 1568 |
* |
| 1569 |
* heap_vac_scan_next_block() uses the visibility map, vacuum options, and |
| 1570 |
* various thresholds to skip blocks which do not need to be processed and |
| 1571 |
* returns the next block to process or InvalidBlockNumber if there are no |
| 1572 |
* remaining blocks. |
| 1573 |
* |
| 1574 |
* The visibility status of the next block to process and whether or not it |
| 1575 |
* was eager scanned is set in the per_buffer_data. |
| 1576 |
* |
| 1577 |
* callback_private_data contains a reference to the LVRelState, passed to the |
| 1578 |
* read stream API during stream setup. The LVRelState is an in/out parameter |
| 1579 |
* here (locally named `vacrel`). Vacuum options and information about the |
| 1580 |
* relation are read from it. vacrel->skippedallvis is set if we skip a block |
| 1581 |
* that's all-visible but not all-frozen (to ensure that we don't update |
| 1582 |
* relfrozenxid in that case). vacrel also holds information about the next |
| 1583 |
* unskippable block -- as bookkeeping for this function. |
| 1584 |
*/ |
| 1585 |
static BlockNumber |
| 1586 |
heap_vac_scan_next_block(ReadStream *stream, |
| 1587 |
void *callback_private_data, |
| 1588 |
void *per_buffer_data) |
| 1589 |
{ |
| 1590 |
BlockNumber next_block; |
| 1591 |
LVRelState *vacrel = callback_private_data; |
| 1592 |
uint8 blk_info = 0; |
| 1593 |
|
| 1594 |
/* relies on InvalidBlockNumber + 1 overflowing to 0 on first call */ |
| 1595 |
next_block = vacrel->current_block + 1; |
| 1596 |
|
| 1597 |
/* Have we reached the end of the relation? */ |
| 1598 |
if (next_block >= vacrel->rel_pages) |
| 1599 |
{ |
| 1600 |
if (BufferIsValid(vacrel->next_unskippable_vmbuffer)) |
| 1601 |
{ |
| 1602 |
ReleaseBuffer(vacrel->next_unskippable_vmbuffer); |
| 1603 |
vacrel->next_unskippable_vmbuffer = InvalidBuffer; |
| 1604 |
} |
| 1605 |
return InvalidBlockNumber; |
| 1606 |
} |
| 1607 |
|
| 1608 |
/* |
| 1609 |
* We must be in one of the three following states: |
| 1610 |
*/ |
| 1611 |
if (next_block > vacrel->next_unskippable_block || |
| 1612 |
vacrel->next_unskippable_block == InvalidBlockNumber) |
| 1613 |
{ |
| 1614 |
/* |
| 1615 |
* 1. We have just processed an unskippable block (or we're at the |
| 1616 |
* beginning of the scan). Find the next unskippable block using the |
| 1617 |
* visibility map. |
| 1618 |
*/ |
| 1619 |
bool skipsallvis; |
| 1620 |
|
| 1621 |
find_next_unskippable_block(vacrel, &skipsallvis); |
| 1622 |
|
| 1623 |
/* |
| 1624 |
* We now know the next block that we must process. It can be the |
| 1625 |
* next block after the one we just processed, or something further |
| 1626 |
* ahead. If it's further ahead, we can jump to it, but we choose to |
| 1627 |
* do so only if we can skip at least SKIP_PAGES_THRESHOLD consecutive |
| 1628 |
* pages. Since we're reading sequentially, the OS should be doing |
| 1629 |
* readahead for us, so there's no gain in skipping a page now and |
| 1630 |
* then. Skipping such a range might even discourage sequential |
| 1631 |
* detection. |
| 1632 |
* |
| 1633 |
* This test also enables more frequent relfrozenxid advancement |
| 1634 |
* during non-aggressive VACUUMs. If the range has any all-visible |
| 1635 |
* pages then skipping makes updating relfrozenxid unsafe, which is a |
| 1636 |
* real downside. |
| 1637 |
*/ |
| 1638 |
if (vacrel->next_unskippable_block - next_block >= SKIP_PAGES_THRESHOLD) |
| 1639 |
{ |
| 1640 |
next_block = vacrel->next_unskippable_block; |
| 1641 |
if (skipsallvis) |
| 1642 |
vacrel->skippedallvis = true; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
/* Now we must be in one of the two remaining states: */ |
| 1647 |
if (next_block < vacrel->next_unskippable_block) |
| 1648 |
{ |
| 1649 |
/* |
| 1650 |
* 2. We are processing a range of blocks that we could have skipped |
| 1651 |
* but chose not to. We know that they are all-visible in the VM, |
| 1652 |
* otherwise they would've been unskippable. |
| 1653 |
*/ |
| 1654 |
vacrel->current_block = next_block; |
| 1655 |
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM; |
| 1656 |
*((uint8 *) per_buffer_data) = blk_info; |
| 1657 |
return vacrel->current_block; |
| 1658 |
} |
| 1659 |
else |
| 1660 |
{ |
| 1661 |
/* |
| 1662 |
* 3. We reached the next unskippable block. Process it. On next |
| 1663 |
* iteration, we will be back in state 1. |
| 1664 |
*/ |
| 1665 |
Assert(next_block == vacrel->next_unskippable_block); |
| 1666 |
|
| 1667 |
vacrel->current_block = next_block; |
| 1668 |
if (vacrel->next_unskippable_allvis) |
| 1669 |
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM; |
| 1670 |
if (vacrel->next_unskippable_eager_scanned) |
| 1671 |
blk_info |= VAC_BLK_WAS_EAGER_SCANNED; |
| 1672 |
*((uint8 *) per_buffer_data) = blk_info; |
| 1673 |
return vacrel->current_block; |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
/* |
| 1678 |
* Find the next unskippable block in a vacuum scan using the visibility map. |
| 1679 |
* The next unskippable block and its visibility information is updated in |
| 1680 |
* vacrel. |
| 1681 |
* |
| 1682 |
* Note: our opinion of which blocks can be skipped can go stale immediately. |
| 1683 |
* It's okay if caller "misses" a page whose all-visible or all-frozen marking |
| 1684 |
* was concurrently cleared, though. All that matters is that caller scan all |
| 1685 |
* pages whose tuples might contain XIDs < OldestXmin, or MXIDs < OldestMxact. |
| 1686 |
* (Actually, non-aggressive VACUUMs can choose to skip all-visible pages with |
| 1687 |
* older XIDs/MXIDs. The *skippedallvis flag will be set here when the choice |
| 1688 |
* to skip such a range is actually made, making everything safe.) |
| 1689 |
*/ |
| 1690 |
static void |
| 1691 |
find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis) |
| 1692 |
{ |
| 1693 |
BlockNumber rel_pages = vacrel->rel_pages; |
| 1694 |
BlockNumber next_unskippable_block = vacrel->next_unskippable_block + 1; |
| 1695 |
Buffer next_unskippable_vmbuffer = vacrel->next_unskippable_vmbuffer; |
| 1696 |
bool next_unskippable_eager_scanned = false; |
| 1697 |
bool next_unskippable_allvis; |
| 1698 |
|
| 1699 |
*skipsallvis = false; |
| 1700 |
|
| 1701 |
for (;; next_unskippable_block++) |
| 1702 |
{ |
| 1703 |
uint8 mapbits = visibilitymap_get_status(vacrel->rel, |
| 1704 |
next_unskippable_block, |
| 1705 |
&next_unskippable_vmbuffer); |
| 1706 |
|
| 1707 |
next_unskippable_allvis = (mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0; |
| 1708 |
|
| 1709 |
/* |
| 1710 |
* At the start of each eager scan region, normal vacuums with eager |
| 1711 |
* scanning enabled reset the failure counter, allowing vacuum to |
| 1712 |
* resume eager scanning if it had been suspended in the previous |
| 1713 |
* region. |
| 1714 |
*/ |
| 1715 |
if (next_unskippable_block >= vacrel->next_eager_scan_region_start) |
| 1716 |
{ |
| 1717 |
vacrel->eager_scan_remaining_fails = |
| 1718 |
vacrel->eager_scan_max_fails_per_region; |
| 1719 |
vacrel->next_eager_scan_region_start += EAGER_SCAN_REGION_SIZE; |
| 1720 |
} |
| 1721 |
|
| 1722 |
/* |
| 1723 |
* A block is unskippable if it is not all visible according to the |
| 1724 |
* visibility map. |
| 1725 |
*/ |
| 1726 |
if (!next_unskippable_allvis) |
| 1727 |
{ |
| 1728 |
Assert((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0); |
| 1729 |
break; |
| 1730 |
} |
| 1731 |
|
| 1732 |
/* |
| 1733 |
* Caller must scan the last page to determine whether it has tuples |
| 1734 |
* (caller must have the opportunity to set vacrel->nonempty_pages). |
| 1735 |
* This rule avoids having lazy_truncate_heap() take access-exclusive |
| 1736 |
* lock on rel to attempt a truncation that fails anyway, just because |
| 1737 |
* there are tuples on the last page (it is likely that there will be |
| 1738 |
* tuples on other nearby pages as well, but those can be skipped). |
| 1739 |
* |
| 1740 |
* Implement this by always treating the last block as unsafe to skip. |
| 1741 |
*/ |
| 1742 |
if (next_unskippable_block == rel_pages - 1) |
| 1743 |
break; |
| 1744 |
|
| 1745 |
/* DISABLE_PAGE_SKIPPING makes all skipping unsafe */ |
| 1746 |
if (!vacrel->skipwithvm) |
| 1747 |
break; |
| 1748 |
|
| 1749 |
/* |
| 1750 |
* All-frozen pages cannot contain XIDs < OldestXmin (XIDs that aren't |
| 1751 |
* already frozen by now), so this page can be skipped. |
| 1752 |
*/ |
| 1753 |
if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0) |
| 1754 |
continue; |
| 1755 |
|
| 1756 |
/* |
| 1757 |
* Aggressive vacuums cannot skip any all-visible pages that are not |
| 1758 |
* also all-frozen. |
| 1759 |
*/ |
| 1760 |
if (vacrel->aggressive) |
| 1761 |
break; |
| 1762 |
|
| 1763 |
/* |
| 1764 |
* Normal vacuums with eager scanning enabled only skip all-visible |
| 1765 |
* but not all-frozen pages if they have hit the failure limit for the |
| 1766 |
* current eager scan region. |
| 1767 |
*/ |
| 1768 |
if (vacrel->eager_scan_remaining_fails > 0) |
| 1769 |
{ |
| 1770 |
next_unskippable_eager_scanned = true; |
| 1771 |
break; |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* |
| 1775 |
* All-visible blocks are safe to skip in a normal vacuum. But |
| 1776 |
* remember that the final range contains such a block for later. |
| 1777 |
*/ |
| 1778 |
*skipsallvis = true; |
| 1779 |
} |
| 1780 |
|
| 1781 |
/* write the local variables back to vacrel */ |
| 1782 |
vacrel->next_unskippable_block = next_unskippable_block; |
| 1783 |
vacrel->next_unskippable_allvis = next_unskippable_allvis; |
| 1784 |
vacrel->next_unskippable_eager_scanned = next_unskippable_eager_scanned; |
| 1785 |
vacrel->next_unskippable_vmbuffer = next_unskippable_vmbuffer; |
| 1786 |
} |
| 1787 |
|
| 1788 |
/* |
| 1789 |
* lazy_scan_new_or_empty() -- lazy_scan_heap() new/empty page handling. |
| 1790 |
* |
| 1791 |
* Must call here to handle both new and empty pages before calling |
| 1792 |
* lazy_scan_prune or lazy_scan_noprune, since they're not prepared to deal |
| 1793 |
* with new or empty pages. |
| 1794 |
* |
| 1795 |
* It's necessary to consider new pages as a special case, since the rules for |
| 1796 |
* maintaining the visibility map and FSM with empty pages are a little |
| 1797 |
* different (though new pages can be truncated away during rel truncation). |
| 1798 |
* |
| 1799 |
* Empty pages are not really a special case -- they're just heap pages that |
| 1800 |
* have no allocated tuples (including even LP_UNUSED items). You might |
| 1801 |
* wonder why we need to handle them here all the same. It's only necessary |
| 1802 |
* because of a corner-case involving a hard crash during heap relation |
| 1803 |
* extension. If we ever make relation-extension crash safe, then it should |
| 1804 |
* no longer be necessary to deal with empty pages here (or new pages, for |
| 1805 |
* that matter). |
| 1806 |
* |
| 1807 |
* Caller must hold at least a shared lock. We might need to escalate the |
| 1808 |
* lock in that case, so the type of lock caller holds needs to be specified |
| 1809 |
* using 'sharelock' argument. |
| 1810 |
* |
| 1811 |
* Returns false in common case where caller should go on to call |
| 1812 |
* lazy_scan_prune (or lazy_scan_noprune). Otherwise returns true, indicating |
| 1813 |
* that lazy_scan_heap is done processing the page, releasing lock on caller's |
| 1814 |
* behalf. |
| 1815 |
* |
| 1816 |
* No vm_page_frozen output parameter (like that passed to lazy_scan_prune()) |
| 1817 |
* is passed here because neither empty nor new pages can be eagerly frozen. |
| 1818 |
* New pages are never frozen. Empty pages are always set frozen in the VM at |
| 1819 |
* the same time that they are set all-visible, and we don't eagerly scan |
| 1820 |
* frozen pages. |
| 1821 |
*/ |
| 1822 |
static bool |
| 1823 |
lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, BlockNumber blkno, |
| 1824 |
Page page, bool sharelock, Buffer vmbuffer) |
| 1825 |
{ |
| 1826 |
Size freespace; |
| 1827 |
|
| 1828 |
if (PageIsNew(page)) |
| 1829 |
{ |
| 1830 |
/* |
| 1831 |
* All-zeroes pages can be left over if either a backend extends the |
| 1832 |
* relation by a single page, but crashes before the newly initialized |
| 1833 |
* page has been written out, or when bulk-extending the relation |
| 1834 |
* (which creates a number of empty pages at the tail end of the |
| 1835 |
* relation), and then enters them into the FSM. |
| 1836 |
* |
| 1837 |
* Note we do not enter the page into the visibilitymap. That has the |
| 1838 |
* downside that we repeatedly visit this page in subsequent vacuums, |
| 1839 |
* but otherwise we'll never discover the space on a promoted standby. |
| 1840 |
* The harm of repeated checking ought to normally not be too bad. The |
| 1841 |
* space usually should be used at some point, otherwise there |
| 1842 |
* wouldn't be any regular vacuums. |
| 1843 |
* |
| 1844 |
* Make sure these pages are in the FSM, to ensure they can be reused. |
| 1845 |
* Do that by testing if there's any space recorded for the page. If |
| 1846 |
* not, enter it. We do so after releasing the lock on the heap page, |
| 1847 |
* the FSM is approximate, after all. |
| 1848 |
*/ |
| 1849 |
UnlockReleaseBuffer(buf); |
| 1850 |
|
| 1851 |
if (GetRecordedFreeSpace(vacrel->rel, blkno) == 0) |
| 1852 |
{ |
| 1853 |
freespace = BLCKSZ - SizeOfPageHeaderData; |
| 1854 |
|
| 1855 |
RecordPageWithFreeSpace(vacrel->rel, blkno, freespace); |
| 1856 |
} |
| 1857 |
|
| 1858 |
return true; |
| 1859 |
} |
| 1860 |
|
| 1861 |
if (PageIsEmpty(page)) |
| 1862 |
{ |
| 1863 |
/* |
| 1864 |
* It seems likely that caller will always be able to get a cleanup |
| 1865 |
* lock on an empty page. But don't take any chances -- escalate to |
| 1866 |
* an exclusive lock (still don't need a cleanup lock, though). |
| 1867 |
*/ |
| 1868 |
if (sharelock) |
| 1869 |
{ |
| 1870 |
LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
| 1871 |
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); |
| 1872 |
|
| 1873 |
if (!PageIsEmpty(page)) |
| 1874 |
{ |
| 1875 |
/* page isn't new or empty -- keep lock and pin for now */ |
| 1876 |
return false; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
else |
| 1880 |
{ |
| 1881 |
/* Already have a full cleanup lock (which is more than enough) */ |
| 1882 |
} |
| 1883 |
|
| 1884 |
/* |
| 1885 |
* Unlike new pages, empty pages are always set all-visible and |
| 1886 |
* all-frozen. |
| 1887 |
*/ |
| 1888 |
if (!PageIsAllVisible(page)) |
| 1889 |
{ |
| 1890 |
START_CRIT_SECTION(); |
| 1891 |
|
| 1892 |
/* mark buffer dirty before writing a WAL record */ |
| 1893 |
MarkBufferDirty(buf); |
| 1894 |
|
| 1895 |
/* |
| 1896 |
* It's possible that another backend has extended the heap, |
| 1897 |
* initialized the page, and then failed to WAL-log the page due |
| 1898 |
* to an ERROR. Since heap extension is not WAL-logged, recovery |
| 1899 |
* might try to replay our record setting the page all-visible and |
| 1900 |
* find that the page isn't initialized, which will cause a PANIC. |
| 1901 |
* To prevent that, check whether the page has been previously |
| 1902 |
* WAL-logged, and if not, do that now. |
| 1903 |
*/ |
| 1904 |
if (RelationNeedsWAL(vacrel->rel) && |
| 1905 |
PageGetLSN(page) == InvalidXLogRecPtr) |
| 1906 |
log_newpage_buffer(buf, true); |
| 1907 |
|
| 1908 |
PageSetAllVisible(page); |
| 1909 |
visibilitymap_set(vacrel->rel, blkno, buf, |
| 1910 |
InvalidXLogRecPtr, |
| 1911 |
vmbuffer, InvalidTransactionId, |
| 1912 |
VISIBILITYMAP_ALL_VISIBLE | |
| 1913 |
VISIBILITYMAP_ALL_FROZEN); |
| 1914 |
END_CRIT_SECTION(); |
| 1915 |
|
| 1916 |
/* Count the newly all-frozen pages for logging */ |
| 1917 |
vacrel->vm_new_visible_pages++; |
| 1918 |
vacrel->vm_new_visible_frozen_pages++; |
| 1919 |
} |
| 1920 |
|
| 1921 |
freespace = PageGetHeapFreeSpace(page); |
| 1922 |
UnlockReleaseBuffer(buf); |
| 1923 |
RecordPageWithFreeSpace(vacrel->rel, blkno, freespace); |
| 1924 |
return true; |
| 1925 |
} |
| 1926 |
|
| 1927 |
/* page isn't new or empty -- keep lock and pin */ |
| 1928 |
return false; |
| 1929 |
} |
| 1930 |
|
| 1931 |
/* qsort comparator for sorting OffsetNumbers */ |
| 1932 |
static int |
| 1933 |
cmpOffsetNumbers(const void *a, const void *b) |
| 1934 |
{ |
| 1935 |
return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b); |
| 1936 |
} |
| 1937 |
|
| 1938 |
/* |
| 1939 |
* lazy_scan_prune() -- lazy_scan_heap() pruning and freezing. |
| 1940 |
* |
| 1941 |
* Caller must hold pin and buffer cleanup lock on the buffer. |
| 1942 |
* |
| 1943 |
* vmbuffer is the buffer containing the VM block with visibility information |
| 1944 |
* for the heap block, blkno. all_visible_according_to_vm is the saved |
| 1945 |
* visibility status of the heap block looked up earlier by the caller. We |
| 1946 |
* won't rely entirely on this status, as it may be out of date. |
| 1947 |
* |
| 1948 |
* *has_lpdead_items is set to true or false depending on whether, upon return |
| 1949 |
* from this function, any LP_DEAD items are still present on the page. |
| 1950 |
* |
| 1951 |
* *vm_page_frozen is set to true if the page is newly set all-frozen in the |
| 1952 |
* VM. The caller currently only uses this for determining whether an eagerly |
| 1953 |
* scanned page was successfully set all-frozen. |
| 1954 |
* |
| 1955 |
* Returns the number of tuples deleted from the page during HOT pruning. |
| 1956 |
*/ |
| 1957 |
static int |
| 1958 |
lazy_scan_prune(LVRelState *vacrel, |
| 1959 |
Buffer buf, |
| 1960 |
BlockNumber blkno, |
| 1961 |
Page page, |
| 1962 |
Buffer vmbuffer, |
| 1963 |
bool all_visible_according_to_vm, |
| 1964 |
bool *has_lpdead_items, |
| 1965 |
bool *vm_page_frozen) |
| 1966 |
{ |
| 1967 |
Relation rel = vacrel->rel; |
| 1968 |
PruneFreezeResult presult; |
| 1969 |
int prune_options = 0; |
| 1970 |
|
| 1971 |
Assert(BufferGetBlockNumber(buf) == blkno); |
| 1972 |
|
| 1973 |
/* |
| 1974 |
* Prune all HOT-update chains and potentially freeze tuples on this page. |
| 1975 |
* |
| 1976 |
* If the relation has no indexes, we can immediately mark would-be dead |
| 1977 |
* items LP_UNUSED. |
| 1978 |
* |
| 1979 |
* The number of tuples removed from the page is returned in |
| 1980 |
* presult.ndeleted. It should not be confused with presult.lpdead_items; |
| 1981 |
* presult.lpdead_items's final value can be thought of as the number of |
| 1982 |
* tuples that were deleted from indexes. |
| 1983 |
* |
| 1984 |
* We will update the VM after collecting LP_DEAD items and freezing |
| 1985 |
* tuples. Pruning will have determined whether or not the page is |
| 1986 |
* all-visible. |
| 1987 |
*/ |
| 1988 |
prune_options = HEAP_PAGE_PRUNE_FREEZE; |
| 1989 |
if (vacrel->nindexes == 0) |
| 1990 |
prune_options |= HEAP_PAGE_PRUNE_MARK_UNUSED_NOW; |
| 1991 |
|
| 1992 |
heap_page_prune_and_freeze(rel, buf, vacrel->vistest, prune_options, |
| 1993 |
&vacrel->cutoffs, &presult, PRUNE_VACUUM_SCAN, |
| 1994 |
&vacrel->offnum, |
| 1995 |
&vacrel->NewRelfrozenXid, &vacrel->NewRelminMxid); |
| 1996 |
|
| 1997 |
Assert(MultiXactIdIsValid(vacrel->NewRelminMxid)); |
| 1998 |
Assert(TransactionIdIsValid(vacrel->NewRelfrozenXid)); |
| 1999 |
|
| 2000 |
if (presult.nfrozen > 0) |
| 2001 |
{ |
| 2002 |
/* |
| 2003 |
* We don't increment the new_frozen_tuple_pages instrumentation |
| 2004 |
* counter when nfrozen == 0, since it only counts pages with newly |
| 2005 |
* frozen tuples (don't confuse that with pages newly set all-frozen |
| 2006 |
* in VM). |
| 2007 |
*/ |
| 2008 |
vacrel->new_frozen_tuple_pages++; |
| 2009 |
} |
| 2010 |
|
| 2011 |
/* |
| 2012 |
* VACUUM will call heap_page_is_all_visible() during the second pass over |
| 2013 |
* the heap to determine all_visible and all_frozen for the page -- this |
| 2014 |
* is a specialized version of the logic from this function. Now that |
| 2015 |
* we've finished pruning and freezing, make sure that we're in total |
| 2016 |
* agreement with heap_page_is_all_visible() using an assertion. |
| 2017 |
*/ |
| 2018 |
#ifdef USE_ASSERT_CHECKING |
| 2019 |
/* Note that all_frozen value does not matter when !all_visible */ |
| 2020 |
if (presult.all_visible) |
| 2021 |
{ |
| 2022 |
TransactionId debug_cutoff; |
| 2023 |
bool debug_all_frozen; |
| 2024 |
|
| 2025 |
Assert(presult.lpdead_items == 0); |
| 2026 |
|
| 2027 |
if (!heap_page_is_all_visible(vacrel, buf, |
| 2028 |
&debug_cutoff, &debug_all_frozen)) |
| 2029 |
Assert(false); |
| 2030 |
|
| 2031 |
Assert(presult.all_frozen == debug_all_frozen); |
| 2032 |
|
| 2033 |
Assert(!TransactionIdIsValid(debug_cutoff) || |
| 2034 |
debug_cutoff == presult.vm_conflict_horizon); |
| 2035 |
} |
| 2036 |
#endif |
| 2037 |
|
| 2038 |
/* |
| 2039 |
* Now save details of the LP_DEAD items from the page in vacrel |
| 2040 |
*/ |
| 2041 |
if (presult.lpdead_items > 0) |
| 2042 |
{ |
| 2043 |
vacrel->lpdead_item_pages++; |
| 2044 |
|
| 2045 |
/* |
| 2046 |
* deadoffsets are collected incrementally in |
| 2047 |
* heap_page_prune_and_freeze() as each dead line pointer is recorded, |
| 2048 |
* with an indeterminate order, but dead_items_add requires them to be |
| 2049 |
* sorted. |
| 2050 |
*/ |
| 2051 |
qsort(presult.deadoffsets, presult.lpdead_items, sizeof(OffsetNumber), |
| 2052 |
cmpOffsetNumbers); |
| 2053 |
|
| 2054 |
dead_items_add(vacrel, blkno, presult.deadoffsets, presult.lpdead_items); |
| 2055 |
} |
| 2056 |
|
| 2057 |
/* Finally, add page-local counts to whole-VACUUM counts */ |
| 2058 |
vacrel->tuples_deleted += presult.ndeleted; |
| 2059 |
vacrel->tuples_frozen += presult.nfrozen; |
| 2060 |
vacrel->lpdead_items += presult.lpdead_items; |
| 2061 |
vacrel->live_tuples += presult.live_tuples; |
| 2062 |
vacrel->recently_dead_tuples += presult.recently_dead_tuples; |
| 2063 |
|
| 2064 |
/* Can't truncate this page */ |
| 2065 |
if (presult.hastup) |
| 2066 |
vacrel->nonempty_pages = blkno + 1; |
| 2067 |
|
| 2068 |
/* Did we find LP_DEAD items? */ |
| 2069 |
*has_lpdead_items = (presult.lpdead_items > 0); |
| 2070 |
|
| 2071 |
Assert(!presult.all_visible || !(*has_lpdead_items)); |
| 2072 |
|
| 2073 |
/* |
| 2074 |
* Handle setting visibility map bit based on information from the VM (as |
| 2075 |
* of last heap_vac_scan_next_block() call), and from all_visible and |
| 2076 |
* all_frozen variables |
| 2077 |
*/ |
| 2078 |
if (!all_visible_according_to_vm && presult.all_visible) |
| 2079 |
{ |
| 2080 |
uint8 old_vmbits; |
| 2081 |
uint8 flags = VISIBILITYMAP_ALL_VISIBLE; |
| 2082 |
|
| 2083 |
if (presult.all_frozen) |
| 2084 |
{ |
| 2085 |
Assert(!TransactionIdIsValid(presult.vm_conflict_horizon)); |
| 2086 |
flags |= VISIBILITYMAP_ALL_FROZEN; |
| 2087 |
} |
| 2088 |
|
| 2089 |
/* |
| 2090 |
* It should never be the case that the visibility map page is set |
| 2091 |
* while the page-level bit is clear, but the reverse is allowed (if |
| 2092 |
* checksums are not enabled). Regardless, set both bits so that we |
| 2093 |
* get back in sync. |
| 2094 |
* |
| 2095 |
* NB: If the heap page is all-visible but the VM bit is not set, we |
| 2096 |
* don't need to dirty the heap page. However, if checksums are |
| 2097 |
* enabled, we do need to make sure that the heap page is dirtied |
| 2098 |
* before passing it to visibilitymap_set(), because it may be logged. |
| 2099 |
* Given that this situation should only happen in rare cases after a |
| 2100 |
* crash, it is not worth optimizing. |
| 2101 |
*/ |
| 2102 |
PageSetAllVisible(page); |
| 2103 |
MarkBufferDirty(buf); |
| 2104 |
old_vmbits = visibilitymap_set(vacrel->rel, blkno, buf, |
| 2105 |
InvalidXLogRecPtr, |
| 2106 |
vmbuffer, presult.vm_conflict_horizon, |
| 2107 |
flags); |
| 2108 |
|
| 2109 |
/* |
| 2110 |
* If the page wasn't already set all-visible and/or all-frozen in the |
| 2111 |
* VM, count it as newly set for logging. |
| 2112 |
*/ |
| 2113 |
if ((old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0) |
| 2114 |
{ |
| 2115 |
vacrel->vm_new_visible_pages++; |
| 2116 |
if (presult.all_frozen) |
| 2117 |
{ |
| 2118 |
vacrel->vm_new_visible_frozen_pages++; |
| 2119 |
*vm_page_frozen = true; |
| 2120 |
} |
| 2121 |
} |
| 2122 |
else if ((old_vmbits & VISIBILITYMAP_ALL_FROZEN) == 0 && |
| 2123 |
presult.all_frozen) |
| 2124 |
{ |
| 2125 |
vacrel->vm_new_frozen_pages++; |
| 2126 |
*vm_page_frozen = true; |
| 2127 |
} |
| 2128 |
} |
| 2129 |
|
| 2130 |
/* |
| 2131 |
* As of PostgreSQL 9.2, the visibility map bit should never be set if the |
| 2132 |
* page-level bit is clear. However, it's possible that the bit got |
| 2133 |
* cleared after heap_vac_scan_next_block() was called, so we must recheck |
| 2134 |
* with buffer lock before concluding that the VM is corrupt. |
| 2135 |
*/ |
| 2136 |
else if (all_visible_according_to_vm && !PageIsAllVisible(page) && |
| 2137 |
visibilitymap_get_status(vacrel->rel, blkno, &vmbuffer) != 0) |
| 2138 |
{ |
| 2139 |
elog(WARNING, "page is not marked all-visible but visibility map bit is set in relation \"%s\" page %u", |
| 2140 |
vacrel->relname, blkno); |
| 2141 |
visibilitymap_clear(vacrel->rel, blkno, vmbuffer, |
| 2142 |
VISIBILITYMAP_VALID_BITS); |
| 2143 |
} |
| 2144 |
|
| 2145 |
/* |
| 2146 |
* It's possible for the value returned by |
| 2147 |
* GetOldestNonRemovableTransactionId() to move backwards, so it's not |
| 2148 |
* wrong for us to see tuples that appear to not be visible to everyone |
| 2149 |
* yet, while PD_ALL_VISIBLE is already set. The real safe xmin value |
| 2150 |
* never moves backwards, but GetOldestNonRemovableTransactionId() is |
| 2151 |
* conservative and sometimes returns a value that's unnecessarily small, |
| 2152 |
* so if we see that contradiction it just means that the tuples that we |
| 2153 |
* think are not visible to everyone yet actually are, and the |
| 2154 |
* PD_ALL_VISIBLE flag is correct. |
| 2155 |
* |
| 2156 |
* There should never be LP_DEAD items on a page with PD_ALL_VISIBLE set, |
| 2157 |
* however. |
| 2158 |
*/ |
| 2159 |
else if (presult.lpdead_items > 0 && PageIsAllVisible(page)) |
| 2160 |
{ |
| 2161 |
elog(WARNING, "page containing LP_DEAD items is marked as all-visible in relation \"%s\" page %u", |
| 2162 |
vacrel->relname, blkno); |
| 2163 |
PageClearAllVisible(page); |
| 2164 |
MarkBufferDirty(buf); |
| 2165 |
visibilitymap_clear(vacrel->rel, blkno, vmbuffer, |
| 2166 |
VISIBILITYMAP_VALID_BITS); |
| 2167 |
} |
| 2168 |
|
| 2169 |
/* |
| 2170 |
* If the all-visible page is all-frozen but not marked as such yet, mark |
| 2171 |
* it as all-frozen. Note that all_frozen is only valid if all_visible is |
| 2172 |
* true, so we must check both all_visible and all_frozen. |
| 2173 |
*/ |
| 2174 |
else if (all_visible_according_to_vm && presult.all_visible && |
| 2175 |
presult.all_frozen && !VM_ALL_FROZEN(vacrel->rel, blkno, &vmbuffer)) |
| 2176 |
{ |
| 2177 |
uint8 old_vmbits; |
| 2178 |
|
| 2179 |
/* |
| 2180 |
* Avoid relying on all_visible_according_to_vm as a proxy for the |
| 2181 |
* page-level PD_ALL_VISIBLE bit being set, since it might have become |
| 2182 |
* stale -- even when all_visible is set |
| 2183 |
*/ |
| 2184 |
if (!PageIsAllVisible(page)) |
| 2185 |
{ |
| 2186 |
PageSetAllVisible(page); |
| 2187 |
MarkBufferDirty(buf); |
| 2188 |
} |
| 2189 |
|
| 2190 |
/* |
| 2191 |
* Set the page all-frozen (and all-visible) in the VM. |
| 2192 |
* |
| 2193 |
* We can pass InvalidTransactionId as our cutoff_xid, since a |
| 2194 |
* snapshotConflictHorizon sufficient to make everything safe for REDO |
| 2195 |
* was logged when the page's tuples were frozen. |
| 2196 |
*/ |
| 2197 |
Assert(!TransactionIdIsValid(presult.vm_conflict_horizon)); |
| 2198 |
old_vmbits = visibilitymap_set(vacrel->rel, blkno, buf, |
| 2199 |
InvalidXLogRecPtr, |
| 2200 |
vmbuffer, InvalidTransactionId, |
| 2201 |
VISIBILITYMAP_ALL_VISIBLE | |
| 2202 |
VISIBILITYMAP_ALL_FROZEN); |
| 2203 |
|
| 2204 |
/* |
| 2205 |
* The page was likely already set all-visible in the VM. However, |
| 2206 |
* there is a small chance that it was modified sometime between |
| 2207 |
* setting all_visible_according_to_vm and checking the visibility |
| 2208 |
* during pruning. Check the return value of old_vmbits anyway to |
| 2209 |
* ensure the visibility map counters used for logging are accurate. |
| 2210 |
*/ |
| 2211 |
if ((old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0) |
| 2212 |
{ |
| 2213 |
vacrel->vm_new_visible_pages++; |
| 2214 |
vacrel->vm_new_visible_frozen_pages++; |
| 2215 |
*vm_page_frozen = true; |
| 2216 |
} |
| 2217 |
|
| 2218 |
/* |
| 2219 |
* We already checked that the page was not set all-frozen in the VM |
| 2220 |
* above, so we don't need to test the value of old_vmbits. |
| 2221 |
*/ |
| 2222 |
else |
| 2223 |
{ |
| 2224 |
vacrel->vm_new_frozen_pages++; |
| 2225 |
*vm_page_frozen = true; |
| 2226 |
} |
| 2227 |
} |
| 2228 |
|
| 2229 |
return presult.ndeleted; |
| 2230 |
} |
| 2231 |
|
| 2232 |
/* |
| 2233 |
* lazy_scan_noprune() -- lazy_scan_prune() without pruning or freezing |
| 2234 |
* |
| 2235 |
* Caller need only hold a pin and share lock on the buffer, unlike |
| 2236 |
* lazy_scan_prune, which requires a full cleanup lock. While pruning isn't |
| 2237 |
* performed here, it's quite possible that an earlier opportunistic pruning |
| 2238 |
* operation left LP_DEAD items behind. We'll at least collect any such items |
| 2239 |
* in dead_items for removal from indexes. |
| 2240 |
* |
| 2241 |
* For aggressive VACUUM callers, we may return false to indicate that a full |
| 2242 |
* cleanup lock is required for processing by lazy_scan_prune. This is only |
| 2243 |
* necessary when the aggressive VACUUM needs to freeze some tuple XIDs from |
| 2244 |
* one or more tuples on the page. We always return true for non-aggressive |
| 2245 |
* callers. |
| 2246 |
* |
| 2247 |
* If this function returns true, *has_lpdead_items gets set to true or false |
| 2248 |
* depending on whether, upon return from this function, any LP_DEAD items are |
| 2249 |
* present on the page. If this function returns false, *has_lpdead_items |
| 2250 |
* is not updated. |
| 2251 |
*/ |
| 2252 |
static bool |
| 2253 |
lazy_scan_noprune(LVRelState *vacrel, |
| 2254 |
Buffer buf, |
| 2255 |
BlockNumber blkno, |
| 2256 |
Page page, |
| 2257 |
bool *has_lpdead_items) |
| 2258 |
{ |
| 2259 |
OffsetNumber offnum, |
| 2260 |
maxoff; |
| 2261 |
int lpdead_items, |
| 2262 |
live_tuples, |
| 2263 |
recently_dead_tuples, |
| 2264 |
missed_dead_tuples; |
| 2265 |
bool hastup; |
| 2266 |
HeapTupleHeader tupleheader; |
| 2267 |
TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid; |
| 2268 |
MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid; |
| 2269 |
OffsetNumber deadoffsets[MaxHeapTuplesPerPage]; |
| 2270 |
|
| 2271 |
Assert(BufferGetBlockNumber(buf) == blkno); |
| 2272 |
|
| 2273 |
hastup = false; /* for now */ |
| 2274 |
|
| 2275 |
lpdead_items = 0; |
| 2276 |
live_tuples = 0; |
| 2277 |
recently_dead_tuples = 0; |
| 2278 |
missed_dead_tuples = 0; |
| 2279 |
|
| 2280 |
maxoff = PageGetMaxOffsetNumber(page); |
| 2281 |
for (offnum = FirstOffsetNumber; |
| 2282 |
offnum <= maxoff; |
| 2283 |
offnum = OffsetNumberNext(offnum)) |
| 2284 |
{ |
| 2285 |
ItemId itemid; |
| 2286 |
HeapTupleData tuple; |
| 2287 |
|
| 2288 |
vacrel->offnum = offnum; |
| 2289 |
itemid = PageGetItemId(page, offnum); |
| 2290 |
|
| 2291 |
if (!ItemIdIsUsed(itemid)) |
| 2292 |
continue; |
| 2293 |
|
| 2294 |
if (ItemIdIsRedirected(itemid)) |
| 2295 |
{ |
| 2296 |
hastup = true; |
| 2297 |
continue; |
| 2298 |
} |
| 2299 |
|
| 2300 |
if (ItemIdIsDead(itemid)) |
| 2301 |
{ |
| 2302 |
/* |
| 2303 |
* Deliberately don't set hastup=true here. See same point in |
| 2304 |
* lazy_scan_prune for an explanation. |
| 2305 |
*/ |
| 2306 |
deadoffsets[lpdead_items++] = offnum; |
| 2307 |
continue; |
| 2308 |
} |
| 2309 |
|
| 2310 |
hastup = true; /* page prevents rel truncation */ |
| 2311 |
tupleheader = (HeapTupleHeader) PageGetItem(page, itemid); |
| 2312 |
if (heap_tuple_should_freeze(tupleheader, &vacrel->cutoffs, |
| 2313 |
&NoFreezePageRelfrozenXid, |
| 2314 |
&NoFreezePageRelminMxid)) |
| 2315 |
{ |
| 2316 |
/* Tuple with XID < FreezeLimit (or MXID < MultiXactCutoff) */ |
| 2317 |
if (vacrel->aggressive) |
| 2318 |
{ |
| 2319 |
/* |
| 2320 |
* Aggressive VACUUMs must always be able to advance rel's |
| 2321 |
* relfrozenxid to a value >= FreezeLimit (and be able to |
| 2322 |
* advance rel's relminmxid to a value >= MultiXactCutoff). |
| 2323 |
* The ongoing aggressive VACUUM won't be able to do that |
| 2324 |
* unless it can freeze an XID (or MXID) from this tuple now. |
| 2325 |
* |
| 2326 |
* The only safe option is to have caller perform processing |
| 2327 |
* of this page using lazy_scan_prune. Caller might have to |
| 2328 |
* wait a while for a cleanup lock, but it can't be helped. |
| 2329 |
*/ |
| 2330 |
vacrel->offnum = InvalidOffsetNumber; |
| 2331 |
return false; |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* |
| 2335 |
* Non-aggressive VACUUMs are under no obligation to advance |
| 2336 |
* relfrozenxid (even by one XID). We can be much laxer here. |
| 2337 |
* |
| 2338 |
* Currently we always just accept an older final relfrozenxid |
| 2339 |
* and/or relminmxid value. We never make caller wait or work a |
| 2340 |
* little harder, even when it likely makes sense to do so. |
| 2341 |
*/ |
| 2342 |
} |
| 2343 |
|
| 2344 |
ItemPointerSet(&(tuple.t_self), blkno, offnum); |
| 2345 |
tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid); |
| 2346 |
tuple.t_len = ItemIdGetLength(itemid); |
| 2347 |
tuple.t_tableOid = RelationGetRelid(vacrel->rel); |
| 2348 |
|
| 2349 |
switch (HeapTupleSatisfiesVacuum(&tuple, vacrel->cutoffs.OldestXmin, |
| 2350 |
buf)) |
| 2351 |
{ |
| 2352 |
case HEAPTUPLE_DELETE_IN_PROGRESS: |
| 2353 |
case HEAPTUPLE_LIVE: |
| 2354 |
|
| 2355 |
/* |
| 2356 |
* Count both cases as live, just like lazy_scan_prune |
| 2357 |
*/ |
| 2358 |
live_tuples++; |
| 2359 |
|
| 2360 |
break; |
| 2361 |
case HEAPTUPLE_DEAD: |
| 2362 |
|
| 2363 |
/* |
| 2364 |
* There is some useful work for pruning to do, that won't be |
| 2365 |
* done due to failure to get a cleanup lock. |
| 2366 |
*/ |
| 2367 |
missed_dead_tuples++; |
| 2368 |
break; |
| 2369 |
case HEAPTUPLE_RECENTLY_DEAD: |
| 2370 |
|
| 2371 |
/* |
| 2372 |
* Count in recently_dead_tuples, just like lazy_scan_prune |
| 2373 |
*/ |
| 2374 |
recently_dead_tuples++; |
| 2375 |
break; |
| 2376 |
case HEAPTUPLE_INSERT_IN_PROGRESS: |
| 2377 |
|
| 2378 |
/* |
| 2379 |
* Do not count these rows as live, just like lazy_scan_prune |
| 2380 |
*/ |
| 2381 |
break; |
| 2382 |
default: |
| 2383 |
elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result"); |
| 2384 |
break; |
| 2385 |
} |
| 2386 |
} |
| 2387 |
|
| 2388 |
vacrel->offnum = InvalidOffsetNumber; |
| 2389 |
|
| 2390 |
/* |
| 2391 |
* By here we know for sure that caller can put off freezing and pruning |
| 2392 |
* this particular page until the next VACUUM. Remember its details now. |
| 2393 |
* (lazy_scan_prune expects a clean slate, so we have to do this last.) |
| 2394 |
*/ |
| 2395 |
vacrel->NewRelfrozenXid = NoFreezePageRelfrozenXid; |
| 2396 |
vacrel->NewRelminMxid = NoFreezePageRelminMxid; |
| 2397 |
|
| 2398 |
/* Save any LP_DEAD items found on the page in dead_items */ |
| 2399 |
if (vacrel->nindexes == 0) |
| 2400 |
{ |
| 2401 |
/* Using one-pass strategy (since table has no indexes) */ |
| 2402 |
if (lpdead_items > 0) |
| 2403 |
{ |
| 2404 |
/* |
| 2405 |
* Perfunctory handling for the corner case where a single pass |
| 2406 |
* strategy VACUUM cannot get a cleanup lock, and it turns out |
| 2407 |
* that there is one or more LP_DEAD items: just count the LP_DEAD |
| 2408 |
* items as missed_dead_tuples instead. (This is a bit dishonest, |
| 2409 |
* but it beats having to maintain specialized heap vacuuming code |
| 2410 |
* forever, for vanishingly little benefit.) |
| 2411 |
*/ |
| 2412 |
hastup = true; |
| 2413 |
missed_dead_tuples += lpdead_items; |
| 2414 |
} |
| 2415 |
} |
| 2416 |
else if (lpdead_items > 0) |
| 2417 |
{ |
| 2418 |
/* |
| 2419 |
* Page has LP_DEAD items, and so any references/TIDs that remain in |
| 2420 |
* indexes will be deleted during index vacuuming (and then marked |
| 2421 |
* LP_UNUSED in the heap) |
| 2422 |
*/ |
| 2423 |
vacrel->lpdead_item_pages++; |
| 2424 |
|
| 2425 |
dead_items_add(vacrel, blkno, deadoffsets, lpdead_items); |
| 2426 |
|
| 2427 |
vacrel->lpdead_items += lpdead_items; |
| 2428 |
} |
| 2429 |
|
| 2430 |
/* |
| 2431 |
* Finally, add relevant page-local counts to whole-VACUUM counts |
| 2432 |
*/ |
| 2433 |
vacrel->live_tuples += live_tuples; |
| 2434 |
vacrel->recently_dead_tuples += recently_dead_tuples; |
| 2435 |
vacrel->missed_dead_tuples += missed_dead_tuples; |
| 2436 |
if (missed_dead_tuples > 0) |
| 2437 |
vacrel->missed_dead_pages++; |
| 2438 |
|
| 2439 |
/* Can't truncate this page */ |
| 2440 |
if (hastup) |
| 2441 |
vacrel->nonempty_pages = blkno + 1; |
| 2442 |
|
| 2443 |
/* Did we find LP_DEAD items? */ |
| 2444 |
*has_lpdead_items = (lpdead_items > 0); |
| 2445 |
|
| 2446 |
/* Caller won't need to call lazy_scan_prune with same page */ |
| 2447 |
return true; |
| 2448 |
} |
| 2449 |
|
| 2450 |
/* |
| 2451 |
* Main entry point for index vacuuming and heap vacuuming. |
| 2452 |
* |
| 2453 |
* Removes items collected in dead_items from table's indexes, then marks the |
| 2454 |
* same items LP_UNUSED in the heap. See the comments above lazy_scan_heap |
| 2455 |
* for full details. |
| 2456 |
* |
| 2457 |
* Also empties dead_items, freeing up space for later TIDs. |
| 2458 |
* |
| 2459 |
* We may choose to bypass index vacuuming at this point, though only when the |
| 2460 |
* ongoing VACUUM operation will definitely only have one index scan/round of |
| 2461 |
* index vacuuming. |
| 2462 |
*/ |
| 2463 |
static void |
| 2464 |
lazy_vacuum(LVRelState *vacrel) |
| 2465 |
{ |
| 2466 |
bool bypass; |
| 2467 |
|
| 2468 |
/* Should not end up here with no indexes */ |
| 2469 |
Assert(vacrel->nindexes > 0); |
| 2470 |
Assert(vacrel->lpdead_item_pages > 0); |
| 2471 |
|
| 2472 |
if (!vacrel->do_index_vacuuming) |
| 2473 |
{ |
| 2474 |
Assert(!vacrel->do_index_cleanup); |
| 2475 |
dead_items_reset(vacrel); |
| 2476 |
return; |
| 2477 |
} |
| 2478 |
|
| 2479 |
/* |
| 2480 |
* Consider bypassing index vacuuming (and heap vacuuming) entirely. |
| 2481 |
* |
| 2482 |
* We currently only do this in cases where the number of LP_DEAD items |
| 2483 |
* for the entire VACUUM operation is close to zero. This avoids sharp |
| 2484 |
* discontinuities in the duration and overhead of successive VACUUM |
| 2485 |
* operations that run against the same table with a fixed workload. |
| 2486 |
* Ideally, successive VACUUM operations will behave as if there are |
| 2487 |
* exactly zero LP_DEAD items in cases where there are close to zero. |
| 2488 |
* |
| 2489 |
* This is likely to be helpful with a table that is continually affected |
| 2490 |
* by UPDATEs that can mostly apply the HOT optimization, but occasionally |
| 2491 |
* have small aberrations that lead to just a few heap pages retaining |
| 2492 |
* only one or two LP_DEAD items. This is pretty common; even when the |
| 2493 |
* DBA goes out of their way to make UPDATEs use HOT, it is practically |
| 2494 |
* impossible to predict whether HOT will be applied in 100% of cases. |
| 2495 |
* It's far easier to ensure that 99%+ of all UPDATEs against a table use |
| 2496 |
* HOT through careful tuning. |
| 2497 |
*/ |
| 2498 |
bypass = false; |
| 2499 |
if (vacrel->consider_bypass_optimization && vacrel->rel_pages > 0) |
| 2500 |
{ |
| 2501 |
BlockNumber threshold; |
| 2502 |
|
| 2503 |
Assert(vacrel->num_index_scans == 0); |
| 2504 |
Assert(vacrel->lpdead_items == vacrel->dead_items_info->num_items); |
| 2505 |
Assert(vacrel->do_index_vacuuming); |
| 2506 |
Assert(vacrel->do_index_cleanup); |
| 2507 |
|
| 2508 |
/* |
| 2509 |
* This crossover point at which we'll start to do index vacuuming is |
| 2510 |
* expressed as a percentage of the total number of heap pages in the |
| 2511 |
* table that are known to have at least one LP_DEAD item. This is |
| 2512 |
* much more important than the total number of LP_DEAD items, since |
| 2513 |
* it's a proxy for the number of heap pages whose visibility map bits |
| 2514 |
* cannot be set on account of bypassing index and heap vacuuming. |
| 2515 |
* |
| 2516 |
* We apply one further precautionary test: the space currently used |
| 2517 |
* to store the TIDs (TIDs that now all point to LP_DEAD items) must |
| 2518 |
* not exceed 32MB. This limits the risk that we will bypass index |
| 2519 |
* vacuuming again and again until eventually there is a VACUUM whose |
| 2520 |
* dead_items space is not CPU cache resident. |
| 2521 |
* |
| 2522 |
* We don't take any special steps to remember the LP_DEAD items (such |
| 2523 |
* as counting them in our final update to the stats system) when the |
| 2524 |
* optimization is applied. Though the accounting used in analyze.c's |
| 2525 |
* acquire_sample_rows() will recognize the same LP_DEAD items as dead |
| 2526 |
* rows in its own stats report, that's okay. The discrepancy should |
| 2527 |
* be negligible. If this optimization is ever expanded to cover more |
| 2528 |
* cases then this may need to be reconsidered. |
| 2529 |
*/ |
| 2530 |
threshold = (double) vacrel->rel_pages * BYPASS_THRESHOLD_PAGES; |
| 2531 |
bypass = (vacrel->lpdead_item_pages < threshold && |
| 2532 |
TidStoreMemoryUsage(vacrel->dead_items) < 32 * 1024 * 1024); |
| 2533 |
} |
| 2534 |
|
| 2535 |
if (bypass) |
| 2536 |
{ |
| 2537 |
/* |
| 2538 |
* There are almost zero TIDs. Behave as if there were precisely |
| 2539 |
* zero: bypass index vacuuming, but do index cleanup. |
| 2540 |
* |
| 2541 |
* We expect that the ongoing VACUUM operation will finish very |
| 2542 |
* quickly, so there is no point in considering speeding up as a |
| 2543 |
* failsafe against wraparound failure. (Index cleanup is expected to |
| 2544 |
* finish very quickly in cases where there were no ambulkdelete() |
| 2545 |
* calls.) |
| 2546 |
*/ |
| 2547 |
vacrel->do_index_vacuuming = false; |
| 2548 |
} |
| 2549 |
else if (lazy_vacuum_all_indexes(vacrel)) |
| 2550 |
{ |
| 2551 |
/* |
| 2552 |
* We successfully completed a round of index vacuuming. Do related |
| 2553 |
* heap vacuuming now. |
| 2554 |
*/ |
| 2555 |
lazy_vacuum_heap_rel(vacrel); |
| 2556 |
} |
| 2557 |
else |
| 2558 |
{ |
| 2559 |
/* |
| 2560 |
* Failsafe case. |
| 2561 |
* |
| 2562 |
* We attempted index vacuuming, but didn't finish a full round/full |
| 2563 |
* index scan. This happens when relfrozenxid or relminmxid is too |
| 2564 |
* far in the past. |
| 2565 |
* |
| 2566 |
* From this point on the VACUUM operation will do no further index |
| 2567 |
* vacuuming or heap vacuuming. This VACUUM operation won't end up |
| 2568 |
* back here again. |
| 2569 |
*/ |
| 2570 |
Assert(VacuumFailsafeActive); |
| 2571 |
} |
| 2572 |
|
| 2573 |
/* |
| 2574 |
* Forget the LP_DEAD items that we just vacuumed (or just decided to not |
| 2575 |
* vacuum) |
| 2576 |
*/ |
| 2577 |
dead_items_reset(vacrel); |
| 2578 |
} |
| 2579 |
|
| 2580 |
/* |
| 2581 |
* lazy_vacuum_all_indexes() -- Main entry for index vacuuming |
| 2582 |
* |
| 2583 |
* Returns true in the common case when all indexes were successfully |
| 2584 |
* vacuumed. Returns false in rare cases where we determined that the ongoing |
| 2585 |
* VACUUM operation is at risk of taking too long to finish, leading to |
| 2586 |
* wraparound failure. |
| 2587 |
*/ |
| 2588 |
static bool |
| 2589 |
lazy_vacuum_all_indexes(LVRelState *vacrel) |
| 2590 |
{ |
| 2591 |
bool allindexes = true; |
| 2592 |
double old_live_tuples = vacrel->rel->rd_rel->reltuples; |
| 2593 |
const int progress_start_index[] = { |
| 2594 |
PROGRESS_VACUUM_PHASE, |
| 2595 |
PROGRESS_VACUUM_INDEXES_TOTAL |
| 2596 |
}; |
| 2597 |
const int progress_end_index[] = { |
| 2598 |
PROGRESS_VACUUM_INDEXES_TOTAL, |
| 2599 |
PROGRESS_VACUUM_INDEXES_PROCESSED, |
| 2600 |
PROGRESS_VACUUM_NUM_INDEX_VACUUMS |
| 2601 |
}; |
| 2602 |
int64 progress_start_val[2]; |
| 2603 |
int64 progress_end_val[3]; |
| 2604 |
|
| 2605 |
Assert(vacrel->nindexes > 0); |
| 2606 |
Assert(vacrel->do_index_vacuuming); |
| 2607 |
Assert(vacrel->do_index_cleanup); |
| 2608 |
|
| 2609 |
/* Precheck for XID wraparound emergencies */ |
| 2610 |
if (lazy_check_wraparound_failsafe(vacrel)) |
| 2611 |
{ |
| 2612 |
/* Wraparound emergency -- don't even start an index scan */ |
| 2613 |
return false; |
| 2614 |
} |
| 2615 |
|
| 2616 |
/* |
| 2617 |
* Report that we are now vacuuming indexes and the number of indexes to |
| 2618 |
* vacuum. |
| 2619 |
*/ |
| 2620 |
progress_start_val[0] = PROGRESS_VACUUM_PHASE_VACUUM_INDEX; |
| 2621 |
progress_start_val[1] = vacrel->nindexes; |
| 2622 |
pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val); |
| 2623 |
|
| 2624 |
if (!ParallelVacuumIsActive(vacrel)) |
| 2625 |
{ |
| 2626 |
for (int idx = 0; idx < vacrel->nindexes; idx++) |
| 2627 |
{ |
| 2628 |
Relation indrel = vacrel->indrels[idx]; |
| 2629 |
IndexBulkDeleteResult *istat = vacrel->indstats[idx]; |
| 2630 |
|
| 2631 |
vacrel->indstats[idx] = lazy_vacuum_one_index(indrel, istat, |
| 2632 |
old_live_tuples, |
| 2633 |
vacrel); |
| 2634 |
|
| 2635 |
/* Report the number of indexes vacuumed */ |
| 2636 |
pgstat_progress_update_param(PROGRESS_VACUUM_INDEXES_PROCESSED, |
| 2637 |
idx + 1); |
| 2638 |
|
| 2639 |
if (lazy_check_wraparound_failsafe(vacrel)) |
| 2640 |
{ |
| 2641 |
/* Wraparound emergency -- end current index scan */ |
| 2642 |
allindexes = false; |
| 2643 |
break; |
| 2644 |
} |
| 2645 |
} |
| 2646 |
} |
| 2647 |
else |
| 2648 |
{ |
| 2649 |
/* Outsource everything to parallel variant */ |
| 2650 |
parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples, |
| 2651 |
vacrel->num_index_scans); |
| 2652 |
|
| 2653 |
/* |
| 2654 |
* Do a postcheck to consider applying wraparound failsafe now. Note |
| 2655 |
* that parallel VACUUM only gets the precheck and this postcheck. |
| 2656 |
*/ |
| 2657 |
if (lazy_check_wraparound_failsafe(vacrel)) |
| 2658 |
allindexes = false; |
| 2659 |
} |
| 2660 |
|
| 2661 |
/* |
| 2662 |
* We delete all LP_DEAD items from the first heap pass in all indexes on |
| 2663 |
* each call here (except calls where we choose to do the failsafe). This |
| 2664 |
* makes the next call to lazy_vacuum_heap_rel() safe (except in the event |
| 2665 |
* of the failsafe triggering, which prevents the next call from taking |
| 2666 |
* place). |
| 2667 |
*/ |
| 2668 |
Assert(vacrel->num_index_scans > 0 || |
| 2669 |
vacrel->dead_items_info->num_items == vacrel->lpdead_items); |
| 2670 |
Assert(allindexes || VacuumFailsafeActive); |
| 2671 |
|
| 2672 |
/* |
| 2673 |
* Increase and report the number of index scans. Also, we reset |
| 2674 |
* PROGRESS_VACUUM_INDEXES_TOTAL and PROGRESS_VACUUM_INDEXES_PROCESSED. |
| 2675 |
* |
| 2676 |
* We deliberately include the case where we started a round of bulk |
| 2677 |
* deletes that we weren't able to finish due to the failsafe triggering. |
| 2678 |
*/ |
| 2679 |
vacrel->num_index_scans++; |
| 2680 |
progress_end_val[0] = 0; |
| 2681 |
progress_end_val[1] = 0; |
| 2682 |
progress_end_val[2] = vacrel->num_index_scans; |
| 2683 |
pgstat_progress_update_multi_param(3, progress_end_index, progress_end_val); |
| 2684 |
|
| 2685 |
return allindexes; |
| 2686 |
} |
| 2687 |
|
| 2688 |
/* |
| 2689 |
* Read stream callback for vacuum's third phase (second pass over the heap). |
| 2690 |
* Gets the next block from the TID store and returns it or InvalidBlockNumber |
| 2691 |
* if there are no further blocks to vacuum. |
| 2692 |
* |
| 2693 |
* NB: Assumed to be safe to use with READ_STREAM_USE_BATCHING. |
| 2694 |
*/ |
| 2695 |
static BlockNumber |
| 2696 |
vacuum_reap_lp_read_stream_next(ReadStream *stream, |
| 2697 |
void *callback_private_data, |
| 2698 |
void *per_buffer_data) |
| 2699 |
{ |
| 2700 |
TidStoreIter *iter = callback_private_data; |
| 2701 |
TidStoreIterResult *iter_result; |
| 2702 |
|
| 2703 |
iter_result = TidStoreIterateNext(iter); |
| 2704 |
if (iter_result == NULL) |
| 2705 |
return InvalidBlockNumber; |
| 2706 |
|
| 2707 |
/* |
| 2708 |
* Save the TidStoreIterResult for later, so we can extract the offsets. |
| 2709 |
* It is safe to copy the result, according to TidStoreIterateNext(). |
| 2710 |
*/ |
| 2711 |
memcpy(per_buffer_data, iter_result, sizeof(*iter_result)); |
| 2712 |
|
| 2713 |
return iter_result->blkno; |
| 2714 |
} |
| 2715 |
|
| 2716 |
/* |
| 2717 |
* lazy_vacuum_heap_rel() -- second pass over the heap for two pass strategy |
| 2718 |
* |
| 2719 |
* This routine marks LP_DEAD items in vacrel->dead_items as LP_UNUSED. Pages |
| 2720 |
* that never had lazy_scan_prune record LP_DEAD items are not visited at all. |
| 2721 |
* |
| 2722 |
* We may also be able to truncate the line pointer array of the heap pages we |
| 2723 |
* visit. If there is a contiguous group of LP_UNUSED items at the end of the |
| 2724 |
* array, it can be reclaimed as free space. These LP_UNUSED items usually |
| 2725 |
* start out as LP_DEAD items recorded by lazy_scan_prune (we set items from |
| 2726 |
* each page to LP_UNUSED, and then consider if it's possible to truncate the |
| 2727 |
* page's line pointer array). |
| 2728 |
* |
| 2729 |
* Note: the reason for doing this as a second pass is we cannot remove the |
| 2730 |
* tuples until we've removed their index entries, and we want to process |
| 2731 |
* index entry removal in batches as large as possible. |
| 2732 |
*/ |
| 2733 |
static void |
| 2734 |
lazy_vacuum_heap_rel(LVRelState *vacrel) |
| 2735 |
{ |
| 2736 |
ReadStream *stream; |
| 2737 |
BlockNumber vacuumed_pages = 0; |
| 2738 |
Buffer vmbuffer = InvalidBuffer; |
| 2739 |
LVSavedErrInfo saved_err_info; |
| 2740 |
TidStoreIter *iter; |
| 2741 |
|
| 2742 |
Assert(vacrel->do_index_vacuuming); |
| 2743 |
Assert(vacrel->do_index_cleanup); |
| 2744 |
Assert(vacrel->num_index_scans > 0); |
| 2745 |
|
| 2746 |
/* Report that we are now vacuuming the heap */ |
| 2747 |
pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, |
| 2748 |
PROGRESS_VACUUM_PHASE_VACUUM_HEAP); |
| 2749 |
|
| 2750 |
/* Update error traceback information */ |
| 2751 |
update_vacuum_error_info(vacrel, &saved_err_info, |
| 2752 |
VACUUM_ERRCB_PHASE_VACUUM_HEAP, |
| 2753 |
InvalidBlockNumber, InvalidOffsetNumber); |
| 2754 |
|
| 2755 |
iter = TidStoreBeginIterate(vacrel->dead_items); |
| 2756 |
|
| 2757 |
/* |
| 2758 |
* Set up the read stream for vacuum's second pass through the heap. |
| 2759 |
* |
| 2760 |
* It is safe to use batchmode, as vacuum_reap_lp_read_stream_next() does |
| 2761 |
* not need to wait for IO and does not perform locking. Once we support |
| 2762 |
* parallelism it should still be fine, as presumably the holder of locks |
| 2763 |
* would never be blocked by IO while holding the lock. |
| 2764 |
*/ |
| 2765 |
stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE | |
| 2766 |
READ_STREAM_USE_BATCHING, |
| 2767 |
vacrel->bstrategy, |
| 2768 |
vacrel->rel, |
| 2769 |
MAIN_FORKNUM, |
| 2770 |
vacuum_reap_lp_read_stream_next, |
| 2771 |
iter, |
| 2772 |
sizeof(TidStoreIterResult)); |
| 2773 |
|
| 2774 |
while (true) |
| 2775 |
{ |
| 2776 |
BlockNumber blkno; |
| 2777 |
Buffer buf; |
| 2778 |
Page page; |
| 2779 |
TidStoreIterResult *iter_result; |
| 2780 |
Size freespace; |
| 2781 |
OffsetNumber offsets[MaxOffsetNumber]; |
| 2782 |
int num_offsets; |
| 2783 |
|
| 2784 |
vacuum_delay_point(false); |
| 2785 |
|
| 2786 |
buf = read_stream_next_buffer(stream, (void **) &iter_result); |
| 2787 |
|
| 2788 |
/* The relation is exhausted */ |
| 2789 |
if (!BufferIsValid(buf)) |
| 2790 |
break; |
| 2791 |
|
| 2792 |
vacrel->blkno = blkno = BufferGetBlockNumber(buf); |
| 2793 |
|
| 2794 |
Assert(iter_result); |
| 2795 |
num_offsets = TidStoreGetBlockOffsets(iter_result, offsets, lengthof(offsets)); |
| 2796 |
Assert(num_offsets <= lengthof(offsets)); |
| 2797 |
|
| 2798 |
/* |
| 2799 |
* Pin the visibility map page in case we need to mark the page |
| 2800 |
* all-visible. In most cases this will be very cheap, because we'll |
| 2801 |
* already have the correct page pinned anyway. |
| 2802 |
*/ |
| 2803 |
visibilitymap_pin(vacrel->rel, blkno, &vmbuffer); |
| 2804 |
|
| 2805 |
/* We need a non-cleanup exclusive lock to mark dead_items unused */ |
| 2806 |
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); |
| 2807 |
lazy_vacuum_heap_page(vacrel, blkno, buf, offsets, |
| 2808 |
num_offsets, vmbuffer); |
| 2809 |
|
| 2810 |
/* Now that we've vacuumed the page, record its available space */ |
| 2811 |
page = BufferGetPage(buf); |
| 2812 |
freespace = PageGetHeapFreeSpace(page); |
| 2813 |
|
| 2814 |
UnlockReleaseBuffer(buf); |
| 2815 |
RecordPageWithFreeSpace(vacrel->rel, blkno, freespace); |
| 2816 |
vacuumed_pages++; |
| 2817 |
} |
| 2818 |
|
| 2819 |
read_stream_end(stream); |
| 2820 |
TidStoreEndIterate(iter); |
| 2821 |
|
| 2822 |
vacrel->blkno = InvalidBlockNumber; |
| 2823 |
if (BufferIsValid(vmbuffer)) |
| 2824 |
ReleaseBuffer(vmbuffer); |
| 2825 |
|
| 2826 |
/* |
| 2827 |
* We set all LP_DEAD items from the first heap pass to LP_UNUSED during |
| 2828 |
* the second heap pass. No more, no less. |
| 2829 |
*/ |
| 2830 |
Assert(vacrel->num_index_scans > 1 || |
| 2831 |
(vacrel->dead_items_info->num_items == vacrel->lpdead_items && |
| 2832 |
vacuumed_pages == vacrel->lpdead_item_pages)); |
| 2833 |
|
| 2834 |
ereport(DEBUG2, |
| 2835 |
(errmsg("table \"%s\": removed %" PRId64 " dead item identifiers in %u pages", |
| 2836 |
vacrel->relname, vacrel->dead_items_info->num_items, |
| 2837 |
vacuumed_pages))); |
| 2838 |
|
| 2839 |
/* Revert to the previous phase information for error traceback */ |
| 2840 |
restore_vacuum_error_info(vacrel, &saved_err_info); |
| 2841 |
} |
| 2842 |
|
| 2843 |
/* |
| 2844 |
* lazy_vacuum_heap_page() -- free page's LP_DEAD items listed in the |
| 2845 |
* vacrel->dead_items store. |
| 2846 |
* |
| 2847 |
* Caller must have an exclusive buffer lock on the buffer (though a full |
| 2848 |
* cleanup lock is also acceptable). vmbuffer must be valid and already have |
| 2849 |
* a pin on blkno's visibility map page. |
| 2850 |
*/ |
| 2851 |
static void |
| 2852 |
lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer, |
| 2853 |
OffsetNumber *deadoffsets, int num_offsets, |
| 2854 |
Buffer vmbuffer) |
| 2855 |
{ |
| 2856 |
Page page = BufferGetPage(buffer); |
| 2857 |
OffsetNumber unused[MaxHeapTuplesPerPage]; |
| 2858 |
int nunused = 0; |
| 2859 |
TransactionId visibility_cutoff_xid; |
| 2860 |
bool all_frozen; |
| 2861 |
LVSavedErrInfo saved_err_info; |
| 2862 |
|
| 2863 |
Assert(vacrel->do_index_vacuuming); |
| 2864 |
|
| 2865 |
pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, blkno); |
| 2866 |
|
| 2867 |
/* Update error traceback information */ |
| 2868 |
update_vacuum_error_info(vacrel, &saved_err_info, |
| 2869 |
VACUUM_ERRCB_PHASE_VACUUM_HEAP, blkno, |
| 2870 |
InvalidOffsetNumber); |
| 2871 |
|
| 2872 |
START_CRIT_SECTION(); |
| 2873 |
|
| 2874 |
for (int i = 0; i < num_offsets; i++) |
| 2875 |
{ |
| 2876 |
ItemId itemid; |
| 2877 |
OffsetNumber toff = deadoffsets[i]; |
| 2878 |
|
| 2879 |
itemid = PageGetItemId(page, toff); |
| 2880 |
|
| 2881 |
Assert(ItemIdIsDead(itemid) && !ItemIdHasStorage(itemid)); |
| 2882 |
ItemIdSetUnused(itemid); |
| 2883 |
unused[nunused++] = toff; |
| 2884 |
} |
| 2885 |
|
| 2886 |
Assert(nunused > 0); |
| 2887 |
|
| 2888 |
/* Attempt to truncate line pointer array now */ |
| 2889 |
PageTruncateLinePointerArray(page); |
| 2890 |
|
| 2891 |
/* |
| 2892 |
* Mark buffer dirty before we write WAL. |
| 2893 |
*/ |
| 2894 |
MarkBufferDirty(buffer); |
| 2895 |
|
| 2896 |
/* XLOG stuff */ |
| 2897 |
if (RelationNeedsWAL(vacrel->rel)) |
| 2898 |
{ |
| 2899 |
log_heap_prune_and_freeze(vacrel->rel, buffer, |
| 2900 |
InvalidTransactionId, |
| 2901 |
false, /* no cleanup lock required */ |
| 2902 |
PRUNE_VACUUM_CLEANUP, |
| 2903 |
NULL, 0, /* frozen */ |
| 2904 |
NULL, 0, /* redirected */ |
| 2905 |
NULL, 0, /* dead */ |
| 2906 |
unused, nunused); |
| 2907 |
} |
| 2908 |
|
| 2909 |
/* |
| 2910 |
* End critical section, so we safely can do visibility tests (which |
| 2911 |
* possibly need to perform IO and allocate memory!). If we crash now the |
| 2912 |
* page (including the corresponding vm bit) might not be marked all |
| 2913 |
* visible, but that's fine. A later vacuum will fix that. |
| 2914 |
*/ |
| 2915 |
END_CRIT_SECTION(); |
| 2916 |
|
| 2917 |
/* |
| 2918 |
* Now that we have removed the LP_DEAD items from the page, once again |
| 2919 |
* check if the page has become all-visible. The page is already marked |
| 2920 |
* dirty, exclusively locked, and, if needed, a full page image has been |
| 2921 |
* emitted. |
| 2922 |
*/ |
| 2923 |
Assert(!PageIsAllVisible(page)); |
| 2924 |
if (heap_page_is_all_visible(vacrel, buffer, &visibility_cutoff_xid, |
| 2925 |
&all_frozen)) |
| 2926 |
{ |
| 2927 |
uint8 flags = VISIBILITYMAP_ALL_VISIBLE; |
| 2928 |
|
| 2929 |
if (all_frozen) |
| 2930 |
{ |
| 2931 |
Assert(!TransactionIdIsValid(visibility_cutoff_xid)); |
| 2932 |
flags |= VISIBILITYMAP_ALL_FROZEN; |
| 2933 |
} |
| 2934 |
|
| 2935 |
PageSetAllVisible(page); |
| 2936 |
visibilitymap_set(vacrel->rel, blkno, buffer, |
| 2937 |
InvalidXLogRecPtr, |
| 2938 |
vmbuffer, visibility_cutoff_xid, |
| 2939 |
flags); |
| 2940 |
|
| 2941 |
/* Count the newly set VM page for logging */ |
| 2942 |
vacrel->vm_new_visible_pages++; |
| 2943 |
if (all_frozen) |
| 2944 |
vacrel->vm_new_visible_frozen_pages++; |
| 2945 |
} |
| 2946 |
|
| 2947 |
/* Revert to the previous phase information for error traceback */ |
| 2948 |
restore_vacuum_error_info(vacrel, &saved_err_info); |
| 2949 |
} |
| 2950 |
|
| 2951 |
/* |
| 2952 |
* Trigger the failsafe to avoid wraparound failure when vacrel table has a |
| 2953 |
* relfrozenxid and/or relminmxid that is dangerously far in the past. |
| 2954 |
* Triggering the failsafe makes the ongoing VACUUM bypass any further index |
| 2955 |
* vacuuming and heap vacuuming. Truncating the heap is also bypassed. |
| 2956 |
* |
| 2957 |
* Any remaining work (work that VACUUM cannot just bypass) is typically sped |
| 2958 |
* up when the failsafe triggers. VACUUM stops applying any cost-based delay |
| 2959 |
* that it started out with. |
| 2960 |
* |
| 2961 |
* Returns true when failsafe has been triggered. |
| 2962 |
*/ |
| 2963 |
static bool |
| 2964 |
lazy_check_wraparound_failsafe(LVRelState *vacrel) |
| 2965 |
{ |
| 2966 |
/* Don't warn more than once per VACUUM */ |
| 2967 |
if (VacuumFailsafeActive) |
| 2968 |
return true; |
| 2969 |
|
| 2970 |
if (unlikely(vacuum_xid_failsafe_check(&vacrel->cutoffs))) |
| 2971 |
{ |
| 2972 |
const int progress_index[] = { |
| 2973 |
PROGRESS_VACUUM_INDEXES_TOTAL, |
| 2974 |
PROGRESS_VACUUM_INDEXES_PROCESSED |
| 2975 |
}; |
| 2976 |
int64 progress_val[2] = {0, 0}; |
| 2977 |
|
| 2978 |
VacuumFailsafeActive = true; |
| 2979 |
|
| 2980 |
/* |
| 2981 |
* We abandon use of the strategy in failsafe mode to allow use of all |
| 2982 |
* of shared buffers. vacrel->bstrategy is not the source of truth for |
| 2983 |
* an ongoing heap scan, but clear it just for tidiness. Any ongoing |
| 2984 |
* phase I heap scan has its own references to the strategy and clears |
| 2985 |
* them separately (see lazy_scan_heap()). And none of the other |
| 2986 |
* vacuum phases will read from vacrel->bstrategy once failsafe mode |
| 2987 |
* is engaged. The phase I read stream clears the strategy references |
| 2988 |
* held by the ReadBuffersOperations outside of this function because |
| 2989 |
* lazy_check_wraparound_failsafe() may be called from any phase of |
| 2990 |
* vacuum, including when the phase I stream is inactive. |
| 2991 |
* |
| 2992 |
* We assume the caller who allocated the memory for the |
| 2993 |
* BufferAccessStrategy will free it. |
| 2994 |
*/ |
| 2995 |
vacrel->bstrategy = NULL; |
| 2996 |
|
| 2997 |
/* Disable index vacuuming, index cleanup, and heap rel truncation */ |
| 2998 |
vacrel->do_index_vacuuming = false; |
| 2999 |
vacrel->do_index_cleanup = false; |
| 3000 |
vacrel->do_rel_truncate = false; |
| 3001 |
|
| 3002 |
/* Reset the progress counters */ |
| 3003 |
pgstat_progress_update_multi_param(2, progress_index, progress_val); |
| 3004 |
|
| 3005 |
ereport(WARNING, |
| 3006 |
(errmsg("bypassing nonessential maintenance of table \"%s.%s.%s\" as a failsafe after %d index scans", |
| 3007 |
vacrel->dbname, vacrel->relnamespace, vacrel->relname, |
| 3008 |
vacrel->num_index_scans), |
| 3009 |
errdetail("The table's relfrozenxid or relminmxid is too far in the past."), |
| 3010 |
errhint("Consider increasing configuration parameter \"maintenance_work_mem\" or \"autovacuum_work_mem\".\n" |
| 3011 |
"You might also need to consider other ways for VACUUM to keep up with the allocation of transaction IDs."))); |
| 3012 |
|
| 3013 |
/* Stop applying cost limits from this point on */ |
| 3014 |
VacuumCostActive = false; |
| 3015 |
VacuumCostBalance = 0; |
| 3016 |
|
| 3017 |
return true; |
| 3018 |
} |
| 3019 |
|
| 3020 |
return false; |
| 3021 |
} |
| 3022 |
|
| 3023 |
/* |
| 3024 |
* lazy_cleanup_all_indexes() -- cleanup all indexes of relation. |
| 3025 |
*/ |
| 3026 |
static void |
| 3027 |
lazy_cleanup_all_indexes(LVRelState *vacrel) |
| 3028 |
{ |
| 3029 |
double reltuples = vacrel->new_rel_tuples; |
| 3030 |
bool estimated_count = vacrel->scanned_pages < vacrel->rel_pages; |
| 3031 |
const int progress_start_index[] = { |
| 3032 |
PROGRESS_VACUUM_PHASE, |
| 3033 |
PROGRESS_VACUUM_INDEXES_TOTAL |
| 3034 |
}; |
| 3035 |
const int progress_end_index[] = { |
| 3036 |
PROGRESS_VACUUM_INDEXES_TOTAL, |
| 3037 |
PROGRESS_VACUUM_INDEXES_PROCESSED |
| 3038 |
}; |
| 3039 |
int64 progress_start_val[2]; |
| 3040 |
int64 progress_end_val[2] = {0, 0}; |
| 3041 |
|
| 3042 |
Assert(vacrel->do_index_cleanup); |
| 3043 |
Assert(vacrel->nindexes > 0); |
| 3044 |
|
| 3045 |
/* |
| 3046 |
* Report that we are now cleaning up indexes and the number of indexes to |
| 3047 |
* cleanup. |
| 3048 |
*/ |
| 3049 |
progress_start_val[0] = PROGRESS_VACUUM_PHASE_INDEX_CLEANUP; |
| 3050 |
progress_start_val[1] = vacrel->nindexes; |
| 3051 |
pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val); |
| 3052 |
|
| 3053 |
if (!ParallelVacuumIsActive(vacrel)) |
| 3054 |
{ |
| 3055 |
for (int idx = 0; idx < vacrel->nindexes; idx++) |
| 3056 |
{ |
| 3057 |
Relation indrel = vacrel->indrels[idx]; |
| 3058 |
IndexBulkDeleteResult *istat = vacrel->indstats[idx]; |
| 3059 |
|
| 3060 |
vacrel->indstats[idx] = |
| 3061 |
lazy_cleanup_one_index(indrel, istat, reltuples, |
| 3062 |
estimated_count, vacrel); |
| 3063 |
|
| 3064 |
/* Report the number of indexes cleaned up */ |
| 3065 |
pgstat_progress_update_param(PROGRESS_VACUUM_INDEXES_PROCESSED, |
| 3066 |
idx + 1); |
| 3067 |
} |
| 3068 |
} |
| 3069 |
else |
| 3070 |
{ |
| 3071 |
/* Outsource everything to parallel variant */ |
| 3072 |
parallel_vacuum_cleanup_all_indexes(vacrel->pvs, reltuples, |
| 3073 |
vacrel->num_index_scans, |
| 3074 |
estimated_count); |
| 3075 |
} |
| 3076 |
|
| 3077 |
/* Reset the progress counters */ |
| 3078 |
pgstat_progress_update_multi_param(2, progress_end_index, progress_end_val); |
| 3079 |
} |
| 3080 |
|
| 3081 |
/* |
| 3082 |
* lazy_vacuum_one_index() -- vacuum index relation. |
| 3083 |
* |
| 3084 |
* Delete all the index tuples containing a TID collected in |
| 3085 |
* vacrel->dead_items. Also update running statistics. Exact |
| 3086 |
* details depend on index AM's ambulkdelete routine. |
| 3087 |
* |
| 3088 |
* reltuples is the number of heap tuples to be passed to the |
| 3089 |
* bulkdelete callback. It's always assumed to be estimated. |
| 3090 |
* See indexam.sgml for more info. |
| 3091 |
* |
| 3092 |
* Returns bulk delete stats derived from input stats |
| 3093 |
*/ |
| 3094 |
static IndexBulkDeleteResult * |
| 3095 |
lazy_vacuum_one_index(Relation indrel, IndexBulkDeleteResult *istat, |
| 3096 |
double reltuples, LVRelState *vacrel) |
| 3097 |
{ |
| 3098 |
IndexVacuumInfo ivinfo; |
| 3099 |
LVSavedErrInfo saved_err_info; |
| 3100 |
|
| 3101 |
ivinfo.index = indrel; |
| 3102 |
ivinfo.heaprel = vacrel->rel; |
| 3103 |
ivinfo.analyze_only = false; |
| 3104 |
ivinfo.report_progress = false; |
| 3105 |
ivinfo.estimated_count = true; |
| 3106 |
ivinfo.message_level = DEBUG2; |
| 3107 |
ivinfo.num_heap_tuples = reltuples; |
| 3108 |
ivinfo.strategy = vacrel->bstrategy; |
| 3109 |
|
| 3110 |
/* |
| 3111 |
* Update error traceback information. |
| 3112 |
* |
| 3113 |
* The index name is saved during this phase and restored immediately |
| 3114 |
* after this phase. See vacuum_error_callback. |
| 3115 |
*/ |
| 3116 |
Assert(vacrel->indname == NULL); |
| 3117 |
vacrel->indname = pstrdup(RelationGetRelationName(indrel)); |
| 3118 |
update_vacuum_error_info(vacrel, &saved_err_info, |
| 3119 |
VACUUM_ERRCB_PHASE_VACUUM_INDEX, |
| 3120 |
InvalidBlockNumber, InvalidOffsetNumber); |
| 3121 |
|
| 3122 |
/* Do bulk deletion */ |
| 3123 |
istat = vac_bulkdel_one_index(&ivinfo, istat, vacrel->dead_items, |
| 3124 |
vacrel->dead_items_info); |
| 3125 |
|
| 3126 |
/* Revert to the previous phase information for error traceback */ |
| 3127 |
restore_vacuum_error_info(vacrel, &saved_err_info); |
| 3128 |
pfree(vacrel->indname); |
| 3129 |
vacrel->indname = NULL; |
| 3130 |
|
| 3131 |
return istat; |
| 3132 |
} |
| 3133 |
|
| 3134 |
/* |
| 3135 |
* lazy_cleanup_one_index() -- do post-vacuum cleanup for index relation. |
| 3136 |
* |
| 3137 |
* Calls index AM's amvacuumcleanup routine. reltuples is the number |
| 3138 |
* of heap tuples and estimated_count is true if reltuples is an |
| 3139 |
* estimated value. See indexam.sgml for more info. |
| 3140 |
* |
| 3141 |
* Returns bulk delete stats derived from input stats |
| 3142 |
*/ |
| 3143 |
static IndexBulkDeleteResult * |
| 3144 |
lazy_cleanup_one_index(Relation indrel, IndexBulkDeleteResult *istat, |
| 3145 |
double reltuples, bool estimated_count, |
| 3146 |
LVRelState *vacrel) |
| 3147 |
{ |
| 3148 |
IndexVacuumInfo ivinfo; |
| 3149 |
LVSavedErrInfo saved_err_info; |
| 3150 |
|
| 3151 |
ivinfo.index = indrel; |
| 3152 |
ivinfo.heaprel = vacrel->rel; |
| 3153 |
ivinfo.analyze_only = false; |
| 3154 |
ivinfo.report_progress = false; |
| 3155 |
ivinfo.estimated_count = estimated_count; |
| 3156 |
ivinfo.message_level = DEBUG2; |
| 3157 |
|
| 3158 |
ivinfo.num_heap_tuples = reltuples; |
| 3159 |
ivinfo.strategy = vacrel->bstrategy; |
| 3160 |
|
| 3161 |
/* |
| 3162 |
* Update error traceback information. |
| 3163 |
* |
| 3164 |
* The index name is saved during this phase and restored immediately |
| 3165 |
* after this phase. See vacuum_error_callback. |
| 3166 |
*/ |
| 3167 |
Assert(vacrel->indname == NULL); |
| 3168 |
vacrel->indname = pstrdup(RelationGetRelationName(indrel)); |
| 3169 |
update_vacuum_error_info(vacrel, &saved_err_info, |
| 3170 |
VACUUM_ERRCB_PHASE_INDEX_CLEANUP, |
| 3171 |
InvalidBlockNumber, InvalidOffsetNumber); |
| 3172 |
|
| 3173 |
istat = vac_cleanup_one_index(&ivinfo, istat); |
| 3174 |
|
| 3175 |
/* Revert to the previous phase information for error traceback */ |
| 3176 |
restore_vacuum_error_info(vacrel, &saved_err_info); |
| 3177 |
pfree(vacrel->indname); |
| 3178 |
vacrel->indname = NULL; |
| 3179 |
|
| 3180 |
return istat; |
| 3181 |
} |
| 3182 |
|
| 3183 |
/* |
| 3184 |
* should_attempt_truncation - should we attempt to truncate the heap? |
| 3185 |
* |
| 3186 |
* Don't even think about it unless we have a shot at releasing a goodly |
| 3187 |
* number of pages. Otherwise, the time taken isn't worth it, mainly because |
| 3188 |
* an AccessExclusive lock must be replayed on any hot standby, where it can |
| 3189 |
* be particularly disruptive. |
| 3190 |
* |
| 3191 |
* Also don't attempt it if wraparound failsafe is in effect. The entire |
| 3192 |
* system might be refusing to allocate new XIDs at this point. The system |
| 3193 |
* definitely won't return to normal unless and until VACUUM actually advances |
| 3194 |
* the oldest relfrozenxid -- which hasn't happened for target rel just yet. |
| 3195 |
* If lazy_truncate_heap attempted to acquire an AccessExclusiveLock to |
| 3196 |
* truncate the table under these circumstances, an XID exhaustion error might |
| 3197 |
* make it impossible for VACUUM to fix the underlying XID exhaustion problem. |
| 3198 |
* There is very little chance of truncation working out when the failsafe is |
| 3199 |
* in effect in any case. lazy_scan_prune makes the optimistic assumption |
| 3200 |
* that any LP_DEAD items it encounters will always be LP_UNUSED by the time |
| 3201 |
* we're called. |
| 3202 |
*/ |
| 3203 |
static bool |
| 3204 |
should_attempt_truncation(LVRelState *vacrel) |
| 3205 |
{ |
| 3206 |
BlockNumber possibly_freeable; |
| 3207 |
|
| 3208 |
if (!vacrel->do_rel_truncate || VacuumFailsafeActive) |
| 3209 |
return false; |
| 3210 |
|
| 3211 |
possibly_freeable = vacrel->rel_pages - vacrel->nonempty_pages; |
| 3212 |
if (possibly_freeable > 0 && |
| 3213 |
(possibly_freeable >= REL_TRUNCATE_MINIMUM || |
| 3214 |
possibly_freeable >= vacrel->rel_pages / REL_TRUNCATE_FRACTION)) |
| 3215 |
return true; |
| 3216 |
|
| 3217 |
return false; |
| 3218 |
} |
| 3219 |
|
| 3220 |
/* |
| 3221 |
* lazy_truncate_heap - try to truncate off any empty pages at the end |
| 3222 |
*/ |
| 3223 |
static void |
| 3224 |
lazy_truncate_heap(LVRelState *vacrel) |
| 3225 |
{ |
| 3226 |
BlockNumber orig_rel_pages = vacrel->rel_pages; |
| 3227 |
BlockNumber new_rel_pages; |
| 3228 |
bool lock_waiter_detected; |
| 3229 |
int lock_retry; |
| 3230 |
|
| 3231 |
/* Report that we are now truncating */ |
| 3232 |
pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, |
| 3233 |
PROGRESS_VACUUM_PHASE_TRUNCATE); |
| 3234 |
|
| 3235 |
/* Update error traceback information one last time */ |
| 3236 |
update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_TRUNCATE, |
| 3237 |
vacrel->nonempty_pages, InvalidOffsetNumber); |
| 3238 |
|
| 3239 |
/* |
| 3240 |
* Loop until no more truncating can be done. |
| 3241 |
*/ |
| 3242 |
do |
| 3243 |
{ |
| 3244 |
/* |
| 3245 |
* We need full exclusive lock on the relation in order to do |
| 3246 |
* truncation. If we can't get it, give up rather than waiting --- we |
| 3247 |
* don't want to block other backends, and we don't want to deadlock |
| 3248 |
* (which is quite possible considering we already hold a lower-grade |
| 3249 |
* lock). |
| 3250 |
*/ |
| 3251 |
lock_waiter_detected = false; |
| 3252 |
lock_retry = 0; |
| 3253 |
while (true) |
| 3254 |
{ |
| 3255 |
if (ConditionalLockRelation(vacrel->rel, AccessExclusiveLock)) |
| 3256 |
break; |
| 3257 |
|
| 3258 |
/* |
| 3259 |
* Check for interrupts while trying to (re-)acquire the exclusive |
| 3260 |
* lock. |
| 3261 |
*/ |
| 3262 |
CHECK_FOR_INTERRUPTS(); |
| 3263 |
|
| 3264 |
if (++lock_retry > (VACUUM_TRUNCATE_LOCK_TIMEOUT / |
| 3265 |
VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL)) |
| 3266 |
{ |
| 3267 |
/* |
| 3268 |
* We failed to establish the lock in the specified number of |
| 3269 |
* retries. This means we give up truncating. |
| 3270 |
*/ |
| 3271 |
ereport(vacrel->verbose ? INFO : DEBUG2, |
| 3272 |
(errmsg("\"%s\": stopping truncate due to conflicting lock request", |
| 3273 |
vacrel->relname))); |
| 3274 |
return; |
| 3275 |
} |
| 3276 |
|
| 3277 |
(void) WaitLatch(MyLatch, |
| 3278 |
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, |
| 3279 |
VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL, |
| 3280 |
WAIT_EVENT_VACUUM_TRUNCATE); |
| 3281 |
ResetLatch(MyLatch); |
| 3282 |
} |
| 3283 |
|
| 3284 |
/* |
| 3285 |
* Now that we have exclusive lock, look to see if the rel has grown |
| 3286 |
* whilst we were vacuuming with non-exclusive lock. If so, give up; |
| 3287 |
* the newly added pages presumably contain non-deletable tuples. |
| 3288 |
*/ |
| 3289 |
new_rel_pages = RelationGetNumberOfBlocks(vacrel->rel); |
| 3290 |
if (new_rel_pages != orig_rel_pages) |
| 3291 |
{ |
| 3292 |
/* |
| 3293 |
* Note: we intentionally don't update vacrel->rel_pages with the |
| 3294 |
* new rel size here. If we did, it would amount to assuming that |
| 3295 |
* the new pages are empty, which is unlikely. Leaving the numbers |
| 3296 |
* alone amounts to assuming that the new pages have the same |
| 3297 |
* tuple density as existing ones, which is less unlikely. |
| 3298 |
*/ |
| 3299 |
UnlockRelation(vacrel->rel, AccessExclusiveLock); |
| 3300 |
return; |
| 3301 |
} |
| 3302 |
|
| 3303 |
/* |
| 3304 |
* Scan backwards from the end to verify that the end pages actually |
| 3305 |
* contain no tuples. This is *necessary*, not optional, because |
| 3306 |
* other backends could have added tuples to these pages whilst we |
| 3307 |
* were vacuuming. |
| 3308 |
*/ |
| 3309 |
new_rel_pages = count_nondeletable_pages(vacrel, &lock_waiter_detected); |
| 3310 |
vacrel->blkno = new_rel_pages; |
| 3311 |
|
| 3312 |
if (new_rel_pages >= orig_rel_pages) |
| 3313 |
{ |
| 3314 |
/* can't do anything after all */ |
| 3315 |
UnlockRelation(vacrel->rel, AccessExclusiveLock); |
| 3316 |
return; |
| 3317 |
} |
| 3318 |
|
| 3319 |
/* |
| 3320 |
* Okay to truncate. |
| 3321 |
*/ |
| 3322 |
RelationTruncate(vacrel->rel, new_rel_pages); |
| 3323 |
|
| 3324 |
/* |
| 3325 |
* We can release the exclusive lock as soon as we have truncated. |
| 3326 |
* Other backends can't safely access the relation until they have |
| 3327 |
* processed the smgr invalidation that smgrtruncate sent out ... but |
| 3328 |
* that should happen as part of standard invalidation processing once |
| 3329 |
* they acquire lock on the relation. |
| 3330 |
*/ |
| 3331 |
UnlockRelation(vacrel->rel, AccessExclusiveLock); |
| 3332 |
|
| 3333 |
/* |
| 3334 |
* Update statistics. Here, it *is* correct to adjust rel_pages |
| 3335 |
* without also touching reltuples, since the tuple count wasn't |
| 3336 |
* changed by the truncation. |
| 3337 |
*/ |
| 3338 |
vacrel->removed_pages += orig_rel_pages - new_rel_pages; |
| 3339 |
vacrel->rel_pages = new_rel_pages; |
| 3340 |
|
| 3341 |
ereport(vacrel->verbose ? INFO : DEBUG2, |
| 3342 |
(errmsg("table \"%s\": truncated %u to %u pages", |
| 3343 |
vacrel->relname, |
| 3344 |
orig_rel_pages, new_rel_pages))); |
| 3345 |
orig_rel_pages = new_rel_pages; |
| 3346 |
} while (new_rel_pages > vacrel->nonempty_pages && lock_waiter_detected); |
| 3347 |
} |
| 3348 |
|
| 3349 |
/* |
| 3350 |
* Rescan end pages to verify that they are (still) empty of tuples. |
| 3351 |
* |
| 3352 |
* Returns number of nondeletable pages (last nonempty page + 1). |
| 3353 |
*/ |
| 3354 |
static BlockNumber |
| 3355 |
count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected) |
| 3356 |
{ |
| 3357 |
BlockNumber blkno; |
| 3358 |
BlockNumber prefetchedUntil; |
| 3359 |
instr_time starttime; |
| 3360 |
|
| 3361 |
/* Initialize the starttime if we check for conflicting lock requests */ |
| 3362 |
INSTR_TIME_SET_CURRENT(starttime); |
| 3363 |
|
| 3364 |
/* |
| 3365 |
* Start checking blocks at what we believe relation end to be and move |
| 3366 |
* backwards. (Strange coding of loop control is needed because blkno is |
| 3367 |
* unsigned.) To make the scan faster, we prefetch a few blocks at a time |
| 3368 |
* in forward direction, so that OS-level readahead can kick in. |
| 3369 |
*/ |
| 3370 |
blkno = vacrel->rel_pages; |
| 3371 |
StaticAssertStmt((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0, |
| 3372 |
"prefetch size must be power of 2"); |
| 3373 |
prefetchedUntil = InvalidBlockNumber; |
| 3374 |
while (blkno > vacrel->nonempty_pages) |
| 3375 |
{ |
| 3376 |
Buffer buf; |
| 3377 |
Page page; |
| 3378 |
OffsetNumber offnum, |
| 3379 |
maxoff; |
| 3380 |
bool hastup; |
| 3381 |
|
| 3382 |
/* |
| 3383 |
* Check if another process requests a lock on our relation. We are |
| 3384 |
* holding an AccessExclusiveLock here, so they will be waiting. We |
| 3385 |
* only do this once per VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL, and we |
| 3386 |
* only check if that interval has elapsed once every 32 blocks to |
| 3387 |
* keep the number of system calls and actual shared lock table |
| 3388 |
* lookups to a minimum. |
| 3389 |
*/ |
| 3390 |
if ((blkno % 32) == 0) |
| 3391 |
{ |
| 3392 |
instr_time currenttime; |
| 3393 |
instr_time elapsed; |
| 3394 |
|
| 3395 |
INSTR_TIME_SET_CURRENT(currenttime); |
| 3396 |
elapsed = currenttime; |
| 3397 |
INSTR_TIME_SUBTRACT(elapsed, starttime); |
| 3398 |
if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000) |
| 3399 |
>= VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL) |
| 3400 |
{ |
| 3401 |
if (LockHasWaitersRelation(vacrel->rel, AccessExclusiveLock)) |
| 3402 |
{ |
| 3403 |
ereport(vacrel->verbose ? INFO : DEBUG2, |
| 3404 |
(errmsg("table \"%s\": suspending truncate due to conflicting lock request", |
| 3405 |
vacrel->relname))); |
| 3406 |
|
| 3407 |
*lock_waiter_detected = true; |
| 3408 |
return blkno; |
| 3409 |
} |
| 3410 |
starttime = currenttime; |
| 3411 |
} |
| 3412 |
} |
| 3413 |
|
| 3414 |
/* |
| 3415 |
* We don't insert a vacuum delay point here, because we have an |
| 3416 |
* exclusive lock on the table which we want to hold for as short a |
| 3417 |
* time as possible. We still need to check for interrupts however. |
| 3418 |
*/ |
| 3419 |
CHECK_FOR_INTERRUPTS(); |
| 3420 |
|
| 3421 |
blkno--; |
| 3422 |
|
| 3423 |
/* If we haven't prefetched this lot yet, do so now. */ |
| 3424 |
if (prefetchedUntil > blkno) |
| 3425 |
{ |
| 3426 |
BlockNumber prefetchStart; |
| 3427 |
BlockNumber pblkno; |
| 3428 |
|
| 3429 |
prefetchStart = blkno & ~(PREFETCH_SIZE - 1); |
| 3430 |
for (pblkno = prefetchStart; pblkno <= blkno; pblkno++) |
| 3431 |
{ |
| 3432 |
PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, pblkno); |
| 3433 |
CHECK_FOR_INTERRUPTS(); |
| 3434 |
} |
| 3435 |
prefetchedUntil = prefetchStart; |
| 3436 |
} |
| 3437 |
|
| 3438 |
buf = ReadBufferExtended(vacrel->rel, MAIN_FORKNUM, blkno, RBM_NORMAL, |
| 3439 |
vacrel->bstrategy); |
| 3440 |
|
| 3441 |
/* In this phase we only need shared access to the buffer */ |
| 3442 |
LockBuffer(buf, BUFFER_LOCK_SHARE); |
| 3443 |
|
| 3444 |
page = BufferGetPage(buf); |
| 3445 |
|
| 3446 |
if (PageIsNew(page) || PageIsEmpty(page)) |
| 3447 |
{ |
| 3448 |
UnlockReleaseBuffer(buf); |
| 3449 |
continue; |
| 3450 |
} |
| 3451 |
|
| 3452 |
hastup = false; |
| 3453 |
maxoff = PageGetMaxOffsetNumber(page); |
| 3454 |
for (offnum = FirstOffsetNumber; |
| 3455 |
offnum <= maxoff; |
| 3456 |
offnum = OffsetNumberNext(offnum)) |
| 3457 |
{ |
| 3458 |
ItemId itemid; |
| 3459 |
|
| 3460 |
itemid = PageGetItemId(page, offnum); |
| 3461 |
|
| 3462 |
/* |
| 3463 |
* Note: any non-unused item should be taken as a reason to keep |
| 3464 |
* this page. Even an LP_DEAD item makes truncation unsafe, since |
| 3465 |
* we must not have cleaned out its index entries. |
| 3466 |
*/ |
| 3467 |
if (ItemIdIsUsed(itemid)) |
| 3468 |
{ |
| 3469 |
hastup = true; |
| 3470 |
break; /* can stop scanning */ |
| 3471 |
} |
| 3472 |
} /* scan along page */ |
| 3473 |
|
| 3474 |
UnlockReleaseBuffer(buf); |
| 3475 |
|
| 3476 |
/* Done scanning if we found a tuple here */ |
| 3477 |
if (hastup) |
| 3478 |
return blkno + 1; |
| 3479 |
} |
| 3480 |
|
| 3481 |
/* |
| 3482 |
* If we fall out of the loop, all the previously-thought-to-be-empty |
| 3483 |
* pages still are; we need not bother to look at the last known-nonempty |
| 3484 |
* page. |
| 3485 |
*/ |
| 3486 |
return vacrel->nonempty_pages; |
| 3487 |
} |
| 3488 |
|
| 3489 |
/* |
| 3490 |
* Allocate dead_items and dead_items_info (either using palloc, or in dynamic |
| 3491 |
* shared memory). Sets both in vacrel for caller. |
| 3492 |
* |
| 3493 |
* Also handles parallel initialization as part of allocating dead_items in |
| 3494 |
* DSM when required. |
| 3495 |
*/ |
| 3496 |
static void |
| 3497 |
dead_items_alloc(LVRelState *vacrel, int nworkers) |
| 3498 |
{ |
| 3499 |
VacDeadItemsInfo *dead_items_info; |
| 3500 |
int vac_work_mem = AmAutoVacuumWorkerProcess() && |
| 3501 |
autovacuum_work_mem != -1 ? |
| 3502 |
autovacuum_work_mem : maintenance_work_mem; |
| 3503 |
|
| 3504 |
/* |
| 3505 |
* Initialize state for a parallel vacuum. As of now, only one worker can |
| 3506 |
* be used for an index, so we invoke parallelism only if there are at |
| 3507 |
* least two indexes on a table. |
| 3508 |
*/ |
| 3509 |
if (nworkers >= 0 && vacrel->nindexes > 1 && vacrel->do_index_vacuuming) |
| 3510 |
{ |
| 3511 |
/* |
| 3512 |
* Since parallel workers cannot access data in temporary tables, we |
| 3513 |
* can't perform parallel vacuum on them. |
| 3514 |
*/ |
| 3515 |
if (RelationUsesLocalBuffers(vacrel->rel)) |
| 3516 |
{ |
| 3517 |
/* |
| 3518 |
* Give warning only if the user explicitly tries to perform a |
| 3519 |
* parallel vacuum on the temporary table. |
| 3520 |
*/ |
| 3521 |
if (nworkers > 0) |
| 3522 |
ereport(WARNING, |
| 3523 |
(errmsg("disabling parallel option of vacuum on \"%s\" --- cannot vacuum temporary tables in parallel", |
| 3524 |
vacrel->relname))); |
| 3525 |
} |
| 3526 |
else |
| 3527 |
vacrel->pvs = parallel_vacuum_init(vacrel->rel, vacrel->indrels, |
| 3528 |
vacrel->nindexes, nworkers, |
| 3529 |
vac_work_mem, |
| 3530 |
vacrel->verbose ? INFO : DEBUG2, |
| 3531 |
vacrel->bstrategy); |
| 3532 |
|
| 3533 |
/* |
| 3534 |
* If parallel mode started, dead_items and dead_items_info spaces are |
| 3535 |
* allocated in DSM. |
| 3536 |
*/ |
| 3537 |
if (ParallelVacuumIsActive(vacrel)) |
| 3538 |
{ |
| 3539 |
vacrel->dead_items = parallel_vacuum_get_dead_items(vacrel->pvs, |
| 3540 |
&vacrel->dead_items_info); |
| 3541 |
return; |
| 3542 |
} |
| 3543 |
} |
| 3544 |
|
| 3545 |
/* |
| 3546 |
* Serial VACUUM case. Allocate both dead_items and dead_items_info |
| 3547 |
* locally. |
| 3548 |
*/ |
| 3549 |
|
| 3550 |
dead_items_info = (VacDeadItemsInfo *) palloc(sizeof(VacDeadItemsInfo)); |
| 3551 |
dead_items_info->max_bytes = vac_work_mem * (Size) 1024; |
| 3552 |
dead_items_info->num_items = 0; |
| 3553 |
vacrel->dead_items_info = dead_items_info; |
| 3554 |
|
| 3555 |
vacrel->dead_items = TidStoreCreateLocal(dead_items_info->max_bytes, true); |
| 3556 |
} |
| 3557 |
|
| 3558 |
/* |
| 3559 |
* Add the given block number and offset numbers to dead_items. |
| 3560 |
*/ |
| 3561 |
static void |
| 3562 |
dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets, |
| 3563 |
int num_offsets) |
| 3564 |
{ |
| 3565 |
const int prog_index[2] = { |
| 3566 |
PROGRESS_VACUUM_NUM_DEAD_ITEM_IDS, |
| 3567 |
PROGRESS_VACUUM_DEAD_TUPLE_BYTES |
| 3568 |
}; |
| 3569 |
int64 prog_val[2]; |
| 3570 |
|
| 3571 |
TidStoreSetBlockOffsets(vacrel->dead_items, blkno, offsets, num_offsets); |
| 3572 |
vacrel->dead_items_info->num_items += num_offsets; |
| 3573 |
|
| 3574 |
/* update the progress information */ |
| 3575 |
prog_val[0] = vacrel->dead_items_info->num_items; |
| 3576 |
prog_val[1] = TidStoreMemoryUsage(vacrel->dead_items); |
| 3577 |
pgstat_progress_update_multi_param(2, prog_index, prog_val); |
| 3578 |
} |
| 3579 |
|
| 3580 |
/* |
| 3581 |
* Forget all collected dead items. |
| 3582 |
*/ |
| 3583 |
static void |
| 3584 |
dead_items_reset(LVRelState *vacrel) |
| 3585 |
{ |
| 3586 |
if (ParallelVacuumIsActive(vacrel)) |
| 3587 |
{ |
| 3588 |
parallel_vacuum_reset_dead_items(vacrel->pvs); |
| 3589 |
vacrel->dead_items = parallel_vacuum_get_dead_items(vacrel->pvs, |
| 3590 |
&vacrel->dead_items_info); |
| 3591 |
return; |
| 3592 |
} |
| 3593 |
|
| 3594 |
/* Recreate the tidstore with the same max_bytes limitation */ |
| 3595 |
TidStoreDestroy(vacrel->dead_items); |
| 3596 |
vacrel->dead_items = TidStoreCreateLocal(vacrel->dead_items_info->max_bytes, true); |
| 3597 |
|
| 3598 |
/* Reset the counter */ |
| 3599 |
vacrel->dead_items_info->num_items = 0; |
| 3600 |
} |
| 3601 |
|
| 3602 |
/* |
| 3603 |
* Perform cleanup for resources allocated in dead_items_alloc |
| 3604 |
*/ |
| 3605 |
static void |
| 3606 |
dead_items_cleanup(LVRelState *vacrel) |
| 3607 |
{ |
| 3608 |
if (!ParallelVacuumIsActive(vacrel)) |
| 3609 |
{ |
| 3610 |
/* Don't bother with pfree here */ |
| 3611 |
return; |
| 3612 |
} |
| 3613 |
|
| 3614 |
/* End parallel mode */ |
| 3615 |
parallel_vacuum_end(vacrel->pvs, vacrel->indstats); |
| 3616 |
vacrel->pvs = NULL; |
| 3617 |
} |
| 3618 |
|
| 3619 |
/* |
| 3620 |
* Check if every tuple in the given page is visible to all current and future |
| 3621 |
* transactions. Also return the visibility_cutoff_xid which is the highest |
| 3622 |
* xmin amongst the visible tuples. Set *all_frozen to true if every tuple |
| 3623 |
* on this page is frozen. |
| 3624 |
* |
| 3625 |
* This is a stripped down version of lazy_scan_prune(). If you change |
| 3626 |
* anything here, make sure that everything stays in sync. Note that an |
| 3627 |
* assertion calls us to verify that everybody still agrees. Be sure to avoid |
| 3628 |
* introducing new side-effects here. |
| 3629 |
*/ |
| 3630 |
static bool |
| 3631 |
heap_page_is_all_visible(LVRelState *vacrel, Buffer buf, |
| 3632 |
TransactionId *visibility_cutoff_xid, |
| 3633 |
bool *all_frozen) |
| 3634 |
{ |
| 3635 |
Page page = BufferGetPage(buf); |
| 3636 |
BlockNumber blockno = BufferGetBlockNumber(buf); |
| 3637 |
OffsetNumber offnum, |
| 3638 |
maxoff; |
| 3639 |
bool all_visible = true; |
| 3640 |
|
| 3641 |
*visibility_cutoff_xid = InvalidTransactionId; |
| 3642 |
*all_frozen = true; |
| 3643 |
|
| 3644 |
maxoff = PageGetMaxOffsetNumber(page); |
| 3645 |
for (offnum = FirstOffsetNumber; |
| 3646 |
offnum <= maxoff && all_visible; |
| 3647 |
offnum = OffsetNumberNext(offnum)) |
| 3648 |
{ |
| 3649 |
ItemId itemid; |
| 3650 |
HeapTupleData tuple; |
| 3651 |
|
| 3652 |
/* |
| 3653 |
* Set the offset number so that we can display it along with any |
| 3654 |
* error that occurred while processing this tuple. |
| 3655 |
*/ |
| 3656 |
vacrel->offnum = offnum; |
| 3657 |
itemid = PageGetItemId(page, offnum); |
| 3658 |
|
| 3659 |
/* Unused or redirect line pointers are of no interest */ |
| 3660 |
if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid)) |
| 3661 |
continue; |
| 3662 |
|
| 3663 |
ItemPointerSet(&(tuple.t_self), blockno, offnum); |
| 3664 |
|
| 3665 |
/* |
| 3666 |
* Dead line pointers can have index pointers pointing to them. So |
| 3667 |
* they can't be treated as visible |
| 3668 |
*/ |
| 3669 |
if (ItemIdIsDead(itemid)) |
| 3670 |
{ |
| 3671 |
all_visible = false; |
| 3672 |
*all_frozen = false; |
| 3673 |
break; |
| 3674 |
} |
| 3675 |
|
| 3676 |
Assert(ItemIdIsNormal(itemid)); |
| 3677 |
|
| 3678 |
tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid); |
| 3679 |
tuple.t_len = ItemIdGetLength(itemid); |
| 3680 |
tuple.t_tableOid = RelationGetRelid(vacrel->rel); |
| 3681 |
|
| 3682 |
switch (HeapTupleSatisfiesVacuum(&tuple, vacrel->cutoffs.OldestXmin, |
| 3683 |
buf)) |
| 3684 |
{ |
| 3685 |
case HEAPTUPLE_LIVE: |
| 3686 |
{ |
| 3687 |
TransactionId xmin; |
| 3688 |
|
| 3689 |
/* Check comments in lazy_scan_prune. */ |
| 3690 |
if (!HeapTupleHeaderXminCommitted(tuple.t_data)) |
| 3691 |
{ |
| 3692 |
all_visible = false; |
| 3693 |
*all_frozen = false; |
| 3694 |
break; |
| 3695 |
} |
| 3696 |
|
| 3697 |
/* |
| 3698 |
* The inserter definitely committed. But is it old enough |
| 3699 |
* that everyone sees it as committed? |
| 3700 |
*/ |
| 3701 |
xmin = HeapTupleHeaderGetXmin(tuple.t_data); |
| 3702 |
if (!TransactionIdPrecedes(xmin, |
| 3703 |
vacrel->cutoffs.OldestXmin)) |
| 3704 |
{ |
| 3705 |
all_visible = false; |
| 3706 |
*all_frozen = false; |
| 3707 |
break; |
| 3708 |
} |
| 3709 |
|
| 3710 |
/* Track newest xmin on page. */ |
| 3711 |
if (TransactionIdFollows(xmin, *visibility_cutoff_xid) && |
| 3712 |
TransactionIdIsNormal(xmin)) |
| 3713 |
*visibility_cutoff_xid = xmin; |
| 3714 |
|
| 3715 |
/* Check whether this tuple is already frozen or not */ |
| 3716 |
if (all_visible && *all_frozen && |
| 3717 |
heap_tuple_needs_eventual_freeze(tuple.t_data)) |
| 3718 |
*all_frozen = false; |
| 3719 |
} |
| 3720 |
break; |
| 3721 |
|
| 3722 |
case HEAPTUPLE_DEAD: |
| 3723 |
case HEAPTUPLE_RECENTLY_DEAD: |
| 3724 |
case HEAPTUPLE_INSERT_IN_PROGRESS: |
| 3725 |
case HEAPTUPLE_DELETE_IN_PROGRESS: |
| 3726 |
{ |
| 3727 |
all_visible = false; |
| 3728 |
*all_frozen = false; |
| 3729 |
break; |
| 3730 |
} |
| 3731 |
default: |
| 3732 |
elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result"); |
| 3733 |
break; |
| 3734 |
} |
| 3735 |
} /* scan along page */ |
| 3736 |
|
| 3737 |
/* Clear the offset information once we have processed the given page. */ |
| 3738 |
vacrel->offnum = InvalidOffsetNumber; |
| 3739 |
|
| 3740 |
return all_visible; |
| 3741 |
} |
| 3742 |
|
| 3743 |
/* |
| 3744 |
* Update index statistics in pg_class if the statistics are accurate. |
| 3745 |
*/ |
| 3746 |
static void |
| 3747 |
update_relstats_all_indexes(LVRelState *vacrel) |
| 3748 |
{ |
| 3749 |
Relation *indrels = vacrel->indrels; |
| 3750 |
int nindexes = vacrel->nindexes; |
| 3751 |
IndexBulkDeleteResult **indstats = vacrel->indstats; |
| 3752 |
|
| 3753 |
Assert(vacrel->do_index_cleanup); |
| 3754 |
|
| 3755 |
for (int idx = 0; idx < nindexes; idx++) |
| 3756 |
{ |
| 3757 |
Relation indrel = indrels[idx]; |
| 3758 |
IndexBulkDeleteResult *istat = indstats[idx]; |
| 3759 |
|
| 3760 |
if (istat == NULL || istat->estimated_count) |
| 3761 |
continue; |
| 3762 |
|
| 3763 |
/* Update index statistics */ |
| 3764 |
vac_update_relstats(indrel, |
| 3765 |
istat->num_pages, |
| 3766 |
istat->num_index_tuples, |
| 3767 |
0, 0, |
| 3768 |
false, |
| 3769 |
InvalidTransactionId, |
| 3770 |
InvalidMultiXactId, |
| 3771 |
NULL, NULL, false); |
| 3772 |
} |
| 3773 |
} |
| 3774 |
|
| 3775 |
/* |
| 3776 |
* Error context callback for errors occurring during vacuum. The error |
| 3777 |
* context messages for index phases should match the messages set in parallel |
| 3778 |
* vacuum. If you change this function for those phases, change |
| 3779 |
* parallel_vacuum_error_callback() as well. |
| 3780 |
*/ |
| 3781 |
static void |
| 3782 |
vacuum_error_callback(void *arg) |
| 3783 |
{ |
| 3784 |
LVRelState *errinfo = arg; |
| 3785 |
|
| 3786 |
switch (errinfo->phase) |
| 3787 |
{ |
| 3788 |
case VACUUM_ERRCB_PHASE_SCAN_HEAP: |
| 3789 |
if (BlockNumberIsValid(errinfo->blkno)) |
| 3790 |
{ |
| 3791 |
if (OffsetNumberIsValid(errinfo->offnum)) |
| 3792 |
errcontext("while scanning block %u offset %u of relation \"%s.%s\"", |
| 3793 |
errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname); |
| 3794 |
else |
| 3795 |
errcontext("while scanning block %u of relation \"%s.%s\"", |
| 3796 |
errinfo->blkno, errinfo->relnamespace, errinfo->relname); |
| 3797 |
} |
| 3798 |
else |
| 3799 |
errcontext("while scanning relation \"%s.%s\"", |
| 3800 |
errinfo->relnamespace, errinfo->relname); |
| 3801 |
break; |
| 3802 |
|
| 3803 |
case VACUUM_ERRCB_PHASE_VACUUM_HEAP: |
| 3804 |
if (BlockNumberIsValid(errinfo->blkno)) |
| 3805 |
{ |
| 3806 |
if (OffsetNumberIsValid(errinfo->offnum)) |
| 3807 |
errcontext("while vacuuming block %u offset %u of relation \"%s.%s\"", |
| 3808 |
errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname); |
| 3809 |
else |
| 3810 |
errcontext("while vacuuming block %u of relation \"%s.%s\"", |
| 3811 |
errinfo->blkno, errinfo->relnamespace, errinfo->relname); |
| 3812 |
} |
| 3813 |
else |
| 3814 |
errcontext("while vacuuming relation \"%s.%s\"", |
| 3815 |
errinfo->relnamespace, errinfo->relname); |
| 3816 |
break; |
| 3817 |
|
| 3818 |
case VACUUM_ERRCB_PHASE_VACUUM_INDEX: |
| 3819 |
errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"", |
| 3820 |
errinfo->indname, errinfo->relnamespace, errinfo->relname); |
| 3821 |
break; |
| 3822 |
|
| 3823 |
case VACUUM_ERRCB_PHASE_INDEX_CLEANUP: |
| 3824 |
errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"", |
| 3825 |
errinfo->indname, errinfo->relnamespace, errinfo->relname); |
| 3826 |
break; |
| 3827 |
|
| 3828 |
case VACUUM_ERRCB_PHASE_TRUNCATE: |
| 3829 |
if (BlockNumberIsValid(errinfo->blkno)) |
| 3830 |
errcontext("while truncating relation \"%s.%s\" to %u blocks", |
| 3831 |
errinfo->relnamespace, errinfo->relname, errinfo->blkno); |
| 3832 |
break; |
| 3833 |
|
| 3834 |
case VACUUM_ERRCB_PHASE_UNKNOWN: |
| 3835 |
default: |
| 3836 |
return; /* do nothing; the errinfo may not be |
| 3837 |
* initialized */ |
| 3838 |
} |
| 3839 |
} |
| 3840 |
|
| 3841 |
/* |
| 3842 |
* Updates the information required for vacuum error callback. This also saves |
| 3843 |
* the current information which can be later restored via restore_vacuum_error_info. |
| 3844 |
*/ |
| 3845 |
static void |
| 3846 |
update_vacuum_error_info(LVRelState *vacrel, LVSavedErrInfo *saved_vacrel, |
| 3847 |
int phase, BlockNumber blkno, OffsetNumber offnum) |
| 3848 |
{ |
| 3849 |
if (saved_vacrel) |
| 3850 |
{ |
| 3851 |
saved_vacrel->offnum = vacrel->offnum; |
| 3852 |
saved_vacrel->blkno = vacrel->blkno; |
| 3853 |
saved_vacrel->phase = vacrel->phase; |
| 3854 |
} |
| 3855 |
|
| 3856 |
vacrel->blkno = blkno; |
| 3857 |
vacrel->offnum = offnum; |
| 3858 |
vacrel->phase = phase; |
| 3859 |
} |
| 3860 |
|
| 3861 |
/* |
| 3862 |
* Restores the vacuum information saved via a prior call to update_vacuum_error_info. |
| 3863 |
*/ |
| 3864 |
static void |
| 3865 |
restore_vacuum_error_info(LVRelState *vacrel, |
| 3866 |
const LVSavedErrInfo *saved_vacrel) |
| 3867 |
{ |
| 3868 |
vacrel->blkno = saved_vacrel->blkno; |
| 3869 |
vacrel->offnum = saved_vacrel->offnum; |
| 3870 |
vacrel->phase = saved_vacrel->phase; |
| 3871 |
} |