PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pruneheap.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/heapam_xlog.h"
#include "access/transam.h"
#include "access/htup_details.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "utils/snapmgr.h"
#include "utils/rel.h"
#include "utils/tqual.h"
Include dependency graph for pruneheap.c:

Go to the source code of this file.

Data Structures

struct  PruneState
 

Functions

static int heap_prune_chain (Relation relation, Buffer buffer, OffsetNumber rootoffnum, TransactionId OldestXmin, PruneState *prstate)
 
static void heap_prune_record_prunable (PruneState *prstate, TransactionId xid)
 
static void heap_prune_record_redirect (PruneState *prstate, OffsetNumber offnum, OffsetNumber rdoffnum)
 
static void heap_prune_record_dead (PruneState *prstate, OffsetNumber offnum)
 
static void heap_prune_record_unused (PruneState *prstate, OffsetNumber offnum)
 
void heap_page_prune_opt (Relation relation, Buffer buffer)
 
int heap_page_prune (Relation relation, Buffer buffer, TransactionId OldestXmin, bool report_stats, TransactionId *latestRemovedXid)
 
void heap_page_prune_execute (Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused)
 
void heap_get_root_tuples (Page page, OffsetNumber *root_offsets)
 

Function Documentation

void heap_get_root_tuples ( Page  page,
OffsetNumber root_offsets 
)

Definition at line 743 of file pruneheap.c.

References Assert, FirstOffsetNumber, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIsHeapOnly, HeapTupleHeaderIsHotUpdated, InvalidTransactionId, ItemIdGetRedirect, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerGetOffsetNumber, MaxHeapTuplesPerPage, MemSet, OffsetNumberNext, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, HeapTupleHeaderData::t_ctid, TransactionIdEquals, and TransactionIdIsValid.

Referenced by IndexBuildHeapRangeScan(), and validate_index_heapscan().

