Improve trace_sort code to also show the total memory or disk space used.

Per request from Marc.
This commit is contained in:
Tom Lane 2005-10-18 22:59:37 +00:00
parent 48f3d77858
commit b33a732264
3 changed files with 39 additions and 6 deletions

View File

@ -64,7 +64,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.16 2005/10/15 02:49:37 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.17 2005/10/18 22:59:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -925,3 +925,12 @@ LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
*blocknum = lt->curBlockNumber;
*offset = lt->pos;
}
/*
* Obtain total disk space currently used by a LogicalTapeSet, in blocks.
*/
long
LogicalTapeSetBlocks(LogicalTapeSet *lts)
{
return lts->nFileBlocks;
}

View File

@ -78,7 +78,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.52 2005/10/15 02:49:37 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.53 2005/10/18 22:59:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -134,6 +134,7 @@ struct Tuplesortstate
TupSortStatus status; /* enumerated value as shown above */
bool randomAccess; /* did caller request random access? */
long availMem; /* remaining memory available, in bytes */
long allowedMem; /* total memory allowed, in bytes */
LogicalTapeSet *tapeset; /* logtape.c object for tapes in a temp file */
/*
@ -433,7 +434,8 @@ tuplesort_begin_common(int workMem, bool randomAccess)
state->status = TSS_INITIAL;
state->randomAccess = randomAccess;
state->availMem = workMem * 1024L;
state->allowedMem = workMem * 1024L;
state->availMem = state->allowedMem;
state->tapeset = NULL;
state->memtupcount = 0;
@ -582,9 +584,24 @@ void
tuplesort_end(Tuplesortstate *state)
{
int i;
#ifdef TRACE_SORT
long spaceUsed;
#endif
if (state->tapeset)
{
#ifdef TRACE_SORT
spaceUsed = LogicalTapeSetBlocks(state->tapeset);
#endif
LogicalTapeSetClose(state->tapeset);
}
else
{
#ifdef TRACE_SORT
spaceUsed = (state->allowedMem - state->availMem + 1023) / 1024;
#endif
}
if (state->memtuples)
{
for (i = 0; i < state->memtupcount; i++)
@ -604,8 +621,14 @@ tuplesort_end(Tuplesortstate *state)
#ifdef TRACE_SORT
if (trace_sort)
elog(NOTICE, "sort ended: %s",
pg_rusage_show(&state->ru_start));
{
if (state->tapeset)
elog(NOTICE, "external sort ended, %ld disk blocks used: %s",
spaceUsed, pg_rusage_show(&state->ru_start));
else
elog(NOTICE, "internal sort ended, %ld KB used: %s",
spaceUsed, pg_rusage_show(&state->ru_start));
}
#endif
pfree(state);

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.12 2004/12/31 22:03:46 pgsql Exp $
* $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.13 2005/10/18 22:59:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -38,5 +38,6 @@ extern bool LogicalTapeSeek(LogicalTapeSet *lts, int tapenum,
long blocknum, int offset);
extern void LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
long *blocknum, int *offset);
extern long LogicalTapeSetBlocks(LogicalTapeSet *lts);
#endif /* LOGTAPE_H */