Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 53f1ca5

Browse files
committed
Allow hint bits to be set sooner for temporary and unlogged tables.
We need not wait until the commit record is durably on disk, because in the event of a crash the page we're updating with hint bits will be gone anyway. Per off-list report from Heikki Linnakangas, this can significantly degrade the performance of unlogged tables; I was able to show a 2x speedup from this patch on a pgbench run with scale factor 15. In practice, this will mostly help small, heavily updated tables, because on larger tables you're unlikely to run into the same row again before the commit record makes it out to disk.
1 parent b6335a3 commit 53f1ca5

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/backend/storage/buffer/bufmgr.c

+29
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,35 @@ RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
19291929
return smgrnblocks(relation->rd_smgr, forkNum);
19301930
}
19311931

1932+
/*
1933+
* BufferIsPermanent
1934+
* Determines whether a buffer will potentially still be around after
1935+
* a crash. Caller must hold a buffer pin.
1936+
*/
1937+
bool
1938+
BufferIsPermanent(Buffer buffer)
1939+
{
1940+
volatile BufferDesc *bufHdr;
1941+
1942+
/* Local buffers are used only for temp relations. */
1943+
if (BufferIsLocal(buffer))
1944+
return false;
1945+
1946+
/* Make sure we've got a real buffer, and that we hold a pin on it. */
1947+
Assert(BufferIsValid(buffer));
1948+
Assert(BufferIsPinned(buffer));
1949+
1950+
/*
1951+
* BM_PERMANENT can't be changed while we hold a pin on the buffer, so
1952+
* we need not bother with the buffer header spinlock. Even if someone
1953+
* else changes the buffer header flags while we're doing this, we assume
1954+
* that changing an aligned 2-byte BufFlags value is atomic, so we'll read
1955+
* the old value or the new value, but not random garbage.
1956+
*/
1957+
bufHdr = &BufferDescriptors[buffer - 1];
1958+
return (bufHdr->flags & BM_PERMANENT) != 0;
1959+
}
1960+
19321961
/* ---------------------------------------------------------------------
19331962
* DropRelFileNodeBuffers
19341963
*

src/backend/utils/time/tqual.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ static bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
8282
* Set commit/abort hint bits on a tuple, if appropriate at this time.
8383
*
8484
* It is only safe to set a transaction-committed hint bit if we know the
85-
* transaction's commit record has been flushed to disk. We cannot change
86-
* the LSN of the page here because we may hold only a share lock on the
87-
* buffer, so we can't use the LSN to interlock this; we have to just refrain
88-
* from setting the hint bit until some future re-examination of the tuple.
85+
* transaction's commit record has been flushed to disk, or if the table is
86+
* temporary or unlogged and will be obliterated by a crash anyway. We
87+
* cannot change the LSN of the page here because we may hold only a share
88+
* lock on the buffer, so we can't use the LSN to interlock this; we have to
89+
* just refrain from setting the hint bit until some future re-examination
90+
* of the tuple.
8991
*
9092
* We can always set hint bits when marking a transaction aborted. (Some
9193
* code in heapam.c relies on that!)
@@ -113,7 +115,7 @@ SetHintBits(HeapTupleHeader tuple, Buffer buffer,
113115
/* NB: xid must be known committed here! */
114116
XLogRecPtr commitLSN = TransactionIdGetCommitLSN(xid);
115117

116-
if (XLogNeedsFlush(commitLSN))
118+
if (XLogNeedsFlush(commitLSN) && BufferIsPermanent(buffer))
117119
return; /* not flushed yet, so don't set hint */
118120
}
119121

src/include/storage/bufmgr.h

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ extern void DropDatabaseBuffers(Oid dbid);
192192
#define RelationGetNumberOfBlocks(reln) \
193193
RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
194194

195+
extern bool BufferIsPermanent(Buffer buffer);
196+
195197
#ifdef NOT_USED
196198
extern void PrintPinnedBufs(void);
197199
#endif

0 commit comments

Comments
 (0)