Commit cdd5178c authored by Tom Lane's avatar Tom Lane

Extend the MinimalTuple concept to tuplesort.c, thereby reducing the

per-tuple space overhead for sorts in memory.  I chose to replace the
previous patch that tried to write out the bare minimum amount of data
when sorting on disk; instead, just dump the MinimalTuples as-is.  This
wastes 3 to 10 bytes per tuple depending on architecture and null-bitmap
length, but the simplification in the writetup/readtup routines seems
worth it.
parent e99507ea
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsort.c,v 1.101 2006/05/08 00:00:10 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsort.c,v 1.102 2006/06/27 16:53:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -179,7 +179,7 @@ _bt_spooldestroy(BTSpool *btspool) ...@@ -179,7 +179,7 @@ _bt_spooldestroy(BTSpool *btspool)
void void
_bt_spool(IndexTuple itup, BTSpool *btspool) _bt_spool(IndexTuple itup, BTSpool *btspool)
{ {
tuplesort_puttuple(btspool->sortstate, (void *) itup); tuplesort_putindextuple(btspool->sortstate, itup);
} }
/* /*
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/nodeSort.c,v 1.56 2006/03/05 15:58:26 momjian Exp $ * $PostgreSQL: pgsql/src/backend/executor/nodeSort.c,v 1.57 2006/06/27 16:53:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -41,9 +41,7 @@ ExecSort(SortState *node) ...@@ -41,9 +41,7 @@ ExecSort(SortState *node)
EState *estate; EState *estate;
ScanDirection dir; ScanDirection dir;
Tuplesortstate *tuplesortstate; Tuplesortstate *tuplesortstate;
HeapTuple heapTuple;
TupleTableSlot *slot; TupleTableSlot *slot;
bool should_free;
/* /*
* get state info from node * get state info from node
...@@ -103,8 +101,7 @@ ExecSort(SortState *node) ...@@ -103,8 +101,7 @@ ExecSort(SortState *node)
if (TupIsNull(slot)) if (TupIsNull(slot))
break; break;
tuplesort_puttuple(tuplesortstate, tuplesort_puttupleslot(tuplesortstate, slot);
(void *) ExecFetchSlotTuple(slot));
} }
/* /*
...@@ -131,15 +128,11 @@ ExecSort(SortState *node) ...@@ -131,15 +128,11 @@ ExecSort(SortState *node)
* Get the first or next tuple from tuplesort. Returns NULL if no more * Get the first or next tuple from tuplesort. Returns NULL if no more
* tuples. * tuples.
*/ */
heapTuple = tuplesort_getheaptuple(tuplesortstate,
ScanDirectionIsForward(dir),
&should_free);
slot = node->ss.ps.ps_ResultTupleSlot; slot = node->ss.ps.ps_ResultTupleSlot;
if (heapTuple) (void) tuplesort_gettupleslot(tuplesortstate,
return ExecStoreTuple(heapTuple, slot, InvalidBuffer, should_free); ScanDirectionIsForward(dir),
else slot);
return ExecClearTuple(slot); return slot;
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
......
This diff is collapsed.
...@@ -13,29 +13,34 @@ ...@@ -13,29 +13,34 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/utils/tuplesort.h,v 1.20 2006/05/23 21:37:59 tgl Exp $ * $PostgreSQL: pgsql/src/include/utils/tuplesort.h,v 1.21 2006/06/27 16:53:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#ifndef TUPLESORT_H #ifndef TUPLESORT_H
#define TUPLESORT_H #define TUPLESORT_H
#include "access/htup.h"
#include "access/itup.h" #include "access/itup.h"
#include "executor/tuptable.h"
#include "fmgr.h" #include "fmgr.h"
/* Tuplesortstate is an opaque type whose details are not known outside tuplesort.c. */
/* Tuplesortstate is an opaque type whose details are not known outside
* tuplesort.c.
*/
typedef struct Tuplesortstate Tuplesortstate; typedef struct Tuplesortstate Tuplesortstate;
/* /*
* We provide two different interfaces to what is essentially the same * We provide two different interfaces to what is essentially the same
* code: one for sorting HeapTuples and one for sorting IndexTuples. * code: one for sorting HeapTuples and one for sorting IndexTuples.
* They differ primarily in the way that the sort key information is * They differ primarily in the way that the sort key information is
* supplied. Also, tuplesort.c guarantees to preserve all the header * supplied. Also, the HeapTuple case actually stores MinimalTuples,
* fields of an IndexTuple, but when sorting HeapTuples only the user data * which means it doesn't preserve the "system columns" (tuple identity and
* is guaranteed preserved, not the "system columns" (tuple identity and * transaction visibility info). The IndexTuple case does preserve all
* transaction visibility info). * the header fields of an index entry. In the HeapTuple case we can
* save some cycles by passing and returning the tuples in TupleTableSlots,
* rather than forming actual HeapTuples (which'd have to be converted to
* MinimalTuples).
* *
* Yet a third slightly different interface supports sorting bare Datums. * Yet a third slightly different interface supports sorting bare Datums.
*/ */
...@@ -51,21 +56,18 @@ extern Tuplesortstate *tuplesort_begin_datum(Oid datumType, ...@@ -51,21 +56,18 @@ extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
Oid sortOperator, Oid sortOperator,
int workMem, bool randomAccess); int workMem, bool randomAccess);
extern void tuplesort_puttuple(Tuplesortstate *state, void *tuple); extern void tuplesort_puttupleslot(Tuplesortstate *state,
TupleTableSlot *slot);
extern void tuplesort_putindextuple(Tuplesortstate *state, IndexTuple tuple);
extern void tuplesort_putdatum(Tuplesortstate *state, Datum val, extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
bool isNull); bool isNull);
extern void tuplesort_performsort(Tuplesortstate *state); extern void tuplesort_performsort(Tuplesortstate *state);
extern void *tuplesort_gettuple(Tuplesortstate *state, bool forward, extern bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
bool *should_free); TupleTableSlot *slot);
extern IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward,
#define tuplesort_getheaptuple(state, forward, should_free) \ bool *should_free);
((HeapTuple) tuplesort_gettuple(state, forward, should_free))
#define tuplesort_getindextuple(state, forward, should_free) \
((IndexTuple) tuplesort_gettuple(state, forward, should_free))
extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward, extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
Datum *val, bool *isNull); Datum *val, bool *isNull);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment