#include "utils/logtape.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/pg_rusage.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
+/* GUC variable */
+#ifdef TRACE_SORT
+bool trace_sort = false;
+#endif
+
+
/*
* Possible states of a Tuplesort object. These denote the states that
* persist between calls of Tuplesort routines.
/* we need typelen and byval in order to know how to copy the Datums. */
int datumTypeLen;
bool datumTypeByVal;
+
+ /*
+ * Resource snapshot for time of sort start.
+ */
+#ifdef TRACE_SORT
+ PGRUsage ru_start;
+#endif
};
#define COMPARETUP(state,a,b) ((*(state)->comparetup) (state, a, b))
state = (Tuplesortstate *) palloc0(sizeof(Tuplesortstate));
+#ifdef TRACE_SORT
+ if (trace_sort)
+ pg_rusage_init(&state->ru_start);
+#endif
+
state->status = TSS_INITIAL;
state->randomAccess = randomAccess;
state->availMem = workMem * 1024L;
AssertArg(nkeys > 0);
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE,
+ "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
+ nkeys, workMem, randomAccess ? 't' : 'f');
+#endif
+
state->comparetup = comparetup_heap;
state->copytup = copytup_heap;
state->writetup = writetup_heap;
{
Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE,
+ "begin index sort: unique = %c, workMem = %d, randomAccess = %c",
+ enforceUnique ? 't' : 'f',
+ workMem, randomAccess ? 't' : 'f');
+#endif
+
state->comparetup = comparetup_index;
state->copytup = copytup_index;
state->writetup = writetup_index;
int16 typlen;
bool typbyval;
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE,
+ "begin datum sort: workMem = %d, randomAccess = %c",
+ workMem, randomAccess ? 't' : 'f');
+#endif
+
state->comparetup = comparetup_datum;
state->copytup = copytup_datum;
state->writetup = writetup_datum;
if (state->sortFnKinds)
pfree(state->sortFnKinds);
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "sort ended: %s",
+ pg_rusage_show(&state->ru_start));
+#endif
+
pfree(state);
}
void
tuplesort_performsort(Tuplesortstate *state)
{
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "performsort starting: %s",
+ pg_rusage_show(&state->ru_start));
+#endif
+
switch (state->status)
{
case TSS_INITIAL:
elog(ERROR, "invalid tuplesort state");
break;
}
+
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "performsort done%s: %s",
+ (state->status == TSS_FINALMERGE) ? " (except final merge)" : "",
+ pg_rusage_show(&state->ru_start));
+#endif
}
/*
int ntuples,
j;
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "switching to external sort: %s",
+ pg_rusage_show(&state->ru_start));
+#endif
+
state->tapeset = LogicalTapeSetCreate(MAXTAPES);
/*
*/
markrunend(state, destTape);
state->tp_runs[TAPERANGE]++;
+
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "finished merge step: %s",
+ pg_rusage_show(&state->ru_start));
+#endif
}
/*
state->tp_runs[state->destTape]++;
state->tp_dummy[state->destTape]--; /* per Alg D step D2 */
+#ifdef TRACE_SORT
+ if (trace_sort)
+ elog(NOTICE, "finished writing%s run %d: %s",
+ (state->memtupcount == 0) ? " final" : "",
+ state->currentRun,
+ pg_rusage_show(&state->ru_start));
+#endif
+
/*
* Done if heap is empty, else prepare for new run.
*/