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

Commite24615a

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 parent99504ff commite24615a

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
@@ -262,6 +262,11 @@ static ProcArrayStruct *procArray;
262262

263263
staticPGPROC*allProcs;
264264

265+
/*
266+
* Cache to reduce overhead of repeated calls to TransactionIdIsInProgress()
267+
*/
268+
staticTransactionIdcachedXidIsNotInProgress=InvalidTransactionId;
269+
265270
/*
266271
* Bookkeeping for tracking emulated transactions in recovery
267272
*/
@@ -1404,7 +1409,7 @@ TransactionIdIsInProgress(TransactionId xid)
14041409
* already known to be completed, we can fall out without any access to
14051410
* shared memory.
14061411
*/
1407-
if (TransactionIdIsKnownCompleted(xid))
1412+
if (TransactionIdEquals(cachedXidIsNotInProgress,xid))
14081413
{
14091414
xc_by_known_xact_inc();
14101415
return false;
@@ -1562,6 +1567,7 @@ TransactionIdIsInProgress(TransactionId xid)
15621567
if (nxids==0)
15631568
{
15641569
xc_no_overflow_inc();
1570+
cachedXidIsNotInProgress=xid;
15651571
return false;
15661572
}
15671573

@@ -1576,7 +1582,10 @@ TransactionIdIsInProgress(TransactionId xid)
15761582
xc_slow_answer_inc();
15771583

15781584
if (TransactionIdDidAbort(xid))
1585+
{
1586+
cachedXidIsNotInProgress=xid;
15791587
return false;
1588+
}
15801589

15811590
/*
15821591
* It isn't aborted, so check whether the transaction tree it belongs to
@@ -1594,6 +1603,7 @@ TransactionIdIsInProgress(TransactionId xid)
15941603
}
15951604
}
15961605

1606+
cachedXidIsNotInProgress=xid;
15971607
return false;
15981608
}
15991609

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include"miscadmin.h"
3737
#include"postmaster/postmaster.h"
3838
#include"storage/lwlock.h"
39+
#include"storage/procarray.h"
3940
#include"utils/builtins.h"
4041
#include"utils/memutils.h"
4142
#include"utils/snapmgr.h"
@@ -677,29 +678,22 @@ pg_xact_status(PG_FUNCTION_ARGS)
677678
{
678679
Assert(TransactionIdIsValid(xid));
679680

680-
if (TransactionIdIsCurrentTransactionId(xid))
681+
/*
682+
* Like when doing visiblity checks on a row, check whether the
683+
* transaction is still in progress before looking into the CLOG.
684+
* Otherwise we would incorrectly return "committed" for a transaction
685+
* that is committing and has already updated the CLOG, but hasn't
686+
* removed its XID from the proc array yet. (See comment on that race
687+
* condition at the top of heapam_visibility.c)
688+
*/
689+
if (TransactionIdIsInProgress(xid))
681690
status="in progress";
682691
elseif (TransactionIdDidCommit(xid))
683692
status="committed";
684-
elseif (TransactionIdDidAbort(xid))
685-
status="aborted";
686693
else
687694
{
688-
/*
689-
* The xact is not marked as either committed or aborted in clog.
690-
*
691-
* It could be a transaction that ended without updating clog or
692-
* writing an abort record due to a crash. We can safely assume
693-
* it's aborted if it isn't committed and is older than our
694-
* snapshot xmin.
695-
*
696-
* Otherwise it must be in-progress (or have been at the time we
697-
* checked commit/abort status).
698-
*/
699-
if (TransactionIdPrecedes(xid,GetActiveSnapshot()->xmin))
700-
status="aborted";
701-
else
702-
status="in progress";
695+
/* it must have aborted or crashed */
696+
status="aborted";
703697
}
704698
}
705699
else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp