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

Commitb4721f3

Browse files
committed
Initialize TransactionState and user ID consistently at transaction start
If a failure happens when a transaction is starting between the momentthe transaction status is changed from TRANS_DEFAULT to TRANS_START andthe moment the current user ID and security context flags are fetchedvia GetUserIdAndSecContext(), or before initializing its basic fields,then those may get reset to incorrect values when the transactionaborts, leaving the session in an inconsistent state.One problem reported is that failing a starting transaction at the firstquery of a session could cause several kinds of system crashes on thefollow-up queries.In order to solve that, move the initialization of the transaction statefields and the call of GetUserIdAndSecContext() in charge of fetchingthe current user ID close to the point where the transaction status isswitched to TRANS_START, where there cannot be any error triggeredin-between, per an idea of Tom Lane. This properly ensures that thecurrent user ID, the security context flags and that the basic fields ofTransactionState remain consistent even if the transaction fails whilestarting.Reported-by: Richard GuoDiagnosed-By: Richard GuoAuthor: Michael PaquierReviewed-by: Tom LaneDiscussion:https://postgr.es/m/CAN_9JTxECSb=pEPcb0a8d+6J+bDcOZ4=DgRo_B7Y5gRHJUM=Rw@mail.gmail.comBackpatch-through: 9.4
1 parent3be97b9 commitb4721f3

File tree

1 file changed

+26
-22
lines changed
  • src/backend/access/transam

1 file changed

+26
-22
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,20 +1809,38 @@ StartTransaction(void)
18091809

18101810
Assert(XactTopTransactionId==InvalidTransactionId);
18111811

1812-
/*
1813-
* check the current transaction state
1814-
*/
1815-
if (s->state!=TRANS_DEFAULT)
1816-
elog(WARNING,"StartTransaction while in %s state",
1817-
TransStateAsString(s->state));
1812+
/* check the current transaction state */
1813+
Assert(s->state==TRANS_DEFAULT);
18181814

18191815
/*
1820-
* set the current transaction state information appropriately during
1821-
* start processing
1816+
* Set the current transaction state information appropriately during
1817+
* start processing. Note that once the transaction status is switched
1818+
* this process cannot fail until the user ID and the security context
1819+
* flags are fetched below.
18221820
*/
18231821
s->state=TRANS_START;
18241822
s->transactionId=InvalidTransactionId;/* until assigned */
18251823

1824+
/*
1825+
* initialize current transaction state fields
1826+
*
1827+
* note: prevXactReadOnly is not used at the outermost level
1828+
*/
1829+
s->nestingLevel=1;
1830+
s->gucNestLevel=1;
1831+
s->childXids=NULL;
1832+
s->nChildXids=0;
1833+
s->maxChildXids=0;
1834+
1835+
/*
1836+
* Once the current user ID and the security context flags are fetched,
1837+
* both will be properly reset even if transaction startup fails.
1838+
*/
1839+
GetUserIdAndSecContext(&s->prevUser,&s->prevSecContext);
1840+
1841+
/* SecurityRestrictionContext should never be set outside a transaction */
1842+
Assert(s->prevSecContext==0);
1843+
18261844
/*
18271845
* Make sure we've reset xact state variables
18281846
*
@@ -1909,20 +1927,6 @@ StartTransaction(void)
19091927
/* Mark xactStopTimestamp as unset. */
19101928
xactStopTimestamp=0;
19111929

1912-
/*
1913-
* initialize current transaction state fields
1914-
*
1915-
* note: prevXactReadOnly is not used at the outermost level
1916-
*/
1917-
s->nestingLevel=1;
1918-
s->gucNestLevel=1;
1919-
s->childXids=NULL;
1920-
s->nChildXids=0;
1921-
s->maxChildXids=0;
1922-
GetUserIdAndSecContext(&s->prevUser,&s->prevSecContext);
1923-
/* SecurityRestrictionContext should never be set outside a transaction */
1924-
Assert(s->prevSecContext==0);
1925-
19261930
/*
19271931
* initialize other subsystems for new transaction
19281932
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp