Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf53089

Browse files
committed
Fix visibility check when XID is committed in CLOG but not in procarray.
TransactionIdIsInProgress had a fast path to return 'false' if thesingle-item CLOG cache said that the transaction was known to becommitted. However, that was wrong, because a transaction is firstmarked as committed in the CLOG but doesn't become visible to othersuntil it has removed its XID from the proc array. That could lead to anerror: ERROR: t_xmin is uncommitted in tuple to be updatedor for an UPDATE to go ahead without blocking, before the previousUPDATE on the same row was made visible.The window is usually very short, but synchronous replication makes itmuch wider, because the wait for synchronous replica happens in thatwindow.Another thing that makes it hard to hit is that it's hard to get sucha commit-in-progress transaction into the single item CLOG cache.Normally, if you call TransactionIdIsInProgress on such a transaction,it determines that the XID is in progress without checking the CLOGand without populating the cache. One way to prime the cache is toexplicitly call pg_xact_status() on the XID. Another way is to use alot of subtransactions, so that the subxid cache in the proc array isoverflown, making TransactionIdIsInProgress rely on pg_subtrans andCLOG checks.This has been broken ever since it was introduced in 2008, but the racecondition is very hard to hit, especially without synchronousreplication. There were a couple of reports of the error starting fromsummer 2021, but no one was able to find the root cause then.TransactionIdIsKnownCompleted() is now unused. In 'master', remove it,but I left it in place in backbranches in case it's used by extensions.Also change pg_xact_status() to check TransactionIdIsInProgress().Previously, it only checked the CLOG, and returned "committed" beforethe transaction was actually made visible to other queries. Note thatthis also means that you cannot use pg_xact_status() to reproduce thebug anymore, even if the code wasn't fixed.Report and analysis by Konstantin Knizhnik. Patch by Simon Riggs, withthe pg_xact_status() change added by me.Author: Simon RiggsReviewed-by: Andres FreundDiscussion:https://www.postgresql.org/message-id/flat/4da7913d-398c-e2ad-d777-f752cf7f0bbb%40garret.ru
1 parentfe25c85 commitaf53089

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

‎src/backend/access/transam/transam.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,15 @@ TransactionIdDidAbort(TransactionId transactionId)
226226
*
227227
* This does NOT look into pg_xact but merely probes our local cache
228228
* (and so it's not named TransactionIdDidComplete, which would be the
229-
* appropriate name for a function that worked that way). The intended
230-
* use is just to short-circuit TransactionIdIsInProgress calls when doing
231-
* repeated heapam_visibility.c checks for the same XID. If this isn't
232-
* extremely fast then it will be counterproductive.
229+
* appropriate name for a function that worked that way).
230+
*
231+
* NB: This is unused, and will be removed in v15. This was used to
232+
* short-circuit TransactionIdIsInProgress, but that was wrong for a
233+
* transaction that was known to be marked as committed in CLOG but not
234+
* yet removed from the proc array. This is kept in backbranches just in
235+
* case it is still used by extensions. However, extensions doing
236+
* something similar to tuple visibility checks should also be careful to
237+
* check the proc array first!
233238
*
234239
* Note:
235240
*Assumes transaction identifier is valid.

‎src/backend/storage/ipc/procarray.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ static ProcArrayStruct *procArray;
101101
staticPGPROC*allProcs;
102102
staticPGXACT*allPgXact;
103103

104+
/*
105+
* Cache to reduce overhead of repeated calls to TransactionIdIsInProgress()
106+
*/
107+
staticTransactionIdcachedXidIsNotInProgress=InvalidTransactionId;
108+
104109
/*
105110
* Bookkeeping for tracking emulated transactions in recovery
106111
*/
@@ -1029,7 +1034,7 @@ TransactionIdIsInProgress(TransactionId xid)
10291034
* already known to be completed, we can fall out without any access to
10301035
* shared memory.
10311036
*/
1032-
if (TransactionIdIsKnownCompleted(xid))
1037+
if (TransactionIdEquals(cachedXidIsNotInProgress,xid))
10331038
{
10341039
xc_by_known_xact_inc();
10351040
return false;
@@ -1179,6 +1184,7 @@ TransactionIdIsInProgress(TransactionId xid)
11791184
if (nxids==0)
11801185
{
11811186
xc_no_overflow_inc();
1187+
cachedXidIsNotInProgress=xid;
11821188
return false;
11831189
}
11841190

@@ -1193,7 +1199,10 @@ TransactionIdIsInProgress(TransactionId xid)
11931199
xc_slow_answer_inc();
11941200

11951201
if (TransactionIdDidAbort(xid))
1202+
{
1203+
cachedXidIsNotInProgress=xid;
11961204
return false;
1205+
}
11971206

11981207
/*
11991208
* It isn't aborted, so check whether the transaction tree it belongs to
@@ -1211,6 +1220,7 @@ TransactionIdIsInProgress(TransactionId xid)
12111220
}
12121221
}
12131222

1223+
cachedXidIsNotInProgress=xid;
12141224
return false;
12151225
}
12161226

‎src/backend/utils/adt/txid.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include"libpq/pqformat.h"
3131
#include"postmaster/postmaster.h"
3232
#include"storage/lwlock.h"
33+
#include"storage/procarray.h"
3334
#include"utils/builtins.h"
3435
#include"utils/memutils.h"
3536
#include"utils/snapmgr.h"
@@ -759,29 +760,22 @@ txid_status(PG_FUNCTION_ARGS)
759760
{
760761
Assert(TransactionIdIsValid(xid));
761762

762-
if (TransactionIdIsCurrentTransactionId(xid))
763+
/*
764+
* Like when doing visiblity checks on a row, check whether the
765+
* transaction is still in progress before looking into the CLOG.
766+
* Otherwise we would incorrectly return "committed" for a transaction
767+
* that is committing and has already updated the CLOG, but hasn't
768+
* removed its XID from the proc array yet. (See comment on that race
769+
* condition at the top of heapam_visibility.c)
770+
*/
771+
if (TransactionIdIsInProgress(xid))
763772
status="in progress";
764773
elseif (TransactionIdDidCommit(xid))
765774
status="committed";
766-
elseif (TransactionIdDidAbort(xid))
767-
status="aborted";
768775
else
769776
{
770-
/*
771-
* The xact is not marked as either committed or aborted in clog.
772-
*
773-
* It could be a transaction that ended without updating clog or
774-
* writing an abort record due to a crash. We can safely assume
775-
* it's aborted if it isn't committed and is older than our
776-
* snapshot xmin.
777-
*
778-
* Otherwise it must be in-progress (or have been at the time we
779-
* checked commit/abort status).
780-
*/
781-
if (TransactionIdPrecedes(xid,GetActiveSnapshot()->xmin))
782-
status="aborted";
783-
else
784-
status="in progress";
777+
/* it must have aborted or crashed */
778+
status="aborted";
785779
}
786780
}
787781
else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp