Skip to content

Commit b33a732

Browse files
committed
Improve trace_sort code to also show the total memory or disk space used.
Per request from Marc.
1 parent 48f3d77 commit b33a732

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/backend/utils/sort/logtape.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* Portions Copyright (c) 1994, Regents of the University of California
6565
*
6666
* IDENTIFICATION
67-
* $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.16 2005/10/15 02:49:37 momjian Exp $
67+
* $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.17 2005/10/18 22:59:37 tgl Exp $
6868
*
6969
*-------------------------------------------------------------------------
7070
*/
@@ -925,3 +925,12 @@ LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
925925
*blocknum = lt->curBlockNumber;
926926
*offset = lt->pos;
927927
}
928+
929+
/*
930+
* Obtain total disk space currently used by a LogicalTapeSet, in blocks.
931+
*/
932+
long
933+
LogicalTapeSetBlocks(LogicalTapeSet *lts)
934+
{
935+
return lts->nFileBlocks;
936+
}

src/backend/utils/sort/tuplesort.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* Portions Copyright (c) 1994, Regents of the University of California
7979
*
8080
* 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 $
8282
*
8383
*-------------------------------------------------------------------------
8484
*/
@@ -134,6 +134,7 @@ struct Tuplesortstate
134134
TupSortStatus status; /* enumerated value as shown above */
135135
bool randomAccess; /* did caller request random access? */
136136
long availMem; /* remaining memory available, in bytes */
137+
long allowedMem; /* total memory allowed, in bytes */
137138
LogicalTapeSet *tapeset; /* logtape.c object for tapes in a temp file */
138139

139140
/*
@@ -433,7 +434,8 @@ tuplesort_begin_common(int workMem, bool randomAccess)
433434

434435
state->status = TSS_INITIAL;
435436
state->randomAccess = randomAccess;
436-
state->availMem = workMem * 1024L;
437+
state->allowedMem = workMem * 1024L;
438+
state->availMem = state->allowedMem;
437439
state->tapeset = NULL;
438440

439441
state->memtupcount = 0;
@@ -582,9 +584,24 @@ void
582584
tuplesort_end(Tuplesortstate *state)
583585
{
584586
int i;
587+
#ifdef TRACE_SORT
588+
long spaceUsed;
589+
#endif
585590

586591
if (state->tapeset)
592+
{
593+
#ifdef TRACE_SORT
594+
spaceUsed = LogicalTapeSetBlocks(state->tapeset);
595+
#endif
587596
LogicalTapeSetClose(state->tapeset);
597+
}
598+
else
599+
{
600+
#ifdef TRACE_SORT
601+
spaceUsed = (state->allowedMem - state->availMem + 1023) / 1024;
602+
#endif
603+
}
604+
588605
if (state->memtuples)
589606
{
590607
for (i = 0; i < state->memtupcount; i++)
@@ -604,8 +621,14 @@ tuplesort_end(Tuplesortstate *state)
604621

605622
#ifdef TRACE_SORT
606623
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+
}
609632
#endif
610633

611634
pfree(state);

src/include/utils/logtape.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.12 2004/12/31 22:03:46 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.13 2005/10/18 22:59:37 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -38,5 +38,6 @@ extern bool LogicalTapeSeek(LogicalTapeSet *lts, int tapenum,
3838
long blocknum, int offset);
3939
extern void LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
4040
long *blocknum, int *offset);
41+
extern long LogicalTapeSetBlocks(LogicalTapeSet *lts);
4142

4243
#endif /* LOGTAPE_H */

0 commit comments

Comments
 (0)