Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 464dc03

Browse files
committed
Initialize TransactionState and user ID consistently at transaction start
If a failure happens when a transaction is starting between the moment the transaction status is changed from TRANS_DEFAULT to TRANS_START and the moment the current user ID and security context flags are fetched via GetUserIdAndSecContext(), or before initializing its basic fields, then those may get reset to incorrect values when the transaction aborts, leaving the session in an inconsistent state. One problem reported is that failing a starting transaction at the first query of a session could cause several kinds of system crashes on the follow-up queries. In order to solve that, move the initialization of the transaction state fields and the call of GetUserIdAndSecContext() in charge of fetching the current user ID close to the point where the transaction status is switched to TRANS_START, where there cannot be any error triggered in-between, per an idea of Tom Lane. This properly ensures that the current user ID, the security context flags and that the basic fields of TransactionState remain consistent even if the transaction fails while starting. Reported-by: Richard Guo Diagnosed-By: Richard Guo Author: Michael Paquier Reviewed-by: Tom Lane Discussion: https://postgr.es/m/CAN_9JTxECSb=pEPcb0a8d+6J+bDcOZ4=DgRo_B7Y5gRHJUM=Rw@mail.gmail.com Backpatch-through: 9.4
1 parent 68393f3 commit 464dc03

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)