aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/heap/hio.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2005-05-07 21:32:24 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2005-05-07 21:32:24 +0000
commit30f540be4381d8cd4e45f8393264e1c214d0ddd7 (patch)
tree411bb8bb7c0361842f2b305135bb0b44cbeb2ed2 /src/backend/access/heap/hio.c
parentb72e5fa17b7aef18c744060ede3b1afdf4021dbf (diff)
downloadpostgresql-30f540be4381d8cd4e45f8393264e1c214d0ddd7.tar.gz
postgresql-30f540be4381d8cd4e45f8393264e1c214d0ddd7.zip
Repair very-low-probability race condition between relation extension
and VACUUM: in the interval between adding a new page to the relation and formatting it, it was possible for VACUUM to come along and decide it should format the page too. Though not harmful in itself, this would cause data loss if a third transaction were able to insert tuples into the vacuumed page before the original extender got control back.
Diffstat (limited to 'src/backend/access/heap/hio.c')
-rw-r--r--src/backend/access/heap/hio.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index db0bda6699b..583bb209336 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/heap/hio.c,v 1.55 2005/04/29 22:28:23 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/heap/hio.c,v 1.56 2005/05/07 21:32:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -242,13 +242,6 @@ RelationGetBufferForTuple(Relation relation, Size len,
buffer = ReadBuffer(relation, P_NEW);
/*
- * Release the file-extension lock; it's now OK for someone else to
- * extend the relation some more.
- */
- if (needLock)
- UnlockRelationForExtension(relation, ExclusiveLock);
-
- /*
* We can be certain that locking the otherBuffer first is OK, since
* it must have a lower page number.
*/
@@ -256,9 +249,22 @@ RelationGetBufferForTuple(Relation relation, Size len,
LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
/*
- * We need to initialize the empty new page.
+ * Now acquire lock on the new page.
*/
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+
+ /*
+ * 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 vacuumlazy.c --- see comments therein.
+ */
+ if (needLock)
+ UnlockRelationForExtension(relation, ExclusiveLock);
+
+ /*
+ * We need to initialize the empty new page.
+ */
pageHeader = (Page) BufferGetPage(buffer);
Assert(PageIsNew((PageHeader) pageHeader));
PageInit(pageHeader, BufferGetPageSize(buffer), 0);