From bab163e1218c0c36ccb0c0ef1b8f2d6feb2fc122 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 6 Aug 2015 22:14:07 -0400 Subject: Fix old oversight in join removal logic. Commit 9e7e29c75ad441450f9b8287bd51c13521641e3b introduced an Assert that join removal didn't reduce the eval_at set of any PlaceHolderVar to empty. At first glance it looks like join_is_removable ensures that's true --- but actually, the loop in join_is_removable skips PlaceHolderVars that are not referenced above the join due to be removed. So, if we don't want any empty eval_at sets, the right thing to do is to delete any now-unreferenced PlaceHolderVars from the data structure entirely. Per fuzz testing by Andreas Seltenreich. Back-patch to 9.3 where the aforesaid Assert was added. --- src/backend/optimizer/plan/analyzejoins.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/backend/optimizer/plan/analyzejoins.c') diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 470db878175..dfb2e378312 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -464,16 +464,24 @@ remove_rel_from_query(PlannerInfo *root, int relid, Relids joinrelids) } /* - * Likewise remove references from PlaceHolderVar data structures. + * Likewise remove references from PlaceHolderVar data structures, + * removing any no-longer-needed placeholders entirely. */ - foreach(l, root->placeholder_list) + for (l = list_head(root->placeholder_list); l != NULL; l = nextl) { PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l); - phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid); - Assert(!bms_is_empty(phinfo->ph_eval_at)); - Assert(!bms_is_member(relid, phinfo->ph_lateral)); - phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid); + nextl = lnext(l); + if (bms_is_subset(phinfo->ph_needed, joinrelids)) + root->placeholder_list = list_delete_ptr(root->placeholder_list, + phinfo); + else + { + phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid); + Assert(!bms_is_empty(phinfo->ph_eval_at)); + Assert(!bms_is_member(relid, phinfo->ph_lateral)); + phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid); + } } /* -- cgit v1.2.3