From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Tomonari Katsumata <katsumata.tomonari@po.ntts.co.jp>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8434: Why does dead lock occur many times ?
Date: Thu, 28 Nov 2013 08:22:17 -0300
Message-ID: <20131128112217.GE5513@eldon.alvh.no-ip.org> (raw)
In-Reply-To: <5296BFE1.2090906@po.ntts.co.jp>
References: <E1VH9At-0005VC-Kk@wrigleys.postgresql.org>
<20131004172217.GW5235@eldon.alvh.no-ip.org>
<20131127164226.GA14522@eldon.alvh.no-ip.org>
<5296BFE1.2090906@po.ntts.co.jp>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Tomonari Katsumata wrote:
> Hi Alvaro
>
> I applied all patches you told me against 93_STABLE.
> But I could not build the source with the patches.
>
> It seems your patch use GetMultiXactIdMembers with four arguments,
> but the function requires three arguments.
> ----
> nmembers = GetMultiXactIdMembers(rawxmax, &members, false, false);
> ----
>
> do you have another patch?
Sorry, yes. This is the complete series of patches, although I have not
tried them against 9.3 yet.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:download all (5 files, .tar.gz)0001-Skip-looking-up-very-old-LOCK_ONLY-MultiXactIds.patch (text/x-diff, 9.9K) download
From ef026a8110f40941741585e07671398beea28f11 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 27 Nov 2013 17:19:41 -0300
Subject: [PATCH 1/5] Skip looking up very old LOCK_ONLY MultiXactIds
Commit 0ac5ad5134f2 removed an optimization in multixact.c that skipped
fetching members of MultiXactId that were older than our
OldestVisibleMXactId value. The reason this was removed is that it is
possible for multis that contain updates to be older than that value.
However, if the caller is certain that the multi does not contain an
update (because the infomask bits say so), it can pass this info down to
GetMultiXactIdMembers, enabling it to use the old optimization.
Pointed out by Andres Freund in 20131121200517.GM7240@alap2.anarazel.de
---
contrib/pgrowlocks/pgrowlocks.c | 3 ++-
src/backend/access/heap/heapam.c | 20 +++++++++++++-------
src/backend/access/transam/multixact.c | 29 ++++++++++++++++++++++++-----
src/backend/utils/time/tqual.c | 11 ++++++-----
src/include/access/multixact.h | 4 ++--
5 files changed, 47 insertions(+), 20 deletions(-)
diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.cindex 636ff05..8d4961c 100644--- a/contrib/pgrowlocks/pgrowlocks.c+++ b/contrib/pgrowlocks/pgrowlocks.c@@ -168,7 +168,8 @@ pgrowlocks(PG_FUNCTION_ARGS)
allow_old = !(infomask & HEAP_LOCK_MASK) &&
(infomask & HEAP_XMAX_LOCK_ONLY);
- nmembers = GetMultiXactIdMembers(xmax, &members, allow_old);+ nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,+ false);
if (nmembers == -1)
{
values[Atnum_xids] = "{0}";
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex a21f31b..32ef453 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -3987,7 +3987,9 @@ l3:
* the case, HeapTupleSatisfiesUpdate would have returned
* MayBeUpdated and we wouldn't be here.
*/
- nmembers = GetMultiXactIdMembers(xwait, &members, false);+ nmembers =+ GetMultiXactIdMembers(xwait, &members, false,+ HEAP_XMAX_IS_LOCKED_ONLY(infomask));
for (i = 0; i < nmembers; i++)
{
@@ -4008,7 +4010,8 @@ l3:
}
}
- pfree(members);+ if (members)+ pfree(members);
}
/*
@@ -4157,7 +4160,9 @@ l3:
* been the case, HeapTupleSatisfiesUpdate would have returned
* MayBeUpdated and we wouldn't be here.
*/
- nmembers = GetMultiXactIdMembers(xwait, &members, false);+ nmembers =+ GetMultiXactIdMembers(xwait, &members, false,+ HEAP_XMAX_IS_LOCKED_ONLY(infomask));
if (nmembers <= 0)
{
@@ -4626,7 +4631,7 @@ l5:
* MultiXactIdExpand if we weren't to do this, so this check is not
* incurring extra work anyhow.
*/
- if (!MultiXactIdIsRunning(xmax))+ if (!MultiXactIdIsRunning(xmax, HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)))
{
if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) ||
TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax,
@@ -5217,7 +5222,7 @@ GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask,
* We only use this in multis we just created, so they cannot be values
* pre-pg_upgrade.
*/
- nmembers = GetMultiXactIdMembers(multi, &members, false);+ nmembers = GetMultiXactIdMembers(multi, &members, false, false);
for (i = 0; i < nmembers; i++)
{
@@ -5293,7 +5298,7 @@ MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask)
* Since we know the LOCK_ONLY bit is not set, this cannot be a multi from
* pre-pg_upgrade.
*/
- nmembers = GetMultiXactIdMembers(xmax, &members, false);+ nmembers = GetMultiXactIdMembers(xmax, &members, false, false);
if (nmembers > 0)
{
@@ -5376,7 +5381,8 @@ Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
int remain = 0;
allow_old = !(infomask & HEAP_LOCK_MASK) && HEAP_XMAX_IS_LOCKED_ONLY(infomask);
- nmembers = GetMultiXactIdMembers(multi, &members, allow_old);+ nmembers = GetMultiXactIdMembers(multi, &members, allow_old,+ HEAP_XMAX_IS_LOCKED_ONLY(infomask));
if (nmembers >= 0)
{
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.cindex e3f5cbc..20b0ea2 100644--- a/src/backend/access/transam/multixact.c+++ b/src/backend/access/transam/multixact.c@@ -391,7 +391,7 @@ MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
* caller of this function does a check that the multixact is no longer
* running.
*/
- nmembers = GetMultiXactIdMembers(multi, &members, false);+ nmembers = GetMultiXactIdMembers(multi, &members, false, false);
if (nmembers < 0)
{
@@ -477,7 +477,7 @@ MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
* a pg_upgraded share-locked tuple.
*/
bool
-MultiXactIdIsRunning(MultiXactId multi)+MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
{
MultiXactMember *members;
int nmembers;
@@ -489,7 +489,7 @@ MultiXactIdIsRunning(MultiXactId multi)
* "false" here means we assume our callers have checked that the given
* multi cannot possibly come from a pg_upgraded database.
*/
- nmembers = GetMultiXactIdMembers(multi, &members, false);+ nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly);
if (nmembers < 0)
{
@@ -1029,10 +1029,15 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
* the value currently known as the next to assign, raise an error. Previously
* these also returned -1, but since this can lead to the wrong visibility
* results, it is dangerous to do that.
+ *+ * onlyLock must be set to true if caller is certain that the given multi+ * is used only to lock tuples; can be false without loss of correctness,+ * but passing a true means we can return quickly without checking for+ * old updates.
*/
int
GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
- bool allow_old)+ bool allow_old, bool onlyLock)
{
int pageno;
int prev_pageno;
@@ -1066,6 +1071,19 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
MultiXactIdSetOldestVisible();
/*
+ * If we know the multi is used only for locking and not for updates,+ * then we can skip checking if the value is older than our oldest+ * visible multi. It cannot possibly still be running.+ */+ if (onlyLock &&+ MultiXactIdPrecedes(multi, OldestVisibleMXactId[MyBackendId]))+ {+ debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old");+ *members = NULL;+ return -1;+ }++ /*
* We check known limits on MultiXact before resorting to the SLRU area.
*
* An ID older than MultiXactState->oldestMultiXactId cannot possibly be
@@ -2507,7 +2525,8 @@ pg_get_multixact_members(PG_FUNCTION_ARGS)
multi = palloc(sizeof(mxact));
/* no need to allow for old values here */
- multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false);+ multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false,+ false);
multi->iter = 0;
tupdesc = CreateTemplateTupleDesc(2, false);
diff --git a/src/backend/utils/time/tqual.c b/src/backend/utils/time/tqual.cindex ed66c49..e0d627b 100644--- a/src/backend/utils/time/tqual.c+++ b/src/backend/utils/time/tqual.c@@ -565,7 +565,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid,
*/
if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK |
HEAP_XMAX_KEYSHR_LOCK)) &&
- MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))+ MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), true))
return HeapTupleBeingUpdated;
SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId);
@@ -575,7 +575,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid,
xmax = HeapTupleGetUpdateXid(tuple);
if (!TransactionIdIsValid(xmax))
{
- if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))+ if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false))
return HeapTupleBeingUpdated;
SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId);
@@ -590,7 +590,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid,
return HeapTupleInvisible; /* updated before scan started */
}
- if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))+ if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false))
return HeapTupleBeingUpdated;
if (TransactionIdDidCommit(xmax))
@@ -1158,7 +1158,8 @@ HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin,
*/
if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK |
HEAP_XMAX_KEYSHR_LOCK)) &&
- MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))+ MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple),+ true))
return HEAPTUPLE_LIVE;
SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId);
@@ -1185,7 +1186,7 @@ HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin,
{
TransactionId xmax;
- if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))+ if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false))
{
/* already checked above */
Assert(!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask));
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.hindex 8a9edde..3572992 100644--- a/src/include/access/multixact.h+++ b/src/include/access/multixact.h@@ -82,10 +82,10 @@ extern MultiXactId MultiXactIdCreate(TransactionId xid1,
extern MultiXactId MultiXactIdExpand(MultiXactId multi, TransactionId xid,
MultiXactStatus status);
extern MultiXactId ReadNextMultiXactId(void);
-extern bool MultiXactIdIsRunning(MultiXactId multi);+extern bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly);
extern void MultiXactIdSetOldestMember(void);
extern int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **xids,
- bool allow_old);+ bool allow_old, bool isLockOnly);
extern bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2);
extern void AtEOXact_MultiXact(void);
--
1.7.10.4
0002-Cope-with-heap_fetch-failure-while-locking-an-update.patch (text/x-diff, 1.5K) download
From 2d227750252da8e66849525f7ca5879cd2e0cb7d Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 27 Nov 2013 17:45:25 -0300
Subject: [PATCH 2/5] Cope with heap_fetch failure while locking an update
chain
The reason for the fetch failure is that the tuple was removed because
it was dead; so the failure is innocuous and can be ignored. Moreover,
there's no need for further work and we can return success to the caller
immediately. EvalPlanQualFetch is doing something very similar to this
already.
Report and test case from Andres Freund in
20131124000203.GA4403@alap2.anarazel.de
---
src/backend/access/heap/heapam.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 32ef453..6d7cd0c 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -4829,7 +4829,16 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
ItemPointerCopy(&tupid, &(mytup.t_self));
if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false, NULL))
- elog(ERROR, "unable to fetch updated version of tuple");+ {+ /*+ * if we fail to find the updated version of the tuple, it's+ * because it was vacuumed/pruned away after its creator+ * transaction aborted. So behave as if we got to the end of the+ * chain, and there's no further tuple to lock: return success to+ * caller.+ */+ return HeapTupleMayBeUpdated;+ }
l4:
CHECK_FOR_INTERRUPTS();
--
1.7.10.4
0003-Don-t-try-to-set-InvalidXid-as-page-pruning-hint.patch (text/x-diff, 2.0K) download
From 8ce864afd98bc669db856d7c75f0177981a46fd6 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 27 Nov 2013 17:47:16 -0300
Subject: [PATCH 3/5] Don't try to set InvalidXid as page pruning hint
If a transaction updates/deletes a tuple just before aborting, and a
concurrent transaction tries to prune the page concurrently, the pruner
may see HeapTupleSatisfiesVacuum return HEAPTUPLE_DELETE_IN_PROGRESS,
but a later call to HeapTupleGetUpdateXid() return InvalidXid. This
would cause an assertion failure in development builds, but would be
otherwise Mostly Harmless.
Fix by checking whether the updater Xid is valid before trying to apply
it as page prune point.
Reported by Andres in 20131124000203.GA4403@alap2.anarazel.de
---
src/backend/access/heap/pruneheap.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.cindex 3ec10a0..fdfa37c 100644--- a/src/backend/access/heap/pruneheap.c+++ b/src/backend/access/heap/pruneheap.c@@ -479,13 +479,22 @@ heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum,
break;
case HEAPTUPLE_DELETE_IN_PROGRESS:
+ {+ TransactionId xmax;++ /*+ * This tuple may soon become DEAD. Update the hint field+ * so that the page is reconsidered for pruning in future.+ * If there was a MultiXactId updater, and it aborted after+ * HTSV checked, then we will get an invalid Xid here.+ * There is no need for future pruning of the page in that+ * case, so skip it.+ */+ xmax = HeapTupleHeaderGetUpdateXid(htup);+ if (TransactionIdIsValid(xmax))+ heap_prune_record_prunable(prstate, xmax);+ }- /*- * This tuple may soon become DEAD. Update the hint field so- * that the page is reconsidered for pruning in future.- */- heap_prune_record_prunable(prstate,- HeapTupleHeaderGetUpdateXid(htup));
break;
case HEAPTUPLE_LIVE:
--
1.7.10.4
0004-Compare-Xmin-to-previous-Xmax-when-locking-an-update.patch (text/x-diff, 1.9K) download
From 793296739bd38e6166510311b9134e0da7db6e74 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 27 Nov 2013 17:49:12 -0300
Subject: [PATCH 4/5] Compare Xmin to previous Xmax when locking an update
chain
Not doing so causes us to traverse an update chain that has been broken
by concurrent page pruning. All other code that traverses update chains
uses this check as one of the cases in which to stop iterating, so
replicate it here too. Failure to do so leads to erroneous CLOG,
subtrans or multixact lookups.
Per bug report by J Smith in
CADFUPgc5bmtv-yg9znxV-vcfkb+JPRqs7m2OesQXaM_4Z1JpdQ@mail.gmail.com
diagnosed by Andres Freund.
---
src/backend/access/heap/heapam.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 6d7cd0c..f70e1f7 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -4819,6 +4819,7 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
old_infomask;
TransactionId xmax,
new_xmax;
+ TransactionId priorXmax = InvalidTransactionId;
ItemPointerCopy(tid, &tupid);
@@ -4844,6 +4845,18 @@ l4:
CHECK_FOR_INTERRUPTS();
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
+ /*+ * Check the tuple XMIN against prior XMAX, if any. If we reached+ * the end of the chain, we're done, so return success.+ */+ if (TransactionIdIsValid(priorXmax) &&+ !TransactionIdEquals(HeapTupleHeaderGetXmin(mytup.t_data),+ priorXmax))+ {+ UnlockReleaseBuffer(buf);+ return HeapTupleMayBeUpdated;+ }+
old_infomask = mytup.t_data->t_infomask;
xmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
@@ -4944,6 +4957,7 @@ l4:
}
/* tail recursion */
+ priorXmax = HeapTupleHeaderGetUpdateXid(mytup.t_data);
ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid);
UnlockReleaseBuffer(buf);
}
--
1.7.10.4
0005-Use-a-more-granular-approach-to-follow-update-chains.patch (text/x-diff, 7.7K) download
From 447b5325503fca11a413240c15dc1d837d9d8151 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 27 Nov 2013 17:50:33 -0300
Subject: [PATCH 5/5] Use a more granular approach to follow update chains
Instead of simply checking the KEYS_UPDATED bit, we need to check
whether each lock held on the future version of the tuple conflicts with
the lock we're trying to acquire.
Per bug report #8434 by Tomonari Katsumata
---
src/backend/access/heap/heapam.c | 202 ++++++++++++++++++++++++++++++++------
1 file changed, 171 insertions(+), 31 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex f70e1f7..6802cbb 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -4799,6 +4799,83 @@ l5:
*result_xmax = new_xmax;
}
+/*+ * Subroutine for heap_lock_updated_tuple_rec.+ *+ * Given an hypothetical multixact status held by the transaction identified+ * with the given xid, does the current transaction need to wait, fail, or can+ * it continue if it wanted to acquire a lock of the given mode? "needwait"+ * is set to true if waiting is necessary; if it can continue, then+ * HeapTupleMayBeUpdated is returned. In case of a conflict, a different+ * HeapTupleSatisfiesUpdate return code is returned.+ *+ * The held status is said to be hypothetical because it might correspond to a+ * lock held by a single Xid, i.e. not a real MultiXactId; we express it this+ * way for simplicity of API.+ */+static HTSU_Result+test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid,+ LockTupleMode mode, bool *needwait)+{+ MultiXactStatus wantedstatus;++ *needwait = false;+ wantedstatus = get_mxact_status_for_lock(mode, false);++ /*+ * Note: we *must* check TransactionIdIsInProgress before+ * TransactionIdDidAbort/Commit; see comment at top of tqual.c for an+ * explanation.+ */+ if (TransactionIdIsCurrentTransactionId(xid))+ {+ /*+ * Updated by our own transaction? Just return failure. This shouldn't+ * normally happen.+ */+ return HeapTupleSelfUpdated;+ }+ else if (TransactionIdIsInProgress(xid))+ {+ /*+ * If the locking transaction is running, what we do depends on whether+ * the lock modes conflict: if they do, then we must wait for it to+ * finish; otherwise we can fall through to lock this tuple version+ * without waiting.+ */+ if (DoLockModesConflict(LOCKMODE_from_mxstatus(status),+ LOCKMODE_from_mxstatus(wantedstatus)))+ {+ *needwait = true;+ }++ /*+ * If we set needwait above, then this value doesn't matter; otherwise,+ * this value signals to caller that it's okay to proceed.+ */+ return HeapTupleMayBeUpdated;+ }+ else if (TransactionIdDidAbort(xid))+ return HeapTupleMayBeUpdated;+ else if (TransactionIdDidCommit(xid))+ {+ /*+ * If the updating transaction committed, what we do depends on whether+ * the lock modes conflict: if they do, then we must report error to+ * caller. But if they don't, we can fall through to lock it.+ */+ if (DoLockModesConflict(LOCKMODE_from_mxstatus(status),+ LOCKMODE_from_mxstatus(wantedstatus)))+ /* bummer */+ return HeapTupleUpdated;++ return HeapTupleMayBeUpdated;+ }++ /* Not in progress, not aborted, not committed -- must have crashed */+ return HeapTupleMayBeUpdated;+}+
/*
* Recursive part of heap_lock_updated_tuple
@@ -4816,7 +4893,8 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
Buffer buf;
uint16 new_infomask,
new_infomask2,
- old_infomask;+ old_infomask,+ old_infomask2;
TransactionId xmax,
new_xmax;
TransactionId priorXmax = InvalidTransactionId;
@@ -4858,45 +4936,107 @@ l4:
}
old_infomask = mytup.t_data->t_infomask;
+ old_infomask2 = mytup.t_data->t_infomask2;
xmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
/*
- * If this tuple is updated and the key has been modified (or- * deleted), what we do depends on the status of the updating- * transaction: if it's live, we sleep until it finishes; if it has- * committed, we have to fail (i.e. return HeapTupleUpdated); if it- * aborted, we ignore it. For updates that didn't touch the key, we- * can just plough ahead.+ * If this tuple version has been updated or locked by some concurrent+ * transaction(s), what we do depends on whether our lock mode+ * conflicts with what those other transactions hold, and also on the+ * status of them.
*/
- if (!(old_infomask & HEAP_XMAX_INVALID) &&- (mytup.t_data->t_infomask2 & HEAP_KEYS_UPDATED))+ if (!(old_infomask & HEAP_XMAX_INVALID))
{
- TransactionId update_xid;+ TransactionId rawxmax;+ bool needwait;- /*- * Note: we *must* check TransactionIdIsInProgress before- * TransactionIdDidAbort/Commit; see comment at top of tqual.c for- * an explanation.- */- update_xid = HeapTupleHeaderGetUpdateXid(mytup.t_data);- if (TransactionIdIsCurrentTransactionId(update_xid))+ rawxmax = HeapTupleHeaderGetRawXmax(mytup.t_data);+ if (old_infomask & HEAP_XMAX_IS_MULTI)
{
- UnlockReleaseBuffer(buf);- return HeapTupleSelfUpdated;- }- else if (TransactionIdIsInProgress(update_xid))- {- LockBuffer(buf, BUFFER_LOCK_UNLOCK);- /* No LockTupleTuplock here -- see heap_lock_updated_tuple */- XactLockTableWait(update_xid);- goto l4;+ int nmembers;+ int i;+ MultiXactMember *members;++ nmembers = GetMultiXactIdMembers(rawxmax, &members, false, false);+ for (i = 0; i < nmembers; i++)+ {+ HTSU_Result res;++ res = test_lockmode_for_conflict(members[i].status,+ members[i].xid,+ mode, &needwait);++ if (needwait)+ {+ LockBuffer(buf, BUFFER_LOCK_UNLOCK);+ XactLockTableWait(members[i].xid);+ pfree(members);+ goto l4;+ }+ if (res != HeapTupleMayBeUpdated)+ {+ UnlockReleaseBuffer(buf);+ pfree(members);+ return res;+ }+ }+ if (members)+ pfree(members);
}
- else if (TransactionIdDidAbort(update_xid))- ; /* okay to proceed */- else if (TransactionIdDidCommit(update_xid))+ else
{
- UnlockReleaseBuffer(buf);- return HeapTupleUpdated;+ HTSU_Result res;+ MultiXactStatus status;++ /*+ * For a non-multi Xmax, we first need to compute the+ * corresponding MultiXactStatus by using the infomask bits.+ */+ if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))+ {+ if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask))+ status = MultiXactStatusForKeyShare;+ else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask))+ status = MultiXactStatusForShare;+ else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))+ {+ if (old_infomask2 & HEAP_KEYS_UPDATED)+ status = MultiXactStatusForUpdate;+ else+ status = MultiXactStatusForNoKeyUpdate;+ }+ else+ {+ /*+ * LOCK_ONLY present alone (a pg_upgraded tuple+ * marked as share-locked in the old cluster) shouldn't+ * be seen in the middle of an update chain.+ */+ elog(ERROR, "invalid lock status in tuple");+ }+ }+ else+ {+ /* it's an update, but which kind? */+ if (old_infomask2 & HEAP_KEYS_UPDATED)+ status = MultiXactStatusUpdate;+ else+ status = MultiXactStatusNoKeyUpdate;+ }++ res = test_lockmode_for_conflict(status, rawxmax, mode,+ &needwait);+ if (needwait)+ {+ LockBuffer(buf, BUFFER_LOCK_UNLOCK);+ XactLockTableWait(rawxmax);+ goto l4;+ }+ if (res != HeapTupleMayBeUpdated)+ {+ UnlockReleaseBuffer(buf);+ return res;+ }
}
}
--
1.7.10.4
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: alvherre@2ndquadrant.com, katsumata.tomonari@po.ntts.co.jp
Subject: Re: BUG #8434: Why does dead lock occur many times ?
In-Reply-To: <20131128112217.GE5513@eldon.alvh.no-ip.org>
References: <E1VH9At-0005VC-Kk@wrigleys.postgresql.org> <20131004172217.GW5235@eldon.alvh.no-ip.org> <20131127164226.GA14522@eldon.alvh.no-ip.org> <5296BFE1.2090906@po.ntts.co.jp> <20131128112217.GE5513@eldon.alvh.no-ip.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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