744 {
745  OffsetNumber offnum,
746  maxoff;
747 
748  MemSet(root_offsets, 0, MaxHeapTuplesPerPage * sizeof(OffsetNumber));
749 
750  maxoff = PageGetMaxOffsetNumber(page);
751  for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
752  {
753  ItemId lp = PageGetItemId(page, offnum);
754  HeapTupleHeader htup;
755  OffsetNumber nextoffnum;
756  TransactionId priorXmax;
757 
758  /* skip unused and dead items */
759  if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
760  continue;
761 
762  if (ItemIdIsNormal(lp))
763  {
764  htup = (HeapTupleHeader) PageGetItem(page, lp);
765 
766  /*
767  * Check if this tuple is part of a HOT-chain rooted at some other
768  * tuple. If so, skip it for now; we'll process it when we find
769  * its root.
770  */
771  if (HeapTupleHeaderIsHeapOnly(htup))
772  continue;
773 
774  /*
775  * This is either a plain tuple or the root of a HOT-chain.
776  * Remember it in the mapping.
777  */
778  root_offsets[offnum - 1] = offnum;
779 
780  /* If it's not the start of a HOT-chain, we're done with it */
781  if (!HeapTupleHeaderIsHotUpdated(htup))
782  continue;
783 
784  /* Set up to scan the HOT-chain */
785  nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
786  priorXmax = HeapTupleHeaderGetUpdateXid(htup);
787  }
788  else
789  {
790  /* Must be a redirect item. We do not set its root_offsets entry */
792  /* Set up to scan the HOT-chain */
793  nextoffnum = ItemIdGetRedirect(lp);
794  priorXmax = InvalidTransactionId;
795  }
796 
797  /*
798  * Now follow the HOT-chain and collect other tuples in the chain.
799  *
800  * Note: Even though this is a nested loop, the complexity of the
801  * function is O(N) because a tuple in the page should be visited not
802  * more than twice, once in the outer loop and once in HOT-chain
803  * chases.
804  */
805  for (;;)
806  {
807  lp = PageGetItemId(page, nextoffnum);
808 
809  /* Check for broken chains */
810  if (!ItemIdIsNormal(lp))
811  break;
812 
813  htup = (HeapTupleHeader) PageGetItem(page, lp);
814 
815  if (TransactionIdIsValid(priorXmax) &&
816  !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
817  break;
818 
819  /* Remember the root line pointer for this item */
820  root_offsets[nextoffnum - 1] = offnum;
821 
822  /* Advance to next chain member, if any */
823  if (!HeapTupleHeaderIsHotUpdated(htup))
824  break;
825 
826  nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
827  priorXmax = HeapTupleHeaderGetUpdateXid(htup);
828  }
829  }
830 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:359
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:105
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define ItemIdGetRedirect(itemId)
Definition: itemid.h:77
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
#define MemSet(start, val, len)
Definition: c.h:857
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
#define HeapTupleHeaderIsHeapOnly(tup)
Definition: htup_details.h:502
ItemPointerData t_ctid
Definition: htup_details.h:150
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidTransactionId
Definition: transam.h:31
#define HeapTupleHeaderIsHotUpdated(tup)
Definition: htup_details.h:485
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define Assert(condition)
Definition: c.h:675
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:307
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
int heap_page_prune ( Relation  relation,
Buffer  buffer,
TransactionId  OldestXmin,
bool  report_stats,
TransactionId latestRemovedXid 
)

Definition at line 181 of file pruneheap.c.

References BufferGetPage, END_CRIT_SECTION, FirstOffsetNumber, heap_page_prune_execute(), heap_prune_chain(), InvalidTransactionId, ItemIdIsDead, ItemIdIsUsed, PruneState::latestRemovedXid, log_heap_clean(), MarkBufferDirty(), MarkBufferDirtyHint(), PruneState::marked, PruneState::ndead, PruneState::new_prune_xid, PruneState::nowdead, PruneState::nowunused, PruneState::nredirected, PruneState::nunused, OffsetNumberNext, PageClearFull, PageGetItemId, PageGetMaxOffsetNumber, PageIsFull, PageSetLSN, pgstat_update_heap_dead_tuples(), PruneState::redirected, RelationNeedsWAL, and START_CRIT_SECTION.

Referenced by heap_page_prune_opt(), and lazy_scan_heap().

183 {
184  int ndeleted = 0;
185  Page page = BufferGetPage(buffer);
186  OffsetNumber offnum,
187  maxoff;
188  PruneState prstate;
189 
190  /*
191  * Our strategy is to scan the page and make lists of items to change,
192  * then apply the changes within a critical section. This keeps as much
193  * logic as possible out of the critical section, and also ensures that
194  * WAL replay will work the same as the normal case.
195  *
196  * First, initialize the new pd_prune_xid value to zero (indicating no
197  * prunable tuples). If we find any tuples which may soon become
198  * prunable, we will save the lowest relevant XID in new_prune_xid. Also
199  * initialize the rest of our working state.
200  */
202  prstate.latestRemovedXid = *latestRemovedXid;
203  prstate.nredirected = prstate.ndead = prstate.nunused = 0;
204  memset(prstate.marked, 0, sizeof(prstate.marked));
205 
206  /* Scan the page */
207  maxoff = PageGetMaxOffsetNumber(page);
208  for (offnum = FirstOffsetNumber;
209  offnum <= maxoff;
210  offnum = OffsetNumberNext(offnum))
211  {
212  ItemId itemid;
213 
214  /* Ignore items already processed as part of an earlier chain */
215  if (prstate.marked[offnum])
216  continue;
217 
218  /* Nothing to do if slot is empty or already dead */
219  itemid = PageGetItemId(page, offnum);
220  if (!ItemIdIsUsed(itemid) || ItemIdIsDead(itemid))
221  continue;
222 
223  /* Process this item or chain of items */
224  ndeleted += heap_prune_chain(relation, buffer, offnum,
225  OldestXmin,
226  &prstate);
227  }
228 
229  /* Any error while applying the changes is critical */
231 
232  /* Have we found any prunable items? */
233  if (prstate.nredirected > 0 || prstate.ndead > 0 || prstate.nunused > 0)
234  {
235  /*
236  * Apply the planned item changes, then repair page fragmentation, and
237  * update the page's hint bit about whether it has free line pointers.
238  */
240  prstate.redirected, prstate.nredirected,
241  prstate.nowdead, prstate.ndead,
242  prstate.nowunused, prstate.nunused);
243 
244  /*
245  * Update the page's pd_prune_xid field to either zero, or the lowest
246  * XID of any soon-prunable tuple.
247  */
248  ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;
249 
250  /*
251  * Also clear the "page is full" flag, since there's no point in
252  * repeating the prune/defrag process until something else happens to
253  * the page.
254  */
255  PageClearFull(page);
256 
258 
259  /*
260  * Emit a WAL HEAP_CLEAN record showing what we did
261  */
262  if (RelationNeedsWAL(relation))
263  {
264  XLogRecPtr recptr;
265 
266  recptr = log_heap_clean(relation, buffer,
267  prstate.redirected, prstate.nredirected,
268  prstate.nowdead, prstate.ndead,
269  prstate.nowunused, prstate.nunused,
270  prstate.latestRemovedXid);
271 
272  PageSetLSN(BufferGetPage(buffer), recptr);
273  }
274  }
275  else
276  {
277  /*
278  * If we didn't prune anything, but have found a new value for the
279  * pd_prune_xid field, update it and mark the buffer dirty. This is
280  * treated as a non-WAL-logged hint.
281  *
282  * Also clear the "page is full" flag if it is set, since there's no
283  * point in repeating the prune/defrag process until something else
284  * happens to the page.
285  */
286  if (((PageHeader) page)->pd_prune_xid != prstate.new_prune_xid ||
287  PageIsFull(page))
288  {
289  ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;
290  PageClearFull(page);
292  }
293  }
294 
296 
297  /*
298  * If requested, report the number of tuples reclaimed to pgstats. This is
299  * ndeleted minus ndead, because we don't want to count a now-DEAD root
300  * item as a deletion for this purpose.
301  */
302  if (report_stats && ndeleted > prstate.ndead)
303  pgstat_update_heap_dead_tuples(relation, ndeleted - prstate.ndead);
304 
305  *latestRemovedXid = prstate.latestRemovedXid;
306 
307  /*
308  * XXX Should we update the FSM information of this page ?
309  *
310  * There are two schools of thought here. We may not want to update FSM
311  * information so that the page is not used for unrelated UPDATEs/INSERTs
312  * and any free space in this page will remain available for further
313  * UPDATEs in *this* page, thus improving chances for doing HOT updates.
314  *
315  * But for a large table and where a page does not receive further UPDATEs
316  * for a long time, we might waste this space by not updating the FSM
317  * information. The relation may get extended and fragmented further.
318  *
319  * One possibility is to leave "fillfactor" worth of space in this page
320  * and update FSM with the remaining space.
321  */
322 
323  return ndeleted;
324 }
static int heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum, TransactionId OldestXmin, PruneState *prstate)
Definition: pruneheap.c:353
int nredirected
Definition: pruneheap.c:35
void pgstat_update_heap_dead_tuples(Relation rel, int delta)
Definition: pgstat.c:2037
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3379
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
#define END_CRIT_SECTION()
Definition: miscadmin.h:135
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define START_CRIT_SECTION()
Definition: miscadmin.h:133
OffsetNumber nowdead[MaxHeapTuplesPerPage]
Definition: pruneheap.c:40
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
bool marked[MaxHeapTuplesPerPage+1]
Definition: pruneheap.c:43
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
TransactionId new_prune_xid
Definition: pruneheap.c:33
#define PageIsFull(page)
Definition: bufpage.h:374
int nunused
Definition: pruneheap.c:37
uint16 OffsetNumber
Definition: off.h:24
TransactionId latestRemovedXid
Definition: pruneheap.c:34
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidTransactionId
Definition: transam.h:31
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageClearFull(page)
Definition: bufpage.h:378
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void heap_page_prune_execute(Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused)
Definition: pruneheap.c:679
PageHeaderData * PageHeader
Definition: bufpage.h:162
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
OffsetNumber nowunused[MaxHeapTuplesPerPage]
Definition: pruneheap.c:41
XLogRecPtr log_heap_clean(Relation reln, Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused, TransactionId latestRemovedXid)
Definition: heapam.c:7348
#define RelationNeedsWAL(relation)
Definition: rel.h:505
OffsetNumber redirected[MaxHeapTuplesPerPage *2]
Definition: pruneheap.c:39
int ndead
Definition: pruneheap.c:36
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
Pointer Page
Definition: bufpage.h:74
void heap_page_prune_execute ( Buffer  buffer,
OffsetNumber redirected,
int  nredirected,
OffsetNumber nowdead,
int  ndead,
OffsetNumber nowunused,
int  nunused 
)

Definition at line 679 of file pruneheap.c.

References BufferGetPage, i, ItemIdSetDead, ItemIdSetRedirect, ItemIdSetUnused, PageGetItemId, and PageRepairFragmentation().

Referenced by heap_page_prune(), and heap_xlog_clean().

683 {
684  Page page = (Page) BufferGetPage(buffer);
685  OffsetNumber *offnum;
686  int i;
687 
688  /* Update all redirected line pointers */
689  offnum = redirected;
690  for (i = 0; i < nredirected; i++)
691  {
692  OffsetNumber fromoff = *offnum++;
693  OffsetNumber tooff = *offnum++;
694  ItemId fromlp = PageGetItemId(page, fromoff);
695 
696  ItemIdSetRedirect(fromlp, tooff);
697  }
698 
699  /* Update all now-dead line pointers */
700  offnum = nowdead;
701  for (i = 0; i < ndead; i++)
702  {
703  OffsetNumber off = *offnum++;
704  ItemId lp = PageGetItemId(page, off);
705 
706  ItemIdSetDead(lp);
707  }
708 
709  /* Update all now-unused line pointers */
710  offnum = nowunused;
711  for (i = 0; i < nunused; i++)
712  {
713  OffsetNumber off = *offnum++;
714  ItemId lp = PageGetItemId(page, off);
715 
716  ItemIdSetUnused(lp);
717  }
718 
719  /*
720  * Finally, repair any fragmentation, and update the page's hint bit about
721  * whether it has free pointers.
722  */
724 }
uint16 OffsetNumber
Definition: off.h:24
#define ItemIdSetRedirect(itemId, link)
Definition: itemid.h:151
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
void PageRepairFragmentation(Page page)
Definition: bufpage.c:479
int i
#define ItemIdSetDead(itemId)
Definition: itemid.h:163
#define ItemIdSetUnused(itemId)
Definition: itemid.h:127
Pointer Page
Definition: bufpage.h:74
void heap_page_prune_opt ( Relation  relation,
Buffer  buffer 
)

