mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Fix longstanding recursion hazard in sinval message processing.
LockRelationOid and sibling routines supposed that, if our session already
holds the lock they were asked to acquire, they could skip calling
AcceptInvalidationMessages on the grounds that we must have already read
any remote sinval messages issued against the relation being locked.
This is normally true, but there's a critical special case where it's not:
processing inside AcceptInvalidationMessages might attempt to access system
relations, resulting in a recursive call to acquire a relation lock.
Hence, if the outer call had acquired that same system catalog lock, we'd
fall through, despite the possibility that there's an as-yet-unread sinval
message for that system catalog. This could, for example, result in
failure to access a system catalog or index that had just been processed
by VACUUM FULL. This is the explanation for buildfarm failures we've been
seeing intermittently for the past three months. The bug is far older
than that, but commits a54e1f158
et al added a new recursion case within
AcceptInvalidationMessages that is apparently easier to hit than any
previous case.
To fix this, we must not skip calling AcceptInvalidationMessages until
we have *finished* a call to it since acquiring a relation lock, not
merely acquired the lock. (There's already adequate logic inside
AcceptInvalidationMessages to deal with being called recursively.)
Fortunately, we can implement that at trivial cost, by adding a flag
to LOCALLOCK hashtable entries that tracks whether we know we have
completed such a call.
There is an API hazard added by this patch for external callers of
LockAcquire: if anything is testing for LOCKACQUIRE_ALREADY_HELD,
it might be fooled by the new return code LOCKACQUIRE_ALREADY_CLEAR
into thinking the lock wasn't already held. This should be a fail-soft
condition, though, unless something very bizarre is being done in
response to the test.
Also, I added an additional output argument to LockAcquireExtended,
assuming that that probably isn't called by any outside code given
the very limited usefulness of its additional functionality.
Back-patch to all supported branches.
Discussion: https://postgr.es/m/12259.1532117714@sss.pgh.pa.us
This commit is contained in:
@@ -105,11 +105,12 @@ void
|
||||
LockRelationOid(Oid relid, LOCKMODE lockmode)
|
||||
{
|
||||
LOCKTAG tag;
|
||||
LOCALLOCK *locallock;
|
||||
LockAcquireResult res;
|
||||
|
||||
SetLocktagRelationOid(&tag, relid);
|
||||
|
||||
res = LockAcquire(&tag, lockmode, false, false);
|
||||
res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
|
||||
|
||||
/*
|
||||
* Now that we have the lock, check for invalidation messages, so that we
|
||||
@@ -120,9 +121,18 @@ LockRelationOid(Oid relid, LOCKMODE lockmode)
|
||||
* relcache entry in an undesirable way. (In the case where our own xact
|
||||
* modifies the rel, the relcache update happens via
|
||||
* CommandCounterIncrement, not here.)
|
||||
*
|
||||
* However, in corner cases where code acts on tables (usually catalogs)
|
||||
* recursively, we might get here while still processing invalidation
|
||||
* messages in some outer execution of this function or a sibling. The
|
||||
* "cleared" status of the lock tells us whether we really are done
|
||||
* absorbing relevant inval messages.
|
||||
*/
|
||||
if (res != LOCKACQUIRE_ALREADY_HELD)
|
||||
if (res != LOCKACQUIRE_ALREADY_CLEAR)
|
||||
{
|
||||
AcceptInvalidationMessages();
|
||||
MarkLockClear(locallock);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -138,11 +148,12 @@ bool
|
||||
ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
|
||||
{
|
||||
LOCKTAG tag;
|
||||
LOCALLOCK *locallock;
|
||||
LockAcquireResult res;
|
||||
|
||||
SetLocktagRelationOid(&tag, relid);
|
||||
|
||||
res = LockAcquire(&tag, lockmode, false, true);
|
||||
res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
|
||||
|
||||
if (res == LOCKACQUIRE_NOT_AVAIL)
|
||||
return false;
|
||||
@@ -151,8 +162,11 @@ ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
|
||||
* Now that we have the lock, check for invalidation messages; see notes
|
||||
* in LockRelationOid.
|
||||
*/
|
||||
if (res != LOCKACQUIRE_ALREADY_HELD)
|
||||
if (res != LOCKACQUIRE_ALREADY_CLEAR)
|
||||
{
|
||||
AcceptInvalidationMessages();
|
||||
MarkLockClear(locallock);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -199,20 +213,24 @@ void
|
||||
LockRelation(Relation relation, LOCKMODE lockmode)
|
||||
{
|
||||
LOCKTAG tag;
|
||||
LOCALLOCK *locallock;
|
||||
LockAcquireResult res;
|
||||
|
||||
SET_LOCKTAG_RELATION(tag,
|
||||
relation->rd_lockInfo.lockRelId.dbId,
|
||||
relation->rd_lockInfo.lockRelId.relId);
|
||||
|
||||
res = LockAcquire(&tag, lockmode, false, false);
|
||||
res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
|
||||
|
||||
/*
|
||||
* Now that we have the lock, check for invalidation messages; see notes
|
||||
* in LockRelationOid.
|
||||
*/
|
||||
if (res != LOCKACQUIRE_ALREADY_HELD)
|
||||
if (res != LOCKACQUIRE_ALREADY_CLEAR)
|
||||
{
|
||||
AcceptInvalidationMessages();
|
||||
MarkLockClear(locallock);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -226,13 +244,14 @@ bool
|
||||
ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
|
||||
{
|
||||
LOCKTAG tag;
|
||||
LOCALLOCK *locallock;
|
||||
LockAcquireResult res;
|
||||
|
||||
SET_LOCKTAG_RELATION(tag,
|
||||
relation->rd_lockInfo.lockRelId.dbId,
|
||||
relation->rd_lockInfo.lockRelId.relId);
|
||||
|
||||
res = LockAcquire(&tag, lockmode, false, true);
|
||||
res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
|
||||
|
||||
if (res == LOCKACQUIRE_NOT_AVAIL)
|
||||
return false;
|
||||
@@ -241,8 +260,11 @@ ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
|
||||
* Now that we have the lock, check for invalidation messages; see notes
|
||||
* in LockRelationOid.
|
||||
*/
|
||||
if (res != LOCKACQUIRE_ALREADY_HELD)
|
||||
if (res != LOCKACQUIRE_ALREADY_CLEAR)
|
||||
{
|
||||
AcceptInvalidationMessages();
|
||||
MarkLockClear(locallock);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user