From 2a3c0a769ae147d8b1b67dacc85cf923d55901ca Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 2 Nov 2022 12:23:49 -0700 Subject: [PATCH] Ensure only one backend tries to extend relation at the same time - fixes #41 --- src/ivfinsert.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ivfinsert.c b/src/ivfinsert.c index c896437..e6158e1 100644 --- a/src/ivfinsert.c +++ b/src/ivfinsert.c @@ -97,9 +97,21 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values) } else { + Buffer metabuf; + Buffer newbuf; + Page newpage; + + /* + * From ReadBufferExtended: Caller is responsible for ensuring + * that only one backend tries to extend a relation at the same + * time! + */ + metabuf = ReadBuffer(rel, IVFFLAT_METAPAGE_BLKNO); + LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE); + /* Add a new page */ - Buffer newbuf = IvfflatNewBuffer(rel, MAIN_FORKNUM); - Page newpage = GenericXLogRegisterBuffer(state, newbuf, GENERIC_XLOG_FULL_IMAGE); + newbuf = IvfflatNewBuffer(rel, MAIN_FORKNUM); + newpage = GenericXLogRegisterBuffer(state, newbuf, GENERIC_XLOG_FULL_IMAGE); insertPage = BufferGetBlockNumber(newbuf); @@ -117,6 +129,7 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values) /* Unlock */ UnlockReleaseBuffer(buf); UnlockReleaseBuffer(newbuf); + UnlockReleaseBuffer(metabuf); } }