Definition at line 74 of file pruneheap.c.

References Assert, BUFFER_LOCK_UNLOCK, BufferGetPage, ConditionalLockBufferForCleanup(), HEAP_DEFAULT_FILLFACTOR, heap_page_prune(), InvalidTransactionId, IsCatalogRelation(), LockBuffer(), Max, OldestXmin, PageGetHeapFreeSpace(), PageIsFull, PageIsPrunable, RecentGlobalDataXmin, RecentGlobalXmin, RecoveryInProgress(), RelationGetTargetPageFreeSpace, RelationIsAccessibleInLogicalDecoding, TransactionIdIsValid, and TransactionIdLimitedForOldSnapshots().

Referenced by bitgetpage(), heapgetpage(), and index_fetch_heap().

75 {
76  Page page = BufferGetPage(buffer);
77  Size minfree;
79 
80  /*
81  * We can't write WAL in recovery mode, so there's no point trying to
82  * clean the page. The master will likely issue a cleaning WAL record soon
83  * anyway, so this is no particular loss.
84  */
85  if (RecoveryInProgress())
86  return;
87 
88  /*
89  * Use the appropriate xmin horizon for this relation. If it's a proper
90  * catalog relation or a user defined, additional, catalog relation, we
91  * need to use the horizon that includes slots, otherwise the data-only
92  * horizon can be used. Note that the toast relation of user defined
93  * relations are *not* considered catalog relations.
94  *
95  * It is OK to apply the old snapshot limit before acquiring the cleanup
96  * lock because the worst that can happen is that we are not quite as
97  * aggressive about the cleanup (by however many transaction IDs are
98  * consumed between this point and acquiring the lock). This allows us to
99  * save significant overhead in the case where the page is found not to be
100  * prunable.
101  */
102  if (IsCatalogRelation(relation) ||
104  OldestXmin = RecentGlobalXmin;
105  else
106  OldestXmin =
108  relation);
109 
110  Assert(TransactionIdIsValid(OldestXmin));
111 
112  /*
113  * Let's see if we really need pruning.
114  *
115  * Forget it if page is not hinted to contain something prunable that's
116  * older than OldestXmin.
117  */
118  if (!PageIsPrunable(page, OldestXmin))
119  return;
120 
121  /*
122  * We prune when a previous UPDATE failed to find enough space on the page
123  * for a new tuple version, or when free space falls below the relation's
124  * fill-factor target (but not less than 10%).
125  *
126  * Checking free space here is questionable since we aren't holding any
127  * lock on the buffer; in the worst case we could get a bogus answer. It's
128  * unlikely to be *seriously* wrong, though, since reading either pd_lower
129  * or pd_upper is probably atomic. Avoiding taking a lock seems more
130  * important than sometimes getting a wrong answer in what is after all
131  * just a heuristic estimate.
132  */
133  minfree = RelationGetTargetPageFreeSpace(relation,
135  minfree = Max(minfree, BLCKSZ / 10);
136 
137  if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
138  {
139  /* OK, try to get exclusive buffer lock */
141  return;
142 
143  /*
144  * Now that we have buffer lock, get accurate information about the
145  * page's free space, and recheck the heuristic about whether to
146  * prune. (We needn't recheck PageIsPrunable, since no one else could
147  * have pruned while we hold pin.)
148  */
149  if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
150  {
151  TransactionId ignore = InvalidTransactionId; /* return value not
152  * needed */
153 
154  /* OK to prune */
155  (void) heap_page_prune(relation, buffer, OldestXmin, true, &ignore);
156  }
157 
158  /* And release buffer lock */
160  }
161 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
int heap_page_prune(Relation relation, Buffer buffer, TransactionId OldestXmin, bool report_stats, TransactionId *latestRemovedXid)
Definition: pruneheap.c:181
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:92
uint32 TransactionId
Definition: c.h:397
TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation)
Definition: snapmgr.c:1735
bool RecoveryInProgress(void)
Definition: xlog.c:7872
#define PageIsFull(page)
Definition: bufpage.h:374
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3718
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:666
TransactionId RecentGlobalXmin
Definition: snapmgr.c:166
#define InvalidTransactionId
Definition: transam.h:31
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
TransactionId RecentGlobalDataXmin
Definition: snapmgr.c:167
#define RelationGetTargetPageFreeSpace(relation, defaultff)
Definition: rel.h:307
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:559
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define Max(x, y)
Definition: c.h:800
#define Assert(condition)
Definition: c.h:675
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
size_t Size
Definition: c.h:356
#define PageIsPrunable(page, oldestxmin)
Definition: bufpage.h:388
#define HEAP_DEFAULT_FILLFACTOR
Definition: rel.h:286
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Pointer Page
Definition: bufpage.h:74
static int heap_prune_chain ( Relation  relation,
Buffer  buffer,
OffsetNumber  rootoffnum,
TransactionId  OldestXmin,
PruneState prstate 
)
static

Definition at line 353 of file pruneheap.c.

References Assert, BufferGetBlockNumber(), BufferGetPage, elog, ERROR, heap_prune_record_dead(), heap_prune_record_prunable(), heap_prune_record_redirect(), heap_prune_record_unused(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderAdvanceLatestRemovedXid(), HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIsHeapOnly, HeapTupleHeaderIsHotUpdated, HeapTupleSatisfiesVacuum(), i, InvalidOffsetNumber, InvalidTransactionId, ItemIdGetLength, ItemIdGetRedirect, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerSet, PruneState::latestRemovedXid, PruneState::marked, MaxHeapTuplesPerPage, OffsetNumberIsValid, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, RelationGetRelid, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdEquals, and TransactionIdIsValid.

Referenced by heap_page_prune().

356 {
357  int ndeleted = 0;
358  Page dp = (Page) BufferGetPage(buffer);
360  ItemId rootlp;
361  HeapTupleHeader htup;
362  OffsetNumber latestdead = InvalidOffsetNumber,
363  maxoff = PageGetMaxOffsetNumber(dp),
364  offnum;
366  int nchain = 0,
367  i;
368  HeapTupleData tup;
369 
370  tup.t_tableOid = RelationGetRelid(relation);
371 
372  rootlp = PageGetItemId(dp, rootoffnum);
373 
374  /*
375  * If it's a heap-only tuple, then it is not the start of a HOT chain.
376  */
377  if (ItemIdIsNormal(rootlp))
378  {
379  htup = (HeapTupleHeader) PageGetItem(dp, rootlp);
380 
381  tup.t_data = htup;
382  tup.t_len = ItemIdGetLength(rootlp);
383  ItemPointerSet(&(tup.t_self), BufferGetBlockNumber(buffer), rootoffnum);
384 
385  if (HeapTupleHeaderIsHeapOnly(htup))
386  {
387  /*
388  * If the tuple is DEAD and doesn't chain to anything else, mark
389  * it unused immediately. (If it does chain, we can only remove
390  * it as part of pruning its chain.)
391  *
392  * We need this primarily to handle aborted HOT updates, that is,
393  * XMIN_INVALID heap-only tuples. Those might not be linked to by
394  * any chain, since the parent tuple might be re-updated before
395  * any pruning occurs. So we have to be able to reap them
396  * separately from chain-pruning. (Note that
397  * HeapTupleHeaderIsHotUpdated will never return true for an
398  * XMIN_INVALID tuple, so this code will work even when there were
399  * sequential updates within the aborted transaction.)
400  *
401  * Note that we might first arrive at a dead heap-only tuple
402  * either here or while following a chain below. Whichever path
403  * gets there first will mark the tuple unused.
404  */
407  {
408  heap_prune_record_unused(prstate, rootoffnum);
410  &prstate->latestRemovedXid);
411  ndeleted++;
412  }
413 
414  /* Nothing more to do */
415  return ndeleted;
416  }
417  }
418 
419  /* Start from the root tuple */
420  offnum = rootoffnum;
421 
422  /* while not end of the chain */
423  for (;;)
424  {
425  ItemId lp;
426  bool tupdead,
427  recent_dead;
428 
429  /* Some sanity checks */
430  if (offnum < FirstOffsetNumber || offnum > maxoff)
431  break;
432 
433  /* If item is already processed, stop --- it must not be same chain */
434  if (prstate->marked[offnum])
435  break;
436 
437  lp = PageGetItemId(dp, offnum);
438 
439  /* Unused item obviously isn't part of the chain */
440  if (!ItemIdIsUsed(lp))
441  break;
442 
443  /*
444  * If we are looking at the redirected root line pointer, jump to the
445  * first normal tuple in the chain. If we find a redirect somewhere
446  * else, stop --- it must not be same chain.
447  */
448  if (ItemIdIsRedirected(lp))
449  {
450  if (nchain > 0)
451  break; /* not at start of chain */
452  chainitems[nchain++] = offnum;
453  offnum = ItemIdGetRedirect(rootlp);
454  continue;
455  }
456 
457  /*
458  * Likewise, a dead item pointer can't be part of the chain. (We
459  * already eliminated the case of dead root tuple outside this
460  * function.)
461  */
462  if (ItemIdIsDead(lp))
463  break;
464 
465  Assert(ItemIdIsNormal(lp));
466  htup = (HeapTupleHeader) PageGetItem(dp, lp);
467 
468  tup.t_data = htup;
469  tup.t_len = ItemIdGetLength(lp);
471 
472  /*
473  * Check the tuple XMIN against prior XMAX, if any
474  */
475  if (TransactionIdIsValid(priorXmax) &&
476  !TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
477  break;
478 
479  /*
480  * OK, this tuple is indeed a member of the chain.
481  */
482  chainitems[nchain++] = offnum;
483 
484  /*
485  * Check tuple's visibility status.
486  */
487  tupdead = recent_dead = false;
488 
490  {
491  case HEAPTUPLE_DEAD:
492  tupdead = true;
493  break;
494 
496  recent_dead = true;
497 
498  /*
499  * This tuple may soon become DEAD. Update the hint field so
500  * that the page is reconsidered for pruning in future.
501  */
504  break;
505 
507 
508  /*
509  * This tuple may soon become DEAD. Update the hint field so
510  * that the page is reconsidered for pruning in future.
511  */
514  break;
515 
516  case HEAPTUPLE_LIVE:
518 
519  /*
520  * If we wanted to optimize for aborts, we might consider
521  * marking the page prunable when we see INSERT_IN_PROGRESS.
522  * But we don't. See related decisions about when to mark the
523  * page prunable in heapam.c.
524  */
525  break;
526 
527  default:
528  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
529  break;
530  }
531 
532  /*
533  * Remember the last DEAD tuple seen. We will advance past
534  * RECENTLY_DEAD tuples just in case there's a DEAD one after them;
535  * but we can't advance past anything else. (XXX is it really worth
536  * continuing to scan beyond RECENTLY_DEAD? The case where we will
537  * find another DEAD tuple is a fairly unusual corner case.)
538  */
539  if (tupdead)
540  {
541  latestdead = offnum;
543  &prstate->latestRemovedXid);
544  }
545  else if (!recent_dead)
546  break;
547 
548  /*
549  * If the tuple is not HOT-updated, then we are at the end of this
550  * HOT-update chain.
551  */
552  if (!HeapTupleHeaderIsHotUpdated(htup))
553  break;
554 
555  /*
556  * Advance to next chain member.
557  */
560  offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
561  priorXmax = HeapTupleHeaderGetUpdateXid(htup);
562  }
563 
564  /*
565  * If we found a DEAD tuple in the chain, adjust the HOT chain so that all
566  * the DEAD tuples at the start of the chain are removed and the root line
567  * pointer is appropriately redirected.
568  */
569  if (OffsetNumberIsValid(latestdead))
570  {
571  /*
572  * Mark as unused each intermediate item that we are able to remove
573  * from the chain.
574  *
575  * When the previous item is the last dead tuple seen, we are at the
576  * right candidate for redirection.
577  */
578  for (i = 1; (i < nchain) && (chainitems[i - 1] != latestdead); i++)
579  {
580  heap_prune_record_unused(prstate, chainitems[i]);
581  ndeleted++;
582  }
583 
584  /*
585  * If the root entry had been a normal tuple, we are deleting it, so
586  * count it in the result. But changing a redirect (even to DEAD
587  * state) doesn't count.
588  */
589  if (ItemIdIsNormal(rootlp))
590  ndeleted++;
591 
592  /*
593  * If the DEAD tuple is at the end of the chain, the entire chain is
594  * dead and the root line pointer can be marked dead. Otherwise just
595  * redirect the root to the correct chain member.
596  */
597  if (i >= nchain)
598  heap_prune_record_dead(prstate, rootoffnum);
599  else
600  heap_prune_record_redirect(prstate, rootoffnum, chainitems[i]);
601  }
602  else if (nchain < 2 && ItemIdIsRedirected(rootlp))
603  {
604  /*
605  * We found a redirect item that doesn't point to a valid follow-on
606  * item. This can happen if the loop in heap_page_prune caused us to
607  * visit the dead successor of a redirect item before visiting the
608  * redirect item. We can clean up by setting the redirect item to
609  * DEAD state.
610  */
611  heap_prune_record_dead(prstate, rootoffnum);
612  }
613 
614  return ndeleted;
615 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:359
void HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple, TransactionId *latestRemovedXid)
Definition: heapam.c:7280
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:105
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define ItemIdGetRedirect(itemId)
Definition: itemid.h:77
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
Definition: tqual.c:1164
static void heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum)
Definition: pruneheap.c:648
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
bool marked[MaxHeapTuplesPerPage+1]
Definition: pruneheap.c:43
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderIsHeapOnly(tup)
Definition: htup_details.h:502
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define ERROR
Definition: elog.h:43
ItemPointerData t_ctid
Definition: htup_details.h:150
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
static void heap_prune_record_prunable(PruneState *prstate, TransactionId xid)
Definition: pruneheap.c:619
TransactionId latestRemovedXid
Definition: pruneheap.c:34
#define InvalidTransactionId
Definition: transam.h:31
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define HeapTupleHeaderIsHotUpdated(tup)
Definition: htup_details.h:485
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define InvalidOffsetNumber
Definition: off.h:26
#define Assert(condition)
Definition: c.h:675
static void heap_prune_record_redirect(PruneState *prstate, OffsetNumber offnum, OffsetNumber rdoffnum)
Definition: pruneheap.c:633
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:307
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
int i
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define elog
Definition: elog.h:219
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define RelationGetRelid(relation)
Definition: rel.h:416
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:105
static void heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum)
Definition: pruneheap.c:659
static void heap_prune_record_dead ( PruneState prstate,
OffsetNumber  offnum 
)
static

