* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.19 2006/03/05 15:58:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.20 2006/03/07 19:06:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
* When there are no such blocks, we extend the underlying file. Note
* that the block numbers in freeBlocks are always in *decreasing* order,
* so that removing the last entry gives us the lowest free block.
+ *
+ * If forgetFreeSpace is true then any freed blocks are simply forgotten
+ * rather than being remembered in freeBlocks[]. See notes for
+ * LogicalTapeSetForgetFreeSpace().
*/
+ bool forgetFreeSpace; /* are we remembering free blocks? */
long *freeBlocks; /* resizable array */
int nFreeBlocks; /* # of currently free blocks */
int freeBlocksLen; /* current allocated length of freeBlocks[] */
int ndx;
long *ptr;
+ /*
+ * Do nothing if we're no longer interested in remembering free space.
+ */
+ if (lts->forgetFreeSpace)
+ return;
+
/*
* Enlarge freeBlocks array if full.
*/
(ntapes - 1) * sizeof(LogicalTape));
lts->pfile = BufFileCreateTemp(false);
lts->nFileBlocks = 0L;
+ lts->forgetFreeSpace = false;
lts->freeBlocksLen = 32; /* reasonable initial guess */
lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
lts->nFreeBlocks = 0;
pfree(lts);
}
+/*
+ * Mark a logical tape set as not needing management of free space anymore.
+ *
+ * This should be called if the caller does not intend to write any more data
+ * into the tape set, but is reading from un-frozen tapes. Since no more
+ * writes are planned, remembering free blocks is no longer useful. Setting
+ * this flag lets us avoid wasting time and space in ltsReleaseBlock(), which
+ * is not designed to handle large numbers of free blocks.
+ */
+void
+LogicalTapeSetForgetFreeSpace(LogicalTapeSet *lts)
+{
+ lts->forgetFreeSpace = true;
+}
+
/*
* Dump the dirty buffer of a logical tape.
*/
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.62 2006/03/05 15:58:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.63 2006/03/07 19:06:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/*
* If we produced only one initial run (quite likely if the total data
* volume is between 1X and 2X workMem), we can just use that tape as the
- * finished output, rather than doing a useless merge.
+ * finished output, rather than doing a useless merge. (This obvious
+ * optimization is not in Knuth's algorithm.)
*/
if (state->currentRun == 1)
{
for (;;)
{
- /* Step D5: merge runs onto tape[T] until tape[P] is empty */
- while (state->tp_runs[state->tapeRange - 1] ||
- state->tp_dummy[state->tapeRange - 1])
+ /*
+ * At this point we know that tape[T] is empty. If there's just one
+ * (real or dummy) run left on each input tape, then only one merge
+ * pass remains. If we don't have to produce a materialized sorted
+ * tape, we can stop at this point and do the final merge on-the-fly.
+ */
+ if (!state->randomAccess)
{
- bool allDummy = true;
bool allOneRun = true;
+ Assert(state->tp_runs[state->tapeRange] == 0);
for (tapenum = 0; tapenum < state->tapeRange; tapenum++)
{
- if (state->tp_dummy[tapenum] == 0)
- allDummy = false;
if (state->tp_runs[tapenum] + state->tp_dummy[tapenum] != 1)
+ {
allOneRun = false;
+ break;
+ }
}
-
- /*
- * If we don't have to produce a materialized sorted tape, quit as
- * soon as we're down to one real/dummy run per tape.
- */
- if (!state->randomAccess && allOneRun)
+ if (allOneRun)
{
- Assert(!allDummy);
+ /* Tell logtape.c we won't be writing anymore */
+ LogicalTapeSetForgetFreeSpace(state->tapeset);
/* Initialize for the final merge pass */
beginmerge(state);
state->status = TSS_FINALMERGE;
return;
}
+ }
+
+ /* Step D5: merge runs onto tape[T] until tape[P] is empty */
+ while (state->tp_runs[state->tapeRange - 1] ||
+ state->tp_dummy[state->tapeRange - 1])
+ {
+ bool allDummy = true;
+
+ for (tapenum = 0; tapenum < state->tapeRange; tapenum++)
+ {
+ if (state->tp_dummy[tapenum] == 0)
+ {
+ allDummy = false;
+ break;
+ }
+ }
+
if (allDummy)
{
state->tp_dummy[state->tapeRange]++;
else
mergeonerun(state);
}
+
/* Step D6: decrease level */
if (--state->Level == 0)
break;
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.14 2006/03/05 15:59:07 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/logtape.h,v 1.15 2006/03/07 19:06:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
extern LogicalTapeSet *LogicalTapeSetCreate(int ntapes);
extern void LogicalTapeSetClose(LogicalTapeSet *lts);
+extern void LogicalTapeSetForgetFreeSpace(LogicalTapeSet *lts);
extern size_t LogicalTapeRead(LogicalTapeSet *lts, int tapenum,
void *ptr, size_t size);
extern void LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,