78
78
* Portions Copyright (c) 1994, Regents of the University of California
79
79
*
80
80
* IDENTIFICATION
81
- * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.52 2005/10/15 02:49 :37 momjian Exp $
81
+ * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.53 2005/10/18 22:59 :37 tgl Exp $
82
82
*
83
83
*-------------------------------------------------------------------------
84
84
*/
@@ -134,6 +134,7 @@ struct Tuplesortstate
134
134
TupSortStatus status ; /* enumerated value as shown above */
135
135
bool randomAccess ; /* did caller request random access? */
136
136
long availMem ; /* remaining memory available, in bytes */
137
+ long allowedMem ; /* total memory allowed, in bytes */
137
138
LogicalTapeSet * tapeset ; /* logtape.c object for tapes in a temp file */
138
139
139
140
/*
@@ -433,7 +434,8 @@ tuplesort_begin_common(int workMem, bool randomAccess)
433
434
434
435
state -> status = TSS_INITIAL ;
435
436
state -> randomAccess = randomAccess ;
436
- state -> availMem = workMem * 1024L ;
437
+ state -> allowedMem = workMem * 1024L ;
438
+ state -> availMem = state -> allowedMem ;
437
439
state -> tapeset = NULL ;
438
440
439
441
state -> memtupcount = 0 ;
@@ -582,9 +584,24 @@ void
582
584
tuplesort_end (Tuplesortstate * state )
583
585
{
584
586
int i ;
587
+ #ifdef TRACE_SORT
588
+ long spaceUsed ;
589
+ #endif
585
590
586
591
if (state -> tapeset )
592
+ {
593
+ #ifdef TRACE_SORT
594
+ spaceUsed = LogicalTapeSetBlocks (state -> tapeset );
595
+ #endif
587
596
LogicalTapeSetClose (state -> tapeset );
597
+ }
598
+ else
599
+ {
600
+ #ifdef TRACE_SORT
601
+ spaceUsed = (state -> allowedMem - state -> availMem + 1023 ) / 1024 ;
602
+ #endif
603
+ }
604
+
588
605
if (state -> memtuples )
589
606
{
590
607
for (i = 0 ; i < state -> memtupcount ; i ++ )
@@ -604,8 +621,14 @@ tuplesort_end(Tuplesortstate *state)
604
621
605
622
#ifdef TRACE_SORT
606
623
if (trace_sort )
607
- elog (NOTICE , "sort ended: %s" ,
608
- pg_rusage_show (& state -> ru_start ));
624
+ {
625
+ if (state -> tapeset )
626
+ elog (NOTICE , "external sort ended, %ld disk blocks used: %s" ,
627
+ spaceUsed , pg_rusage_show (& state -> ru_start ));
628
+ else
629
+ elog (NOTICE , "internal sort ended, %ld KB used: %s" ,
630
+ spaceUsed , pg_rusage_show (& state -> ru_start ));
631
+ }
609
632
#endif
610
633
611
634
pfree (state );
0 commit comments