aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2018-01-28 13:39:07 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2018-01-28 13:39:07 -0500
commit4e9fb4bfe2e33aff941e29ec55972d68f4cdb69c (patch)
treeb321e2ffd122be67d7e96d6a24eb3a9062e1d78c
parent462402be873dfa252c8ab34ff42a7af252cfa481 (diff)
downloadpostgresql-4e9fb4bfe2e33aff941e29ec55972d68f4cdb69c.tar.gz
postgresql-4e9fb4bfe2e33aff941e29ec55972d68f4cdb69c.zip
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/20180127050845.28812.58244@wrigleys.postgresql.org
-rw-r--r--src/backend/optimizer/plan/createplan.c3
-rw-r--r--src/backend/optimizer/prep/prepunion.c3
2 files changed, 6 insertions, 0 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index e230329a452..5de9235f5dd 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -343,6 +343,9 @@ create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
{
Plan *plan;
+ /* Guard against stack overflow due to overly complex plans */
+ check_stack_depth();
+
switch (best_path->pathtype)
{
case T_SeqScan:
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 266ee4db5e8..1d61a0d616f 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -253,6 +253,9 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
List **pTargetList,
double *pNumGroups)
{
+ /* Guard against stack overflow due to overly complex setop nests */
+ check_stack_depth();
+
if (IsA(setOp, RangeTblRef))
{
RangeTblRef *rtr = (RangeTblRef *) setOp;