Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pruneheap.c
4 : : * heap page pruning and HOT-chain management code
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/heap/pruneheap.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/heapam.h"
18 : : #include "access/heapam_xlog.h"
19 : : #include "access/htup_details.h"
20 : : #include "access/multixact.h"
21 : : #include "access/transam.h"
22 : : #include "access/visibilitymapdefs.h"
23 : : #include "access/xlog.h"
24 : : #include "access/xloginsert.h"
25 : : #include "commands/vacuum.h"
26 : : #include "executor/instrument.h"
27 : : #include "miscadmin.h"
28 : : #include "pgstat.h"
29 : : #include "storage/bufmgr.h"
30 : : #include "utils/rel.h"
31 : : #include "utils/snapmgr.h"
32 : :
33 : : /* Working data for heap_page_prune_and_freeze() and subroutines */
34 : : typedef struct
35 : : {
36 : : /*-------------------------------------------------------
37 : : * Arguments passed to heap_page_prune_and_freeze()
38 : : *-------------------------------------------------------
39 : : */
40 : :
41 : : /* tuple visibility test, initialized for the relation */
42 : : GlobalVisState *vistest;
43 : : /* whether or not dead items can be set LP_UNUSED during pruning */
44 : : bool mark_unused_now;
45 : : /* whether to attempt freezing tuples */
46 : : bool attempt_freeze;
47 : : struct VacuumCutoffs *cutoffs;
48 : : Relation relation;
49 : :
50 : : /*
51 : : * Keep the buffer, block, and page handy so that helpers needing to
52 : : * access them don't need to make repeated calls to BufferGetBlockNumber()
53 : : * and BufferGetPage().
54 : : */
55 : : BlockNumber block;
56 : : Buffer buffer;
57 : : Page page;
58 : :
59 : : /*-------------------------------------------------------
60 : : * Fields describing what to do to the page
61 : : *-------------------------------------------------------
62 : : */
63 : : TransactionId new_prune_xid; /* new prune hint value */
64 : : TransactionId latest_xid_removed;
65 : : int nredirected; /* numbers of entries in arrays below */
66 : : int ndead;
67 : : int nunused;
68 : : int nfrozen;
69 : : /* arrays that accumulate indexes of items to be changed */
70 : : OffsetNumber redirected[MaxHeapTuplesPerPage * 2];
71 : : OffsetNumber nowdead[MaxHeapTuplesPerPage];
72 : : OffsetNumber nowunused[MaxHeapTuplesPerPage];
73 : : HeapTupleFreeze frozen[MaxHeapTuplesPerPage];
74 : :
75 : : /*-------------------------------------------------------
76 : : * Working state for HOT chain processing
77 : : *-------------------------------------------------------
78 : : */
79 : :
80 : : /*
81 : : * 'root_items' contains offsets of all LP_REDIRECT line pointers and
82 : : * normal non-HOT tuples. They can be stand-alone items or the first item
83 : : * in a HOT chain. 'heaponly_items' contains heap-only tuples which can
84 : : * only be removed as part of a HOT chain.
85 : : */
86 : : int nroot_items;
87 : : OffsetNumber root_items[MaxHeapTuplesPerPage];
88 : : int nheaponly_items;
89 : : OffsetNumber heaponly_items[MaxHeapTuplesPerPage];
90 : :
91 : : /*
92 : : * processed[offnum] is true if item at offnum has been processed.
93 : : *
94 : : * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
95 : : * 1. Otherwise every access would need to subtract 1.
96 : : */
97 : : bool processed[MaxHeapTuplesPerPage + 1];
98 : :
99 : : /*
100 : : * Tuple visibility is only computed once for each tuple, for correctness
101 : : * and efficiency reasons; see comment in heap_page_prune_and_freeze() for
102 : : * details. This is of type int8[], instead of HTSV_Result[], so we can
103 : : * use -1 to indicate no visibility has been computed, e.g. for LP_DEAD
104 : : * items.
105 : : *
106 : : * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
107 : : * 1. Otherwise every access would need to subtract 1.
108 : : */
109 : : int8 htsv[MaxHeapTuplesPerPage + 1];
110 : :
111 : : /*-------------------------------------------------------
112 : : * Working state for freezing
113 : : *-------------------------------------------------------
114 : : */
115 : : HeapPageFreeze pagefrz;
116 : :
117 : : /*-------------------------------------------------------
118 : : * Information about what was done
119 : : *
120 : : * These fields are not used by pruning itself for the most part, but are
121 : : * used to collect information about what was pruned and what state the
122 : : * page is in after pruning, for the benefit of the caller. They are
123 : : * copied to the caller's PruneFreezeResult at the end.
124 : : * -------------------------------------------------------
125 : : */
126 : :
127 : : int ndeleted; /* Number of tuples deleted from the page */
128 : :
129 : : /* Number of live and recently dead tuples, after pruning */
130 : : int live_tuples;
131 : : int recently_dead_tuples;
132 : :
133 : : /* Whether or not the page makes rel truncation unsafe */
134 : : bool hastup;
135 : :
136 : : /*
137 : : * LP_DEAD items on the page after pruning. Includes existing LP_DEAD
138 : : * items
139 : : */
140 : : int lpdead_items; /* number of items in the array */
141 : : OffsetNumber *deadoffsets; /* points directly to presult->deadoffsets */
142 : :
143 : : /*
144 : : * set_all_visible and set_all_frozen indicate if the all-visible and
145 : : * all-frozen bits in the visibility map can be set for this page after
146 : : * pruning.
147 : : *
148 : : * visibility_cutoff_xid is the newest xmin of live tuples on the page.
149 : : * The caller can use it as the conflict horizon, when setting the VM
150 : : * bits. It is only valid if we froze some tuples, and set_all_frozen is
151 : : * true.
152 : : *
153 : : * NOTE: set_all_visible and set_all_frozen initially don't include
154 : : * LP_DEAD items. That's convenient for heap_page_prune_and_freeze() to
155 : : * use them to decide whether to freeze the page or not. The
156 : : * set_all_visible and set_all_frozen values returned to the caller are
157 : : * adjusted to include LP_DEAD items after we determine whether to
158 : : * opportunistically freeze.
159 : : */
160 : : bool set_all_visible;
161 : : bool set_all_frozen;
162 : : TransactionId visibility_cutoff_xid;
163 : : } PruneState;
164 : :
165 : : /* Local functions */
166 : : static void prune_freeze_setup(PruneFreezeParams *params,
167 : : TransactionId *new_relfrozen_xid,
168 : : MultiXactId *new_relmin_mxid,
169 : : PruneFreezeResult *presult,
170 : : PruneState *prstate);
171 : : static void prune_freeze_plan(PruneState *prstate,
172 : : OffsetNumber *off_loc);
173 : : static HTSV_Result heap_prune_satisfies_vacuum(PruneState *prstate,
174 : : HeapTuple tup);
175 : : static inline HTSV_Result htsv_get_valid_status(int status);
176 : : static void heap_prune_chain(OffsetNumber maxoff,
177 : : OffsetNumber rootoffnum, PruneState *prstate);
178 : : static void heap_prune_record_prunable(PruneState *prstate, TransactionId xid);
179 : : static void heap_prune_record_redirect(PruneState *prstate,
180 : : OffsetNumber offnum, OffsetNumber rdoffnum,
181 : : bool was_normal);
182 : : static void heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum,
183 : : bool was_normal);
184 : : static void heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum,
185 : : bool was_normal);
186 : : static void heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum, bool was_normal);
187 : :
188 : : static void heap_prune_record_unchanged_lp_unused(PruneState *prstate, OffsetNumber offnum);
189 : : static void heap_prune_record_unchanged_lp_normal(PruneState *prstate, OffsetNumber offnum);
190 : : static void heap_prune_record_unchanged_lp_dead(PruneState *prstate, OffsetNumber offnum);
191 : : static void heap_prune_record_unchanged_lp_redirect(PruneState *prstate, OffsetNumber offnum);
192 : :
193 : : static void page_verify_redirects(Page page);
194 : :
195 : : static bool heap_page_will_freeze(bool did_tuple_hint_fpi, bool do_prune, bool do_hint_prune,
196 : : PruneState *prstate);
197 : :
198 : :
199 : : /*
200 : : * Optionally prune and repair fragmentation in the specified page.
201 : : *
202 : : * This is an opportunistic function. It will perform housekeeping
203 : : * only if the page heuristically looks like a candidate for pruning and we
204 : : * can acquire buffer cleanup lock without blocking.
205 : : *
206 : : * Note: this is called quite often. It's important that it fall out quickly
207 : : * if there's not any use in pruning.
208 : : *
209 : : * Caller must have pin on the buffer, and must *not* have a lock on it.
210 : : */
211 : : void
4395 rhaas@postgresql.org 212 :CBC 16297556 : heap_page_prune_opt(Relation relation, Buffer buffer)
213 : : {
3616 kgrittn@postgresql.o 214 : 16297556 : Page page = BufferGetPage(buffer);
215 : : TransactionId prune_xid;
216 : : GlobalVisState *vistest;
217 : : Size minfree;
218 : :
219 : : /*
220 : : * We can't write WAL in recovery mode, so there's no point trying to
221 : : * clean the page. The primary will likely issue a cleaning WAL record
222 : : * soon anyway, so this is no particular loss.
223 : : */
4395 rhaas@postgresql.org 224 [ + + ]: 16297556 : if (RecoveryInProgress())
225 : 232656 : return;
226 : :
227 : : /*
228 : : * First check whether there's any chance there's something to prune,
229 : : * determining the appropriate horizon is a waste if there's no prune_xid
230 : : * (i.e. no updates/deletes left potentially dead tuples around).
231 : : */
10 melanieplageman@gmai 232 :GNC 16064900 : prune_xid = PageGetPruneXid(page);
2041 andres@anarazel.de 233 [ + + ]:CBC 16064900 : if (!TransactionIdIsValid(prune_xid))
234 : 8216903 : return;
235 : :
236 : : /*
237 : : * Check whether prune_xid indicates that there may be dead rows that can
238 : : * be cleaned up.
239 : : */
240 : 7847997 : vistest = GlobalVisTestFor(relation);
241 : :
242 [ + + ]: 7847997 : if (!GlobalVisTestIsRemovableXid(vistest, prune_xid))
922 tmunro@postgresql.or 243 : 6555893 : return;
244 : :
245 : : /*
246 : : * We prune when a previous UPDATE failed to find enough space on the page
247 : : * for a new tuple version, or when free space falls below the relation's
248 : : * fill-factor target (but not less than 10%).
249 : : *
250 : : * Checking free space here is questionable since we aren't holding any
251 : : * lock on the buffer; in the worst case we could get a bogus answer. It's
252 : : * unlikely to be *seriously* wrong, though, since reading either pd_lower
253 : : * or pd_upper is probably atomic. Avoiding taking a lock seems more
254 : : * important than sometimes getting a wrong answer in what is after all
255 : : * just a heuristic estimate.
256 : : */
703 akorotkov@postgresql 257 [ + + ]: 1292104 : minfree = RelationGetTargetPageFreeSpace(relation,
258 : : HEAP_DEFAULT_FILLFACTOR);
6751 tgl@sss.pgh.pa.us 259 : 1292104 : minfree = Max(minfree, BLCKSZ / 10);
260 : :
6454 261 [ + + + + ]: 1292104 : if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
262 : : {
263 : : /* OK, try to get exclusive buffer lock */
6751 264 [ + + ]: 37954 : if (!ConditionalLockBufferForCleanup(buffer))
265 : 395 : return;
266 : :
267 : : /*
268 : : * Now that we have buffer lock, get accurate information about the
269 : : * page's free space, and recheck the heuristic about whether to
270 : : * prune.
271 : : */
6454 272 [ + + + - ]: 37559 : if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
273 : : {
274 : : OffsetNumber dummy_off_loc;
275 : : PruneFreezeResult presult;
276 : :
277 : : /*
278 : : * We don't pass the HEAP_PAGE_PRUNE_MARK_UNUSED_NOW option
279 : : * regardless of whether or not the relation has indexes, since we
280 : : * cannot safely determine that during on-access pruning with the
281 : : * current implementation.
282 : : */
115 melanieplageman@gmai 283 :GNC 37559 : PruneFreezeParams params = {
284 : : .relation = relation,
285 : : .buffer = buffer,
286 : : .reason = PRUNE_ON_ACCESS,
287 : : .options = 0,
288 : : .vistest = vistest,
289 : : .cutoffs = NULL,
290 : : };
291 : :
292 : 37559 : heap_page_prune_and_freeze(¶ms, &presult, &dummy_off_loc,
293 : : NULL, NULL);
294 : :
295 : : /*
296 : : * Report the number of tuples reclaimed to pgstats. This is
297 : : * presult.ndeleted minus the number of newly-LP_DEAD-set items.
298 : : *
299 : : * We derive the number of dead tuples like this to avoid totally
300 : : * forgetting about items that were set to LP_DEAD, since they
301 : : * still need to be cleaned up by VACUUM. We only want to count
302 : : * heap-only tuples that just became LP_UNUSED in our report,
303 : : * which don't.
304 : : *
305 : : * VACUUM doesn't have to compensate in the same way when it
306 : : * tracks ndeleted, since it will set the same LP_DEAD items to
307 : : * LP_UNUSED separately.
308 : : */
899 rhaas@postgresql.org 309 [ + + ]:CBC 37559 : if (presult.ndeleted > presult.nnewlpdead)
1584 pg@bowt.ie 310 : 16664 : pgstat_update_heap_dead_tuples(relation,
899 rhaas@postgresql.org 311 : 16664 : presult.ndeleted - presult.nnewlpdead);
312 : : }
313 : :
314 : : /* And release buffer lock */
6751 tgl@sss.pgh.pa.us 315 : 37559 : LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
316 : :
317 : : /*
318 : : * We avoid reuse of any free space created on the page by unrelated
319 : : * UPDATEs/INSERTs by opting to not update the FSM at this point. The
320 : : * free space should be reused by UPDATEs to *this* page.
321 : : */
322 : : }
323 : : }
324 : :
325 : : /*
326 : : * Helper for heap_page_prune_and_freeze() to initialize the PruneState using
327 : : * the provided parameters.
328 : : *
329 : : * params, new_relfrozen_xid, new_relmin_mxid, and presult are input
330 : : * parameters and are not modified by this function. Only prstate is modified.
331 : : */
332 : : static void
109 melanieplageman@gmai 333 :GNC 112828 : prune_freeze_setup(PruneFreezeParams *params,
334 : : TransactionId *new_relfrozen_xid,
335 : : MultiXactId *new_relmin_mxid,
336 : : PruneFreezeResult *presult,
337 : : PruneState *prstate)
338 : : {
339 : : /* Copy parameters to prstate */
340 : 112828 : prstate->vistest = params->vistest;
341 : 112828 : prstate->mark_unused_now =
115 342 : 112828 : (params->options & HEAP_PAGE_PRUNE_MARK_UNUSED_NOW) != 0;
343 : :
344 : : /* cutoffs must be provided if we will attempt freezing */
110 345 [ + + - + ]: 112828 : Assert(!(params->options & HEAP_PAGE_PRUNE_FREEZE) || params->cutoffs);
109 346 : 112828 : prstate->attempt_freeze = (params->options & HEAP_PAGE_PRUNE_FREEZE) != 0;
347 : 112828 : prstate->cutoffs = params->cutoffs;
10 348 : 112828 : prstate->relation = params->relation;
349 : 112828 : prstate->block = BufferGetBlockNumber(params->buffer);
350 : 112828 : prstate->buffer = params->buffer;
351 : 112828 : prstate->page = BufferGetPage(params->buffer);
352 : :
353 : : /*
354 : : * Our strategy is to scan the page and make lists of items to change,
355 : : * then apply the changes within a critical section. This keeps as much
356 : : * logic as possible out of the critical section, and also ensures that
357 : : * WAL replay will work the same as the normal case.
358 : : *
359 : : * First, initialize the new pd_prune_xid value to zero (indicating no
360 : : * prunable tuples). If we find any tuples which may soon become
361 : : * prunable, we will save the lowest relevant XID in new_prune_xid. Also
362 : : * initialize the rest of our working state.
363 : : */
109 364 : 112828 : prstate->new_prune_xid = InvalidTransactionId;
365 : 112828 : prstate->latest_xid_removed = InvalidTransactionId;
366 : 112828 : prstate->nredirected = prstate->ndead = prstate->nunused = 0;
367 : 112828 : prstate->nfrozen = 0;
368 : 112828 : prstate->nroot_items = 0;
369 : 112828 : prstate->nheaponly_items = 0;
370 : :
371 : : /* initialize page freezing working state */
372 : 112828 : prstate->pagefrz.freeze_required = false;
5 373 : 112828 : prstate->pagefrz.FreezePageConflictXid = InvalidTransactionId;
109 374 [ + + ]: 112828 : if (prstate->attempt_freeze)
375 : : {
101 melanieplageman@gmai 376 [ + - - + ]:CBC 75269 : Assert(new_relfrozen_xid && new_relmin_mxid);
101 melanieplageman@gmai 377 :GNC 75269 : prstate->pagefrz.FreezePageRelfrozenXid = *new_relfrozen_xid;
378 : 75269 : prstate->pagefrz.NoFreezePageRelfrozenXid = *new_relfrozen_xid;
379 : 75269 : prstate->pagefrz.FreezePageRelminMxid = *new_relmin_mxid;
380 : 75269 : prstate->pagefrz.NoFreezePageRelminMxid = *new_relmin_mxid;
381 : : }
382 : : else
383 : : {
384 [ + - - + ]: 37559 : Assert(!new_relfrozen_xid && !new_relmin_mxid);
109 385 : 37559 : prstate->pagefrz.FreezePageRelminMxid = InvalidMultiXactId;
386 : 37559 : prstate->pagefrz.NoFreezePageRelminMxid = InvalidMultiXactId;
387 : 37559 : prstate->pagefrz.FreezePageRelfrozenXid = InvalidTransactionId;
388 : 37559 : prstate->pagefrz.NoFreezePageRelfrozenXid = InvalidTransactionId;
389 : : }
390 : :
391 : 112828 : prstate->ndeleted = 0;
392 : 112828 : prstate->live_tuples = 0;
393 : 112828 : prstate->recently_dead_tuples = 0;
394 : 112828 : prstate->hastup = false;
395 : 112828 : prstate->lpdead_items = 0;
396 : :
397 : : /*
398 : : * deadoffsets are filled in during pruning but are only used to populate
399 : : * PruneFreezeResult->deadoffsets. To avoid needing two copies of the
400 : : * array, just save a pointer to the result offsets array in the
401 : : * PruneState.
402 : : */
89 403 : 112828 : prstate->deadoffsets = presult->deadoffsets;
404 : :
405 : : /*
406 : : * Vacuum may update the VM after we're done. We can keep track of
407 : : * whether the page will be all-visible and all-frozen after pruning and
408 : : * freezing to help the caller to do that.
409 : : *
410 : : * Currently, only VACUUM sets the VM bits. To save the effort, only do
411 : : * the bookkeeping if the caller needs it. Currently, that's tied to
412 : : * HEAP_PAGE_PRUNE_FREEZE, but it could be a separate flag if you wanted
413 : : * to update the VM bits without also freezing or freeze without also
414 : : * setting the VM bits.
415 : : *
416 : : * In addition to telling the caller whether it can set the VM bit, we
417 : : * also use 'set_all_visible' and 'set_all_frozen' for our own
418 : : * decision-making. If the whole page would become frozen, we consider
419 : : * opportunistically freezing tuples. We will not be able to freeze the
420 : : * whole page if there are tuples present that are not visible to everyone
421 : : * or if there are dead tuples which are not yet removable. However, dead
422 : : * tuples which will be removed by the end of vacuuming should not
423 : : * preclude us from opportunistically freezing. Because of that, we do
424 : : * not immediately clear set_all_visible and set_all_frozen when we see
425 : : * LP_DEAD items. We fix that after scanning the line pointers. We must
426 : : * correct set_all_visible and set_all_frozen before we return them to the
427 : : * caller, so that the caller doesn't set the VM bits incorrectly.
428 : : */
109 429 [ + + ]: 112828 : if (prstate->attempt_freeze)
430 : : {
10 431 : 75269 : prstate->set_all_visible = true;
432 : 75269 : prstate->set_all_frozen = true;
433 : : }
434 : : else
435 : : {
436 : : /*
437 : : * Initializing to false allows skipping the work to update them in
438 : : * heap_prune_record_unchanged_lp_normal().
439 : : */
440 : 37559 : prstate->set_all_visible = false;
441 : 37559 : prstate->set_all_frozen = false;
442 : : }
443 : :
444 : : /*
445 : : * The visibility cutoff xid is the newest xmin of live tuples on the
446 : : * page. In the common case, this will be set as the conflict horizon the
447 : : * caller can use for updating the VM. If, at the end of freezing and
448 : : * pruning, the page is all-frozen, there is no possibility that any
449 : : * running transaction on the standby does not see tuples on the page as
450 : : * all-visible, so the conflict horizon remains InvalidTransactionId.
451 : : */
109 452 : 112828 : prstate->visibility_cutoff_xid = InvalidTransactionId;
453 : 112828 : }
454 : :
455 : : /*
456 : : * Helper for heap_page_prune_and_freeze(). Iterates over every tuple on the
457 : : * page, examines its visibility information, and determines the appropriate
458 : : * action for each tuple. All tuples are processed and classified during this
459 : : * phase, but no modifications are made to the page until the later execution
460 : : * stage.
461 : : *
462 : : * *off_loc is used for error callback and cleared before returning.
463 : : */
464 : : static void
10 465 : 112828 : prune_freeze_plan(PruneState *prstate, OffsetNumber *off_loc)
466 : : {
467 : 112828 : Page page = prstate->page;
468 : 112828 : BlockNumber blockno = prstate->block;
469 : 112828 : OffsetNumber maxoff = PageGetMaxOffsetNumber(prstate->page);
470 : : OffsetNumber offnum;
471 : : HeapTupleData tup;
472 : :
473 : 112828 : tup.t_tableOid = RelationGetRelid(prstate->relation);
474 : :
475 : : /*
476 : : * Determine HTSV for all tuples, and queue them up for processing as HOT
477 : : * chain roots or as heap-only items.
478 : : *
479 : : * Determining HTSV only once for each tuple is required for correctness,
480 : : * to deal with cases where running HTSV twice could result in different
481 : : * results. For example, RECENTLY_DEAD can turn to DEAD if another
482 : : * checked item causes GlobalVisTestIsRemovableFullXid() to update the
483 : : * horizon, or INSERT_IN_PROGRESS can change to DEAD if the inserting
484 : : * transaction aborts.
485 : : *
486 : : * It's also good for performance. Most commonly tuples within a page are
487 : : * stored at decreasing offsets (while the items are stored at increasing
488 : : * offsets). When processing all tuples on a page this leads to reading
489 : : * memory at decreasing offsets within a page, with a variable stride.
490 : : * That's hard for CPU prefetchers to deal with. Processing the items in
491 : : * reverse order (and thus the tuples in increasing order) increases
492 : : * prefetching efficiency significantly / decreases the number of cache
493 : : * misses.
494 : : */
1556 andres@anarazel.de 495 :CBC 112828 : for (offnum = maxoff;
496 [ + + ]: 9248464 : offnum >= FirstOffsetNumber;
497 : 9135636 : offnum = OffsetNumberPrev(offnum))
498 : : {
499 : 9135636 : ItemId itemid = PageGetItemId(page, offnum);
500 : : HeapTupleHeader htup;
501 : :
502 : : /*
503 : : * Set the offset number so that we can display it along with any
504 : : * error that occurred while processing this tuple.
505 : : */
713 heikki.linnakangas@i 506 : 9135636 : *off_loc = offnum;
507 : :
109 melanieplageman@gmai 508 :GNC 9135636 : prstate->processed[offnum] = false;
509 : 9135636 : prstate->htsv[offnum] = -1;
510 : :
511 : : /* Nothing to do if slot doesn't contain a tuple */
713 heikki.linnakangas@i 512 [ + + ]:CBC 9135636 : if (!ItemIdIsUsed(itemid))
513 : : {
10 melanieplageman@gmai 514 :GNC 148392 : heap_prune_record_unchanged_lp_unused(prstate, offnum);
1556 andres@anarazel.de 515 :CBC 148392 : continue;
516 : : }
517 : :
713 heikki.linnakangas@i 518 [ + + ]: 8987244 : if (ItemIdIsDead(itemid))
519 : : {
520 : : /*
521 : : * If the caller set mark_unused_now true, we can set dead line
522 : : * pointers LP_UNUSED now.
523 : : */
109 melanieplageman@gmai 524 [ + + ]:GNC 1108393 : if (unlikely(prstate->mark_unused_now))
525 : 1026 : heap_prune_record_unused(prstate, offnum, false);
526 : : else
10 527 : 1107367 : heap_prune_record_unchanged_lp_dead(prstate, offnum);
713 heikki.linnakangas@i 528 :CBC 1108393 : continue;
529 : : }
530 : :
531 [ + + ]: 7878851 : if (ItemIdIsRedirected(itemid))
532 : : {
533 : : /* This is the start of a HOT chain */
109 melanieplageman@gmai 534 :GNC 146892 : prstate->root_items[prstate->nroot_items++] = offnum;
713 heikki.linnakangas@i 535 :CBC 146892 : continue;
536 : : }
537 : :
538 [ - + ]: 7731959 : Assert(ItemIdIsNormal(itemid));
539 : :
540 : : /*
541 : : * Get the tuple's visibility status and queue it up for processing.
542 : : */
543 : 7731959 : htup = (HeapTupleHeader) PageGetItem(page, itemid);
544 : 7731959 : tup.t_data = htup;
545 : 7731959 : tup.t_len = ItemIdGetLength(itemid);
546 : 7731959 : ItemPointerSet(&tup.t_self, blockno, offnum);
547 : :
10 melanieplageman@gmai 548 :GNC 7731959 : prstate->htsv[offnum] = heap_prune_satisfies_vacuum(prstate, &tup);
549 : :
713 heikki.linnakangas@i 550 [ + + ]:CBC 7731959 : if (!HeapTupleHeaderIsHeapOnly(htup))
109 melanieplageman@gmai 551 :GNC 7461946 : prstate->root_items[prstate->nroot_items++] = offnum;
552 : : else
553 : 270013 : prstate->heaponly_items[prstate->nheaponly_items++] = offnum;
554 : : }
555 : :
556 : : /*
557 : : * Process HOT chains.
558 : : *
559 : : * We added the items to the array starting from 'maxoff', so by
560 : : * processing the array in reverse order, we process the items in
561 : : * ascending offset number order. The order doesn't matter for
562 : : * correctness, but some quick micro-benchmarking suggests that this is
563 : : * faster. (Earlier PostgreSQL versions, which scanned all the items on
564 : : * the page instead of using the root_items array, also did it in
565 : : * ascending offset number order.)
566 : : */
567 [ + + ]: 7721666 : for (int i = prstate->nroot_items - 1; i >= 0; i--)
568 : : {
569 : 7608838 : offnum = prstate->root_items[i];
570 : :
571 : : /* Ignore items already processed as part of an earlier chain */
572 [ - + ]: 7608838 : if (prstate->processed[offnum])
6581 tgl@sss.pgh.pa.us 573 :UBC 0 : continue;
574 : :
575 : : /* see preceding loop */
713 heikki.linnakangas@i 576 :CBC 7608838 : *off_loc = offnum;
577 : :
578 : : /* Process this item or chain of items */
10 melanieplageman@gmai 579 :GNC 7608838 : heap_prune_chain(maxoff, offnum, prstate);
580 : : }
581 : :
582 : : /*
583 : : * Process any heap-only tuples that were not already processed as part of
584 : : * a HOT chain.
585 : : */
109 586 [ + + ]: 382841 : for (int i = prstate->nheaponly_items - 1; i >= 0; i--)
587 : : {
588 : 270013 : offnum = prstate->heaponly_items[i];
589 : :
590 [ + + ]: 270013 : if (prstate->processed[offnum])
713 heikki.linnakangas@i 591 :CBC 256301 : continue;
592 : :
593 : : /* see preceding loop */
594 : 13712 : *off_loc = offnum;
595 : :
596 : : /*
597 : : * If the tuple is DEAD and doesn't chain to anything else, mark it
598 : : * unused. (If it does chain, we can only remove it as part of
599 : : * pruning its chain.)
600 : : *
601 : : * We need this primarily to handle aborted HOT updates, that is,
602 : : * XMIN_INVALID heap-only tuples. Those might not be linked to by any
603 : : * chain, since the parent tuple might be re-updated before any
604 : : * pruning occurs. So we have to be able to reap them separately from
605 : : * chain-pruning. (Note that HeapTupleHeaderIsHotUpdated will never
606 : : * return true for an XMIN_INVALID tuple, so this code will work even
607 : : * when there were sequential updates within the aborted transaction.)
608 : : */
109 melanieplageman@gmai 609 [ + + ]:GNC 13712 : if (prstate->htsv[offnum] == HEAPTUPLE_DEAD)
610 : : {
713 heikki.linnakangas@i 611 :CBC 2374 : ItemId itemid = PageGetItemId(page, offnum);
612 : 2374 : HeapTupleHeader htup = (HeapTupleHeader) PageGetItem(page, itemid);
613 : :
614 [ + - ]: 2374 : if (likely(!HeapTupleHeaderIsHotUpdated(htup)))
615 : : {
616 : 2374 : HeapTupleHeaderAdvanceConflictHorizon(htup,
617 : : &prstate->latest_xid_removed);
109 melanieplageman@gmai 618 :GNC 2374 : heap_prune_record_unused(prstate, offnum, true);
619 : : }
620 : : else
621 : : {
622 : : /*
623 : : * This tuple should've been processed and removed as part of
624 : : * a HOT chain, so something's wrong. To preserve evidence,
625 : : * we don't dare to remove it. We cannot leave behind a DEAD
626 : : * tuple either, because that will cause VACUUM to error out.
627 : : * Throwing an error with a distinct error message seems like
628 : : * the least bad option.
629 : : */
713 heikki.linnakangas@i 630 [ # # ]:UBC 0 : elog(ERROR, "dead heap-only tuple (%u, %d) is not linked to from any HOT chain",
631 : : blockno, offnum);
632 : : }
633 : : }
634 : : else
10 melanieplageman@gmai 635 :GNC 11338 : heap_prune_record_unchanged_lp_normal(prstate, offnum);
636 : : }
637 : :
638 : : /* We should now have processed every tuple exactly once */
639 : : #ifdef USE_ASSERT_CHECKING
713 heikki.linnakangas@i 640 :CBC 112828 : for (offnum = FirstOffsetNumber;
641 [ + + ]: 9248464 : offnum <= maxoff;
642 : 9135636 : offnum = OffsetNumberNext(offnum))
643 : : {
644 : 9135636 : *off_loc = offnum;
645 : :
109 melanieplageman@gmai 646 [ - + ]:GNC 9135636 : Assert(prstate->processed[offnum]);
647 : : }
648 : : #endif
649 : :
650 : : /* Clear the offset information once we have processed the given page. */
713 heikki.linnakangas@i 651 :CBC 112828 : *off_loc = InvalidOffsetNumber;
109 melanieplageman@gmai 652 :GNC 112828 : }
653 : :
654 : : /*
655 : : * Decide whether to proceed with freezing according to the freeze plans
656 : : * prepared for the current heap buffer. If freezing is chosen, this function
657 : : * performs several pre-freeze checks.
658 : : *
659 : : * The values of do_prune, do_hint_prune, and did_tuple_hint_fpi must be
660 : : * determined before calling this function.
661 : : *
662 : : * prstate is both an input and output parameter.
663 : : *
664 : : * Returns true if we should apply the freeze plans and freeze tuples on the
665 : : * page, and false otherwise.
666 : : */
667 : : static bool
10 668 : 112828 : heap_page_will_freeze(bool did_tuple_hint_fpi,
669 : : bool do_prune,
670 : : bool do_hint_prune,
671 : : PruneState *prstate)
672 : : {
109 673 : 112828 : bool do_freeze = false;
674 : :
675 : : /*
676 : : * If the caller specified we should not attempt to freeze any tuples,
677 : : * validate that everything is in the right state and return.
678 : : */
679 [ + + ]: 112828 : if (!prstate->attempt_freeze)
680 : : {
10 681 [ + - - + ]: 37559 : Assert(!prstate->set_all_frozen && prstate->nfrozen == 0);
682 [ + + - + ]: 37559 : Assert(prstate->lpdead_items == 0 || !prstate->set_all_visible);
109 683 : 37559 : return false;
684 : : }
685 : :
686 [ + + ]: 75269 : if (prstate->pagefrz.freeze_required)
687 : : {
688 : : /*
689 : : * heap_prepare_freeze_tuple indicated that at least one XID/MXID from
690 : : * before FreezeLimit/MultiXactCutoff is present. Must freeze to
691 : : * advance relfrozenxid/relminmxid.
692 : : */
693 : 17915 : do_freeze = true;
694 : : }
695 : : else
696 : : {
697 : : /*
698 : : * Opportunistically freeze the page if we are generating an FPI
699 : : * anyway and if doing so means that we can set the page all-frozen
700 : : * afterwards (might not happen until VACUUM's final heap pass).
701 : : *
702 : : * XXX: Previously, we knew if pruning emitted an FPI by checking
703 : : * pgWalUsage.wal_fpi before and after pruning. Once the freeze and
704 : : * prune records were combined, this heuristic couldn't be used
705 : : * anymore. The opportunistic freeze heuristic must be improved;
706 : : * however, for now, try to approximate the old logic.
707 : : */
10 708 [ + + + + ]: 57354 : if (prstate->set_all_frozen && prstate->nfrozen > 0)
709 : : {
710 [ - + ]: 21046 : Assert(prstate->set_all_visible);
711 : :
712 : : /*
713 : : * Freezing would make the page all-frozen. Have already emitted
714 : : * an FPI or will do so anyway?
715 : : */
716 [ + + + + : 21046 : if (RelationNeedsWAL(prstate->relation))
+ - + - ]
717 : : {
109 718 [ + + ]: 19391 : if (did_tuple_hint_fpi)
719 : 1345 : do_freeze = true;
720 [ + + ]: 18046 : else if (do_prune)
721 : : {
10 722 [ + + ]: 1910 : if (XLogCheckBufferNeedsBackup(prstate->buffer))
109 723 : 972 : do_freeze = true;
724 : : }
725 [ + + ]: 16136 : else if (do_hint_prune)
726 : : {
10 727 [ - + - - : 16 : if (XLogHintBitIsNeeded() &&
+ + ]
728 : 8 : XLogCheckBufferNeedsBackup(prstate->buffer))
109 melanieplageman@gmai 729 :CBC 4 : do_freeze = true;
730 : : }
731 : : }
732 : : }
733 : : }
734 : :
735 [ + + ]: 75269 : if (do_freeze)
736 : : {
737 : : /*
738 : : * Validate the tuples we will be freezing before entering the
739 : : * critical section.
740 : : */
10 melanieplageman@gmai 741 :GNC 20236 : heap_pre_freeze_checks(prstate->buffer, prstate->frozen, prstate->nfrozen);
5 742 [ - + ]: 20236 : Assert(TransactionIdPrecedes(prstate->pagefrz.FreezePageConflictXid,
743 : : prstate->cutoffs->OldestXmin));
744 : : }
109 745 [ + + ]: 55033 : else if (prstate->nfrozen > 0)
746 : : {
747 : : /*
748 : : * The page contained some tuples that were not already frozen, and we
749 : : * chose not to freeze them now. The page won't be all-frozen then.
750 : : */
751 [ - + ]: 19299 : Assert(!prstate->pagefrz.freeze_required);
752 : :
10 753 : 19299 : prstate->set_all_frozen = false;
109 754 : 19299 : prstate->nfrozen = 0; /* avoid miscounts in instrumentation */
755 : : }
756 : : else
757 : : {
758 : : /*
759 : : * We have no freeze plans to execute. The page might already be
760 : : * all-frozen (perhaps only following pruning), though. Such pages
761 : : * can be marked all-frozen in the VM by our caller, even though none
762 : : * of its tuples were newly frozen here.
763 : : */
764 : : }
765 : :
766 : 75269 : return do_freeze;
767 : : }
768 : :
769 : :
770 : : /*
771 : : * Prune and repair fragmentation and potentially freeze tuples on the
772 : : * specified page.
773 : : *
774 : : * Caller must have pin and buffer cleanup lock on the page. Note that we
775 : : * don't update the FSM information for page on caller's behalf. Caller might
776 : : * also need to account for a reduction in the length of the line pointer
777 : : * array following array truncation by us.
778 : : *
779 : : * params contains the input parameters used to control freezing and pruning
780 : : * behavior. See the definition of PruneFreezeParams for more on what each
781 : : * parameter does.
782 : : *
783 : : * If the HEAP_PAGE_PRUNE_FREEZE option is set in params, we will freeze
784 : : * tuples if it's required in order to advance relfrozenxid / relminmxid, or
785 : : * if it's considered advantageous for overall system performance to do so
786 : : * now. The 'params.cutoffs', 'presult', 'new_relfrozen_xid' and
787 : : * 'new_relmin_mxid' arguments are required when freezing. When
788 : : * HEAP_PAGE_PRUNE_FREEZE option is passed, we also set
789 : : * presult->set_all_visible and presult->set_all_frozen after determining
790 : : * whether or not to opportunistically freeze, to indicate if the VM bits can
791 : : * be set. They are always set to false when the HEAP_PAGE_PRUNE_FREEZE
792 : : * option is not passed, because at the moment only callers that also freeze
793 : : * need that information.
794 : : *
795 : : * presult contains output parameters needed by callers, such as the number of
796 : : * tuples removed and the offsets of dead items on the page after pruning.
797 : : * heap_page_prune_and_freeze() is responsible for initializing it. Required
798 : : * by all callers.
799 : : *
800 : : * off_loc is the offset location required by the caller to use in error
801 : : * callback.
802 : : *
803 : : * new_relfrozen_xid and new_relmin_mxid must be provided by the caller if the
804 : : * HEAP_PAGE_PRUNE_FREEZE option is set in params. On entry, they contain the
805 : : * oldest XID and multi-XID seen on the relation so far. They will be updated
806 : : * with the oldest values present on the page after pruning. After processing
807 : : * the whole relation, VACUUM can use these values as the new
808 : : * relfrozenxid/relminmxid for the relation.
809 : : */
810 : : void
811 : 112828 : heap_page_prune_and_freeze(PruneFreezeParams *params,
812 : : PruneFreezeResult *presult,
813 : : OffsetNumber *off_loc,
814 : : TransactionId *new_relfrozen_xid,
815 : : MultiXactId *new_relmin_mxid)
816 : : {
817 : : PruneState prstate;
818 : : bool do_freeze;
819 : : bool do_prune;
820 : : bool do_hint_prune;
821 : : bool did_tuple_hint_fpi;
822 : 112828 : int64 fpi_before = pgWalUsage.wal_fpi;
823 : :
824 : : /* Initialize prstate */
825 : 112828 : prune_freeze_setup(params,
826 : : new_relfrozen_xid, new_relmin_mxid,
827 : : presult, &prstate);
828 : :
829 : : /*
830 : : * Examine all line pointers and tuple visibility information to determine
831 : : * which line pointers should change state and which tuples may be frozen.
832 : : * Prepare queue of state changes to later be executed in a critical
833 : : * section.
834 : : */
10 835 : 112828 : prune_freeze_plan(&prstate, off_loc);
836 : :
837 : : /*
838 : : * If checksums are enabled, calling heap_prune_satisfies_vacuum() while
839 : : * checking tuple visibility information in prune_freeze_plan() may have
840 : : * caused an FPI to be emitted.
841 : : */
109 842 : 112828 : did_tuple_hint_fpi = fpi_before != pgWalUsage.wal_fpi;
843 : :
711 heikki.linnakangas@i 844 : 324077 : do_prune = prstate.nredirected > 0 ||
845 [ + + + + ]: 177708 : prstate.ndead > 0 ||
846 [ + + ]: 64880 : prstate.nunused > 0;
847 : :
848 : : /*
849 : : * Even if we don't prune anything, if we found a new value for the
850 : : * pd_prune_xid field or the page was marked full, we will update the hint
851 : : * bit.
852 : : */
10 melanieplageman@gmai 853 [ + + + + ]: 177441 : do_hint_prune = PageGetPruneXid(prstate.page) != prstate.new_prune_xid ||
854 : 64613 : PageIsFull(prstate.page);
855 : :
856 : : /*
857 : : * Decide if we want to go ahead with freezing according to the freeze
858 : : * plans we prepared, or not.
859 : : */
860 : 112828 : do_freeze = heap_page_will_freeze(did_tuple_hint_fpi,
861 : : do_prune,
862 : : do_hint_prune,
863 : : &prstate);
864 : :
865 : : /*
866 : : * While scanning the line pointers, we did not clear
867 : : * set_all_visible/set_all_frozen when encountering LP_DEAD items because
868 : : * we wanted the decision whether or not to freeze the page to be
869 : : * unaffected by the short-term presence of LP_DEAD items. These LP_DEAD
870 : : * items are effectively assumed to be LP_UNUSED items in the making. It
871 : : * doesn't matter which vacuum heap pass (initial pass or final pass) ends
872 : : * up setting the page all-frozen, as long as the ongoing VACUUM does it.
873 : : *
874 : : * Now that we finished determining whether or not to freeze the page,
875 : : * update set_all_visible and set_all_frozen so that they reflect the true
876 : : * state of the page for setting PD_ALL_VISIBLE and VM bits.
877 : : */
115 878 [ + + ]: 112828 : if (prstate.lpdead_items > 0)
10 879 : 48941 : prstate.set_all_visible = prstate.set_all_frozen = false;
880 : :
881 [ + + - + ]: 112828 : Assert(!prstate.set_all_frozen || prstate.set_all_visible);
882 : :
883 : : /* Any error while applying the changes is critical */
711 heikki.linnakangas@i 884 :CBC 112828 : START_CRIT_SECTION();
885 : :
152 melanieplageman@gmai 886 [ + + ]:GNC 112828 : if (do_hint_prune)
887 : : {
888 : : /*
889 : : * Update the page's pd_prune_xid field to either zero, or the lowest
890 : : * XID of any soon-prunable tuple.
891 : : */
10 892 : 48328 : ((PageHeader) prstate.page)->pd_prune_xid = prstate.new_prune_xid;
893 : :
894 : : /*
895 : : * Also clear the "page is full" flag, since there's no point in
896 : : * repeating the prune/defrag process until something else happens to
897 : : * the page.
898 : : */
899 : 48328 : PageClearFull(prstate.page);
900 : :
901 : : /*
902 : : * If that's all we had to do to the page, this is a non-WAL-logged
903 : : * hint. If we are going to freeze or prune the page, we will mark
904 : : * the buffer dirty below.
905 : : */
711 heikki.linnakangas@i 906 [ + + + + ]:CBC 48328 : if (!do_freeze && !do_prune)
10 melanieplageman@gmai 907 :GNC 239 : MarkBufferDirtyHint(prstate.buffer, true);
908 : : }
909 : :
711 heikki.linnakangas@i 910 [ + + + + ]:CBC 112828 : if (do_prune || do_freeze)
911 : : {
912 : : /* Apply the planned item changes and repair page fragmentation. */
913 [ + + ]: 66286 : if (do_prune)
914 : : {
10 melanieplageman@gmai 915 :GNC 48341 : heap_page_prune_execute(prstate.buffer, false,
916 : : prstate.redirected, prstate.nredirected,
917 : : prstate.nowdead, prstate.ndead,
918 : : prstate.nowunused, prstate.nunused);
919 : : }
920 : :
711 heikki.linnakangas@i 921 [ + + ]:CBC 66286 : if (do_freeze)
10 melanieplageman@gmai 922 :GNC 20236 : heap_freeze_prepared_tuples(prstate.buffer, prstate.frozen, prstate.nfrozen);
923 : :
924 : 66286 : MarkBufferDirty(prstate.buffer);
925 : :
926 : : /*
927 : : * Emit a WAL XLOG_HEAP2_PRUNE* record showing what we did
928 : : */
929 [ + + + + : 66286 : if (RelationNeedsWAL(prstate.relation))
+ - + - ]
930 : : {
931 : : /*
932 : : * The snapshotConflictHorizon for the whole record should be the
933 : : * most conservative of all the horizons calculated for any of the
934 : : * possible modifications. If this record will prune tuples, any
935 : : * queries on the standby older than the newest xid of the most
936 : : * recently removed tuple this record will prune will conflict. If
937 : : * this record will freeze tuples, any queries on the standby with
938 : : * xids older than the newest tuple this record will freeze will
939 : : * conflict.
940 : : */
941 : : TransactionId conflict_xid;
942 : :
5 943 [ + + ]: 65411 : if (TransactionIdFollows(prstate.pagefrz.FreezePageConflictXid,
944 : : prstate.latest_xid_removed))
945 : 18474 : conflict_xid = prstate.pagefrz.FreezePageConflictXid;
946 : : else
711 heikki.linnakangas@i 947 :CBC 46937 : conflict_xid = prstate.latest_xid_removed;
948 : :
10 melanieplageman@gmai 949 :GNC 65411 : log_heap_prune_and_freeze(prstate.relation, prstate.buffer,
950 : : InvalidBuffer, /* vmbuffer */
951 : : 0, /* vmflags */
952 : : conflict_xid,
953 : : true, params->reason,
954 : : prstate.frozen, prstate.nfrozen,
955 : : prstate.redirected, prstate.nredirected,
956 : : prstate.nowdead, prstate.ndead,
957 : : prstate.nowunused, prstate.nunused);
958 : : }
959 : : }
960 : :
6751 tgl@sss.pgh.pa.us 961 [ - + ]:CBC 112828 : END_CRIT_SECTION();
962 : :
963 : : /* Copy information back for caller */
713 heikki.linnakangas@i 964 : 112828 : presult->ndeleted = prstate.ndeleted;
711 965 : 112828 : presult->nnewlpdead = prstate.ndead;
966 : 112828 : presult->nfrozen = prstate.nfrozen;
967 : 112828 : presult->live_tuples = prstate.live_tuples;
968 : 112828 : presult->recently_dead_tuples = prstate.recently_dead_tuples;
10 melanieplageman@gmai 969 :GNC 112828 : presult->set_all_visible = prstate.set_all_visible;
970 : 112828 : presult->set_all_frozen = prstate.set_all_frozen;
711 heikki.linnakangas@i 971 :CBC 112828 : presult->hastup = prstate.hastup;
972 : :
973 : : /*
974 : : * For callers planning to update the visibility map, the conflict horizon
975 : : * for that record must be the newest xmin on the page. However, if the
976 : : * page is completely frozen, there can be no conflict and the
977 : : * vm_conflict_horizon should remain InvalidTransactionId. This includes
978 : : * the case that we just froze all the tuples; the prune-freeze record
979 : : * included the conflict XID already so the caller doesn't need it.
980 : : */
10 melanieplageman@gmai 981 [ + + ]:GNC 112828 : if (presult->set_all_frozen)
711 heikki.linnakangas@i 982 :CBC 39711 : presult->vm_conflict_horizon = InvalidTransactionId;
983 : : else
984 : 73117 : presult->vm_conflict_horizon = prstate.visibility_cutoff_xid;
985 : :
986 : 112828 : presult->lpdead_items = prstate.lpdead_items;
987 : : /* the presult->deadoffsets array was already filled in */
988 : :
152 melanieplageman@gmai 989 [ + + ]:GNC 112828 : if (prstate.attempt_freeze)
990 : : {
711 heikki.linnakangas@i 991 [ + + ]:CBC 75269 : if (presult->nfrozen > 0)
992 : : {
993 : 20236 : *new_relfrozen_xid = prstate.pagefrz.FreezePageRelfrozenXid;
994 : 20236 : *new_relmin_mxid = prstate.pagefrz.FreezePageRelminMxid;
995 : : }
996 : : else
997 : : {
998 : 55033 : *new_relfrozen_xid = prstate.pagefrz.NoFreezePageRelfrozenXid;
999 : 55033 : *new_relmin_mxid = prstate.pagefrz.NoFreezePageRelminMxid;
1000 : : }
1001 : : }
6751 tgl@sss.pgh.pa.us 1002 : 112828 : }
1003 : :
1004 : :
1005 : : /*
1006 : : * Perform visibility checks for heap pruning.
1007 : : */
1008 : : static HTSV_Result
10 melanieplageman@gmai 1009 :GNC 7731959 : heap_prune_satisfies_vacuum(PruneState *prstate, HeapTuple tup)
1010 : : {
1011 : : HTSV_Result res;
1012 : : TransactionId dead_after;
1013 : :
1014 : 7731959 : res = HeapTupleSatisfiesVacuumHorizon(tup, prstate->buffer, &dead_after);
1015 : :
2041 andres@anarazel.de 1016 [ + + ]:CBC 7731959 : if (res != HEAPTUPLE_RECENTLY_DEAD)
1017 : 5987669 : return res;
1018 : :
1019 : : /*
1020 : : * For VACUUM, we must be sure to prune tuples with xmax older than
1021 : : * OldestXmin -- a visibility cutoff determined at the beginning of
1022 : : * vacuuming the relation. OldestXmin is used for freezing determination
1023 : : * and we cannot freeze dead tuples' xmaxes.
1024 : : */
604 melanieplageman@gmai 1025 [ + + ]: 1744290 : if (prstate->cutoffs &&
1026 [ + - ]: 950881 : TransactionIdIsValid(prstate->cutoffs->OldestXmin) &&
1027 [ + - - + : 950881 : NormalTransactionIdPrecedes(dead_after, prstate->cutoffs->OldestXmin))
+ + ]
1028 : 689614 : return HEAPTUPLE_DEAD;
1029 : :
1030 : : /*
1031 : : * Determine whether or not the tuple is considered dead when compared
1032 : : * with the provided GlobalVisState. On-access pruning does not provide
1033 : : * VacuumCutoffs. And for vacuum, even if the tuple's xmax is not older
1034 : : * than OldestXmin, GlobalVisTestIsRemovableXid() could find the row dead
1035 : : * if the GlobalVisState has been updated since the beginning of vacuuming
1036 : : * the relation.
1037 : : */
2041 andres@anarazel.de 1038 [ + + ]: 1054676 : if (GlobalVisTestIsRemovableXid(prstate->vistest, dead_after))
604 melanieplageman@gmai 1039 : 760237 : return HEAPTUPLE_DEAD;
1040 : :
2041 andres@anarazel.de 1041 : 294439 : return res;
1042 : : }
1043 : :
1044 : :
1045 : : /*
1046 : : * Pruning calculates tuple visibility once and saves the results in an array
1047 : : * of int8. See PruneState.htsv for details. This helper function is meant
1048 : : * to guard against examining visibility status array members which have not
1049 : : * yet been computed.
1050 : : */
1051 : : static inline HTSV_Result
711 heikki.linnakangas@i 1052 : 7718247 : htsv_get_valid_status(int status)
1053 : : {
1054 [ + - - + ]: 7718247 : Assert(status >= HEAPTUPLE_DEAD &&
1055 : : status <= HEAPTUPLE_DELETE_IN_PROGRESS);
1056 : 7718247 : return (HTSV_Result) status;
1057 : : }
1058 : :
1059 : : /*
1060 : : * Prune specified line pointer or a HOT chain originating at line pointer.
1061 : : *
1062 : : * Tuple visibility information is provided in prstate->htsv.
1063 : : *
1064 : : * If the item is an index-referenced tuple (i.e. not a heap-only tuple),
1065 : : * the HOT chain is pruned by removing all DEAD tuples at the start of the HOT
1066 : : * chain. We also prune any RECENTLY_DEAD tuples preceding a DEAD tuple.
1067 : : * This is OK because a RECENTLY_DEAD tuple preceding a DEAD tuple is really
1068 : : * DEAD, our visibility test is just too coarse to detect it.
1069 : : *
1070 : : * Pruning must never leave behind a DEAD tuple that still has tuple storage.
1071 : : * VACUUM isn't prepared to deal with that case.
1072 : : *
1073 : : * The root line pointer is redirected to the tuple immediately after the
1074 : : * latest DEAD tuple. If all tuples in the chain are DEAD, the root line
1075 : : * pointer is marked LP_DEAD. (This includes the case of a DEAD simple
1076 : : * tuple, which we treat as a chain of length 1.)
1077 : : *
1078 : : * We don't actually change the page here. We just add entries to the arrays in
1079 : : * prstate showing the changes to be made. Items to be redirected are added
1080 : : * to the redirected[] array (two entries per redirection); items to be set to
1081 : : * LP_DEAD state are added to nowdead[]; and items to be set to LP_UNUSED
1082 : : * state are added to nowunused[]. We perform bookkeeping of live tuples,
1083 : : * visibility etc. based on what the page will look like after the changes
1084 : : * applied. All that bookkeeping is performed in the heap_prune_record_*()
1085 : : * subroutines. The division of labor is that heap_prune_chain() decides the
1086 : : * fate of each tuple, ie. whether it's going to be removed, redirected or
1087 : : * left unchanged, and the heap_prune_record_*() subroutines update PruneState
1088 : : * based on that outcome.
1089 : : */
1090 : : static void
10 melanieplageman@gmai 1091 :GNC 7608838 : heap_prune_chain(OffsetNumber maxoff, OffsetNumber rootoffnum,
1092 : : PruneState *prstate)
1093 : : {
6695 bruce@momjian.us 1094 :CBC 7608838 : TransactionId priorXmax = InvalidTransactionId;
1095 : : ItemId rootlp;
1096 : : OffsetNumber offnum;
1097 : : OffsetNumber chainitems[MaxHeapTuplesPerPage];
10 melanieplageman@gmai 1098 :GNC 7608838 : Page page = prstate->page;
1099 : :
1100 : : /*
1101 : : * After traversing the HOT chain, ndeadchain is the index in chainitems
1102 : : * of the first live successor after the last dead item.
1103 : : */
713 heikki.linnakangas@i 1104 :CBC 7608838 : int ndeadchain = 0,
1105 : 7608838 : nchain = 0;
1106 : :
1107 : 7608838 : rootlp = PageGetItemId(page, rootoffnum);
1108 : :
1109 : : /* Start from the root tuple */
6751 tgl@sss.pgh.pa.us 1110 : 7608838 : offnum = rootoffnum;
1111 : :
1112 : : /* while not end of the chain */
1113 : : for (;;)
1114 : 256301 : {
1115 : : HeapTupleHeader htup;
1116 : : ItemId lp;
1117 : :
1118 : : /* Sanity check (pure paranoia) */
1635 pg@bowt.ie 1119 [ - + ]: 7865139 : if (offnum < FirstOffsetNumber)
1635 pg@bowt.ie 1120 :UBC 0 : break;
1121 : :
1122 : : /*
1123 : : * An offset past the end of page's line pointer array is possible
1124 : : * when the array was truncated (original item must have been unused)
1125 : : */
1635 pg@bowt.ie 1126 [ - + ]:CBC 7865139 : if (offnum > maxoff)
6751 tgl@sss.pgh.pa.us 1127 :UBC 0 : break;
1128 : :
1129 : : /* If item is already processed, stop --- it must not be same chain */
713 heikki.linnakangas@i 1130 [ - + ]:CBC 7865139 : if (prstate->processed[offnum])
6581 tgl@sss.pgh.pa.us 1131 :UBC 0 : break;
1132 : :
713 heikki.linnakangas@i 1133 :CBC 7865139 : lp = PageGetItemId(page, offnum);
1134 : :
1135 : : /*
1136 : : * Unused item obviously isn't part of the chain. Likewise, a dead
1137 : : * line pointer can't be part of the chain. Both of those cases were
1138 : : * already marked as processed.
1139 : : */
1140 [ - + ]: 7865139 : Assert(ItemIdIsUsed(lp));
1141 [ - + ]: 7865139 : Assert(!ItemIdIsDead(lp));
1142 : :
1143 : : /*
1144 : : * If we are looking at the redirected root line pointer, jump to the
1145 : : * first normal tuple in the chain. If we find a redirect somewhere
1146 : : * else, stop --- it must not be same chain.
1147 : : */
6751 tgl@sss.pgh.pa.us 1148 [ + + ]: 7865139 : if (ItemIdIsRedirected(lp))
1149 : : {
1150 [ - + ]: 146892 : if (nchain > 0)
6751 tgl@sss.pgh.pa.us 1151 :UBC 0 : break; /* not at start of chain */
6751 tgl@sss.pgh.pa.us 1152 :CBC 146892 : chainitems[nchain++] = offnum;
1153 : 146892 : offnum = ItemIdGetRedirect(rootlp);
1154 : 146892 : continue;
1155 : : }
1156 : :
1157 [ - + ]: 7718247 : Assert(ItemIdIsNormal(lp));
1158 : :
713 heikki.linnakangas@i 1159 : 7718247 : htup = (HeapTupleHeader) PageGetItem(page, lp);
1160 : :
1161 : : /*
1162 : : * Check the tuple XMIN against prior XMAX, if any
1163 : : */
6751 tgl@sss.pgh.pa.us 1164 [ + + - + ]: 7827656 : if (TransactionIdIsValid(priorXmax) &&
3055 alvherre@alvh.no-ip. 1165 : 109409 : !TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
6751 tgl@sss.pgh.pa.us 1166 :UBC 0 : break;
1167 : :
1168 : : /*
1169 : : * OK, this tuple is indeed a member of the chain.
1170 : : */
6751 tgl@sss.pgh.pa.us 1171 :CBC 7718247 : chainitems[nchain++] = offnum;
1172 : :
711 heikki.linnakangas@i 1173 [ + + + - ]: 7718247 : switch (htsv_get_valid_status(prstate->htsv[offnum]))
1174 : : {
6751 tgl@sss.pgh.pa.us 1175 : 1493391 : case HEAPTUPLE_DEAD:
1176 : :
1177 : : /* Remember the last DEAD tuple seen */
713 heikki.linnakangas@i 1178 : 1493391 : ndeadchain = nchain;
1179 : 1493391 : HeapTupleHeaderAdvanceConflictHorizon(htup,
1180 : : &prstate->latest_xid_removed);
1181 : : /* Advance to next chain member */
6751 tgl@sss.pgh.pa.us 1182 : 1493391 : break;
1183 : :
1184 : 294439 : case HEAPTUPLE_RECENTLY_DEAD:
1185 : :
1186 : : /*
1187 : : * We don't need to advance the conflict horizon for
1188 : : * RECENTLY_DEAD tuples, even if we are removing them. This
1189 : : * is because we only remove RECENTLY_DEAD tuples if they
1190 : : * precede a DEAD tuple, and the DEAD tuple must have been
1191 : : * inserted by a newer transaction than the RECENTLY_DEAD
1192 : : * tuple by virtue of being later in the chain. We will have
1193 : : * advanced the conflict horizon for the DEAD tuple.
1194 : : */
1195 : :
1196 : : /*
1197 : : * Advance past RECENTLY_DEAD tuples just in case there's a
1198 : : * DEAD one after them. We have to make sure that we don't
1199 : : * miss any DEAD tuples, since DEAD tuples that still have
1200 : : * tuple storage after pruning will confuse VACUUM.
1201 : : */
1202 : 294439 : break;
1203 : :
1204 : 5930417 : case HEAPTUPLE_DELETE_IN_PROGRESS:
1205 : : case HEAPTUPLE_LIVE:
1206 : : case HEAPTUPLE_INSERT_IN_PROGRESS:
713 heikki.linnakangas@i 1207 : 5930417 : goto process_chain;
1208 : :
6751 tgl@sss.pgh.pa.us 1209 :UBC 0 : default:
1210 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1211 : : goto process_chain;
1212 : : }
1213 : :
1214 : : /*
1215 : : * If the tuple is not HOT-updated, then we are at the end of this
1216 : : * HOT-update chain.
1217 : : */
6751 tgl@sss.pgh.pa.us 1218 [ + + ]:CBC 1787830 : if (!HeapTupleHeaderIsHotUpdated(htup))
713 heikki.linnakangas@i 1219 : 1678421 : goto process_chain;
1220 : :
1221 : : /* HOT implies it can't have moved to different partition */
2899 andres@anarazel.de 1222 [ - + ]: 109409 : Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
1223 : :
1224 : : /*
1225 : : * Advance to next chain member.
1226 : : */
10 melanieplageman@gmai 1227 [ - + ]:GNC 109409 : Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == prstate->block);
6751 tgl@sss.pgh.pa.us 1228 :CBC 109409 : offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
4799 alvherre@alvh.no-ip. 1229 : 109409 : priorXmax = HeapTupleHeaderGetUpdateXid(htup);
1230 : : }
1231 : :
713 heikki.linnakangas@i 1232 [ # # # # ]:UBC 0 : if (ItemIdIsRedirected(rootlp) && nchain < 2)
1233 : : {
1234 : : /*
1235 : : * We found a redirect item that doesn't point to a valid follow-on
1236 : : * item. This can happen if the loop in heap_page_prune_and_freeze()
1237 : : * caused us to visit the dead successor of a redirect item before
1238 : : * visiting the redirect item. We can clean up by setting the
1239 : : * redirect item to LP_DEAD state or LP_UNUSED if the caller
1240 : : * indicated.
1241 : : */
1242 : 0 : heap_prune_record_dead_or_unused(prstate, rootoffnum, false);
1243 : 0 : return;
1244 : : }
1245 : :
1246 : 0 : process_chain:
1247 : :
713 heikki.linnakangas@i 1248 [ + + ]:CBC 7608838 : if (ndeadchain == 0)
1249 : : {
1250 : : /*
1251 : : * No DEAD tuple was found, so the chain is entirely composed of
1252 : : * normal, unchanged tuples. Leave it alone.
1253 : : */
711 1254 : 6153845 : int i = 0;
1255 : :
1256 [ + + ]: 6153845 : if (ItemIdIsRedirected(rootlp))
1257 : : {
1258 : 129436 : heap_prune_record_unchanged_lp_redirect(prstate, rootoffnum);
1259 : 129436 : i++;
1260 : : }
1261 [ + + ]: 12312824 : for (; i < nchain; i++)
10 melanieplageman@gmai 1262 :GNC 6158979 : heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
1263 : : }
713 heikki.linnakangas@i 1264 [ + + ]:CBC 1454993 : else if (ndeadchain == nchain)
1265 : : {
1266 : : /*
1267 : : * The entire chain is dead. Mark the root line pointer LP_DEAD, and
1268 : : * fully remove the other tuples in the chain.
1269 : : */
1270 : 1391925 : heap_prune_record_dead_or_unused(prstate, rootoffnum, ItemIdIsNormal(rootlp));
1271 [ + + ]: 1429003 : for (int i = 1; i < nchain; i++)
1272 : 37078 : heap_prune_record_unused(prstate, chainitems[i], true);
1273 : : }
1274 : : else
1275 : : {
1276 : : /*
1277 : : * We found a DEAD tuple in the chain. Redirect the root line pointer
1278 : : * to the first non-DEAD tuple, and mark as unused each intermediate
1279 : : * item that we are able to remove from the chain.
1280 : : */
1281 : 63068 : heap_prune_record_redirect(prstate, rootoffnum, chainitems[ndeadchain],
1282 : 63068 : ItemIdIsNormal(rootlp));
1283 [ + + ]: 81844 : for (int i = 1; i < ndeadchain; i++)
1284 : 18776 : heap_prune_record_unused(prstate, chainitems[i], true);
1285 : :
1286 : : /* the rest of tuples in the chain are normal, unchanged tuples */
1287 [ + + ]: 128945 : for (int i = ndeadchain; i < nchain; i++)
10 melanieplageman@gmai 1288 :GNC 65877 : heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
1289 : : }
1290 : : }
1291 : :
1292 : : /* Record lowest soon-prunable XID */
1293 : : static void
6581 tgl@sss.pgh.pa.us 1294 :CBC 309309 : heap_prune_record_prunable(PruneState *prstate, TransactionId xid)
1295 : : {
1296 : : /*
1297 : : * This should exactly match the PageSetPrunable macro. We can't store
1298 : : * directly into the page header yet, so we update working state.
1299 : : */
1300 [ - + ]: 309309 : Assert(TransactionIdIsNormal(xid));
1301 [ + + + + ]: 610063 : if (!TransactionIdIsValid(prstate->new_prune_xid) ||
1302 : 300754 : TransactionIdPrecedes(xid, prstate->new_prune_xid))
1303 : 9670 : prstate->new_prune_xid = xid;
1304 : 309309 : }
1305 : :
1306 : : /* Record line pointer to be redirected */
1307 : : static void
1308 : 63068 : heap_prune_record_redirect(PruneState *prstate,
1309 : : OffsetNumber offnum, OffsetNumber rdoffnum,
1310 : : bool was_normal)
1311 : : {
713 heikki.linnakangas@i 1312 [ - + ]: 63068 : Assert(!prstate->processed[offnum]);
1313 : 63068 : prstate->processed[offnum] = true;
1314 : :
1315 : : /*
1316 : : * Do not mark the redirect target here. It needs to be counted
1317 : : * separately as an unchanged tuple.
1318 : : */
1319 : :
6581 tgl@sss.pgh.pa.us 1320 [ - + ]: 63068 : Assert(prstate->nredirected < MaxHeapTuplesPerPage);
1321 : 63068 : prstate->redirected[prstate->nredirected * 2] = offnum;
1322 : 63068 : prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
1323 : :
1324 : 63068 : prstate->nredirected++;
1325 : :
1326 : : /*
1327 : : * If the root entry had been a normal tuple, we are deleting it, so count
1328 : : * it in the result. But changing a redirect (even to DEAD state) doesn't
1329 : : * count.
1330 : : */
713 heikki.linnakangas@i 1331 [ + + ]: 63068 : if (was_normal)
1332 : 55811 : prstate->ndeleted++;
1333 : :
711 1334 : 63068 : prstate->hastup = true;
6751 tgl@sss.pgh.pa.us 1335 : 63068 : }
1336 : :
1337 : : /* Record line pointer to be marked dead */
1338 : : static void
713 heikki.linnakangas@i 1339 : 1357878 : heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum,
1340 : : bool was_normal)
1341 : : {
1342 [ - + ]: 1357878 : Assert(!prstate->processed[offnum]);
1343 : 1357878 : prstate->processed[offnum] = true;
1344 : :
6581 tgl@sss.pgh.pa.us 1345 [ - + ]: 1357878 : Assert(prstate->ndead < MaxHeapTuplesPerPage);
1346 : 1357878 : prstate->nowdead[prstate->ndead] = offnum;
1347 : 1357878 : prstate->ndead++;
1348 : :
1349 : : /*
1350 : : * Deliberately delay unsetting set_all_visible and set_all_frozen until
1351 : : * later during pruning. Removable dead tuples shouldn't preclude freezing
1352 : : * the page.
1353 : : */
1354 : :
1355 : : /* Record the dead offset for vacuum */
711 heikki.linnakangas@i 1356 : 1357878 : prstate->deadoffsets[prstate->lpdead_items++] = offnum;
1357 : :
1358 : : /*
1359 : : * If the root entry had been a normal tuple, we are deleting it, so count
1360 : : * it in the result. But changing a redirect (even to DEAD state) doesn't
1361 : : * count.
1362 : : */
713 1363 [ + + ]: 1357878 : if (was_normal)
1364 : 1347679 : prstate->ndeleted++;
6751 tgl@sss.pgh.pa.us 1365 : 1357878 : }
1366 : :
1367 : : /*
1368 : : * Depending on whether or not the caller set mark_unused_now to true, record that a
1369 : : * line pointer should be marked LP_DEAD or LP_UNUSED. There are other cases in
1370 : : * which we will mark line pointers LP_UNUSED, but we will not mark line
1371 : : * pointers LP_DEAD if mark_unused_now is true.
1372 : : */
1373 : : static void
713 heikki.linnakangas@i 1374 : 1391925 : heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum,
1375 : : bool was_normal)
1376 : : {
1377 : : /*
1378 : : * If the caller set mark_unused_now to true, we can remove dead tuples
1379 : : * during pruning instead of marking their line pointers dead. Set this
1380 : : * tuple's line pointer LP_UNUSED. We hint that this option is less
1381 : : * likely.
1382 : : */
787 rhaas@postgresql.org 1383 [ + + ]: 1391925 : if (unlikely(prstate->mark_unused_now))
713 heikki.linnakangas@i 1384 : 34047 : heap_prune_record_unused(prstate, offnum, was_normal);
1385 : : else
1386 : 1357878 : heap_prune_record_dead(prstate, offnum, was_normal);
787 rhaas@postgresql.org 1387 : 1391925 : }
1388 : :
1389 : : /* Record line pointer to be marked unused */
1390 : : static void
713 heikki.linnakangas@i 1391 : 93301 : heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum, bool was_normal)
1392 : : {
1393 [ - + ]: 93301 : Assert(!prstate->processed[offnum]);
1394 : 93301 : prstate->processed[offnum] = true;
1395 : :
6581 tgl@sss.pgh.pa.us 1396 [ - + ]: 93301 : Assert(prstate->nunused < MaxHeapTuplesPerPage);
1397 : 93301 : prstate->nowunused[prstate->nunused] = offnum;
1398 : 93301 : prstate->nunused++;
1399 : :
1400 : : /*
1401 : : * If the root entry had been a normal tuple, we are deleting it, so count
1402 : : * it in the result. But changing a redirect (even to DEAD state) doesn't
1403 : : * count.
1404 : : */
713 heikki.linnakangas@i 1405 [ + + ]: 93301 : if (was_normal)
1406 : 92275 : prstate->ndeleted++;
6581 tgl@sss.pgh.pa.us 1407 : 93301 : }
1408 : :
1409 : : /*
1410 : : * Record an unused line pointer that is left unchanged.
1411 : : */
1412 : : static void
10 melanieplageman@gmai 1413 :GNC 148392 : heap_prune_record_unchanged_lp_unused(PruneState *prstate, OffsetNumber offnum)
1414 : : {
711 heikki.linnakangas@i 1415 [ - + ]:CBC 148392 : Assert(!prstate->processed[offnum]);
1416 : 148392 : prstate->processed[offnum] = true;
1417 : 148392 : }
1418 : :
1419 : : /*
1420 : : * Record line pointer that is left unchanged. We consider freezing it, and
1421 : : * update bookkeeping of tuple counts and page visibility.
1422 : : */
1423 : : static void
10 melanieplageman@gmai 1424 :GNC 6236194 : heap_prune_record_unchanged_lp_normal(PruneState *prstate, OffsetNumber offnum)
1425 : : {
1426 : : HeapTupleHeader htup;
1427 : 6236194 : Page page = prstate->page;
1428 : :
711 heikki.linnakangas@i 1429 [ - + ]:CBC 6236194 : Assert(!prstate->processed[offnum]);
1430 : 6236194 : prstate->processed[offnum] = true;
1431 : :
1432 : 6236194 : prstate->hastup = true; /* the page is not empty */
1433 : :
1434 : : /*
1435 : : * The criteria for counting a tuple as live in this block need to match
1436 : : * what analyze.c's acquire_sample_rows() does, otherwise VACUUM and
1437 : : * ANALYZE may produce wildly different reltuples values, e.g. when there
1438 : : * are many recently-dead tuples.
1439 : : *
1440 : : * The logic here is a bit simpler than acquire_sample_rows(), as VACUUM
1441 : : * can't run inside a transaction block, which makes some cases impossible
1442 : : * (e.g. in-progress insert from the same transaction).
1443 : : *
1444 : : * HEAPTUPLE_DEAD are handled by the other heap_prune_record_*()
1445 : : * subroutines. They don't count dead items like acquire_sample_rows()
1446 : : * does, because we assume that all dead items will become LP_UNUSED
1447 : : * before VACUUM finishes. This difference is only superficial. VACUUM
1448 : : * effectively agrees with ANALYZE about DEAD items, in the end. VACUUM
1449 : : * won't remember LP_DEAD items, but only because they're not supposed to
1450 : : * be left behind when it is done. (Cases where we bypass index vacuuming
1451 : : * will violate this optimistic assumption, but the overall impact of that
1452 : : * should be negligible.)
1453 : : */
1454 : 6236194 : htup = (HeapTupleHeader) PageGetItem(page, PageGetItemId(page, offnum));
1455 : :
1456 [ + + + + : 6236194 : switch (prstate->htsv[offnum])
- ]
1457 : : {
1458 : 5859050 : case HEAPTUPLE_LIVE:
1459 : :
1460 : : /*
1461 : : * Count it as live. Not only is this natural, but it's also what
1462 : : * acquire_sample_rows() does.
1463 : : */
1464 : 5859050 : prstate->live_tuples++;
1465 : :
1466 : : /*
1467 : : * Is the tuple definitely visible to all transactions?
1468 : : *
1469 : : * NB: Like with per-tuple hint bits, we can't set the
1470 : : * PD_ALL_VISIBLE flag if the inserter committed asynchronously.
1471 : : * See SetHintBits for more info. Check that the tuple is hinted
1472 : : * xmin-committed because of that.
1473 : : */
10 melanieplageman@gmai 1474 [ + + ]:GNC 5859050 : if (prstate->set_all_visible)
1475 : : {
1476 : : TransactionId xmin;
1477 : :
711 heikki.linnakangas@i 1478 [ + + ]:CBC 4292149 : if (!HeapTupleHeaderXminCommitted(htup))
1479 : : {
10 melanieplageman@gmai 1480 :GNC 257 : prstate->set_all_visible = false;
1481 : 257 : prstate->set_all_frozen = false;
711 heikki.linnakangas@i 1482 :CBC 257 : break;
1483 : : }
1484 : :
1485 : : /*
1486 : : * The inserter definitely committed. But is it old enough
1487 : : * that everyone sees it as committed? A FrozenTransactionId
1488 : : * is seen as committed to everyone. Otherwise, we check if
1489 : : * there is a snapshot that considers this xid to still be
1490 : : * running, and if so, we don't consider the page all-visible.
1491 : : */
1492 : 4291892 : xmin = HeapTupleHeaderGetXmin(htup);
1493 : :
1494 : : /*
1495 : : * For now always use prstate->cutoffs for this test, because
1496 : : * we only update 'set_all_visible' and 'set_all_frozen' when
1497 : : * freezing is requested. We could use
1498 : : * GlobalVisTestIsRemovableXid instead, if a non-freezing
1499 : : * caller wanted to set the VM bit.
1500 : : */
1501 [ - + ]: 4291892 : Assert(prstate->cutoffs);
1502 [ + + ]: 4291892 : if (!TransactionIdPrecedes(xmin, prstate->cutoffs->OldestXmin))
1503 : : {
10 melanieplageman@gmai 1504 :GNC 2473 : prstate->set_all_visible = false;
1505 : 2473 : prstate->set_all_frozen = false;
711 heikki.linnakangas@i 1506 :CBC 2473 : break;
1507 : : }
1508 : :
1509 : : /* Track newest xmin on page. */
1510 [ + + + + ]: 4289419 : if (TransactionIdFollows(xmin, prstate->visibility_cutoff_xid) &&
1511 : : TransactionIdIsNormal(xmin))
1512 : 120707 : prstate->visibility_cutoff_xid = xmin;
1513 : : }
1514 : 5856320 : break;
1515 : :
1516 : 294439 : case HEAPTUPLE_RECENTLY_DEAD:
1517 : 294439 : prstate->recently_dead_tuples++;
10 melanieplageman@gmai 1518 :GNC 294439 : prstate->set_all_visible = false;
1519 : 294439 : prstate->set_all_frozen = false;
1520 : :
1521 : : /*
1522 : : * This tuple will soon become DEAD. Update the hint field so
1523 : : * that the page is reconsidered for pruning in future.
1524 : : */
711 heikki.linnakangas@i 1525 :CBC 294439 : heap_prune_record_prunable(prstate,
1526 : : HeapTupleHeaderGetUpdateXid(htup));
1527 : 294439 : break;
1528 : :
1529 : 67835 : case HEAPTUPLE_INSERT_IN_PROGRESS:
1530 : :
1531 : : /*
1532 : : * We do not count these rows as live, because we expect the
1533 : : * inserting transaction to update the counters at commit, and we
1534 : : * assume that will happen only after we report our results. This
1535 : : * assumption is a bit shaky, but it is what acquire_sample_rows()
1536 : : * does, so be consistent.
1537 : : */
10 melanieplageman@gmai 1538 :GNC 67835 : prstate->set_all_visible = false;
1539 : 67835 : prstate->set_all_frozen = false;
1540 : :
1541 : : /*
1542 : : * If we wanted to optimize for aborts, we might consider marking
1543 : : * the page prunable when we see INSERT_IN_PROGRESS. But we
1544 : : * don't. See related decisions about when to mark the page
1545 : : * prunable in heapam.c.
1546 : : */
711 heikki.linnakangas@i 1547 :CBC 67835 : break;
1548 : :
1549 : 14870 : case HEAPTUPLE_DELETE_IN_PROGRESS:
1550 : :
1551 : : /*
1552 : : * This an expected case during concurrent vacuum. Count such
1553 : : * rows as live. As above, we assume the deleting transaction
1554 : : * will commit and update the counters after we report.
1555 : : */
1556 : 14870 : prstate->live_tuples++;
10 melanieplageman@gmai 1557 :GNC 14870 : prstate->set_all_visible = false;
1558 : 14870 : prstate->set_all_frozen = false;
1559 : :
1560 : : /*
1561 : : * This tuple may soon become DEAD. Update the hint field so that
1562 : : * the page is reconsidered for pruning in future.
1563 : : */
711 heikki.linnakangas@i 1564 :CBC 14870 : heap_prune_record_prunable(prstate,
1565 : : HeapTupleHeaderGetUpdateXid(htup));
1566 : 14870 : break;
1567 : :
711 heikki.linnakangas@i 1568 :UBC 0 : default:
1569 : :
1570 : : /*
1571 : : * DEAD tuples should've been passed to heap_prune_record_dead()
1572 : : * or heap_prune_record_unused() instead.
1573 : : */
1574 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result %d",
1575 : : prstate->htsv[offnum]);
1576 : : break;
1577 : : }
1578 : :
1579 : : /* Consider freezing any normal tuples which will not be removed */
152 melanieplageman@gmai 1580 [ + + ]:GNC 6236194 : if (prstate->attempt_freeze)
1581 : : {
1582 : : bool totally_frozen;
1583 : :
711 heikki.linnakangas@i 1584 [ + + ]:CBC 4885303 : if ((heap_prepare_freeze_tuple(htup,
1585 : 4885303 : prstate->cutoffs,
1586 : : &prstate->pagefrz,
1587 : 4885303 : &prstate->frozen[prstate->nfrozen],
1588 : : &totally_frozen)))
1589 : : {
1590 : : /* Save prepared freeze plan for later */
1591 : 2949535 : prstate->frozen[prstate->nfrozen++].offset = offnum;
1592 : : }
1593 : :
1594 : : /*
1595 : : * If any tuple isn't either totally frozen already or eligible to
1596 : : * become totally frozen (according to its freeze plan), then the page
1597 : : * definitely cannot be set all-frozen in the visibility map later on.
1598 : : */
1599 [ + + ]: 4885303 : if (!totally_frozen)
10 melanieplageman@gmai 1600 :GNC 593297 : prstate->set_all_frozen = false;
1601 : : }
711 heikki.linnakangas@i 1602 :CBC 6236194 : }
1603 : :
1604 : :
1605 : : /*
1606 : : * Record line pointer that was already LP_DEAD and is left unchanged.
1607 : : */
1608 : : static void
10 melanieplageman@gmai 1609 :GNC 1107367 : heap_prune_record_unchanged_lp_dead(PruneState *prstate, OffsetNumber offnum)
1610 : : {
711 heikki.linnakangas@i 1611 [ - + ]:CBC 1107367 : Assert(!prstate->processed[offnum]);
1612 : 1107367 : prstate->processed[offnum] = true;
1613 : :
1614 : : /*
1615 : : * Deliberately don't set hastup for LP_DEAD items. We make the soft
1616 : : * assumption that any LP_DEAD items encountered here will become
1617 : : * LP_UNUSED later on, before count_nondeletable_pages is reached. If we
1618 : : * don't make this assumption then rel truncation will only happen every
1619 : : * other VACUUM, at most. Besides, VACUUM must treat
1620 : : * hastup/nonempty_pages as provisional no matter how LP_DEAD items are
1621 : : * handled (handled here, or handled later on).
1622 : : *
1623 : : * Similarly, don't unset set_all_visible and set_all_frozen until later,
1624 : : * at the end of heap_page_prune_and_freeze(). This will allow us to
1625 : : * attempt to freeze the page after pruning. As long as we unset it
1626 : : * before updating the visibility map, this will be correct.
1627 : : */
1628 : :
1629 : : /* Record the dead offset for vacuum */
1630 : 1107367 : prstate->deadoffsets[prstate->lpdead_items++] = offnum;
1631 : 1107367 : }
1632 : :
1633 : : /*
1634 : : * Record LP_REDIRECT that is left unchanged.
1635 : : */
1636 : : static void
1637 : 129436 : heap_prune_record_unchanged_lp_redirect(PruneState *prstate, OffsetNumber offnum)
1638 : : {
1639 : : /*
1640 : : * A redirect line pointer doesn't count as a live tuple.
1641 : : *
1642 : : * If we leave a redirect line pointer in place, there will be another
1643 : : * tuple on the page that it points to. We will do the bookkeeping for
1644 : : * that separately. So we have nothing to do here, except remember that
1645 : : * we processed this item.
1646 : : */
713 1647 [ - + ]: 129436 : Assert(!prstate->processed[offnum]);
1648 : 129436 : prstate->processed[offnum] = true;
1649 : 129436 : }
1650 : :
1651 : : /*
1652 : : * Perform the actual page changes needed by heap_page_prune_and_freeze().
1653 : : *
1654 : : * If 'lp_truncate_only' is set, we are merely marking LP_DEAD line pointers
1655 : : * as unused, not redirecting or removing anything else. The
1656 : : * PageRepairFragmentation() call is skipped in that case.
1657 : : *
1658 : : * If 'lp_truncate_only' is not set, the caller must hold a cleanup lock on
1659 : : * the buffer. If it is set, an ordinary exclusive lock suffices.
1660 : : */
1661 : : void
720 1662 : 57353 : heap_page_prune_execute(Buffer buffer, bool lp_truncate_only,
1663 : : OffsetNumber *redirected, int nredirected,
1664 : : OffsetNumber *nowdead, int ndead,
1665 : : OffsetNumber *nowunused, int nunused)
1666 : : {
198 peter@eisentraut.org 1667 :GNC 57353 : Page page = BufferGetPage(buffer);
1668 : : OffsetNumber *offnum;
1669 : : HeapTupleHeader htup PG_USED_FOR_ASSERTS_ONLY;
1670 : :
1671 : : /* Shouldn't be called unless there's something to do */
1804 pg@bowt.ie 1672 [ + + + + :CBC 57353 : Assert(nredirected > 0 || ndead > 0 || nunused > 0);
- + ]
1673 : :
1674 : : /* If 'lp_truncate_only', we can only remove already-dead line pointers */
720 heikki.linnakangas@i 1675 [ + + + - : 57353 : Assert(!lp_truncate_only || (nredirected == 0 && ndead == 0));
- + ]
1676 : :
1677 : : /* Update all redirected line pointers */
6581 tgl@sss.pgh.pa.us 1678 : 57353 : offnum = redirected;
1592 pg@bowt.ie 1679 [ + + ]: 139044 : for (int i = 0; i < nredirected; i++)
1680 : : {
6581 tgl@sss.pgh.pa.us 1681 : 81691 : OffsetNumber fromoff = *offnum++;
1682 : 81691 : OffsetNumber tooff = *offnum++;
1683 : 81691 : ItemId fromlp = PageGetItemId(page, fromoff);
1684 : : ItemId tolp PG_USED_FOR_ASSERTS_ONLY;
1685 : :
1686 : : #ifdef USE_ASSERT_CHECKING
1687 : :
1688 : : /*
1689 : : * Any existing item that we set as an LP_REDIRECT (any 'from' item)
1690 : : * must be the first item from a HOT chain. If the item has tuple
1691 : : * storage then it can't be a heap-only tuple. Otherwise we are just
1692 : : * maintaining an existing LP_REDIRECT from an existing HOT chain that
1693 : : * has been pruned at least once before now.
1694 : : */
1592 pg@bowt.ie 1695 [ + + ]: 81691 : if (!ItemIdIsRedirected(fromlp))
1696 : : {
1697 [ + - - + ]: 73992 : Assert(ItemIdHasStorage(fromlp) && ItemIdIsNormal(fromlp));
1698 : :
1699 : 73992 : htup = (HeapTupleHeader) PageGetItem(page, fromlp);
1700 [ - + ]: 73992 : Assert(!HeapTupleHeaderIsHeapOnly(htup));
1701 : : }
1702 : : else
1703 : : {
1704 : : /* We shouldn't need to redundantly set the redirect */
1705 [ - + ]: 7699 : Assert(ItemIdGetRedirect(fromlp) != tooff);
1706 : : }
1707 : :
1708 : : /*
1709 : : * The item that we're about to set as an LP_REDIRECT (the 'from'
1710 : : * item) will point to an existing item (the 'to' item) that is
1711 : : * already a heap-only tuple. There can be at most one LP_REDIRECT
1712 : : * item per HOT chain.
1713 : : *
1714 : : * We need to keep around an LP_REDIRECT item (after original
1715 : : * non-heap-only root tuple gets pruned away) so that it's always
1716 : : * possible for VACUUM to easily figure out what TID to delete from
1717 : : * indexes when an entire HOT chain becomes dead. A heap-only tuple
1718 : : * can never become LP_DEAD; an LP_REDIRECT item or a regular heap
1719 : : * tuple can.
1720 : : *
1721 : : * This check may miss problems, e.g. the target of a redirect could
1722 : : * be marked as unused subsequently. The page_verify_redirects() check
1723 : : * below will catch such problems.
1724 : : */
1725 : 81691 : tolp = PageGetItemId(page, tooff);
1726 [ + - - + ]: 81691 : Assert(ItemIdHasStorage(tolp) && ItemIdIsNormal(tolp));
1727 : 81691 : htup = (HeapTupleHeader) PageGetItem(page, tolp);
1728 [ - + ]: 81691 : Assert(HeapTupleHeaderIsHeapOnly(htup));
1729 : : #endif
1730 : :
5879 tgl@sss.pgh.pa.us 1731 : 81691 : ItemIdSetRedirect(fromlp, tooff);
1732 : : }
1733 : :
1734 : : /* Update all now-dead line pointers */
6581 1735 : 57353 : offnum = nowdead;
1592 pg@bowt.ie 1736 [ + + ]: 1685354 : for (int i = 0; i < ndead; i++)
1737 : : {
6581 tgl@sss.pgh.pa.us 1738 : 1628001 : OffsetNumber off = *offnum++;
1739 : 1628001 : ItemId lp = PageGetItemId(page, off);
1740 : :
1741 : : #ifdef USE_ASSERT_CHECKING
1742 : :
1743 : : /*
1744 : : * An LP_DEAD line pointer must be left behind when the original item
1745 : : * (which is dead to everybody) could still be referenced by a TID in
1746 : : * an index. This should never be necessary with any individual
1747 : : * heap-only tuple item, though. (It's not clear how much of a problem
1748 : : * that would be, but there is no reason to allow it.)
1749 : : */
1592 pg@bowt.ie 1750 [ + + ]: 1628001 : if (ItemIdHasStorage(lp))
1751 : : {
1752 [ - + ]: 1616650 : Assert(ItemIdIsNormal(lp));
1753 : 1616650 : htup = (HeapTupleHeader) PageGetItem(page, lp);
1754 [ - + ]: 1616650 : Assert(!HeapTupleHeaderIsHeapOnly(htup));
1755 : : }
1756 : : else
1757 : : {
1758 : : /* Whole HOT chain becomes dead */
1759 [ - + ]: 11351 : Assert(ItemIdIsRedirected(lp));
1760 : : }
1761 : : #endif
1762 : :
6581 tgl@sss.pgh.pa.us 1763 : 1628001 : ItemIdSetDead(lp);
1764 : : }
1765 : :
1766 : : /* Update all now-unused line pointers */
1767 : 57353 : offnum = nowunused;
1592 pg@bowt.ie 1768 [ + + ]: 277589 : for (int i = 0; i < nunused; i++)
1769 : : {
6581 tgl@sss.pgh.pa.us 1770 : 220236 : OffsetNumber off = *offnum++;
1771 : 220236 : ItemId lp = PageGetItemId(page, off);
1772 : :
1773 : : #ifdef USE_ASSERT_CHECKING
1774 : :
720 heikki.linnakangas@i 1775 [ + + ]: 220236 : if (lp_truncate_only)
1776 : : {
1777 : : /* Setting LP_DEAD to LP_UNUSED in vacuum's second pass */
1778 [ + - - + ]: 104885 : Assert(ItemIdIsDead(lp) && !ItemIdHasStorage(lp));
1779 : : }
1780 : : else
1781 : : {
1782 : : /*
1783 : : * When heap_page_prune_and_freeze() was called, mark_unused_now
1784 : : * may have been passed as true, which allows would-be LP_DEAD
1785 : : * items to be made LP_UNUSED instead. This is only possible if
1786 : : * the relation has no indexes. If there are any dead items, then
1787 : : * mark_unused_now was not true and every item being marked
1788 : : * LP_UNUSED must refer to a heap-only tuple.
1789 : : */
1790 [ + + ]: 115351 : if (ndead > 0)
1791 : : {
1792 [ + - - + ]: 51800 : Assert(ItemIdHasStorage(lp) && ItemIdIsNormal(lp));
1793 : 51800 : htup = (HeapTupleHeader) PageGetItem(page, lp);
1794 [ - + ]: 51800 : Assert(HeapTupleHeaderIsHeapOnly(htup));
1795 : : }
1796 : : else
1797 [ - + ]: 63551 : Assert(ItemIdIsUsed(lp));
1798 : : }
1799 : :
1800 : : #endif
1801 : :
6581 tgl@sss.pgh.pa.us 1802 : 220236 : ItemIdSetUnused(lp);
1803 : : }
1804 : :
720 heikki.linnakangas@i 1805 [ + + ]: 57353 : if (lp_truncate_only)
1806 : 1665 : PageTruncateLinePointerArray(page);
1807 : : else
1808 : : {
1809 : : /*
1810 : : * Finally, repair any fragmentation, and update the page's hint bit
1811 : : * about whether it has free pointers.
1812 : : */
1813 : 55688 : PageRepairFragmentation(page);
1814 : :
1815 : : /*
1816 : : * Now that the page has been modified, assert that redirect items
1817 : : * still point to valid targets.
1818 : : */
1819 : 55688 : page_verify_redirects(page);
1820 : : }
1574 andres@anarazel.de 1821 : 57353 : }
1822 : :
1823 : :
1824 : : /*
1825 : : * If built with assertions, verify that all LP_REDIRECT items point to a
1826 : : * valid item.
1827 : : *
1828 : : * One way that bugs related to HOT pruning show is redirect items pointing to
1829 : : * removed tuples. It's not trivial to reliably check that marking an item
1830 : : * unused will not orphan a redirect item during heap_prune_chain() /
1831 : : * heap_page_prune_execute(), so we additionally check the whole page after
1832 : : * pruning. Without this check such bugs would typically only cause asserts
1833 : : * later, potentially well after the corruption has been introduced.
1834 : : *
1835 : : * Also check comments in heap_page_prune_execute()'s redirection loop.
1836 : : */
1837 : : static void
1838 : 55688 : page_verify_redirects(Page page)
1839 : : {
1840 : : #ifdef USE_ASSERT_CHECKING
1841 : : OffsetNumber offnum;
1842 : : OffsetNumber maxoff;
1843 : :
1844 : 55688 : maxoff = PageGetMaxOffsetNumber(page);
1845 : 55688 : for (offnum = FirstOffsetNumber;
1846 [ + + ]: 4712108 : offnum <= maxoff;
1847 : 4656420 : offnum = OffsetNumberNext(offnum))
1848 : : {
1849 : 4656420 : ItemId itemid = PageGetItemId(page, offnum);
1850 : : OffsetNumber targoff;
1851 : : ItemId targitem;
1852 : : HeapTupleHeader htup;
1853 : :
1854 [ + + ]: 4656420 : if (!ItemIdIsRedirected(itemid))
1855 : 4458126 : continue;
1856 : :
1857 : 198294 : targoff = ItemIdGetRedirect(itemid);
1858 : 198294 : targitem = PageGetItemId(page, targoff);
1859 : :
1860 [ - + ]: 198294 : Assert(ItemIdIsUsed(targitem));
1861 [ - + ]: 198294 : Assert(ItemIdIsNormal(targitem));
1862 [ - + ]: 198294 : Assert(ItemIdHasStorage(targitem));
1863 : 198294 : htup = (HeapTupleHeader) PageGetItem(page, targitem);
1864 [ - + ]: 198294 : Assert(HeapTupleHeaderIsHeapOnly(htup));
1865 : : }
1866 : : #endif
6751 tgl@sss.pgh.pa.us 1867 : 55688 : }
1868 : :
1869 : :
1870 : : /*
1871 : : * For all items in this page, find their respective root line pointers.
1872 : : * If item k is part of a HOT-chain with root at item j, then we set
1873 : : * root_offsets[k - 1] = j.
1874 : : *
1875 : : * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
1876 : : * Unused entries are filled with InvalidOffsetNumber (zero).
1877 : : *
1878 : : * The function must be called with at least share lock on the buffer, to
1879 : : * prevent concurrent prune operations.
1880 : : *
1881 : : * Note: The information collected here is valid only as long as the caller
1882 : : * holds a pin on the buffer. Once pin is released, a tuple might be pruned
1883 : : * and reused by a completely unrelated tuple.
1884 : : */
1885 : : void
1886 : 118019 : heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
1887 : : {
1888 : : OffsetNumber offnum,
1889 : : maxoff;
1890 : :
2040 alvherre@alvh.no-ip. 1891 [ + - - + : 118019 : MemSet(root_offsets, InvalidOffsetNumber,
- - - - -
- ]
1892 : : MaxHeapTuplesPerPage * sizeof(OffsetNumber));
1893 : :
6751 tgl@sss.pgh.pa.us 1894 : 118019 : maxoff = PageGetMaxOffsetNumber(page);
6515 bruce@momjian.us 1895 [ + + ]: 10023956 : for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
1896 : : {
6695 1897 : 9905937 : ItemId lp = PageGetItemId(page, offnum);
1898 : : HeapTupleHeader htup;
1899 : : OffsetNumber nextoffnum;
1900 : : TransactionId priorXmax;
1901 : :
1902 : : /* skip unused and dead items */
6751 tgl@sss.pgh.pa.us 1903 [ + + + + ]: 9905937 : if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
1904 : 11117 : continue;
1905 : :
1906 [ + + ]: 9894820 : if (ItemIdIsNormal(lp))
1907 : : {
1908 : 9891562 : htup = (HeapTupleHeader) PageGetItem(page, lp);
1909 : :
1910 : : /*
1911 : : * Check if this tuple is part of a HOT-chain rooted at some other
1912 : : * tuple. If so, skip it for now; we'll process it when we find
1913 : : * its root.
1914 : : */
1915 [ + + ]: 9891562 : if (HeapTupleHeaderIsHeapOnly(htup))
1916 : 3612 : continue;
1917 : :
1918 : : /*
1919 : : * This is either a plain tuple or the root of a HOT-chain.
1920 : : * Remember it in the mapping.
1921 : : */
1922 : 9887950 : root_offsets[offnum - 1] = offnum;
1923 : :
1924 : : /* If it's not the start of a HOT-chain, we're done with it */
1925 [ + + ]: 9887950 : if (!HeapTupleHeaderIsHotUpdated(htup))
1926 : 9887705 : continue;
1927 : :
1928 : : /* Set up to scan the HOT-chain */
1929 : 245 : nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
4799 alvherre@alvh.no-ip. 1930 : 245 : priorXmax = HeapTupleHeaderGetUpdateXid(htup);
1931 : : }
1932 : : else
1933 : : {
1934 : : /* Must be a redirect item. We do not set its root_offsets entry */
6751 tgl@sss.pgh.pa.us 1935 [ - + ]: 3258 : Assert(ItemIdIsRedirected(lp));
1936 : : /* Set up to scan the HOT-chain */
1937 : 3258 : nextoffnum = ItemIdGetRedirect(lp);
1938 : 3258 : priorXmax = InvalidTransactionId;
1939 : : }
1940 : :
1941 : : /*
1942 : : * Now follow the HOT-chain and collect other tuples in the chain.
1943 : : *
1944 : : * Note: Even though this is a nested loop, the complexity of the
1945 : : * function is O(N) because a tuple in the page should be visited not
1946 : : * more than twice, once in the outer loop and once in HOT-chain
1947 : : * chases.
1948 : : */
1949 : : for (;;)
1950 : : {
1951 : : /* Sanity check (pure paranoia) */
1635 pg@bowt.ie 1952 [ - + ]: 3609 : if (offnum < FirstOffsetNumber)
1635 pg@bowt.ie 1953 :UBC 0 : break;
1954 : :
1955 : : /*
1956 : : * An offset past the end of page's line pointer array is possible
1957 : : * when the array was truncated
1958 : : */
1635 pg@bowt.ie 1959 [ - + ]:CBC 3609 : if (offnum > maxoff)
1803 pg@bowt.ie 1960 :UBC 0 : break;
1961 : :
6751 tgl@sss.pgh.pa.us 1962 :CBC 3609 : lp = PageGetItemId(page, nextoffnum);
1963 : :
1964 : : /* Check for broken chains */
1965 [ - + ]: 3609 : if (!ItemIdIsNormal(lp))
6751 tgl@sss.pgh.pa.us 1966 :UBC 0 : break;
1967 : :
6751 tgl@sss.pgh.pa.us 1968 :CBC 3609 : htup = (HeapTupleHeader) PageGetItem(page, lp);
1969 : :
1970 [ + + - + ]: 3960 : if (TransactionIdIsValid(priorXmax) &&
3055 alvherre@alvh.no-ip. 1971 : 351 : !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
6751 tgl@sss.pgh.pa.us 1972 :UBC 0 : break;
1973 : :
1974 : : /* Remember the root line pointer for this item */
6751 tgl@sss.pgh.pa.us 1975 :CBC 3609 : root_offsets[nextoffnum - 1] = offnum;
1976 : :
1977 : : /* Advance to next chain member, if any */
1978 [ + + ]: 3609 : if (!HeapTupleHeaderIsHotUpdated(htup))
1979 : 3503 : break;
1980 : :
1981 : : /* HOT implies it can't have moved to different partition */
2899 andres@anarazel.de 1982 [ - + ]: 106 : Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
1983 : :
6751 tgl@sss.pgh.pa.us 1984 : 106 : nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
4799 alvherre@alvh.no-ip. 1985 : 106 : priorXmax = HeapTupleHeaderGetUpdateXid(htup);
1986 : : }
1987 : : }
6751 tgl@sss.pgh.pa.us 1988 : 118019 : }
1989 : :
1990 : :
1991 : : /*
1992 : : * Compare fields that describe actions required to freeze tuple with caller's
1993 : : * open plan. If everything matches then the frz tuple plan is equivalent to
1994 : : * caller's plan.
1995 : : */
1996 : : static inline bool
720 heikki.linnakangas@i 1997 : 912536 : heap_log_freeze_eq(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
1998 : : {
1999 [ + + ]: 912536 : if (plan->xmax == frz->xmax &&
2000 [ + + ]: 912508 : plan->t_infomask2 == frz->t_infomask2 &&
2001 [ + + ]: 911596 : plan->t_infomask == frz->t_infomask &&
2002 [ + - ]: 908910 : plan->frzflags == frz->frzflags)
2003 : 908910 : return true;
2004 : :
2005 : : /* Caller must call heap_log_freeze_new_plan again for frz */
2006 : 3626 : return false;
2007 : : }
2008 : :
2009 : : /*
2010 : : * Comparator used to deduplicate the freeze plans used in WAL records.
2011 : : */
2012 : : static int
2013 : 1111861 : heap_log_freeze_cmp(const void *arg1, const void *arg2)
2014 : : {
62 peter@eisentraut.org 2015 :GNC 1111861 : const HeapTupleFreeze *frz1 = arg1;
2016 : 1111861 : const HeapTupleFreeze *frz2 = arg2;
2017 : :
720 heikki.linnakangas@i 2018 [ + + ]:CBC 1111861 : if (frz1->xmax < frz2->xmax)
2019 : 51 : return -1;
2020 [ + + ]: 1111810 : else if (frz1->xmax > frz2->xmax)
2021 : 67 : return 1;
2022 : :
2023 [ + + ]: 1111743 : if (frz1->t_infomask2 < frz2->t_infomask2)
2024 : 3979 : return -1;
2025 [ + + ]: 1107764 : else if (frz1->t_infomask2 > frz2->t_infomask2)
2026 : 5586 : return 1;
2027 : :
2028 [ + + ]: 1102178 : if (frz1->t_infomask < frz2->t_infomask)
2029 : 11245 : return -1;
2030 [ + + ]: 1090933 : else if (frz1->t_infomask > frz2->t_infomask)
2031 : 17269 : return 1;
2032 : :
2033 [ - + ]: 1073664 : if (frz1->frzflags < frz2->frzflags)
720 heikki.linnakangas@i 2034 :UBC 0 : return -1;
720 heikki.linnakangas@i 2035 [ - + ]:CBC 1073664 : else if (frz1->frzflags > frz2->frzflags)
720 heikki.linnakangas@i 2036 :UBC 0 : return 1;
2037 : :
2038 : : /*
2039 : : * heap_log_freeze_eq would consider these tuple-wise plans to be equal.
2040 : : * (So the tuples will share a single canonical freeze plan.)
2041 : : *
2042 : : * We tiebreak on page offset number to keep each freeze plan's page
2043 : : * offset number array individually sorted. (Unnecessary, but be tidy.)
2044 : : */
720 heikki.linnakangas@i 2045 [ + + ]:CBC 1073664 : if (frz1->offset < frz2->offset)
2046 : 988452 : return -1;
2047 [ + - ]: 85212 : else if (frz1->offset > frz2->offset)
2048 : 85212 : return 1;
2049 : :
720 heikki.linnakangas@i 2050 :UBC 0 : Assert(false);
2051 : : return 0;
2052 : : }
2053 : :
2054 : : /*
2055 : : * Start new plan initialized using tuple-level actions. At least one tuple
2056 : : * will have steps required to freeze described by caller's plan during REDO.
2057 : : */
2058 : : static inline void
720 heikki.linnakangas@i 2059 :CBC 23859 : heap_log_freeze_new_plan(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
2060 : : {
2061 : 23859 : plan->xmax = frz->xmax;
2062 : 23859 : plan->t_infomask2 = frz->t_infomask2;
2063 : 23859 : plan->t_infomask = frz->t_infomask;
2064 : 23859 : plan->frzflags = frz->frzflags;
2065 : 23859 : plan->ntuples = 1; /* for now */
2066 : 23859 : }
2067 : :
2068 : : /*
2069 : : * Deduplicate tuple-based freeze plans so that each distinct set of
2070 : : * processing steps is only stored once in the WAL record.
2071 : : * Called during original execution of freezing (for logged relations).
2072 : : *
2073 : : * Return value is number of plans set in *plans_out for caller. Also writes
2074 : : * an array of offset numbers into *offsets_out output argument for caller
2075 : : * (actually there is one array per freeze plan, but that's not of immediate
2076 : : * concern to our caller).
2077 : : */
2078 : : static int
2079 : 20233 : heap_log_freeze_plan(HeapTupleFreeze *tuples, int ntuples,
2080 : : xlhp_freeze_plan *plans_out,
2081 : : OffsetNumber *offsets_out)
2082 : : {
2083 : 20233 : int nplans = 0;
2084 : :
2085 : : /* Sort tuple-based freeze plans in the order required to deduplicate */
2086 : 20233 : qsort(tuples, ntuples, sizeof(HeapTupleFreeze), heap_log_freeze_cmp);
2087 : :
2088 [ + + ]: 953002 : for (int i = 0; i < ntuples; i++)
2089 : : {
2090 : 932769 : HeapTupleFreeze *frz = tuples + i;
2091 : :
2092 [ + + ]: 932769 : if (i == 0)
2093 : : {
2094 : : /* New canonical freeze plan starting with first tup */
2095 : 20233 : heap_log_freeze_new_plan(plans_out, frz);
2096 : 20233 : nplans++;
2097 : : }
2098 [ + + ]: 912536 : else if (heap_log_freeze_eq(plans_out, frz))
2099 : : {
2100 : : /* tup matches open canonical plan -- include tup in it */
2101 [ - + ]: 908910 : Assert(offsets_out[i - 1] < frz->offset);
2102 : 908910 : plans_out->ntuples++;
2103 : : }
2104 : : else
2105 : : {
2106 : : /* Tup doesn't match current plan -- done with it now */
2107 : 3626 : plans_out++;
2108 : :
2109 : : /* New canonical freeze plan starting with this tup */
2110 : 3626 : heap_log_freeze_new_plan(plans_out, frz);
2111 : 3626 : nplans++;
2112 : : }
2113 : :
2114 : : /*
2115 : : * Save page offset number in dedicated buffer in passing.
2116 : : *
2117 : : * REDO routine relies on the record's offset numbers array grouping
2118 : : * offset numbers by freeze plan. The sort order within each grouping
2119 : : * is ascending offset number order, just to keep things tidy.
2120 : : */
2121 : 932769 : offsets_out[i] = frz->offset;
2122 : : }
2123 : :
2124 [ + - - + ]: 20233 : Assert(nplans > 0 && nplans <= ntuples);
2125 : :
2126 : 20233 : return nplans;
2127 : : }
2128 : :
2129 : : /*
2130 : : * Write an XLOG_HEAP2_PRUNE* WAL record
2131 : : *
2132 : : * This is used for several different page maintenance operations:
2133 : : *
2134 : : * - Page pruning, in VACUUM's 1st pass or on access: Some items are
2135 : : * redirected, some marked dead, and some removed altogether.
2136 : : *
2137 : : * - Freezing: Items are marked as 'frozen'.
2138 : : *
2139 : : * - Vacuum, 2nd pass: Items that are already LP_DEAD are marked as unused.
2140 : : *
2141 : : * They have enough commonalities that we use a single WAL record for them
2142 : : * all.
2143 : : *
2144 : : * If replaying the record requires a cleanup lock, pass cleanup_lock = true.
2145 : : * Replaying 'redirected' or 'dead' items always requires a cleanup lock, but
2146 : : * replaying 'unused' items depends on whether they were all previously marked
2147 : : * as dead.
2148 : : *
2149 : : * If the VM is being updated, vmflags will contain the bits to set. In this
2150 : : * case, vmbuffer should already have been updated and marked dirty and should
2151 : : * still be pinned and locked.
2152 : : *
2153 : : * Note: This function scribbles on the 'frozen' array.
2154 : : *
2155 : : * Note: This is called in a critical section, so careful what you do here.
2156 : : */
2157 : : void
2158 : 80433 : log_heap_prune_and_freeze(Relation relation, Buffer buffer,
2159 : : Buffer vmbuffer, uint8 vmflags,
2160 : : TransactionId conflict_xid,
2161 : : bool cleanup_lock,
2162 : : PruneReason reason,
2163 : : HeapTupleFreeze *frozen, int nfrozen,
2164 : : OffsetNumber *redirected, int nredirected,
2165 : : OffsetNumber *dead, int ndead,
2166 : : OffsetNumber *unused, int nunused)
2167 : : {
2168 : : xl_heap_prune xlrec;
2169 : : XLogRecPtr recptr;
2170 : : uint8 info;
2171 : : uint8 regbuf_flags_heap;
2172 : :
2173 : : /* The following local variables hold data registered in the WAL record: */
2174 : : xlhp_freeze_plan plans[MaxHeapTuplesPerPage];
2175 : : xlhp_freeze_plans freeze_plans;
2176 : : xlhp_prune_items redirect_items;
2177 : : xlhp_prune_items dead_items;
2178 : : xlhp_prune_items unused_items;
2179 : : OffsetNumber frz_offsets[MaxHeapTuplesPerPage];
153 melanieplageman@gmai 2180 [ + + + + :GNC 80433 : bool do_prune = nredirected > 0 || ndead > 0 || nunused > 0;
+ + ]
2181 : 80433 : bool do_set_vm = vmflags & VISIBILITYMAP_VALID_BITS;
2182 : :
2183 [ - + ]: 80433 : Assert((vmflags & VISIBILITYMAP_VALID_BITS) == vmflags);
2184 : :
720 heikki.linnakangas@i 2185 :CBC 80433 : xlrec.flags = 0;
153 melanieplageman@gmai 2186 :GNC 80433 : regbuf_flags_heap = REGBUF_STANDARD;
2187 : :
2188 : : /*
2189 : : * We can avoid an FPI of the heap page if the only modification we are
2190 : : * making to it is to set PD_ALL_VISIBLE and checksums/wal_log_hints are
2191 : : * disabled. Note that if we explicitly skip an FPI, we must not stamp the
2192 : : * heap page with this record's LSN. Recovery skips records <= the stamped
2193 : : * LSN, so this could lead to skipping an earlier FPI needed to repair a
2194 : : * torn page.
2195 : : */
2196 [ + + - + ]: 80433 : if (!do_prune &&
153 melanieplageman@gmai 2197 :UNC 0 : nfrozen == 0 &&
2198 [ # # # # : 0 : (!do_set_vm || !XLogHintBitIsNeeded()))
# # ]
2199 : 0 : regbuf_flags_heap |= REGBUF_NO_IMAGE;
2200 : :
2201 : : /*
2202 : : * Prepare data for the buffer. The arrays are not actually in the
2203 : : * buffer, but we pretend that they are. When XLogInsert stores a full
2204 : : * page image, the arrays can be omitted.
2205 : : */
720 heikki.linnakangas@i 2206 :CBC 80433 : XLogBeginInsert();
153 melanieplageman@gmai 2207 :GNC 80433 : XLogRegisterBuffer(0, buffer, regbuf_flags_heap);
2208 : :
2209 [ + + ]: 80433 : if (do_set_vm)
2210 : 14842 : XLogRegisterBuffer(1, vmbuffer, 0);
2211 : :
720 heikki.linnakangas@i 2212 [ + + ]:CBC 80433 : if (nfrozen > 0)
2213 : : {
2214 : : int nplans;
2215 : :
2216 : 20233 : xlrec.flags |= XLHP_HAS_FREEZE_PLANS;
2217 : :
2218 : : /*
2219 : : * Prepare deduplicated representation for use in the WAL record. This
2220 : : * destructively sorts frozen tuples array in-place.
2221 : : */
2222 : 20233 : nplans = heap_log_freeze_plan(frozen, nfrozen, plans, frz_offsets);
2223 : :
2224 : 20233 : freeze_plans.nplans = nplans;
397 peter@eisentraut.org 2225 : 20233 : XLogRegisterBufData(0, &freeze_plans,
2226 : : offsetof(xlhp_freeze_plans, plans));
2227 : 20233 : XLogRegisterBufData(0, plans,
2228 : : sizeof(xlhp_freeze_plan) * nplans);
2229 : : }
720 heikki.linnakangas@i 2230 [ + + ]: 80433 : if (nredirected > 0)
2231 : : {
2232 : 14404 : xlrec.flags |= XLHP_HAS_REDIRECTIONS;
2233 : :
2234 : 14404 : redirect_items.ntargets = nredirected;
397 peter@eisentraut.org 2235 : 14404 : XLogRegisterBufData(0, &redirect_items,
2236 : : offsetof(xlhp_prune_items, data));
2237 : 14404 : XLogRegisterBufData(0, redirected,
2238 : : sizeof(OffsetNumber[2]) * nredirected);
2239 : : }
720 heikki.linnakangas@i 2240 [ + + ]: 80433 : if (ndead > 0)
2241 : : {
2242 : 37050 : xlrec.flags |= XLHP_HAS_DEAD_ITEMS;
2243 : :
2244 : 37050 : dead_items.ntargets = ndead;
397 peter@eisentraut.org 2245 : 37050 : XLogRegisterBufData(0, &dead_items,
2246 : : offsetof(xlhp_prune_items, data));
2247 : 37050 : XLogRegisterBufData(0, dead,
2248 : : sizeof(OffsetNumber) * ndead);
2249 : : }
720 heikki.linnakangas@i 2250 [ + + ]: 80433 : if (nunused > 0)
2251 : : {
2252 : 26585 : xlrec.flags |= XLHP_HAS_NOW_UNUSED_ITEMS;
2253 : :
2254 : 26585 : unused_items.ntargets = nunused;
397 peter@eisentraut.org 2255 : 26585 : XLogRegisterBufData(0, &unused_items,
2256 : : offsetof(xlhp_prune_items, data));
2257 : 26585 : XLogRegisterBufData(0, unused,
2258 : : sizeof(OffsetNumber) * nunused);
2259 : : }
720 heikki.linnakangas@i 2260 [ + + ]: 80433 : if (nfrozen > 0)
397 peter@eisentraut.org 2261 : 20233 : XLogRegisterBufData(0, frz_offsets,
2262 : : sizeof(OffsetNumber) * nfrozen);
2263 : :
2264 : : /*
2265 : : * Prepare the main xl_heap_prune record. We already set the XLHP_HAS_*
2266 : : * flag above.
2267 : : */
153 melanieplageman@gmai 2268 [ + + ]:GNC 80433 : if (vmflags & VISIBILITYMAP_ALL_VISIBLE)
2269 : : {
2270 : 14842 : xlrec.flags |= XLHP_VM_ALL_VISIBLE;
2271 [ + + ]: 14842 : if (vmflags & VISIBILITYMAP_ALL_FROZEN)
2272 : 11109 : xlrec.flags |= XLHP_VM_ALL_FROZEN;
2273 : : }
720 heikki.linnakangas@i 2274 [ + + - + :CBC 80433 : if (RelationIsAccessibleInLogicalDecoding(relation))
+ - - + -
- - - + +
+ + - + -
- + - ]
2275 : 600 : xlrec.flags |= XLHP_IS_CATALOG_REL;
2276 [ + + ]: 80433 : if (TransactionIdIsValid(conflict_xid))
2277 : 64004 : xlrec.flags |= XLHP_HAS_CONFLICT_HORIZON;
2278 [ + + ]: 80433 : if (cleanup_lock)
2279 : 65411 : xlrec.flags |= XLHP_CLEANUP_LOCK;
2280 : : else
2281 : : {
2282 [ + - - + ]: 15022 : Assert(nredirected == 0 && ndead == 0);
2283 : : /* also, any items in 'unused' must've been LP_DEAD previously */
2284 : : }
397 peter@eisentraut.org 2285 : 80433 : XLogRegisterData(&xlrec, SizeOfHeapPrune);
720 heikki.linnakangas@i 2286 [ + + ]: 80433 : if (TransactionIdIsValid(conflict_xid))
397 peter@eisentraut.org 2287 : 64004 : XLogRegisterData(&conflict_xid, sizeof(TransactionId));
2288 : :
720 heikki.linnakangas@i 2289 [ + + + - ]: 80433 : switch (reason)
2290 : : {
2291 : 37308 : case PRUNE_ON_ACCESS:
2292 : 37308 : info = XLOG_HEAP2_PRUNE_ON_ACCESS;
2293 : 37308 : break;
2294 : 28103 : case PRUNE_VACUUM_SCAN:
2295 : 28103 : info = XLOG_HEAP2_PRUNE_VACUUM_SCAN;
2296 : 28103 : break;
2297 : 15022 : case PRUNE_VACUUM_CLEANUP:
2298 : 15022 : info = XLOG_HEAP2_PRUNE_VACUUM_CLEANUP;
2299 : 15022 : break;
720 heikki.linnakangas@i 2300 :UBC 0 : default:
2301 [ # # ]: 0 : elog(ERROR, "unrecognized prune reason: %d", (int) reason);
2302 : : break;
2303 : : }
720 heikki.linnakangas@i 2304 :CBC 80433 : recptr = XLogInsert(RM_HEAP2_ID, info);
2305 : :
153 melanieplageman@gmai 2306 [ + + ]:GNC 80433 : if (do_set_vm)
2307 : : {
2308 [ - + ]: 14842 : Assert(BufferIsDirty(vmbuffer));
2309 : 14842 : PageSetLSN(BufferGetPage(vmbuffer), recptr);
2310 : : }
2311 : :
2312 : : /*
2313 : : * See comment at the top of the function about regbuf_flags_heap for
2314 : : * details on when we can advance the page LSN.
2315 : : */
2316 [ + + - + : 80433 : if (do_prune || nfrozen > 0 || (do_set_vm && XLogHintBitIsNeeded()))
- - - - -
- ]
2317 : : {
2318 [ - + ]: 80433 : Assert(BufferIsDirty(buffer));
2319 : 80433 : PageSetLSN(BufferGetPage(buffer), recptr);
2320 : : }
720 heikki.linnakangas@i 2321 :CBC 80433 : }
|