1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00

Move call to GetTopTransactionId() earlier in LockAcquire(),

removing an infrequently occurring race condition in Hot Standby.
An xid must be assigned before a lock appears in shared memory,
rather than immediately after, else GetRunningTransactionLocks()
may see InvalidTransactionId, causing assertion failures during
lock processing on standby.

Bug report and diagnosis by Fujii Masao, fix by me.
This commit is contained in:
Simon Riggs
2010-11-29 01:08:02 +00:00
parent c623365ff9
commit ed78384acd
3 changed files with 43 additions and 16 deletions

View File

@@ -499,6 +499,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
int partition;
LWLockId partitionLock;
int status;
bool log_lock = false;
if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
@@ -579,6 +580,24 @@ LockAcquireExtended(const LOCKTAG *locktag,
return LOCKACQUIRE_ALREADY_HELD;
}
/*
* Emit a WAL record if acquisition of this lock needs to be replayed in a
* standby server. Only AccessExclusiveLocks can conflict with lock types
* that read-only transactions can acquire in a standby server.
*
* Make sure this definition matches the one in GetRunningTransactionLocks().
*
* First we prepare to log, then after lock acquired we issue log record.
*/
if (lockmode >= AccessExclusiveLock &&
locktag->locktag_type == LOCKTAG_RELATION &&
!RecoveryInProgress() &&
XLogStandbyInfoActive())
{
LogAccessExclusiveLockPrepare();
log_lock = true;
}
/*
* Otherwise we've got to mess with the shared lock table.
*/
@@ -868,15 +887,9 @@ LockAcquireExtended(const LOCKTAG *locktag,
/*
* Emit a WAL record if acquisition of this lock need to be replayed in a
* standby server. Only AccessExclusiveLocks can conflict with lock types
* that read-only transactions can acquire in a standby server.
*
* Make sure this definition matches the one GetRunningTransactionLocks().
* standby server.
*/
if (lockmode >= AccessExclusiveLock &&
locktag->locktag_type == LOCKTAG_RELATION &&
!RecoveryInProgress() &&
XLogStandbyInfoActive())
if (log_lock)
{
/*
* Decode the locktag back to the original values, to avoid sending