patch: fix race in SSI's CheckTargetForConflictsIn
Dan Ports <drkp@csail.mit.edu>
From: Dan Ports <drkp@csail.mit.edu>
To: pgsql-hackers@postgresql.org
Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Date: 2011-05-05T01:40:57Z
Lists: pgsql-hackers
Attachments
- ssi-fix-checktargetforconflictsin-race.patch (text/x-diff) patch
While running some benchmarks to test SSI performance, I found a race condition that's capable of causing a segfault. A patch is attached. The bug is in CheckTargetForConflictsIn, which scans the list of SIREAD locks on a lock target when it's modified. There's an optimization in there where the writing transaction will remove a SIREAD lock that it holds itself, because it's being replaced with a (stronger) write lock. To do that, it needs to drop its shared lwlocks and reacquire them in exclusive mode. The existing code deals with concurrent modifications in that interval by redoing checks. However, it misses the case where some other transaction removes all remaining locks on the target, and proceeds to remove the lock target itself. The attached patch fixes this by deferring the SIREAD lock removal until the end of the function. At that point, there isn't any need to worry about concurrent updates to the target's lock list. The resulting code is also simpler. Dan -- Dan R. K. Ports MIT CSAIL http://drkp.net/