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

Commit464dc03

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 parent68393f3 commit464dc03

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
@@ -1827,20 +1827,38 @@ StartTransaction(void)
18271827

18281828
Assert(XactTopTransactionId==InvalidTransactionId);
18291829

1830-
/*
1831-
* check the current transaction state
1832-
*/
1833-
if (s->state!=TRANS_DEFAULT)
1834-
elog(WARNING,"StartTransaction while in %s state",
1835-
TransStateAsString(s->state));
1830+
/* check the current transaction state */
1831+
Assert(s->state==TRANS_DEFAULT);
18361832

18371833
/*
1838-
* set the current transaction state information appropriately during
1839-
* start processing
1834+
* Set the current transaction state information appropriately during
1835+
* start processing. Note that once the transaction status is switched
1836+
* this process cannot fail until the user ID and the security context
1837+
* flags are fetched below.
18401838
*/
18411839
s->state=TRANS_START;
18421840
s->transactionId=InvalidTransactionId;/* until assigned */
18431841

1842+
/*
1843+
* initialize current transaction state fields
1844+
*
1845+
* note: prevXactReadOnly is not used at the outermost level
1846+
*/
1847+
s->nestingLevel=1;
1848+
s->gucNestLevel=1;
1849+
s->childXids=NULL;
1850+
s->nChildXids=0;
1851+
s->maxChildXids=0;
1852+
1853+
/*
1854+
* Once the current user ID and the security context flags are fetched,
1855+
* both will be properly reset even if transaction startup fails.
1856+
*/
1857+
GetUserIdAndSecContext(&s->prevUser,&s->prevSecContext);
1858+
1859+
/* SecurityRestrictionContext should never be set outside a transaction */
1860+
Assert(s->prevSecContext==0);
1861+
18441862
/*
18451863
* Make sure we've reset xact state variables
18461864
*
@@ -1927,20 +1945,6 @@ StartTransaction(void)
19271945
/* Mark xactStopTimestamp as unset. */
19281946
xactStopTimestamp=0;
19291947

1930-
/*
1931-
* initialize current transaction state fields
1932-
*
1933-
* note: prevXactReadOnly is not used at the outermost level
1934-
*/
1935-
s->nestingLevel=1;
1936-
s->gucNestLevel=1;
1937-
s->childXids=NULL;
1938-
s->nChildXids=0;
1939-
s->maxChildXids=0;
1940-
GetUserIdAndSecContext(&s->prevUser,&s->prevSecContext);
1941-
/* SecurityRestrictionContext should never be set outside a transaction */
1942-
Assert(s->prevSecContext==0);
1943-
19441948
/*
19451949
* initialize other subsystems for new transaction
19461950
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp