Skip to content

Commit e85b729

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 9e5e386 commit e85b729

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
@@ -1737,20 +1737,38 @@ StartTransaction(void)
17371737
s = &TopTransactionStateData;
17381738
CurrentTransactionState = s;
17391739

1740-
/*
1741-
* check the current transaction state
1742-
*/
1743-
if (s->state != TRANS_DEFAULT)
1744-
elog(WARNING, "StartTransaction while in %s state",
1745-
TransStateAsString(s->state));
1740+
/* check the current transaction state */
1741+
Assert(s->state == TRANS_DEFAULT);
17461742

17471743
/*
1748-
* set the current transaction state information appropriately during
1749-
* start processing
1744+
* Set the current transaction state information appropriately during
1745+
* start processing. Note that once the transaction status is switched
1746+
* this process cannot fail until the user ID and the security context
1747+
* flags are fetched below.
17501748
*/
17511749
s->state = TRANS_START;
17521750
s->transactionId = InvalidTransactionId; /* until assigned */
17531751

1752+
/*
1753+
* initialize current transaction state fields
1754+
*
1755+
* note: prevXactReadOnly is not used at the outermost level
1756+
*/
1757+
s->nestingLevel = 1;
1758+
s->gucNestLevel = 1;
1759+
s->childXids = NULL;
1760+
s->nChildXids = 0;
1761+
s->maxChildXids = 0;
1762+
1763+
/*
1764+
* Once the current user ID and the security context flags are fetched,
1765+
* both will be properly reset even if transaction startup fails.
1766+
*/
1767+
GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1768+
1769+
/* SecurityRestrictionContext should never be set outside a transaction */
1770+
Assert(s->prevSecContext == 0);
1771+
17541772
/*
17551773
* Make sure we've reset xact state variables
17561774
*
@@ -1825,20 +1843,6 @@ StartTransaction(void)
18251843
xactStopTimestamp = 0;
18261844
pgstat_report_xact_timestamp(xactStartTimestamp);
18271845

1828-
/*
1829-
* initialize current transaction state fields
1830-
*
1831-
* note: prevXactReadOnly is not used at the outermost level
1832-
*/
1833-
s->nestingLevel = 1;
1834-
s->gucNestLevel = 1;
1835-
s->childXids = NULL;
1836-
s->nChildXids = 0;
1837-
s->maxChildXids = 0;
1838-
GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1839-
/* SecurityRestrictionContext should never be set outside a transaction */
1840-
Assert(s->prevSecContext == 0);
1841-
18421846
/*
18431847
* initialize other subsystems for new transaction
18441848
*/

0 commit comments

Comments
 (0)