pgsql: Initialize TransactionState and user ID consistently at transact

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Initialize TransactionState and user ID consistently at transact
Date: 2018-11-14 07:50:10
Message-ID: E1gMpwA-0006Ck-22@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
REL9_6_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0d472b1e12efc3b860705cdc629a70d8972f0400

Modified Files
--------------
src/backend/access/transam/xact.c | 48 +++++++++++++++++++++------------------
1 file changed, 26 insertions(+), 22 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2018-11-14 14:02:46 Re: pgsql: Add flag values in WAL description to all heap records
Previous Message Michael Paquier 2018-11-14 01:33:57 pgsql: Add flag values in WAL description to all heap records