Definition at line 648 of file pruneheap.c.

References Assert, PruneState::marked, MaxHeapTuplesPerPage, PruneState::ndead, and PruneState::nowdead.

Referenced by heap_prune_chain().

649 {
650  Assert(prstate->ndead < MaxHeapTuplesPerPage);
651  prstate->nowdead[prstate->ndead] = offnum;
652  prstate->ndead++;
653  Assert(!prstate->marked[offnum]);
654  prstate->marked[offnum] = true;
655 }
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
OffsetNumber nowdead[MaxHeapTuplesPerPage]
Definition: pruneheap.c:40
bool marked[MaxHeapTuplesPerPage+1]
Definition: pruneheap.c:43
#define Assert(condition)
Definition: c.h:675
int ndead
Definition: pruneheap.c:36
static void heap_prune_record_prunable ( PruneState prstate,
TransactionId  xid 
)
static

Definition at line 619 of file pruneheap.c.

References Assert, PruneState::new_prune_xid, TransactionIdIsNormal, TransactionIdIsValid, and TransactionIdPrecedes().

Referenced by heap_prune_chain().

620 {
621  /*
622  * This should exactly match the PageSetPrunable macro. We can't store
623  * directly into the page header yet, so we update working state.
624  */
626  if (!TransactionIdIsValid(prstate->new_prune_xid) ||
627  TransactionIdPrecedes(xid, prstate->new_prune_xid))
628  prstate->new_prune_xid = xid;
629 }
TransactionId new_prune_xid
Definition: pruneheap.c:33
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:675
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
static void heap_prune_record_redirect ( PruneState prstate,
OffsetNumber  offnum,
OffsetNumber  rdoffnum 
)
static

