Skip to content

Commit 53e47cd

Browse files
committed
Add a trace_sort option to help with measuring resource usage of external
sort operations. Per recent discussion. Simon Riggs and Tom Lane.
1 parent 12992ab commit 53e47cd

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

doc/src/sgml/config.sgml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.23 2005/09/24 23:25:31 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.24 2005/10/03 22:55:50 tgl Exp $
33
-->
44
<chapter Id="runtime-config">
55
<title>Run-time Configuration</title>
@@ -4071,6 +4071,21 @@ plruby.use_strict = true # generates error: unknown class name
40714071
</listitem>
40724072
</varlistentry>
40734073

4074+
<varlistentry id="guc-trace-sort" xreflabel="trace_sort">
4075+
<term><varname>trace_sort</varname> (<type>boolean</type>)</term>
4076+
<indexterm>
4077+
<primary><varname>trace_sort</> configuration parameter</primary>
4078+
</indexterm>
4079+
<listitem>
4080+
<para>
4081+
If on, emit information about resource usage during sort operations.
4082+
This option is only available if the <symbol>TRACE_SORT</symbol> macro
4083+
was defined when <productname>PostgreSQL</productname> was compiled.
4084+
(However, <symbol>TRACE_SORT</symbol> is currently defined by default.)
4085+
</para>
4086+
</listitem>
4087+
</varlistentry>
4088+
40744089
<varlistentry>
40754090
<term><varname>trace_locks</varname> (<type>boolean</type>)</term>
40764091
<term><varname>trace_lwlocks</varname> (<type>boolean</type>)</term>

src/backend/utils/misc/guc.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.289 2005/09/24 22:54:39 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.290 2005/10/03 22:55:51 tgl Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -92,6 +92,9 @@ extern int CommitDelay;
9292
extern int CommitSiblings;
9393
extern char *default_tablespace;
9494
extern bool fullPageWrites;
95+
#ifdef TRACE_SORT
96+
extern bool trace_sort;
97+
#endif
9598

9699
static const char *assign_log_destination(const char *value,
97100
bool doit, GucSource source);
@@ -891,6 +894,18 @@ static struct config_bool ConfigureNamesBool[] =
891894
false, NULL, NULL
892895
},
893896

897+
#ifdef TRACE_SORT
898+
{
899+
{"trace_sort", PGC_USERSET, DEVELOPER_OPTIONS,
900+
gettext_noop("Emit information about resource usage in sorting."),
901+
NULL,
902+
GUC_NOT_IN_SAMPLE
903+
},
904+
&trace_sort,
905+
false, NULL, NULL
906+
},
907+
#endif
908+
894909
#ifdef WAL_DEBUG
895910
{
896911
{"wal_debug", PGC_SUSET, DEVELOPER_OPTIONS,

src/backend/utils/sort/tuplesort.c

Lines changed: 81 additions & 1 deletion
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.50 2005/09/23 15:36:57 tgl Exp $
81+
* $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.51 2005/10/03 22:55:54 tgl Exp $
8282
*
8383
*-------------------------------------------------------------------------
8484
*/
@@ -95,10 +95,17 @@
9595
#include "utils/logtape.h"
9696
#include "utils/lsyscache.h"
9797
#include "utils/memutils.h"
98+
#include "utils/pg_rusage.h"
9899
#include "utils/syscache.h"
99100
#include "utils/tuplesort.h"
100101

101102

103+
/* GUC variable */
104+
#ifdef TRACE_SORT
105+
bool trace_sort = false;
106+
#endif
107+
108+
102109
/*
103110
* Possible states of a Tuplesort object. These denote the states that
104111
* persist between calls of Tuplesort routines.
@@ -283,6 +290,13 @@ struct Tuplesortstate
283290
/* we need typelen and byval in order to know how to copy the Datums. */
284291
int datumTypeLen;
285292
bool datumTypeByVal;
293+
294+
/*
295+
* Resource snapshot for time of sort start.
296+
*/
297+
#ifdef TRACE_SORT
298+
PGRUsage ru_start;
299+
#endif
286300
};
287301

288302
#define COMPARETUP(state,a,b) ((*(state)->comparetup) (state, a, b))
@@ -422,6 +436,11 @@ tuplesort_begin_common(int workMem, bool randomAccess)
422436

423437
state = (Tuplesortstate *) palloc0(sizeof(Tuplesortstate));
424438

439+
#ifdef TRACE_SORT
440+
if (trace_sort)
441+
pg_rusage_init(&state->ru_start);
442+
#endif
443+
425444
state->status = TSS_INITIAL;
426445
state->randomAccess = randomAccess;
427446
state->availMem = workMem * 1024L;
@@ -456,6 +475,13 @@ tuplesort_begin_heap(TupleDesc tupDesc,
456475

457476
AssertArg(nkeys > 0);
458477

478+
#ifdef TRACE_SORT
479+
if (trace_sort)
480+
elog(NOTICE,
481+
"begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
482+
nkeys, workMem, randomAccess ? 't' : 'f');
483+
#endif
484+
459485
state->comparetup = comparetup_heap;
460486
state->copytup = copytup_heap;
461487
state->writetup = writetup_heap;
@@ -499,6 +525,14 @@ tuplesort_begin_index(Relation indexRel,
499525
{
500526
Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
501527

528+
#ifdef TRACE_SORT
529+
if (trace_sort)
530+
elog(NOTICE,
531+
"begin index sort: unique = %c, workMem = %d, randomAccess = %c",
532+
enforceUnique ? 't' : 'f',
533+
workMem, randomAccess ? 't' : 'f');
534+
#endif
535+
502536
state->comparetup = comparetup_index;
503537
state->copytup = copytup_index;
504538
state->writetup = writetup_index;
@@ -522,6 +556,13 @@ tuplesort_begin_datum(Oid datumType,
522556
int16 typlen;
523557
bool typbyval;
524558

559+
#ifdef TRACE_SORT
560+
if (trace_sort)
561+
elog(NOTICE,
562+
"begin datum sort: workMem = %d, randomAccess = %c",
563+
workMem, randomAccess ? 't' : 'f');
564+
#endif
565+
525566
state->comparetup = comparetup_datum;
526567
state->copytup = copytup_datum;
527568
state->writetup = writetup_datum;
@@ -573,6 +614,12 @@ tuplesort_end(Tuplesortstate *state)
573614
if (state->sortFnKinds)
574615
pfree(state->sortFnKinds);
575616

617+
#ifdef TRACE_SORT
618+
if (trace_sort)
619+
elog(NOTICE, "sort ended: %s",
620+
pg_rusage_show(&state->ru_start));
621+
#endif
622+
576623
pfree(state);
577624
}
578625

@@ -712,6 +759,12 @@ puttuple_common(Tuplesortstate *state, void *tuple)
712759
void
713760
tuplesort_performsort(Tuplesortstate *state)
714761
{
762+
#ifdef TRACE_SORT
763+
if (trace_sort)
764+
elog(NOTICE, "performsort starting: %s",
765+
pg_rusage_show(&state->ru_start));
766+
#endif
767+
715768
switch (state->status)
716769
{
717770
case TSS_INITIAL:
@@ -751,6 +804,13 @@ tuplesort_performsort(Tuplesortstate *state)
751804
elog(ERROR, "invalid tuplesort state");
752805
break;
753806
}
807+
808+
#ifdef TRACE_SORT
809+
if (trace_sort)
810+
elog(NOTICE, "performsort done%s: %s",
811+
(state->status == TSS_FINALMERGE) ? " (except final merge)" : "",
812+
pg_rusage_show(&state->ru_start));
813+
#endif
754814
}
755815

756816
/*
@@ -986,6 +1046,12 @@ inittapes(Tuplesortstate *state)
9861046
int ntuples,
9871047
j;
9881048

1049+
#ifdef TRACE_SORT
1050+
if (trace_sort)
1051+
elog(NOTICE, "switching to external sort: %s",
1052+
pg_rusage_show(&state->ru_start));
1053+
#endif
1054+
9891055
state->tapeset = LogicalTapeSetCreate(MAXTAPES);
9901056

9911057
/*
@@ -1243,6 +1309,12 @@ mergeonerun(Tuplesortstate *state)
12431309
*/
12441310
markrunend(state, destTape);
12451311
state->tp_runs[TAPERANGE]++;
1312+
1313+
#ifdef TRACE_SORT
1314+
if (trace_sort)
1315+
elog(NOTICE, "finished merge step: %s",
1316+
pg_rusage_show(&state->ru_start));
1317+
#endif
12461318
}
12471319

12481320
/*
@@ -1456,6 +1528,14 @@ dumptuples(Tuplesortstate *state, bool alltuples)
14561528
state->tp_runs[state->destTape]++;
14571529
state->tp_dummy[state->destTape]--; /* per Alg D step D2 */
14581530

1531+
#ifdef TRACE_SORT
1532+
if (trace_sort)
1533+
elog(NOTICE, "finished writing%s run %d: %s",
1534+
(state->memtupcount == 0) ? " final" : "",
1535+
state->currentRun,
1536+
pg_rusage_show(&state->ru_start));
1537+
#endif
1538+
14591539
/*
14601540
* Done if heap is empty, else prepare for new run.
14611541
*/

src/include/pg_config_manual.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* for developers. If you edit any of these, be sure to do a *full*
77
* rebuild (and an initdb if noted).
88
*
9-
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.16 2005/03/29 03:01:32 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.17 2005/10/03 22:55:56 tgl Exp $
1010
*------------------------------------------------------------------------
1111
*/
1212

@@ -229,11 +229,15 @@
229229
*/
230230
/* #define WAL_DEBUG */
231231

232+
/*
233+
* Enable tracing of resource consumption during sort operations;
234+
* see also the trace_sort GUC var. For 8.1 this is enabled by default.
235+
*/
236+
#define TRACE_SORT 1
237+
232238
/*
233239
* Other debug #defines (documentation, anyone?)
234240
*/
235-
/* #define IPORTAL_DEBUG */
236-
/* #define HEAPDEBUGALL */
241+
/* #define HEAPDEBUGALL */
237242
/* #define ACLDEBUG */
238243
/* #define RTDEBUG */
239-
/* #define GISTDEBUG */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy