1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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.
This commit is contained in:
Tom Lane
2005-05-07 21:32:24 +00:00
parent b72e5fa17b
commit 30f540be43
4 changed files with 73 additions and 18 deletions

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.128 2005/05/06 17:24:52 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.129 2005/05/07 21:32:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -739,11 +739,35 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
BlockNumber pages_deleted = 0;
MemoryContext mycontext;
MemoryContext oldcontext;
bool needLock;
Assert(stats != NULL);
/*
* First find out the number of pages in the index. We must acquire
* the relation-extension lock while doing this to avoid a race
* condition: if someone else is extending the relation, there is
* a window where bufmgr/smgr have created a new all-zero page but
* it hasn't yet been write-locked by _bt_getbuf(). If we manage to
* scan such a page here, we'll improperly assume it can be recycled.
* Taking the lock synchronizes things enough to prevent a problem:
* either num_pages won't include the new page, or _bt_getbuf already
* has write lock on the buffer and it will be fully initialized before
* we can examine it. (See also vacuumlazy.c, which has the same issue.)
*
* 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);
num_pages = RelationGetNumberOfBlocks(rel);
if (needLock)
UnlockRelationForExtension(rel, ExclusiveLock);
/* No point in remembering more than MaxFSMPages pages */
maxFreePages = MaxFSMPages;
if ((BlockNumber) maxFreePages > num_pages)