On Wed, Mar 4, 2015 at 9:12 PM, Greg Stark <[email protected]> wrote:
> This sounded familiar... I pointed out the same thing a while back and Tom
> had some feedback on what to do about it:
>
> http://www.postgresql.org/message-id/[email protected]
Translated into code I guess that this gives the attached patch.
--
Michael
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c
index 6f2a749..efa4be1 100644
--- a/src/backend/commands/view.c
+++ b/src/backend/commands/view.c
@@ -345,6 +345,7 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
List *new_rt;
RangeTblEntry *rt_entry1,
*rt_entry2;
+ ParseState *pstate;
/*
* Make a copy of the given parsetree. It's not so much that we don't
@@ -356,6 +357,9 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
*/
viewParse = (Query *) copyObject(viewParse);
+ /* Create a dummy ParseState for addRangeTableEntryForRelation */
+ pstate = make_parsestate(NULL);
+
/* need to open the rel for addRangeTableEntryForRelation */
viewRel = relation_open(viewOid, AccessShareLock);
@@ -363,10 +367,10 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
* Create the 2 new range table entries and form the new range table...
* OLD first, then NEW....
*/
- rt_entry1 = addRangeTableEntryForRelation(NULL, viewRel,
+ rt_entry1 = addRangeTableEntryForRelation(pstate, viewRel,
makeAlias("old", NIL),
false, false);
- rt_entry2 = addRangeTableEntryForRelation(NULL, viewRel,
+ rt_entry2 = addRangeTableEntryForRelation(pstate, viewRel,
makeAlias("new", NIL),
false, false);
/* Must override addRangeTableEntry's default access-check flags */
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 5a1d539..acfd0bc 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -1233,6 +1233,7 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
RangeTblRef *rtr;
List *subquery_vars;
Node *quals;
+ ParseState *pstate;
Assert(sublink->subLinkType == ANY_SUBLINK);
@@ -1264,6 +1265,9 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
if (contain_volatile_functions(sublink->testexpr))
return NULL;
+ /* Create a dummy ParseState for addRangeTableEntryForSubquery */
+ pstate = make_parsestate(NULL);
+
/*
* Okay, pull up the sub-select into upper range table.
*
@@ -1272,7 +1276,7 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
* below). Therefore this is a lot easier than what pull_up_subqueries has
* to go through.
*/
- rte = addRangeTableEntryForSubquery(NULL,
+ rte = addRangeTableEntryForSubquery(pstate,
subselect,
makeAlias("ANY_subquery", NIL),
false,
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index f416fc2..24fe998 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -1078,6 +1078,8 @@ addRangeTableEntryForRelation(ParseState *pstate,
RangeTblEntry *rte = makeNode(RangeTblEntry);
char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
+ Assert(pstate != NULL);
+
rte->rtekind = RTE_RELATION;
rte->alias = alias;
rte->relid = RelationGetRelid(rel);
@@ -1109,8 +1111,7 @@ addRangeTableEntryForRelation(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
@@ -1135,6 +1136,8 @@ addRangeTableEntryForSubquery(ParseState *pstate,
int varattno;
ListCell *tlistitem;
+ Assert(pstate != NULL);
+
rte->rtekind = RTE_SUBQUERY;
rte->relid = InvalidOid;
rte->subquery = subquery;
@@ -1187,8 +1190,7 @@ addRangeTableEntryForSubquery(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
@@ -1224,6 +1226,8 @@ addRangeTableEntryForFunction(ParseState *pstate,
int natts,
totalatts;
+ Assert(pstate != NULL);
+
rte->rtekind = RTE_FUNCTION;
rte->relid = InvalidOid;
rte->subquery = NULL;
@@ -1441,8 +1445,7 @@ addRangeTableEntryForFunction(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
@@ -1466,6 +1469,8 @@ addRangeTableEntryForValues(ParseState *pstate,
int numaliases;
int numcolumns;
+ Assert(pstate != NULL);
+
rte->rtekind = RTE_VALUES;
rte->relid = InvalidOid;
rte->subquery = NULL;
@@ -1513,8 +1518,7 @@ addRangeTableEntryForValues(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
@@ -1536,6 +1540,8 @@ addRangeTableEntryForJoin(ParseState *pstate,
Alias *eref;
int numaliases;
+ Assert(pstate != NULL);
+
/*
* Fail if join has too many columns --- we must be able to reference any
* of the columns with an AttrNumber.
@@ -1581,8 +1587,7 @@ addRangeTableEntryForJoin(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
@@ -1607,6 +1612,8 @@ addRangeTableEntryForCTE(ParseState *pstate,
int varattno;
ListCell *lc;
+ Assert(pstate != NULL);
+
rte->rtekind = RTE_CTE;
rte->ctename = cte->ctename;
rte->ctelevelsup = levelsup;
@@ -1681,8 +1688,7 @@ addRangeTableEntryForCTE(ParseState *pstate,
* Add completed RTE to pstate's range table list, but not to join list
* nor namespace --- caller must do that if appropriate.
*/
- if (pstate != NULL)
- pstate->p_rtable = lappend(pstate->p_rtable, rte);
+ pstate->p_rtable = lappend(pstate->p_rtable, rte);
return rte;
}
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers