Skip to content

Commit a316a3b

Browse files
committed
Correctly set userid of subquery relations' child rels
The RelOptInfo->userid field (the user ID to check permissions as) of an "otherrel" relation was being copied from its parent relation, which is correct in most cases but wrong when the parent is a subquery. In that case, using the value from the RTEPermissionInfo of the child itself is the appropriate thing to do. Coming up with a test case where user-visible behavior changes proves hard enough, so we don't add one here. Bug introduced by a61b1f7, discovered by Amit while reviewing nearby code. Author: Amit Langote <amitlangote09@gmail.com> Discussion: https://postgr.es/m/CA+HiwqE0WY_AhLnGtTsY7eYebG212XWbM-D8gr2A_ToOHyCywQ@mail.gmail.com
1 parent 94cad7a commit a316a3b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/backend/optimizer/util/relnode.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,22 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
233233
rel->serverid = InvalidOid;
234234
if (rte->rtekind == RTE_RELATION)
235235
{
236+
Assert(parent == NULL ||
237+
parent->rtekind == RTE_RELATION ||
238+
parent->rtekind == RTE_SUBQUERY);
239+
236240
/*
237-
* Get the userid from the relation's RTEPermissionInfo, though only
238-
* the tables mentioned in query are assigned RTEPermissionInfos.
239-
* Child relations (otherrels) simply use the parent's value.
241+
* For any RELATION rte, we need a userid with which to check
242+
* permission access. Baserels simply use their own
243+
* RTEPermissionInfo's checkAsUser.
244+
*
245+
* For otherrels normally there's no RTEPermissionInfo, so we use the
246+
* parent's, which normally has one. The exceptional case is that the
247+
* parent is a subquery, in which case the otherrel will have its own.
240248
*/
241-
if (parent == NULL)
249+
if (rel->reloptkind == RELOPT_BASEREL ||
250+
(rel->reloptkind == RELOPT_OTHER_MEMBER_REL &&
251+
parent->rtekind == RTE_SUBQUERY))
242252
{
243253
RTEPermissionInfo *perminfo;
244254

0 commit comments

Comments
 (0)