1 /*-------------------------------------------------------------------------
4 * heap page pruning and HOT-chain management code
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/access/heap/pruneheap.c,v 1.25 2010/07/06 19:18:55 momjian Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "access/htup.h"
19 #include "access/transam.h"
20 #include "miscadmin.h"
22 #include "storage/bufmgr.h"
23 #include "storage/off.h"
24 #include "utils/rel.h"
25 #include "utils/tqual.h"
28 /* Working data for heap_page_prune and subroutines */
31 TransactionId new_prune_xid; /* new prune hint value for page */
32 TransactionId latestRemovedXid; /* latest xid to be removed by this
34 int nredirected; /* numbers of entries in arrays below */
37 /* arrays that accumulate indexes of items to be changed */
38 OffsetNumber redirected[MaxHeapTuplesPerPage * 2];
39 OffsetNumber nowdead[MaxHeapTuplesPerPage];
40 OffsetNumber nowunused[MaxHeapTuplesPerPage];
41 /* marked[i] is TRUE if item i is entered in one of the above arrays */
42 bool marked[MaxHeapTuplesPerPage + 1];
46 static int heap_prune_chain(Relation relation, Buffer buffer,
47 OffsetNumber rootoffnum,
48 TransactionId OldestXmin,
50 static void heap_prune_record_prunable(PruneState *prstate, TransactionId xid);
51 static void heap_prune_record_redirect(PruneState *prstate,
52 OffsetNumber offnum, OffsetNumber rdoffnum);
53 static void heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum);
54 static void heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum);
58 * Optionally prune and repair fragmentation in the specified page.
60 * This is an opportunistic function. It will perform housekeeping
61 * only if the page heuristically looks like a candidate for pruning and we
62 * can acquire buffer cleanup lock without blocking.
64 * Note: this is called quite often. It's important that it fall out quickly
65 * if there's not any use in pruning.
67 * Caller must have pin on the buffer, and must *not* have a lock on it.
69 * OldestXmin is the cutoff XID used to distinguish whether tuples are DEAD
70 * or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum).
73 heap_page_prune_opt(Relation relation, Buffer buffer, TransactionId OldestXmin)
75 Page page = BufferGetPage(buffer);
79 * Let's see if we really need pruning.
81 * Forget it if page is not hinted to contain something prunable that's
82 * older than OldestXmin.
84 if (!PageIsPrunable(page, OldestXmin))
88 * We can't write WAL in recovery mode, so there's no point trying to
89 * clean the page. The master will likely issue a cleaning WAL record soon
90 * anyway, so this is no particular loss.
92 if (RecoveryInProgress())
96 * We prune when a previous UPDATE failed to find enough space on the page
97 * for a new tuple version, or when free space falls below the relation's
98 * fill-factor target (but not less than 10%).
100 * Checking free space here is questionable since we aren't holding any
101 * lock on the buffer; in the worst case we could get a bogus answer. It's
102 * unlikely to be *seriously* wrong, though, since reading either pd_lower
103 * or pd_upper is probably atomic. Avoiding taking a lock seems more
104 * important than sometimes getting a wrong answer in what is after all
105 * just a heuristic estimate.
107 minfree = RelationGetTargetPageFreeSpace(relation,
108 HEAP_DEFAULT_FILLFACTOR);
109 minfree = Max(minfree, BLCKSZ / 10);
111 if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
113 /* OK, try to get exclusive buffer lock */
114 if (!ConditionalLockBufferForCleanup(buffer))
118 * Now that we have buffer lock, get accurate information about the
119 * page's free space, and recheck the heuristic about whether to
120 * prune. (We needn't recheck PageIsPrunable, since no one else could
121 * have pruned while we hold pin.)
123 if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
125 TransactionId ignore = InvalidTransactionId; /* return value not
129 (void) heap_page_prune(relation, buffer, OldestXmin, true, &ignore);
132 /* And release buffer lock */
133 LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
139 * Prune and repair fragmentation in the specified page.
141 * Caller must have pin and buffer cleanup lock on the page.
143 * OldestXmin is the cutoff XID used to distinguish whether tuples are DEAD
144 * or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum).
146 * If report_stats is true then we send the number of reclaimed heap-only
147 * tuples to pgstats. (This must be FALSE during vacuum, since vacuum will
148 * send its own new total to pgstats, and we don't want this delta applied
151 * Returns the number of tuples deleted from the page and sets
155 heap_page_prune(Relation relation, Buffer buffer, TransactionId OldestXmin,
156 bool report_stats, TransactionId *latestRemovedXid)
159 Page page = BufferGetPage(buffer);
165 * Our strategy is to scan the page and make lists of items to change,
166 * then apply the changes within a critical section. This keeps as much
167 * logic as possible out of the critical section, and also ensures that
168 * WAL replay will work the same as the normal case.
170 * First, initialize the new pd_prune_xid value to zero (indicating no
171 * prunable tuples). If we find any tuples which may soon become
172 * prunable, we will save the lowest relevant XID in new_prune_xid. Also
173 * initialize the rest of our working state.
175 prstate.new_prune_xid = InvalidTransactionId;
176 prstate.latestRemovedXid = InvalidTransactionId;
177 prstate.nredirected = prstate.ndead = prstate.nunused = 0;
178 memset(prstate.marked, 0, sizeof(prstate.marked));
181 maxoff = PageGetMaxOffsetNumber(page);
182 for (offnum = FirstOffsetNumber;
184 offnum = OffsetNumberNext(offnum))
188 /* Ignore items already processed as part of an earlier chain */
189 if (prstate.marked[offnum])
192 /* Nothing to do if slot is empty or already dead */
193 itemid = PageGetItemId(page, offnum);
194 if (!ItemIdIsUsed(itemid) || ItemIdIsDead(itemid))
197 /* Process this item or chain of items */
198 ndeleted += heap_prune_chain(relation, buffer, offnum,
203 /* Any error while applying the changes is critical */
204 START_CRIT_SECTION();
206 /* Have we found any prunable items? */
207 if (prstate.nredirected > 0 || prstate.ndead > 0 || prstate.nunused > 0)
210 * Apply the planned item changes, then repair page fragmentation, and
211 * update the page's hint bit about whether it has free line pointers.
213 heap_page_prune_execute(buffer,
214 prstate.redirected, prstate.nredirected,
215 prstate.nowdead, prstate.ndead,
216 prstate.nowunused, prstate.nunused);
219 * Update the page's pd_prune_xid field to either zero, or the lowest
220 * XID of any soon-prunable tuple.
222 ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;
225 * Also clear the "page is full" flag, since there's no point in
226 * repeating the prune/defrag process until something else happens to
231 MarkBufferDirty(buffer);
234 * Emit a WAL HEAP_CLEAN record showing what we did
236 if (!relation->rd_istemp)
240 recptr = log_heap_clean(relation, buffer,
241 prstate.redirected, prstate.nredirected,
242 prstate.nowdead, prstate.ndead,
243 prstate.nowunused, prstate.nunused,
244 prstate.latestRemovedXid);
246 PageSetLSN(BufferGetPage(buffer), recptr);
247 PageSetTLI(BufferGetPage(buffer), ThisTimeLineID);
253 * If we didn't prune anything, but have found a new value for the
254 * pd_prune_xid field, update it and mark the buffer dirty. This is
255 * treated as a non-WAL-logged hint.
257 * Also clear the "page is full" flag if it is set, since there's no
258 * point in repeating the prune/defrag process until something else
259 * happens to the page.
261 if (((PageHeader) page)->pd_prune_xid != prstate.new_prune_xid ||
264 ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;
266 SetBufferCommitInfoNeedsSave(buffer);
273 * If requested, report the number of tuples reclaimed to pgstats. This is
274 * ndeleted minus ndead, because we don't want to count a now-DEAD root
275 * item as a deletion for this purpose.
277 if (report_stats && ndeleted > prstate.ndead)
278 pgstat_update_heap_dead_tuples(relation, ndeleted - prstate.ndead);
280 *latestRemovedXid = prstate.latestRemovedXid;
283 * XXX Should we update the FSM information of this page ?
285 * There are two schools of thought here. We may not want to update FSM
286 * information so that the page is not used for unrelated UPDATEs/INSERTs
287 * and any free space in this page will remain available for further
288 * UPDATEs in *this* page, thus improving chances for doing HOT updates.
290 * But for a large table and where a page does not receive further UPDATEs
291 * for a long time, we might waste this space by not updating the FSM
292 * information. The relation may get extended and fragmented further.
294 * One possibility is to leave "fillfactor" worth of space in this page
295 * and update FSM with the remaining space.
297 * In any case, the current FSM implementation doesn't accept
298 * one-page-at-a-time updates, so this is all academic for now.
306 * Prune specified item pointer or a HOT chain originating at that item.
308 * If the item is an index-referenced tuple (i.e. not a heap-only tuple),
309 * the HOT chain is pruned by removing all DEAD tuples at the start of the HOT
310 * chain. We also prune any RECENTLY_DEAD tuples preceding a DEAD tuple.
311 * This is OK because a RECENTLY_DEAD tuple preceding a DEAD tuple is really
312 * DEAD, the OldestXmin test is just too coarse to detect it.
314 * The root line pointer is redirected to the tuple immediately after the
315 * latest DEAD tuple. If all tuples in the chain are DEAD, the root line
316 * pointer is marked LP_DEAD. (This includes the case of a DEAD simple
317 * tuple, which we treat as a chain of length 1.)
319 * OldestXmin is the cutoff XID used to identify dead tuples.
321 * We don't actually change the page here, except perhaps for hint-bit updates
322 * caused by HeapTupleSatisfiesVacuum. We just add entries to the arrays in
323 * prstate showing the changes to be made. Items to be redirected are added
324 * to the redirected[] array (two entries per redirection); items to be set to
325 * LP_DEAD state are added to nowdead[]; and items to be set to LP_UNUSED
326 * state are added to nowunused[].
328 * Returns the number of tuples (to be) deleted from the page.
331 heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum,
332 TransactionId OldestXmin,
336 Page dp = (Page) BufferGetPage(buffer);
337 TransactionId priorXmax = InvalidTransactionId;
339 HeapTupleHeader htup;
340 OffsetNumber latestdead = InvalidOffsetNumber,
341 maxoff = PageGetMaxOffsetNumber(dp),
343 OffsetNumber chainitems[MaxHeapTuplesPerPage];
347 rootlp = PageGetItemId(dp, rootoffnum);
350 * If it's a heap-only tuple, then it is not the start of a HOT chain.
352 if (ItemIdIsNormal(rootlp))
354 htup = (HeapTupleHeader) PageGetItem(dp, rootlp);
355 if (HeapTupleHeaderIsHeapOnly(htup))
358 * If the tuple is DEAD and doesn't chain to anything else, mark
359 * it unused immediately. (If it does chain, we can only remove
360 * it as part of pruning its chain.)
362 * We need this primarily to handle aborted HOT updates, that is,
363 * XMIN_INVALID heap-only tuples. Those might not be linked to by
364 * any chain, since the parent tuple might be re-updated before
365 * any pruning occurs. So we have to be able to reap them
366 * separately from chain-pruning. (Note that
367 * HeapTupleHeaderIsHotUpdated will never return true for an
368 * XMIN_INVALID tuple, so this code will work even when there were
369 * sequential updates within the aborted transaction.)
371 * Note that we might first arrive at a dead heap-only tuple
372 * either here or while following a chain below. Whichever path
373 * gets there first will mark the tuple unused.
375 if (HeapTupleSatisfiesVacuum(htup, OldestXmin, buffer)
376 == HEAPTUPLE_DEAD && !HeapTupleHeaderIsHotUpdated(htup))
378 heap_prune_record_unused(prstate, rootoffnum);
379 HeapTupleHeaderAdvanceLatestRemovedXid(htup,
380 &prstate->latestRemovedXid);
384 /* Nothing more to do */
389 /* Start from the root tuple */
392 /* while not end of the chain */
399 /* Some sanity checks */
400 if (offnum < FirstOffsetNumber || offnum > maxoff)
403 /* If item is already processed, stop --- it must not be same chain */
404 if (prstate->marked[offnum])
407 lp = PageGetItemId(dp, offnum);
409 /* Unused item obviously isn't part of the chain */
410 if (!ItemIdIsUsed(lp))
414 * If we are looking at the redirected root line pointer, jump to the
415 * first normal tuple in the chain. If we find a redirect somewhere
416 * else, stop --- it must not be same chain.
418 if (ItemIdIsRedirected(lp))
421 break; /* not at start of chain */
422 chainitems[nchain++] = offnum;
423 offnum = ItemIdGetRedirect(rootlp);
428 * Likewise, a dead item pointer can't be part of the chain. (We
429 * already eliminated the case of dead root tuple outside this
432 if (ItemIdIsDead(lp))
435 Assert(ItemIdIsNormal(lp));
436 htup = (HeapTupleHeader) PageGetItem(dp, lp);
439 * Check the tuple XMIN against prior XMAX, if any
441 if (TransactionIdIsValid(priorXmax) &&
442 !TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
446 * OK, this tuple is indeed a member of the chain.
448 chainitems[nchain++] = offnum;
451 * Check tuple's visibility status.
453 tupdead = recent_dead = false;
455 switch (HeapTupleSatisfiesVacuum(htup, OldestXmin, buffer))
461 case HEAPTUPLE_RECENTLY_DEAD:
465 * This tuple may soon become DEAD. Update the hint field so
466 * that the page is reconsidered for pruning in future.
468 heap_prune_record_prunable(prstate,
469 HeapTupleHeaderGetXmax(htup));
472 case HEAPTUPLE_DELETE_IN_PROGRESS:
475 * This tuple may soon become DEAD. Update the hint field so
476 * that the page is reconsidered for pruning in future.
478 heap_prune_record_prunable(prstate,
479 HeapTupleHeaderGetXmax(htup));
483 case HEAPTUPLE_INSERT_IN_PROGRESS:
486 * If we wanted to optimize for aborts, we might consider
487 * marking the page prunable when we see INSERT_IN_PROGRESS.
488 * But we don't. See related decisions about when to mark the
489 * page prunable in heapam.c.
494 elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
499 * Remember the last DEAD tuple seen. We will advance past
500 * RECENTLY_DEAD tuples just in case there's a DEAD one after them;
501 * but we can't advance past anything else. (XXX is it really worth
502 * continuing to scan beyond RECENTLY_DEAD? The case where we will
503 * find another DEAD tuple is a fairly unusual corner case.)
508 HeapTupleHeaderAdvanceLatestRemovedXid(htup,
509 &prstate->latestRemovedXid);
511 else if (!recent_dead)
515 * If the tuple is not HOT-updated, then we are at the end of this
518 if (!HeapTupleHeaderIsHotUpdated(htup))
522 * Advance to next chain member.
524 Assert(ItemPointerGetBlockNumber(&htup->t_ctid) ==
525 BufferGetBlockNumber(buffer));
526 offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
527 priorXmax = HeapTupleHeaderGetXmax(htup);
531 * If we found a DEAD tuple in the chain, adjust the HOT chain so that all
532 * the DEAD tuples at the start of the chain are removed and the root line
533 * pointer is appropriately redirected.
535 if (OffsetNumberIsValid(latestdead))
538 * Mark as unused each intermediate item that we are able to remove
541 * When the previous item is the last dead tuple seen, we are at the
542 * right candidate for redirection.
544 for (i = 1; (i < nchain) && (chainitems[i - 1] != latestdead); i++)
546 heap_prune_record_unused(prstate, chainitems[i]);
551 * If the root entry had been a normal tuple, we are deleting it, so
552 * count it in the result. But changing a redirect (even to DEAD
553 * state) doesn't count.
555 if (ItemIdIsNormal(rootlp))
559 * If the DEAD tuple is at the end of the chain, the entire chain is
560 * dead and the root line pointer can be marked dead. Otherwise just
561 * redirect the root to the correct chain member.
564 heap_prune_record_dead(prstate, rootoffnum);
566 heap_prune_record_redirect(prstate, rootoffnum, chainitems[i]);
568 else if (nchain < 2 && ItemIdIsRedirected(rootlp))
571 * We found a redirect item that doesn't point to a valid follow-on
572 * item. This can happen if the loop in heap_page_prune caused us to
573 * visit the dead successor of a redirect item before visiting the
574 * redirect item. We can clean up by setting the redirect item to
577 heap_prune_record_dead(prstate, rootoffnum);
583 /* Record lowest soon-prunable XID */
585 heap_prune_record_prunable(PruneState *prstate, TransactionId xid)
588 * This should exactly match the PageSetPrunable macro. We can't store
589 * directly into the page header yet, so we update working state.
591 Assert(TransactionIdIsNormal(xid));
592 if (!TransactionIdIsValid(prstate->new_prune_xid) ||
593 TransactionIdPrecedes(xid, prstate->new_prune_xid))
594 prstate->new_prune_xid = xid;
597 /* Record item pointer to be redirected */
599 heap_prune_record_redirect(PruneState *prstate,
600 OffsetNumber offnum, OffsetNumber rdoffnum)
602 Assert(prstate->nredirected < MaxHeapTuplesPerPage);
603 prstate->redirected[prstate->nredirected * 2] = offnum;
604 prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
605 prstate->nredirected++;
606 Assert(!prstate->marked[offnum]);
607 prstate->marked[offnum] = true;
608 Assert(!prstate->marked[rdoffnum]);
609 prstate->marked[rdoffnum] = true;
612 /* Record item pointer to be marked dead */
614 heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum)
616 Assert(prstate->ndead < MaxHeapTuplesPerPage);
617 prstate->nowdead[prstate->ndead] = offnum;
619 Assert(!prstate->marked[offnum]);
620 prstate->marked[offnum] = true;
623 /* Record item pointer to be marked unused */
625 heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum)
627 Assert(prstate->nunused < MaxHeapTuplesPerPage);
628 prstate->nowunused[prstate->nunused] = offnum;
630 Assert(!prstate->marked[offnum]);
631 prstate->marked[offnum] = true;
636 * Perform the actual page changes needed by heap_page_prune.
637 * It is expected that the caller has suitable pin and lock on the
638 * buffer, and is inside a critical section.
640 * This is split out because it is also used by heap_xlog_clean()
641 * to replay the WAL record when needed after a crash. Note that the
642 * arguments are identical to those of log_heap_clean().
645 heap_page_prune_execute(Buffer buffer,
646 OffsetNumber *redirected, int nredirected,
647 OffsetNumber *nowdead, int ndead,
648 OffsetNumber *nowunused, int nunused)
650 Page page = (Page) BufferGetPage(buffer);
651 OffsetNumber *offnum;
654 /* Update all redirected line pointers */
656 for (i = 0; i < nredirected; i++)
658 OffsetNumber fromoff = *offnum++;
659 OffsetNumber tooff = *offnum++;
660 ItemId fromlp = PageGetItemId(page, fromoff);
662 ItemIdSetRedirect(fromlp, tooff);
665 /* Update all now-dead line pointers */
667 for (i = 0; i < ndead; i++)
669 OffsetNumber off = *offnum++;
670 ItemId lp = PageGetItemId(page, off);
675 /* Update all now-unused line pointers */
677 for (i = 0; i < nunused; i++)
679 OffsetNumber off = *offnum++;
680 ItemId lp = PageGetItemId(page, off);
686 * Finally, repair any fragmentation, and update the page's hint bit about
687 * whether it has free pointers.
689 PageRepairFragmentation(page);
694 * For all items in this page, find their respective root line pointers.
695 * If item k is part of a HOT-chain with root at item j, then we set
696 * root_offsets[k - 1] = j.
698 * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
699 * We zero out all unused entries.
701 * The function must be called with at least share lock on the buffer, to
702 * prevent concurrent prune operations.
704 * Note: The information collected here is valid only as long as the caller
705 * holds a pin on the buffer. Once pin is released, a tuple might be pruned
706 * and reused by a completely unrelated tuple.
709 heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
714 MemSet(root_offsets, 0, MaxHeapTuplesPerPage * sizeof(OffsetNumber));
716 maxoff = PageGetMaxOffsetNumber(page);
717 for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
719 ItemId lp = PageGetItemId(page, offnum);
720 HeapTupleHeader htup;
721 OffsetNumber nextoffnum;
722 TransactionId priorXmax;
724 /* skip unused and dead items */
725 if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
728 if (ItemIdIsNormal(lp))
730 htup = (HeapTupleHeader) PageGetItem(page, lp);
733 * Check if this tuple is part of a HOT-chain rooted at some other
734 * tuple. If so, skip it for now; we'll process it when we find
737 if (HeapTupleHeaderIsHeapOnly(htup))
741 * This is either a plain tuple or the root of a HOT-chain.
742 * Remember it in the mapping.
744 root_offsets[offnum - 1] = offnum;
746 /* If it's not the start of a HOT-chain, we're done with it */
747 if (!HeapTupleHeaderIsHotUpdated(htup))
750 /* Set up to scan the HOT-chain */
751 nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
752 priorXmax = HeapTupleHeaderGetXmax(htup);
756 /* Must be a redirect item. We do not set its root_offsets entry */
757 Assert(ItemIdIsRedirected(lp));
758 /* Set up to scan the HOT-chain */
759 nextoffnum = ItemIdGetRedirect(lp);
760 priorXmax = InvalidTransactionId;
764 * Now follow the HOT-chain and collect other tuples in the chain.
766 * Note: Even though this is a nested loop, the complexity of the
767 * function is O(N) because a tuple in the page should be visited not
768 * more than twice, once in the outer loop and once in HOT-chain
773 lp = PageGetItemId(page, nextoffnum);
775 /* Check for broken chains */
776 if (!ItemIdIsNormal(lp))
779 htup = (HeapTupleHeader) PageGetItem(page, lp);
781 if (TransactionIdIsValid(priorXmax) &&
782 !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
785 /* Remember the root line pointer for this item */
786 root_offsets[nextoffnum - 1] = offnum;
788 /* Advance to next chain member, if any */
789 if (!HeapTupleHeaderIsHotUpdated(htup))
792 nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
793 priorXmax = HeapTupleHeaderGetXmax(htup);