BloomNewBuffer(Relation index)
{
Buffer buffer;
- bool needLock;
/* First, try to get a page from FSM */
for (;;)
}
/* Must extend the file */
- needLock = !RELATION_IS_LOCAL(index);
- if (needLock)
- LockRelationForExtension(index, ExclusiveLock);
-
- buffer = ReadBuffer(index, P_NEW);
- LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
-
- if (needLock)
- UnlockRelationForExtension(index, ExclusiveLock);
+ buffer = ExtendBufferedRel(EB_REL(index), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
return buffer;
}
* whole relation will be rolled back.
*/
- meta = ReadBuffer(index, P_NEW);
+ meta = ExtendBufferedRel(EB_REL(index), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
Assert(BufferGetBlockNumber(meta) == BRIN_METAPAGE_BLKNO);
- LockBuffer(meta, BUFFER_LOCK_EXCLUSIVE);
brin_metapage_init(BufferGetPage(meta), BrinGetPagesPerRange(index),
BRIN_CURRENT_VERSION);
Buffer metabuf;
/* An empty BRIN index has a metapage only. */
- metabuf =
- ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
- LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
+ metabuf = ExtendBufferedRel(EB_REL(index), INIT_FORKNUM, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
/* Initialize and xlog metabuffer. */
START_CRIT_SECTION();
* There's not enough free space in any existing index page,
* according to the FSM: extend the relation to obtain a shiny new
* page.
+ *
+ * XXX: It's likely possible to use RBM_ZERO_AND_LOCK here,
+ * which'd avoid the need to hold the extension lock during buffer
+ * reclaim.
*/
if (!RELATION_IS_LOCAL(irel))
{
BlockNumber mapBlk;
BlockNumber nblocks;
Relation irel = revmap->rm_irel;
- bool needLock = !RELATION_IS_LOCAL(irel);
/*
* Lock the metapage. This locks out concurrent extensions of the revmap,
}
else
{
- if (needLock)
- LockRelationForExtension(irel, ExclusiveLock);
-
- buf = ReadBuffer(irel, P_NEW);
+ buf = ExtendBufferedRel(EB_REL(irel), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
if (BufferGetBlockNumber(buf) != mapBlk)
{
/*
* up and have caller start over. We will have to evacuate that
* page from under whoever is using it.
*/
- if (needLock)
- UnlockRelationForExtension(irel, ExclusiveLock);
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_UNLOCK);
- ReleaseBuffer(buf);
+ UnlockReleaseBuffer(buf);
return;
}
- LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
page = BufferGetPage(buf);
-
- if (needLock)
- UnlockRelationForExtension(irel, ExclusiveLock);
}
/* Check that it's a regular block (or an empty page) */
MetaBuffer;
/* An empty GIN index has two pages. */
- MetaBuffer =
- ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
- LockBuffer(MetaBuffer, BUFFER_LOCK_EXCLUSIVE);
- RootBuffer =
- ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
- LockBuffer(RootBuffer, BUFFER_LOCK_EXCLUSIVE);
+ MetaBuffer = ExtendBufferedRel(EB_REL(index), INIT_FORKNUM, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
+ RootBuffer = ExtendBufferedRel(EB_REL(index), INIT_FORKNUM, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
/* Initialize and xlog metabuffer and root buffer. */
START_CRIT_SECTION();
GinNewBuffer(Relation index)
{
Buffer buffer;
- bool needLock;
/* First, try to get a page from FSM */
for (;;)
}
/* Must extend the file */
- needLock = !RELATION_IS_LOCAL(index);
- if (needLock)
- LockRelationForExtension(index, ExclusiveLock);
-
- buffer = ReadBuffer(index, P_NEW);
- LockBuffer(buffer, GIN_EXCLUSIVE);
-
- if (needLock)
- UnlockRelationForExtension(index, ExclusiveLock);
+ buffer = ExtendBufferedRel(EB_REL(index), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
return buffer;
}
Buffer buffer;
/* Initialize the root page */
- buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
- LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+ buffer = ExtendBufferedRel(EB_REL(index), INIT_FORKNUM, NULL,
+ EB_SKIP_EXTENSION_LOCK | EB_LOCK_FIRST);
/* Initialize and xlog buffer */
START_CRIT_SECTION();
gistNewBuffer(Relation r, Relation heaprel)
{
Buffer buffer;
- bool needLock;
/* First, try to get a page from FSM */
for (;;)
}
/* Must extend the file */
- needLock = !RELATION_IS_LOCAL(r);
-
- if (needLock)
- LockRelationForExtension(r, ExclusiveLock);
-
- buffer = ReadBuffer(r, P_NEW);
- LockBuffer(buffer, GIST_EXCLUSIVE);
-
- if (needLock)
- UnlockRelationForExtension(r, ExclusiveLock);
+ buffer = ExtendBufferedRel(EB_REL(r), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
return buffer;
}
elog(ERROR, "access to noncontiguous page in hash index \"%s\"",
RelationGetRelationName(rel));
- /* smgr insists we use P_NEW to extend the relation */
+ /* smgr insists we explicitly extend the relation */
if (blkno == nblocks)
{
- buf = ReadBufferExtended(rel, forkNum, P_NEW, RBM_NORMAL, NULL);
+ buf = ExtendBufferedRel(EB_REL(rel), forkNum, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
if (BufferGetBlockNumber(buf) != blkno)
elog(ERROR, "unexpected hash relation size: %u, should be %u",
BufferGetBlockNumber(buf), blkno);
- LockBuffer(buf, HASH_WRITE);
}
else
{
}
else
{
- bool needLock;
Page page;
Assert(access == BT_WRITE);
}
/*
- * Extend the relation by one page.
- *
- * We have to use a lock to ensure no one else is extending the rel at
- * the same time, else we will both try to initialize the same new
- * page. We can skip locking for new or temp relations, however,
- * since no one else could be accessing them.
- */
- needLock = !RELATION_IS_LOCAL(rel);
-
- if (needLock)
- LockRelationForExtension(rel, ExclusiveLock);
-
- buf = ReadBuffer(rel, P_NEW);
-
- /* Acquire buffer lock on new page */
- _bt_lockbuf(rel, buf, BT_WRITE);
-
- /*
- * Release the file-extension lock; it's now OK for someone else to
- * extend the relation some more. Note that we cannot release this
- * lock before we have buffer lock on the new page, or we risk a race
- * condition against btvacuumscan --- see comments therein.
+ * Extend the relation by one page. Need to use RBM_ZERO_AND_LOCK or
+ * we risk a race condition against btvacuumscan --- see comments
+ * therein. This forces us to repeat the valgrind request that
+ * _bt_lockbuf() otherwise would make, as we can't use _bt_lockbuf()
+ * without introducing a race.
*/
- if (needLock)
- UnlockRelationForExtension(rel, ExclusiveLock);
+ buf = ExtendBufferedRel(EB_REL(rel), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
+ if (!RelationUsesLocalBuffers(rel))
+ VALGRIND_MAKE_MEM_DEFINED(BufferGetPage(buf), BLCKSZ);
/* Initialize the new page before returning it */
page = BufferGetPage(buf);
* write-lock on the left page before it adds a right page, so we must
* already have processed any tuples due to be moved into such a page.
*
+ * XXX: Now that new pages are locked with RBM_ZERO_AND_LOCK, I don't
+ * think the use of the extension lock is still required.
+ *
* We can skip locking for new or temp relations, however, since no one
* else could be accessing them.
*/
SpGistNewBuffer(Relation index)
{
Buffer buffer;
- bool needLock;
/* First, try to get a page from FSM */
for (;;)
ReleaseBuffer(buffer);
}
- /* Must extend the file */
- needLock = !RELATION_IS_LOCAL(index);
- if (needLock)
- LockRelationForExtension(index, ExclusiveLock);
-
- buffer = ReadBuffer(index, P_NEW);
- LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
-
- if (needLock)
- UnlockRelationForExtension(index, ExclusiveLock);
+ buffer = ExtendBufferedRel(EB_REL(index), MAIN_FORKNUM, NULL,
+ EB_LOCK_FIRST);
return buffer;
}
/* Initialize first page of relation with special magic number */
- buf = ReadBufferExtended(rel, forkNum, P_NEW, RBM_ZERO_AND_LOCK, NULL);
+ buf = ExtendBufferedRel(EB_REL(rel), forkNum, NULL,
+ EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK);
Assert(BufferGetBlockNumber(buf) == 0);
page = BufferGetPage(buf);