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

Commit 0d472b1

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 e1f2590 commit 0d472b1

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

18311831
Assert(XactTopTransactionId == InvalidTransactionId);
18321832

1833-
/*
1834-
* check the current transaction state
1835-
*/
1836-
if (s->state != TRANS_DEFAULT)
1837-
elog(WARNING, "StartTransaction while in %s state",
1838-
TransStateAsString(s->state));
1833+
/* check the current transaction state */
1834+
Assert(s->state == TRANS_DEFAULT);
18391835

18401836
/*
1841-
* set the current transaction state information appropriately during
1842-
* start processing
1837+
* Set the current transaction state information appropriately during
1838+
* start processing. Note that once the transaction status is switched
1839+
* this process cannot fail until the user ID and the security context
1840+
* flags are fetched below.
18431841
*/
18441842
s->state = TRANS_START;
18451843
s->transactionId = InvalidTransactionId; /* until assigned */
18461844

1845+
/*
1846+
* initialize current transaction state fields
1847+
*
1848+
* note: prevXactReadOnly is not used at the outermost level
1849+
*/
1850+
s->nestingLevel = 1;
1851+
s->gucNestLevel = 1;
1852+
s->childXids = NULL;
1853+
s->nChildXids = 0;
1854+
s->maxChildXids = 0;
1855+
1856+
/*
1857+
* Once the current user ID and the security context flags are fetched,
1858+
* both will be properly reset even if transaction startup fails.
1859+
*/
1860+
GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1861+
1862+
/* SecurityRestrictionContext should never be set outside a transaction */
1863+
Assert(s->prevSecContext == 0);
1864+
18471865
/*
18481866
* Make sure we've reset xact state variables
18491867
*
@@ -1924,20 +1942,6 @@ StartTransaction(void)
19241942
xactStopTimestamp = 0;
19251943
pgstat_report_xact_timestamp(xactStartTimestamp);
19261944

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

0 commit comments

Comments
 (0)