Skip to content

Commit b4721f3

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 3be97b9 commit b4721f3

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

+26-22
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)