⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pruneheap.c

📁 postgresql8.3.4源码,开源数据库
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------- * * pruneheap.c *	  heap page pruning and HOT-chain management code * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/access/heap/pruneheap.c,v 1.6.2.2 2008/03/13 18:00:39 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/heapam.h"#include "access/transam.h"#include "miscadmin.h"#include "pgstat.h"#include "utils/inval.h"/* Working data for heap_page_prune and subroutines */typedef struct{	TransactionId new_prune_xid;	/* new prune hint value for page */	int			nredirected;		/* numbers of entries in arrays below */	int			ndead;	int			nunused;	/* arrays that accumulate indexes of items to be changed */	OffsetNumber redirected[MaxHeapTuplesPerPage * 2];	OffsetNumber nowdead[MaxHeapTuplesPerPage];	OffsetNumber nowunused[MaxHeapTuplesPerPage];	/* marked[i] is TRUE if item i is entered in one of the above arrays */	bool		marked[MaxHeapTuplesPerPage + 1];} PruneState;/* Local functions */static int heap_prune_chain(Relation relation, Buffer buffer,				 OffsetNumber rootoffnum,				 TransactionId OldestXmin,				 PruneState *prstate,				 bool redirect_move);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);/* * Optionally prune and repair fragmentation in the specified page. * * This is an opportunistic function.  It will perform housekeeping * only if the page heuristically looks like a candidate for pruning and we * can acquire buffer cleanup lock without blocking. * * Note: this is called quite often.  It's important that it fall out quickly * if there's not any use in pruning. * * Caller must have pin on the buffer, and must *not* have a lock on it. * * OldestXmin is the cutoff XID used to distinguish whether tuples are DEAD * or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum). */voidheap_page_prune_opt(Relation relation, Buffer buffer, TransactionId OldestXmin){	PageHeader	dp = (PageHeader) BufferGetPage(buffer);	Size		minfree;	/*	 * Let's see if we really need pruning.	 *	 * Forget it if page is not hinted to contain something prunable that's	 * older than OldestXmin.	 */	if (!PageIsPrunable(dp, OldestXmin))		return;	/*	 * We prune when a previous UPDATE failed to find enough space on the page	 * for a new tuple version, or when free space falls below the relation's	 * fill-factor target (but not less than 10%).	 *	 * Checking free space here is questionable since we aren't holding any	 * lock on the buffer; in the worst case we could get a bogus answer. It's	 * unlikely to be *seriously* wrong, though, since reading either pd_lower	 * or pd_upper is probably atomic.	Avoiding taking a lock seems more	 * important than sometimes getting a wrong answer in what is after all	 * just a heuristic estimate.	 */	minfree = RelationGetTargetPageFreeSpace(relation,											 HEAP_DEFAULT_FILLFACTOR);	minfree = Max(minfree, BLCKSZ / 10);	if (PageIsFull(dp) || PageGetHeapFreeSpace((Page) dp) < minfree)	{		/* OK, try to get exclusive buffer lock */		if (!ConditionalLockBufferForCleanup(buffer))			return;		/*		 * Now that we have buffer lock, get accurate information about the		 * page's free space, and recheck the heuristic about whether to		 * prune. (We needn't recheck PageIsPrunable, since no one else could		 * have pruned while we hold pin.)		 */		if (PageIsFull(dp) || PageGetHeapFreeSpace((Page) dp) < minfree)		{			/* OK to prune (though not to remove redirects) */			(void) heap_page_prune(relation, buffer, OldestXmin, false, true);		}		/* And release buffer lock */		LockBuffer(buffer, BUFFER_LOCK_UNLOCK);	}}/* * Prune and repair fragmentation in the specified page. * * Caller must have pin and buffer cleanup lock on the page. * * OldestXmin is the cutoff XID used to distinguish whether tuples are DEAD * or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum). * * If redirect_move is set, we remove redirecting line pointers by * updating the root line pointer to point directly to the first non-dead * tuple in the chain.	NOTE: eliminating the redirect changes the first * tuple's effective CTID, and is therefore unsafe except within VACUUM FULL. * The only reason we support this capability at all is that by using it, * VACUUM FULL need not cope with LP_REDIRECT items at all; which seems a * good thing since VACUUM FULL is overly complicated already. * * If report_stats is true then we send the number of reclaimed heap-only * tuples to pgstats.  (This must be FALSE during vacuum, since vacuum will * send its own new total to pgstats, and we don't want this delta applied * on top of that.) * * Returns the number of tuples deleted from the page. */intheap_page_prune(Relation relation, Buffer buffer, TransactionId OldestXmin,				bool redirect_move, bool report_stats){	int			ndeleted = 0;	Page		page = BufferGetPage(buffer);	OffsetNumber offnum,				maxoff;	PruneState	prstate;	/*	 * Our strategy is to scan the page and make lists of items to change,	 * then apply the changes within a critical section.  This keeps as	 * much logic as possible out of the critical section, and also ensures	 * that WAL replay will work the same as the normal case.	 *	 * First, inform inval.c that upcoming CacheInvalidateHeapTuple calls	 * are nontransactional.	 */	if (redirect_move)		BeginNonTransactionalInvalidation();	/*	 * Initialize the new pd_prune_xid value to zero (indicating no	 * prunable tuples).  If we find any tuples which may soon become	 * prunable, we will save the lowest relevant XID in new_prune_xid.	 * Also initialize the rest of our working state.	 */	prstate.new_prune_xid = InvalidTransactionId;	prstate.nredirected = prstate.ndead = prstate.nunused = 0;	memset(prstate.marked, 0, sizeof(prstate.marked));	/* Scan the page */	maxoff = PageGetMaxOffsetNumber(page);	for (offnum = FirstOffsetNumber;		 offnum <= maxoff;		 offnum = OffsetNumberNext(offnum))	{		ItemId		itemid;		/* Ignore items already processed as part of an earlier chain */		if (prstate.marked[offnum])			continue;		/* Nothing to do if slot is empty or already dead */		itemid = PageGetItemId(page, offnum);		if (!ItemIdIsUsed(itemid) || ItemIdIsDead(itemid))			continue;		/* Process this item or chain of items */		ndeleted += heap_prune_chain(relation, buffer, offnum,									 OldestXmin,									 &prstate,									 redirect_move);	}	/*	 * Send invalidation messages for any tuples we are about to move.	 * It is safe to do this now, even though we could theoretically still	 * fail before making the actual page update, because a useless cache	 * invalidation doesn't hurt anything.  Also, no one else can reload the	 * tuples while we have exclusive buffer lock, so it's not too early to	 * send the invals.  This avoids sending the invals while inside the	 * critical section, which is a good thing for robustness.	 */	if (redirect_move)		EndNonTransactionalInvalidation();	/* Any error while applying the changes is critical */	START_CRIT_SECTION();	/* Have we found any prunable items? */	if (prstate.nredirected > 0 || prstate.ndead > 0 || prstate.nunused > 0)	{		/*		 * Apply the planned item changes, then repair page fragmentation,		 * and update the page's hint bit about whether it has free line		 * pointers.		 */		heap_page_prune_execute(relation, buffer,								prstate.redirected, prstate.nredirected,								prstate.nowdead, prstate.ndead,								prstate.nowunused, prstate.nunused,								redirect_move);		/*		 * Update the page's pd_prune_xid field to either zero, or the lowest		 * XID of any soon-prunable tuple.		 */		((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;		/*		 * Also clear the "page is full" flag, since there's no point in		 * repeating the prune/defrag process until something else happens to		 * the page.		 */		PageClearFull(page);		MarkBufferDirty(buffer);		/*		 * Emit a WAL HEAP_CLEAN or HEAP_CLEAN_MOVE record showing what we did		 */		if (!relation->rd_istemp)		{			XLogRecPtr	recptr;			recptr = log_heap_clean(relation, buffer,									prstate.redirected, prstate.nredirected,									prstate.nowdead, prstate.ndead,									prstate.nowunused, prstate.nunused,									redirect_move);			PageSetLSN(BufferGetPage(buffer), recptr);			PageSetTLI(BufferGetPage(buffer), ThisTimeLineID);		}	}	else	{		/*		 * If we didn't prune anything, but have found a new value for the		 * pd_prune_xid field, update it and mark the buffer dirty.		 * This is treated as a non-WAL-logged hint.		 *		 * Also clear the "page is full" flag if it is set, since there's no		 * point in repeating the prune/defrag process until something else		 * happens to the page.		 */		if (((PageHeader) page)->pd_prune_xid != prstate.new_prune_xid ||			PageIsFull(page))		{			((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;			PageClearFull(page);			SetBufferCommitInfoNeedsSave(buffer);		}	}	END_CRIT_SECTION();	/*	 * If requested, report the number of tuples reclaimed to pgstats. This is	 * ndeleted minus ndead, because we don't want to count a now-DEAD root	 * item as a deletion for this purpose.	 */	if (report_stats && ndeleted > prstate.ndead)		pgstat_update_heap_dead_tuples(relation, ndeleted - prstate.ndead);	/*	 * XXX Should we update the FSM information of this page ?	 *	 * There are two schools of thought here. We may not want to update FSM	 * information so that the page is not used for unrelated UPDATEs/INSERTs	 * and any free space in this page will remain available for further	 * UPDATEs in *this* page, thus improving chances for doing HOT updates.	 *	 * But for a large table and where a page does not receive further UPDATEs	 * for a long time, we might waste this space by not updating the FSM	 * information. The relation may get extended and fragmented further.	 *	 * One possibility is to leave "fillfactor" worth of space in this page	 * and update FSM with the remaining space.	 *	 * In any case, the current FSM implementation doesn't accept	 * one-page-at-a-time updates, so this is all academic for now.	 */	return ndeleted;}/* * Prune specified item pointer or a HOT chain originating at that item. * * If the item is an index-referenced tuple (i.e. not a heap-only tuple), * the HOT chain is pruned by removing all DEAD tuples at the start of the HOT * chain.  We also prune any RECENTLY_DEAD tuples preceding a DEAD tuple. * This is OK because a RECENTLY_DEAD tuple preceding a DEAD tuple is really * DEAD, the OldestXmin test is just too coarse to detect it. * * The root line pointer is redirected to the tuple immediately after the * latest DEAD tuple.  If all tuples in the chain are DEAD, the root line * pointer is marked LP_DEAD.  (This includes the case of a DEAD simple * tuple, which we treat as a chain of length 1.) * * OldestXmin is the cutoff XID used to identify dead tuples. * * We don't actually change the page here, except perhaps for hint-bit updates * caused by HeapTupleSatisfiesVacuum.  We just add entries to the arrays in * prstate showing the changes to be made.  Items to be redirected are added * to the redirected[] array (two entries per redirection); items to be set to * LP_DEAD state are added to nowdead[]; and items to be set to LP_UNUSED * state are added to nowunused[]. * * If redirect_move is true, we intend to get rid of redirecting line pointers, * not just make redirection entries. * * Returns the number of tuples (to be) deleted from the page. */static intheap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum,				 TransactionId OldestXmin,				 PruneState *prstate,				 bool redirect_move){	int			ndeleted = 0;	Page		dp = (Page) BufferGetPage(buffer);	TransactionId priorXmax = InvalidTransactionId;	ItemId		rootlp;	HeapTupleHeader htup;	OffsetNumber latestdead = InvalidOffsetNumber,				redirect_target = InvalidOffsetNumber,				maxoff = PageGetMaxOffsetNumber(dp),				offnum;	OffsetNumber chainitems[MaxHeapTuplesPerPage];	int			nchain = 0,				i;	rootlp = PageGetItemId(dp, rootoffnum);	/*	 * If it's a heap-only tuple, then it is not the start of a HOT chain.	 */	if (ItemIdIsNormal(rootlp))	{		htup = (HeapTupleHeader) PageGetItem(dp, rootlp);		if (HeapTupleHeaderIsHeapOnly(htup))		{			/*			 * If the tuple is DEAD and doesn't chain to anything else, mark			 * it unused immediately.  (If it does chain, we can only remove			 * it as part of pruning its chain.)			 *			 * We need this primarily to handle aborted HOT updates, that is,			 * XMIN_INVALID heap-only tuples.  Those might not be linked to by			 * any chain, since the parent tuple might be re-updated before			 * any pruning occurs.	So we have to be able to reap them			 * separately from chain-pruning.  (Note that			 * HeapTupleHeaderIsHotUpdated will never return true for an			 * XMIN_INVALID tuple, so this code will work even when there were			 * sequential updates within the aborted transaction.)			 *			 * Note that we might first arrive at a dead heap-only tuple			 * either here or while following a chain below.  Whichever path			 * gets there first will mark the tuple unused.			 */			if (HeapTupleSatisfiesVacuum(htup, OldestXmin, buffer)				== HEAPTUPLE_DEAD && !HeapTupleHeaderIsHotUpdated(htup))			{				heap_prune_record_unused(prstate, rootoffnum);				ndeleted++;			}			/* Nothing more to do */			return ndeleted;		}	}	/* Start from the root tuple */	offnum = rootoffnum;	/* while not end of the chain */	for (;;)	{		ItemId		lp;		bool		tupdead,					recent_dead;		/* Some sanity checks */		if (offnum < FirstOffsetNumber || offnum > maxoff)			break;		/* If item is already processed, stop --- it must not be same chain */		if (prstate->marked[offnum])			break;		lp = PageGetItemId(dp, offnum);		/* Unused item obviously isn't part of the chain */		if (!ItemIdIsUsed(lp))			break;		/*		 * If we are looking at the redirected root line pointer, jump to the		 * first normal tuple in the chain.  If we find a redirect somewhere		 * else, stop --- it must not be same chain.		 */		if (ItemIdIsRedirected(lp))		{			if (nchain > 0)				break;			/* not at start of chain */			chainitems[nchain++] = offnum;			offnum = ItemIdGetRedirect(rootlp);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -