pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #13667: SSI violation...
5+ messages / 3 participants
[nested] [flat]
* BUG #13667: SSI violation...
@ 2015-10-08 10:00 sean@chittenden.org
2015-10-09 21:48 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
0 siblings, 1 reply; 5+ messages in thread
From: sean@chittenden.org @ 2015-10-08 10:00 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 13667
Logged by: Sean Chittenden
Email address: sean@chittenden.org
PostgreSQL version: 9.4.4
Operating system: Linux/FreeBSD
Description:
A few weeks back I was pointed in the direction of a bug that appears to be
an SSI violation. The repro case below is written using bash and psql.
In a tight loop, we would expect 50% of these transactions to fail.
Instead, periodically we're seeing both transaction commit successfully.
The concern is that PostgreSQL thinks these transactions are commutable or
the SIREAD lock isn't erected correctly.
https://github.com/gfredericks/pg-serializability-bug
The two concurrent transactions required to tickle this bug are:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO things (id) VALUES ('A');
SELECT id FROM things;
COMMIT;
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO things (id) VALUES ('B');
SELECT id FROM things;
COMMIT;
Cheers. -sc
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #13667: SSI violation...
2015-10-08 10:00 BUG #13667: SSI violation... sean@chittenden.org
@ 2015-10-09 21:48 ` Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:36 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Kevin Grittner @ 2015-10-09 21:48 UTC (permalink / raw)
To: sean@chittenden.org <sean@chittenden.org>; pgsql-bugs
On Thursday, October 8, 2015 5:01 AM, "sean@chittenden.org" <sean@chittenden.org> wrote:
> A few weeks back I was pointed in the direction of a bug that appears to be
> an SSI violation. The repro case below is written using bash and psql.
>
> In a tight loop, we would expect 50% of these transactions to fail.
> Instead, periodically we're seeing both transaction commit successfully.
> The concern is that PostgreSQL thinks these transactions are commutable or
> the SIREAD lock isn't erected correctly.
>
> https://github.com/gfredericks/pg-serializability-bug
>
> The two concurrent transactions required to tickle this bug are:
>
> BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
> INSERT INTO things (id) VALUES ('A');
> SELECT id FROM things;
> COMMIT;
>
> BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
> INSERT INTO things (id) VALUES ('B');
> SELECT id FROM things;
> COMMIT;
I agree this is a bug, and it behave like a race condition with a
very narrow window of time for the second process to hit. It has
proven very hard to find the cause. So far every attempt to
instrument the code to catch the bug in progress has distorted the
timings enough to prevent the bug from showing. Next time I take a
run at this I think I need to insert delays at suspicious points in
the code to try to provoke it at greater volume. If anyone else
wants to take a shot at that and show me what they found that does
that, it would expedite getting a fix.
--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #13667: SSI violation...
2015-10-08 10:00 BUG #13667: SSI violation... sean@chittenden.org
2015-10-09 21:48 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
@ 2015-10-30 17:36 ` Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:41 ` Re: BUG #13667: SSI violation... Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Kevin Grittner @ 2015-10-30 17:36 UTC (permalink / raw)
To: Kevin Grittner <kgrittn@ymail.com>; sean@chittenden.org <sean@chittenden.org>; pgsql-bugs
On Friday, October 9, 2015 4:49 PM, Kevin Grittner <kgrittn@ymail.com> wrote:
> I agree this is a bug, and it behave like a race condition with a
> very narrow window of time for the second process to hit. It has
> proven very hard to find the cause.
Thanks to Thomas Munro joining me in a 2.5 day marathon hunt for
this bug, we have found it and squashed it with the attached patch.
The issue in heap_insert() in heapam.c was the bug that was
actually causing the failures in the specific tests we had, but
other locations doing insert, update, or delete had similar bugs,
and we found a few stray issues in predicate.c that are also fixed
here.
The basic problem was that in the initial implementation we tried a
little too hard to optimize, at the expense of correctness. When
we were going to insert, for example, we checked for an existing
predicate lock on the relation first, to avoid the effort of
inserting the tuple, with WAL-logging, if we were just going to
roll back the transaction anyway because we found a rw-conflict
that completed a "dangerous structure". The problem is, it allowed
this sequence to create an undetected serialization anomaly:
T1
----------
T2
----------
lock
read
checklocks
lock
read
insert
At this point things are hosed, regardless of timings past this.
The window in the test was small because T2 has to acquire its
SIReadLock and scan the whole table before T1 manages to insert a
tuple, but with enough contention T1 can stall long enough to see
this happen. When T2 checks locks prior to its insert it will
detect the rw-conflict from T1 to T2, but the rw-conflict in the
other direction won't be detected, so no dangerous structure is
formed and nothing is rolled back.
Moving the check past the insert would fix the problem, but the
reason it was put in front is enshrined in a comment at each
problem location; for example:
* We're about to do the actual insert -- but check for conflict first, to
* avoid possibly having to roll back work we've just done.
These checks are about as close to free as you can get if the
transaction doing the check is not serializable; it doesn't even
need to take out a LW lock to determine there is nothing to be
done. The reason given in the comment still has merit for
serializable transactions; even for them the check is orders of
magnitude cheaper than a WAL logged tuple insert. It only requires
an occasional serialization failure detection there to come out
ahead. So rather than move the existing check, we added a recheck
after.
Barring objections I will push this tomorrow, including
back-patching it to all supported branches.
--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
ssi-bug-fixes-v1.diff (invalid/octet-stream, 3.2K) download
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 66deb1f..ff2eea8 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -2516,6 +2516,9 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
if (vmbuffer != InvalidBuffer)
ReleaseBuffer(vmbuffer);
+ /* Recheck to close a window for a race condition. */
+ CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
+
/*
* If tuple is cachable, mark it for invalidation from the caches in case
* we abort. Note it is OK to do this after releasing the buffer, because
@@ -2845,6 +2848,9 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
ndone += nthispage;
}
+ /* Recheck to close a window for a race condition. */
+ CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
+
/*
* If tuples are cachable, mark them for invalidation from the caches in
* case we abort. Note it is OK to do this after releasing the buffer,
@@ -3306,6 +3312,9 @@ l1:
*/
CacheInvalidateHeapTuple(relation, &tp, NULL);
+ /* Recheck to close a window for a race condition. */
+ CheckForSerializableConflictIn(relation, &tp, buffer);
+
/* Now we can release the buffer */
ReleaseBuffer(buffer);
@@ -4123,6 +4132,10 @@ l2:
LockBuffer(newbuf, BUFFER_LOCK_UNLOCK);
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+ /* Recheck to close a window for a race condition. */
+ CheckForSerializableConflictIn(relation, &oldtup, buffer);
+ CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
+
/*
* Mark old tuple for invalidation from system caches at next command
* boundary, and mark the new tuple for invalidation in case we abort. We
diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index bad5618..47b4ada 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -3217,22 +3217,21 @@ ReleasePredicateLocks(bool isCommit)
return;
}
+ LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
+
Assert(!isCommit || SxactIsPrepared(MySerializableXact));
Assert(!isCommit || !SxactIsDoomed(MySerializableXact));
Assert(!SxactIsCommitted(MySerializableXact));
Assert(!SxactIsRolledBack(MySerializableXact));
/* may not be serializable during COMMIT/ROLLBACK PREPARED */
- if (MySerializableXact->pid != 0)
- Assert(IsolationIsSerializable());
+ Assert(MySerializableXact->pid == 0 || IsolationIsSerializable());
/* We'd better not already be on the cleanup list. */
Assert(!SxactIsOnFinishedList(MySerializableXact));
topLevelIsDeclaredReadOnly = SxactIsReadOnly(MySerializableXact);
- LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
-
/*
* We don't hold XidGenLock lock here, assuming that TransactionId is
* atomic!
@@ -4369,7 +4368,7 @@ CheckTableForSerializableConflictIn(Relation relation)
LWLockAcquire(SerializablePredicateLockListLock, LW_EXCLUSIVE);
for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
- LWLockAcquire(SerializableXactHashLock, LW_SHARED);
+ LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
/* Scan through target list */
hash_seq_init(&seqstat, PredicateLockTargetHash);
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #13667: SSI violation...
2015-10-08 10:00 BUG #13667: SSI violation... sean@chittenden.org
2015-10-09 21:48 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:36 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
@ 2015-10-30 17:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-10-31 20:12 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2015-10-30 17:41 UTC (permalink / raw)
To: Kevin Grittner <kgrittn@ymail.com>; +Cc: sean@chittenden.org <sean@chittenden.org>; pgsql-bugs
Kevin Grittner <kgrittn@ymail.com> writes:
> Thanks to Thomas Munro joining me in a 2.5 day marathon hunt for
> this bug, we have found it and squashed it with the attached patch.
> ...
> These checks are about as close to free as you can get if the
> transaction doing the check is not serializable; it doesn't even
> need to take out a LW lock to determine there is nothing to be
> done. The reason given in the comment still has merit for
> serializable transactions; even for them the check is orders of
> magnitude cheaper than a WAL logged tuple insert. It only requires
> an occasional serialization failure detection there to come out
> ahead. So rather than move the existing check, we added a recheck
> after.
> Barring objections I will push this tomorrow, including
> back-patching it to all supported branches.
I'm okay with the substance of the patch, but that's a pretty miserable
excuse for fixing the comments. Both the initial checks and the rechecks
ought to have at least a couple of sentences recapping the logic you gave
us here.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #13667: SSI violation...
2015-10-08 10:00 BUG #13667: SSI violation... sean@chittenden.org
2015-10-09 21:48 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:36 ` Re: BUG #13667: SSI violation... Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:41 ` Re: BUG #13667: SSI violation... Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-10-31 20:12 ` Kevin Grittner <kgrittn@ymail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Kevin Grittner @ 2015-10-31 20:12 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: sean@chittenden.org <sean@chittenden.org>; pgsql-bugs; pbailis@cs.berkeley.edu <pbailis@cs.berkeley.edu>
On Friday, October 30, 2015 12:41 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I'm okay with the substance of the patch, but that's a pretty
> miserable excuse for fixing the comments.
Further testing and study of the patch suggested further
enhancements, attached, which I have pushed to all supported
branches (along with more comments) -- so the fix will be in the
next set of releases.
Thanks to those who reported the bug, and I apologize that it took
so long to track down. Thanks also to Thomas Munro for working
with me in tracking it down!
--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
ssi-bug-fixes-v2.diff (invalid/octet-stream, 8.3K) download
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 66deb1f..35a2b05 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -2388,25 +2388,30 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
heaptup = heap_prepare_insert(relation, tup, xid, cid, options);
/*
+ * Find buffer to insert this tuple into. If the page is all visible,
+ * this will also pin the requisite visibility map page.
+ */
+ buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
+ InvalidBuffer, options, bistate,
+ &vmbuffer, NULL);
+
+ /*
* We're about to do the actual insert -- but check for conflict first, to
* avoid possibly having to roll back work we've just done.
*
+ * This is safe without a recheck as long as there is no possibility of
+ * another process scanning the page between this check and the insert
+ * being visible to the scan (i.e., an exclusive buffer content lock is
+ * continuously held from this point until the tuple insert is visible).
+ *
* For a heap insert, we only need to check for table-level SSI locks. Our
* new tuple can't possibly conflict with existing tuple locks, and heap
* page locks are only consolidated versions of tuple locks; they do not
- * lock "gaps" as index page locks do. So we don't need to identify a
- * buffer before making the call.
+ * lock "gaps" as index page locks do. So we don't need to specify a
+ * buffer when making the call, which makes for a faster check.
*/
CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
- /*
- * Find buffer to insert this tuple into. If the page is all visible,
- * this will also pin the requisite visibility map page.
- */
- buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
- InvalidBuffer, options, bistate,
- &vmbuffer, NULL);
-
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -2660,13 +2665,26 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
/*
* We're about to do the actual inserts -- but check for conflict first,
- * to avoid possibly having to roll back work we've just done.
+ * to minimize the possibility of having to roll back work we've just
+ * done.
*
- * For a heap insert, we only need to check for table-level SSI locks. Our
- * new tuple can't possibly conflict with existing tuple locks, and heap
+ * A check here does not definitively prevent a serialization anomaly;
+ * that check MUST be done at least past the point of acquiring an
+ * exclusive buffer content lock on every buffer that will be affected,
+ * and MAY be done after all inserts are reflected in the buffers and
+ * those locks are released; otherwise there race condition. Since
+ * multiple buffers can be locked and unlocked in the loop below, and it
+ * would not be feasible to identify and lock all of those buffers before
+ * the loop, we must do a final check at the end.
+ *
+ * The check here could be omitted with no loss of correctness; it is
+ * present strictly as an optimization.
+ *
+ * For heap inserts, we only need to check for table-level SSI locks. Our
+ * new tuples can't possibly conflict with existing tuple locks, and heap
* page locks are only consolidated versions of tuple locks; they do not
- * lock "gaps" as index page locks do. So we don't need to identify a
- * buffer before making the call.
+ * lock "gaps" as index page locks do. So we don't need to specify a
+ * buffer when making the call, which makes for a faster check.
*/
CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
@@ -2846,6 +2864,22 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
}
/*
+ * We're done with the actual inserts. Check for conflicts again, to
+ * ensure that all rw-conflicts in to these inserts are detected. Without
+ * this final check, a sequential scan of the heap may have locked the
+ * table after the "before" check, missing one opportunity to detect the
+ * conflict, and then scanned the table before the new tuples were there,
+ * missing the other chance to detect the conflict.
+ *
+ * For heap inserts, we only need to check for table-level SSI locks. Our
+ * new tuples can't possibly conflict with existing tuple locks, and heap
+ * page locks are only consolidated versions of tuple locks; they do not
+ * lock "gaps" as index page locks do. So we don't need to specify a
+ * buffer when making the call.
+ */
+ CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
+
+ /*
* If tuples are cachable, mark them for invalidation from the caches in
* case we abort. Note it is OK to do this after releasing the buffer,
* because the heaptuples data structure is all in local memory, not in
@@ -3158,6 +3192,11 @@ l1:
/*
* We're about to do the actual delete -- check for conflict first, to
* avoid possibly having to roll back work we've just done.
+ *
+ * This is safe without a recheck as long as there is no possibility of
+ * another process scanning the page between this check and the delete
+ * being visible to the scan (i.e., an exclusive buffer content lock is
+ * continuously held from this point until the tuple delete is visible).
*/
CheckForSerializableConflictIn(relation, &tp, buffer);
@@ -3785,12 +3824,6 @@ l2:
goto l2;
}
- /*
- * We're about to do the actual update -- check for conflict first, to
- * avoid possibly having to roll back work we've just done.
- */
- CheckForSerializableConflictIn(relation, &oldtup, buffer);
-
/* Fill in transaction status data */
/*
@@ -3979,14 +4012,20 @@ l2:
}
/*
- * We're about to create the new tuple -- check for conflict first, to
+ * We're about to do the actual update -- check for conflict first, to
* avoid possibly having to roll back work we've just done.
*
- * NOTE: For a tuple insert, we only need to check for table locks, since
- * predicate locking at the index level will cover ranges for anything
- * except a table scan. Therefore, only provide the relation.
+ * This is safe without a recheck as long as there is no possibility of
+ * another process scanning the pages between this check and the update
+ * being visible to the scan (i.e., exclusive buffer content lock(s) are
+ * continuously held from this point until the tuple update is visible).
+ *
+ * For the new tuple the only check needed is at the relation level, but
+ * since both tuples are in the same relation and the check for oldtup
+ * will include checking the relation level, there is no benefit to a
+ * separate check for the new tuple.
*/
- CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
+ CheckForSerializableConflictIn(relation, &oldtup, buffer);
/*
* At this point newbuf and buffer are both pinned and locked, and newbuf
diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index bad5618..47b4ada 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -3217,22 +3217,21 @@ ReleasePredicateLocks(bool isCommit)
return;
}
+ LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
+
Assert(!isCommit || SxactIsPrepared(MySerializableXact));
Assert(!isCommit || !SxactIsDoomed(MySerializableXact));
Assert(!SxactIsCommitted(MySerializableXact));
Assert(!SxactIsRolledBack(MySerializableXact));
/* may not be serializable during COMMIT/ROLLBACK PREPARED */
- if (MySerializableXact->pid != 0)
- Assert(IsolationIsSerializable());
+ Assert(MySerializableXact->pid == 0 || IsolationIsSerializable());
/* We'd better not already be on the cleanup list. */
Assert(!SxactIsOnFinishedList(MySerializableXact));
topLevelIsDeclaredReadOnly = SxactIsReadOnly(MySerializableXact);
- LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
-
/*
* We don't hold XidGenLock lock here, assuming that TransactionId is
* atomic!
@@ -4369,7 +4368,7 @@ CheckTableForSerializableConflictIn(Relation relation)
LWLockAcquire(SerializablePredicateLockListLock, LW_EXCLUSIVE);
for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
- LWLockAcquire(SerializableXactHashLock, LW_SHARED);
+ LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
/* Scan through target list */
hash_seq_init(&seqstat, PredicateLockTargetHash);
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2015-10-31 20:12 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-10-08 10:00 BUG #13667: SSI violation... sean@chittenden.org
2015-10-09 21:48 ` Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:36 ` Kevin Grittner <kgrittn@ymail.com>
2015-10-30 17:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-10-31 20:12 ` Kevin Grittner <kgrittn@ymail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox