Fix performance issue in exprTypmod(): for a COALESCE expression, it
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 18 Nov 2005 23:08:13 +0000 (23:08 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 18 Nov 2005 23:08:13 +0000 (23:08 +0000)
recursed twice on its first argument, leading to exponential time spent
on a deep nest of COALESCEs ... such as a deeply nested FULL JOIN would
produce.  Per report from Matt Carter.

src/backend/parser/parse_expr.c

index 82a5e2cd030c56bd4bb664f9990b0b4ce384845e..2a8073d17f5901e6a36e0d2906bd8b8dcc447a54 100644 (file)
@@ -1664,8 +1664,12 @@ exprTypmod(Node *expr)
                                int32           typmod;
                                ListCell   *arg;
 
+                               if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
+                                       return -1;
                                typmod = exprTypmod((Node *) linitial(cexpr->args));
-                               foreach(arg, cexpr->args)
+                               if (typmod < 0)
+                                       return -1;      /* no point in trying harder */
+                               for_each_cell(arg, lnext(list_head(cexpr->args)))
                                {
                                        Node       *e = (Node *) lfirst(arg);
 
@@ -1688,8 +1692,12 @@ exprTypmod(Node *expr)
                                int32           typmod;
                                ListCell   *arg;
 
+                               if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
+                                       return -1;
                                typmod = exprTypmod((Node *) linitial(mexpr->args));
-                               foreach(arg, mexpr->args)
+                               if (typmod < 0)
+                                       return -1;      /* no point in trying harder */
+                               for_each_cell(arg, lnext(list_head(mexpr->args)))
                                {
                                        Node       *e = (Node *) lfirst(arg);