CurrentUserId may be invalid during the rest of a session

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: CurrentUserId may be invalid during the rest of a session
Date: 2017-08-31 10:02:56
Message-ID: CAMbWs4-Mys=hBQSevTA8Zpd-TYFnb=XuHhN2TnktXMsfMUbjiQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

During the first transaction starting phase within a backend, if there is
an 'ereport' after setting transaction state but before
saving CurrentUserId into 'prevUser' in 'TransactionStateData',
CurrentUserId will be invalid in the rest of the session.

Take branch 'REL9_6_STABLE' for example:

1797 static void
1798 StartTransaction(void)
1799 {
1800 TransactionState s;

......

1822 s->state = TRANS_START;

*<=======
'ereport' in this window*
......

1909 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);

......

1927 }

If 'ereport' occurs in the described window, CurrentUserId will have no
chance to be saved into 'prevUser' and 'prevUser' will remain to be
InvalidOid as this is the first transaction of the session.

As transaction state has been set to be TRANS_START, 'AbortTransaction'
will be called then and CurrentUserId will be restored with 'prevUser',
which is InvalidOid. So in the rest of the session, CurrentUserId will be
invalid.

The invalid CurrentUserId may cause assertion failure or other issues, for
example:

(gdb) bt
#0 0x00007f3d8ced9495 in raise () from /lib64/libc.so.6
#1 0x00007f3d8cedac75 in abort () from /lib64/libc.so.6
#2 0x000000000095fdbd in ExceptionalCondition (conditionName=0xb72838
"!(((bool) ((CurrentUserId) != ((Oid) 0))))", errorType=0xb726ff "BadState",
fileName=0xb726c0 "miscinit.c", lineNumber=284) at assert.c:54
#3 0x0000000000971b88 in GetUserId () at miscinit.c:284
#4 0x00000000005559c4 in recomputeNamespacePath () at namespace.c:3496
#5 0x0000000000551d53 in RelnameGetRelid (relname=0x1d3f288 "t1") at
namespace.c:673
#6 0x00000000005514a7 in RangeVarGetRelidExtended (relation=0x1d3f2a8,
lockmode=1, missing_ok=1 '\001', nowait=0 '\000', callback=0x0,
callback_arg=0x0)
at namespace.c:326

Is this expected behavior?

Thanks
-Richard

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Beena Emerson 2017-08-31 11:20:15 Re: increasing the default WAL segment size
Previous Message Jeff Davis 2017-08-31 08:52:05 Re: Range Merge Join v1