@@ -5080,6 +5080,18 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
50805080 */
50815081 ListCell * ilist ;
50825082 ListCell * slist ;
5083+ Oid userid ;
5084+
5085+ /*
5086+ * Determine the user ID to use for privilege checks: either
5087+ * onerel->userid if it's set (e.g., in case we're accessing the table
5088+ * via a view), or the current user otherwise.
5089+ *
5090+ * If we drill down to child relations, we keep using the same userid:
5091+ * it's going to be the same anyway, due to how we set up the relation
5092+ * tree (q.v. build_simple_rel).
5093+ */
5094+ userid = OidIsValid (onerel -> userid ) ? onerel -> userid : GetUserId ();
50835095
50845096 foreach (ilist , onerel -> indexlist )
50855097 {
@@ -5150,18 +5162,10 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
51505162 {
51515163 /* Get index's table for permission check */
51525164 RangeTblEntry * rte ;
5153- Oid userid ;
51545165
51555166 rte = planner_rt_fetch (index -> rel -> relid , root );
51565167 Assert (rte -> rtekind == RTE_RELATION );
51575168
5158- /*
5159- * Use onerel->userid if it's set, in case
5160- * we're accessing the table via a view.
5161- */
5162- userid = OidIsValid (onerel -> userid ) ?
5163- onerel -> userid : GetUserId ();
5164-
51655169 /*
51665170 * For simplicity, we insist on the whole
51675171 * table being selectable, rather than trying
@@ -5212,9 +5216,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
52125216 rte = planner_rt_fetch (varno , root );
52135217 Assert (rte -> rtekind == RTE_RELATION );
52145218
5215- userid = OidIsValid (onerel -> userid ) ?
5216- onerel -> userid : GetUserId ();
5217-
52185219 vardata -> acl_ok =
52195220 rte -> securityQuals == NIL &&
52205221 (pg_class_aclcheck (rte -> relid ,
@@ -5281,8 +5282,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
52815282 /* found a match, see if we can extract pg_statistic row */
52825283 if (equal (node , expr ))
52835284 {
5284- Oid userid ;
5285-
52865285 /*
52875286 * XXX Not sure if we should cache the tuple somewhere.
52885287 * Now we just create a new copy every time.
@@ -5292,13 +5291,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
52925291
52935292 vardata -> freefunc = ReleaseDummy ;
52945293
5295- /*
5296- * Use onerel->userid if it's set, in case we're accessing
5297- * the table via a view.
5298- */
5299- userid = OidIsValid (onerel -> userid ) ?
5300- onerel -> userid : GetUserId ();
5301-
53025294 /*
53035295 * For simplicity, we insist on the whole table being
53045296 * selectable, rather than trying to identify which
@@ -5345,9 +5337,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
53455337 rte = planner_rt_fetch (varno , root );
53465338 Assert (rte -> rtekind == RTE_RELATION );
53475339
5348- userid = OidIsValid (onerel -> userid ) ?
5349- onerel -> userid : GetUserId ();
5350-
53515340 vardata -> acl_ok =
53525341 rte -> securityQuals == NIL &&
53535342 (pg_class_aclcheck (rte -> relid ,
@@ -5486,9 +5475,10 @@ examine_simple_variable(PlannerInfo *root, Var *var,
54865475 rte = planner_rt_fetch (varno , root );
54875476 Assert (rte -> rtekind == RTE_RELATION );
54885477
5489- userid = OidIsValid (onerel -> userid ) ?
5490- onerel -> userid : GetUserId ();
5491-
5478+ /*
5479+ * Fine to use the same userid as it's the same in all
5480+ * relations of a given inheritance tree.
5481+ */
54925482 vardata -> acl_ok =
54935483 rte -> securityQuals == NIL &&
54945484 ((pg_class_aclcheck (rte -> relid , userid ,
0 commit comments