OSDN Git Service

Support window functions a la SQL:2008.
[pg-rex/syncrep.git] / src / backend / optimizer / path / costsize.c
index 0b9c581..7f30dde 100644 (file)
@@ -54,7 +54,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.201 2008/11/22 22:47:05 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.202 2008/12/28 18:53:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1284,6 +1284,40 @@ cost_agg(Path *path, PlannerInfo *root,
 }
 
 /*
+ * cost_windowagg
+ *             Determines and returns the cost of performing a WindowAgg plan node,
+ *             including the cost of its input.
+ *
+ * Input is assumed already properly sorted.
+ */
+void
+cost_windowagg(Path *path, PlannerInfo *root,
+                          int numWindowFuncs, int numPartCols, int numOrderCols,
+                          Cost input_startup_cost, Cost input_total_cost,
+                          double input_tuples)
+{
+       Cost            startup_cost;
+       Cost            total_cost;
+
+       startup_cost = input_startup_cost;
+       total_cost = input_total_cost;
+
+       /*
+        * We charge one cpu_operator_cost per window function per tuple (often a
+        * drastic underestimate, but without a way to gauge how many tuples the
+        * window function will fetch, it's hard to do better).  We also charge
+        * cpu_operator_cost per grouping column per tuple for grouping
+        * comparisons, plus cpu_tuple_cost per tuple for general overhead.
+        */
+       total_cost += cpu_operator_cost * input_tuples * numWindowFuncs;
+       total_cost += cpu_operator_cost * input_tuples * (numPartCols + numOrderCols);
+       total_cost += cpu_tuple_cost * input_tuples;
+
+       path->startup_cost = startup_cost;
+       path->total_cost = total_cost;
+}
+
+/*
  * cost_group
  *             Determines and returns the cost of performing a Group plan node,
  *             including the cost of its input.
@@ -2155,6 +2189,11 @@ cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
         * Vars and Consts are charged zero, and so are boolean operators (AND,
         * OR, NOT). Simplistic, but a lot better than no model at all.
         *
+        * Note that Aggref and WindowFunc nodes are (and should be) treated
+        * like Vars --- whatever execution cost they have is absorbed into
+        * plan-node-specific costing.  As far as expression evaluation is
+        * concerned they're just like Vars.
+        *
         * Should we try to account for the possibility of short-circuit
         * evaluation of AND/OR?  Probably *not*, because that would make the
         * results depend on the clause ordering, and we are not in any position