aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/plan/createplan.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2013-05-01 18:26:50 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2013-05-01 18:26:50 -0400
commit50c137487c96e629e0e5372bb3d1b5f1a2f71a88 (patch)
tree31972d0bdacb621bc30df6271c983844ddb41725 /src/backend/optimizer/plan/createplan.c
parent200ba1667b3a8d7a9d559d2f05f83d209c9d8267 (diff)
downloadpostgresql-50c137487c96e629e0e5372bb3d1b5f1a2f71a88.tar.gz
postgresql-50c137487c96e629e0e5372bb3d1b5f1a2f71a88.zip
Fix permission tests for views/tables proven empty by constraint exclusion.
A view defined as "select <something> where false" had the curious property that the system wouldn't check whether users had the privileges necessary to select from it. More generally, permissions checks could be skipped for tables referenced in sub-selects or views that were proven empty by constraint exclusion (although some quick testing suggests this seldom happens in cases of practical interest). This happened because the planner failed to include rangetable entries for such tables in the finished plan. This was noticed in connection with erroneous handling of materialized views, but actually the issue is quite unrelated to matviews. Therefore, revert commit 200ba1667b3a8d7a9d559d2f05f83d209c9d8267 in favor of a more direct test for the real problem. Back-patch to 9.2 where the bug was introduced (by commit 7741dd6590073719688891898e85f0cb73453159).
Diffstat (limited to 'src/backend/optimizer/plan/createplan.c')
-rw-r--r--src/backend/optimizer/plan/createplan.c54
1 files changed, 40 insertions, 14 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 65a2e38fc33..e43ef3aa898 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -678,30 +678,49 @@ static Plan *
create_append_plan(PlannerInfo *root, AppendPath *best_path)
{
Append *plan;
- List *tlist = build_relation_tlist(best_path->path.parent);
+ RelOptInfo *rel = best_path->path.parent;
+ List *tlist = build_relation_tlist(rel);
List *subplans = NIL;
ListCell *subpaths;
/*
- * It is possible for the subplans list to contain only one entry, or even
- * no entries. Handle these cases specially.
+ * The subpaths list could be empty, if every child was proven empty by
+ * constraint exclusion. In that case generate a dummy plan that returns
+ * no rows.
*
- * XXX ideally, if there's just one entry, we'd not bother to generate an
- * Append node but just return the single child. At the moment this does
- * not work because the varno of the child scan plan won't match the
- * parent-rel Vars it'll be asked to emit.
+ * Note that an AppendPath with no members is also generated in certain
+ * cases where there was no appending construct at all, but we know the
+ * relation is empty (see set_dummy_rel_pathlist).
*/
if (best_path->subpaths == NIL)
{
- /* Generate a Result plan with constant-FALSE gating qual */
- return (Plan *) make_result(root,
- tlist,
- (Node *) list_make1(makeBoolConst(false,
- false)),
- NULL);
+ /*
+ * If this is a dummy path for a subquery, we have to wrap the
+ * subquery's original plan in a SubqueryScan so that setrefs.c will
+ * do the right things. (In particular, it must pull up the
+ * subquery's rangetable so that the executor will apply permissions
+ * checks to those rels at runtime.)
+ */
+ if (rel->rtekind == RTE_SUBQUERY)
+ {
+ Assert(is_dummy_plan(rel->subplan));
+ return (Plan *) make_subqueryscan(tlist,
+ NIL,
+ rel->relid,
+ rel->subplan);
+ }
+ else
+ {
+ /* Generate a Result plan with constant-FALSE gating qual */
+ return (Plan *) make_result(root,
+ tlist,
+ (Node *) list_make1(makeBoolConst(false,
+ false)),
+ NULL);
+ }
}
- /* Normal case with multiple subpaths */
+ /* Build the plan for each child */
foreach(subpaths, best_path->subpaths)
{
Path *subpath = (Path *) lfirst(subpaths);
@@ -709,6 +728,13 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path)
subplans = lappend(subplans, create_plan_recurse(root, subpath));
}
+ /*
+ * XXX ideally, if there's just one child, we'd not bother to generate an
+ * Append node but just return the single child. At the moment this does
+ * not work because the varno of the child scan plan won't match the
+ * parent-rel Vars it'll be asked to emit.
+ */
+
plan = make_append(subplans, tlist);
return (Plan *) plan;