]> granicus.if.org Git - postgresql/commit
Initialize TransactionState and user ID consistently at transaction start
authorMichael Paquier <michael@paquier.xyz>
Wed, 14 Nov 2018 07:48:17 +0000 (16:48 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 14 Nov 2018 07:48:17 +0000 (16:48 +0900)
commit0d472b1e12efc3b860705cdc629a70d8972f0400
treef49dc94e71f99e63c6848490f5a36db26c8255e7
parente1f25901268cd07095329ce2e705df1de244f913
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
src/backend/access/transam/xact.c