Remove some dead code in selfuncs.c
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 19 Jan 2023 11:54:15 +0000 (12:54 +0100)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 19 Jan 2023 11:54:15 +0000 (12:54 +0100)
RelOptInfo.userid is the same for all relations in a given inheritance
tree, so the code in examine_variable() and example_simple_variable()
that repeats the ACL checks on the root parent rel instead of a given
leaf child relations need not recompute userid too.

Author: Amit Langote <amitlangote09@gmail.com>
Reported-by: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/20221210201753.GA27893@telsasoft.com

src/backend/optimizer/util/relnode.c
src/backend/utils/adt/selfuncs.c

index 75bc20c7c9884d24810050e5c545c763a3cd45fa..0a5632699d6f4fbb2b2c23fea95641c51103f1a3 100644 (file)
@@ -500,7 +500,6 @@ find_join_rel(PlannerInfo *root, Relids relids)
  *
  * Otherwise these fields are left invalid, so GetForeignJoinPaths will not be
  * called for the join relation.
- *
  */
 static void
 set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel,
index 57de51f0db20b439f4880e523e654ae54c830921..4e4888dde4d676b2edc106a711c7979ef826ddaf 100644 (file)
@@ -5080,6 +5080,18 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
         */
        ListCell   *ilist;
        ListCell   *slist;
+       Oid         userid;
+
+       /*
+        * Determine the user ID to use for privilege checks: either
+        * onerel->userid if it's set (e.g., in case we're accessing the table
+        * via a view), or the current user otherwise.
+        *
+        * If we drill down to child relations, we keep using the same userid:
+        * it's going to be the same anyway, due to how we set up the relation
+        * tree (q.v. build_simple_rel).
+        */
+       userid = OidIsValid(onerel->userid) ? onerel->userid : GetUserId();
 
        foreach(ilist, onerel->indexlist)
        {
@@ -5150,18 +5162,10 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
                            {
                                /* Get index's table for permission check */
                                RangeTblEntry *rte;
-                               Oid         userid;
 
                                rte = planner_rt_fetch(index->rel->relid, root);
                                Assert(rte->rtekind == RTE_RELATION);
 
-                               /*
-                                * Use onerel->userid if it's set, in case
-                                * we're accessing the table via a view.
-                                */
-                               userid = OidIsValid(onerel->userid) ?
-                                   onerel->userid : GetUserId();
-
                                /*
                                 * For simplicity, we insist on the whole
                                 * table being selectable, rather than trying
@@ -5212,9 +5216,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
                                        rte = planner_rt_fetch(varno, root);
                                        Assert(rte->rtekind == RTE_RELATION);
 
-                                       userid = OidIsValid(onerel->userid) ?
-                                           onerel->userid : GetUserId();
-
                                        vardata->acl_ok =
                                            rte->securityQuals == NIL &&
                                            (pg_class_aclcheck(rte->relid,
@@ -5281,8 +5282,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
                /* found a match, see if we can extract pg_statistic row */
                if (equal(node, expr))
                {
-                   Oid         userid;
-
                    /*
                     * XXX Not sure if we should cache the tuple somewhere.
                     * Now we just create a new copy every time.
@@ -5292,13 +5291,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
 
                    vardata->freefunc = ReleaseDummy;
 
-                   /*
-                    * Use onerel->userid if it's set, in case we're accessing
-                    * the table via a view.
-                    */
-                   userid = OidIsValid(onerel->userid) ?
-                       onerel->userid : GetUserId();
-
                    /*
                     * For simplicity, we insist on the whole table being
                     * selectable, rather than trying to identify which
@@ -5345,9 +5337,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
                            rte = planner_rt_fetch(varno, root);
                            Assert(rte->rtekind == RTE_RELATION);
 
-                           userid = OidIsValid(onerel->userid) ?
-                               onerel->userid : GetUserId();
-
                            vardata->acl_ok =
                                rte->securityQuals == NIL &&
                                (pg_class_aclcheck(rte->relid,
@@ -5486,9 +5475,10 @@ examine_simple_variable(PlannerInfo *root, Var *var,
                rte = planner_rt_fetch(varno, root);
                Assert(rte->rtekind == RTE_RELATION);
 
-               userid = OidIsValid(onerel->userid) ?
-                   onerel->userid : GetUserId();
-
+               /*
+                * Fine to use the same userid as it's the same in all
+                * relations of a given inheritance tree.
+                */
                vardata->acl_ok =
                    rte->securityQuals == NIL &&
                    ((pg_class_aclcheck(rte->relid, userid,