summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2022-12-22 15:35:02 +0000
committerTom Lane2022-12-22 15:35:02 +0000
commit5beb7881fb27d1830fa4c57ba263c8042449a599 (patch)
tree9dd24b5565048c8d2201af195f49bec1a5111165
parent73bb72f0ca2833329b59ebaf9476be3be71e462f (diff)
Add some recursion and looping defenses in prepjointree.c.
Andrey Lepikhov demonstrated a case where we spend an unreasonable amount of time in pull_up_subqueries(). Not only is that recursing with no explicit check for stack overrun, but the code seems not interruptable by control-C. Let's stick a CHECK_FOR_INTERRUPTS there, along with sprinkling some stack depth checks. An actual fix for the excessive time consumption seems a bit risky to back-patch; but this isn't, so let's do so. Discussion: https://postgr.es/m/[email protected]
-rw-r--r--src/backend/optimizer/prep/prepjointree.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index c2239d18b4..411412c5fa 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -27,6 +27,7 @@
#include "catalog/pg_type.h"
#include "funcapi.h"
+#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/multibitmapset.h"
#include "nodes/nodeFuncs.h"
@@ -308,6 +309,9 @@ static Node *
pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
Relids *relids)
{
+ /* Since this function recurses, it could be driven to stack overflow. */
+ check_stack_depth();
+
if (jtnode == NULL)
{
*relids = NULL;
@@ -805,6 +809,11 @@ pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
JoinExpr *lowest_nulling_outer_join,
AppendRelInfo *containing_appendrel)
{
+ /* Since this function recurses, it could be driven to stack overflow. */
+ check_stack_depth();
+ /* Also, since it's a bit expensive, let's check for query cancel. */
+ CHECK_FOR_INTERRUPTS();
+
Assert(jtnode != NULL);
if (IsA(jtnode, RangeTblRef))
{
@@ -1937,6 +1946,9 @@ is_simple_union_all(Query *subquery)
static bool
is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
{
+ /* Since this function recurses, it could be driven to stack overflow. */
+ check_stack_depth();
+
if (IsA(setOp, RangeTblRef))
{
RangeTblRef *rtr = (RangeTblRef *) setOp;