77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.35 1999/04/30 04:01:44 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.36 1999/05/01 19:47:41 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -167,28 +167,23 @@ cost_index(Oid indexid,
167167/*
168168 * cost_sort
169169 * Determines and returns the cost of sorting a relation by considering
170- *1. the cost of doing an external sort:XXX this is probably too low
170+ * the cost of doing an external sort:XXX this is probably too low
171171 *disk = (p lg p)
172172 *cpu = *CPU-PAGE-WEIGHT* * (t lg t)
173- * 2. the cost of reading the sort result into memory (another seqscan)
174- * unless 'noread' is set
175173 *
176174 * 'pathkeys' is a list of sort keys
177175 * 'tuples' is the number of tuples in the relation
178176 * 'width' is the average tuple width in bytes
179- * 'noread' is a flag indicating that the cost of reading the sort
180- *source data should not be included (i.e., the caller
181- *will account for it separately).
182177 *
183178 * NOTE: some callers currently pass NULL for pathkeys because they
184- * can't conveniently supply sort keys. Since this routine doesn't
179+ * can't conveniently supplythe sort keys. Since this routine doesn't
185180 * currently do anything with pathkeys anyway, that doesn't matter...
181+ * but if it ever does, it should react gracefully to lack of key data.
186182 *
187183 * Returns a flonum.
188- *
189184 */
190185Cost
191- cost_sort (List * pathkeys ,int tuples ,int width , bool noread )
186+ cost_sort (List * pathkeys ,int tuples ,int width )
192187{
193188Cost temp = 0 ;
194189int npages = page_size (tuples ,width );
@@ -200,6 +195,8 @@ cost_sort(List *pathkeys, int tuples, int width, bool noread)
200195/* We want to be sure the cost of a sort is never estimated as zero,
201196 * even if passed-in tuple count is zero. Besides, mustn't do log(0)...
202197 */
198+ if (tuples <=0 )
199+ tuples = 1 ;
203200if (npages <=0 )
204201npages = 1 ;
205202
@@ -210,13 +207,10 @@ cost_sort(List *pathkeys, int tuples, int width, bool noread)
210207temp += npages * log_npages ;
211208
212209/*
213- * could be base_log(pages , NBuffers), but we are only doing 2-way
210+ * could be base_log(tuples , NBuffers), but we are only doing 2-way
214211 * merges
215212 */
216- temp += _cpu_page_wight_ * tuples * log_npages ;
217-
218- if (!noread )
219- temp += cost_seqscan (_NONAME_RELATION_ID_ ,npages ,tuples );
213+ temp += _cpu_page_wight_ * tuples * base_log ((double )tuples ,2.0 );
220214
221215Assert (temp > 0 );
222216
@@ -282,7 +276,8 @@ cost_nestloop(Cost outercost,
282276 * 'outercost' and 'innercost' are the (disk+cpu) costs of scanning the
283277 *outer and inner relations
284278 * 'outersortkeys' and 'innersortkeys' are lists of the keys to be used
285- *to sort the outer and inner relations
279+ *to sort the outer and inner relations (or NIL if no explicit
280+ *sort is needed because the source path is already ordered)
286281 * 'outertuples' and 'innertuples' are the number of tuples in the outer
287282 *and inner relations
288283 * 'outerwidth' and 'innerwidth' are the (typical) widths (in bytes)
@@ -309,9 +304,9 @@ cost_mergejoin(Cost outercost,
309304temp += outercost ;
310305temp += innercost ;
311306if (outersortkeys )/* do we need to sort? */
312- temp += cost_sort (outersortkeys ,outersize ,outerwidth , true );
307+ temp += cost_sort (outersortkeys ,outersize ,outerwidth );
313308if (innersortkeys )/* do we need to sort? */
314- temp += cost_sort (innersortkeys ,innersize ,innerwidth , true );
309+ temp += cost_sort (innersortkeys ,innersize ,innerwidth );
315310temp += _cpu_page_wight_ * (outersize + innersize );
316311
317312Assert (temp >=0 );