Definition at line 633 of file pruneheap.c.

References Assert, PruneState::marked, MaxHeapTuplesPerPage, PruneState::nredirected, and PruneState::redirected.

Referenced by heap_prune_chain().

635 {
637  prstate->redirected[prstate->nredirected * 2] = offnum;
638  prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
639  prstate->nredirected++;
640  Assert(!prstate->marked[offnum]);
641  prstate->marked[offnum] = true;
642  Assert(!prstate->marked[rdoffnum]);
643  prstate->marked[rdoffnum] = true;
644 }
int nredirected
Definition: pruneheap.c:35
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
bool marked[MaxHeapTuplesPerPage+1]
Definition: pruneheap.c:43
#define Assert(condition)
Definition: c.h:675
OffsetNumber redirected[MaxHeapTuplesPerPage *2]
Definition: pruneheap.c:39
static void heap_prune_record_unused ( PruneState prstate,
OffsetNumber  offnum 
)
static

Definition at line 659 of file pruneheap.c.

References Assert, PruneState::marked, MaxHeapTuplesPerPage, PruneState::nowunused, and PruneState::nunused.

Referenced by heap_prune_chain().

660 {
661  Assert(prstate->nunused < MaxHeapTuplesPerPage);
662  prstate->nowunused[prstate->nunused] = offnum;
663  prstate->nunused++;
664  Assert(!prstate->marked[offnum]);
665  prstate->marked[offnum] = true;
666 }
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
bool marked[MaxHeapTuplesPerPage+1]
Definition: pruneheap.c:43
int nunused
Definition: pruneheap.c:37
#define Assert(condition)
Definition: c.h:675
OffsetNumber nowunused[MaxHeapTuplesPerPage]
Definition: pruneheap.c:41