Skip to content

Commit c03fc84

Browse files
committed
Add stack-overflow guards in set-operation planning.
create_plan_recurse lacked any stack depth check. This is not per our normal coding rules, but I'd supposed it was safe because earlier planner processing is more complex and presumably should eat more stack. But bug #15033 from Andrew Grossman shows this isn't true, at least not for queries having the form of a many-thousand-way INTERSECT stack. Further testing showed that recurse_set_operations is also capable of being crashed in this way, since it likewise will recurse to the bottom of a parsetree before calling any support functions that might themselves contain any stack checks. However, its stack consumption is only perhaps a third of create_plan_recurse's. It's possible that this particular problem with create_plan_recurse can only manifest in 9.6 and later, since before that we didn't build a Path tree for set operations. But having seen this example, I now have no faith in the proposition that create_plan_recurse doesn't need a stack check, so back-patch to all supported branches. Discussion: https://postgr.es/m/[email protected]
1 parent e5e2cc6 commit c03fc84

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ create_plan_recurse(PlannerInfo *root, Path *best_path)
224224
{
225225
Plan *plan;
226226

227+
/* Guard against stack overflow due to overly complex plans */
228+
check_stack_depth();
229+
227230
switch (best_path->pathtype)
228231
{
229232
case T_SeqScan:

src/backend/optimizer/prep/prepunion.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
219219
int flag, List *refnames_tlist,
220220
List **sortClauses, double *pNumGroups)
221221
{
222+
/* Guard against stack overflow due to overly complex setop nests */
223+
check_stack_depth();
224+
222225
if (IsA(setOp, RangeTblRef))
223226
{
224227
RangeTblRef *rtr = (RangeTblRef *) setOp;

0 commit comments

Comments
 (0)