mirror of
https://github.com/postgres/postgres.git
synced 2025-11-13 16:22:44 +03:00
Avoid copying index tuples when building an index.
The previous code, perhaps out of concern for avoid memory leaks, formed the tuple in one memory context and then copied it to another memory context. However, this doesn't appear to be necessary, since index_form_tuple and the functions it calls take precautions against leaking memory. In my testing, building the tuple directly inside the sort context shaves several percent off the index build time. Rearrange things so we do that. Patch by me. Review by Amit Kapila, Tom Lane, Andres Freund.
This commit is contained in:
@@ -142,26 +142,23 @@ hashbuildCallback(Relation index,
|
||||
HashBuildState *buildstate = (HashBuildState *) state;
|
||||
IndexTuple itup;
|
||||
|
||||
/* form an index tuple and point it at the heap tuple */
|
||||
itup = _hash_form_tuple(index, values, isnull);
|
||||
itup->t_tid = htup->t_self;
|
||||
|
||||
/* Hash indexes don't index nulls, see notes in hashinsert */
|
||||
if (IndexTupleHasNulls(itup))
|
||||
{
|
||||
pfree(itup);
|
||||
if (isnull[0])
|
||||
return;
|
||||
}
|
||||
|
||||
/* Either spool the tuple for sorting, or just put it into the index */
|
||||
if (buildstate->spool)
|
||||
_h_spool(itup, buildstate->spool);
|
||||
_h_spool(buildstate->spool, &htup->t_self, values, isnull);
|
||||
else
|
||||
{
|
||||
/* form an index tuple and point it at the heap tuple */
|
||||
itup = _hash_form_tuple(index, values, isnull);
|
||||
itup->t_tid = htup->t_self;
|
||||
_hash_doinsert(index, itup);
|
||||
pfree(itup);
|
||||
}
|
||||
|
||||
buildstate->indtuples += 1;
|
||||
|
||||
pfree(itup);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -184,10 +181,6 @@ hashinsert(PG_FUNCTION_ARGS)
|
||||
#endif
|
||||
IndexTuple itup;
|
||||
|
||||
/* generate an index tuple */
|
||||
itup = _hash_form_tuple(rel, values, isnull);
|
||||
itup->t_tid = *ht_ctid;
|
||||
|
||||
/*
|
||||
* If the single index key is null, we don't insert it into the index.
|
||||
* Hash tables support scans on '='. Relational algebra says that A = B
|
||||
@@ -197,11 +190,12 @@ hashinsert(PG_FUNCTION_ARGS)
|
||||
* NOTNULL scans, but that's an artifact of the strategy map architecture
|
||||
* chosen in 1986, not of the way nulls are handled here.
|
||||
*/
|
||||
if (IndexTupleHasNulls(itup))
|
||||
{
|
||||
pfree(itup);
|
||||
if (isnull[0])
|
||||
PG_RETURN_BOOL(false);
|
||||
}
|
||||
|
||||
/* generate an index tuple */
|
||||
itup = _hash_form_tuple(rel, values, isnull);
|
||||
itup->t_tid = *ht_ctid;
|
||||
|
||||
_hash_doinsert(rel, itup);
|
||||
|
||||
|
||||
@@ -90,9 +90,10 @@ _h_spooldestroy(HSpool *hspool)
|
||||
* spool an index entry into the sort file.
|
||||
*/
|
||||
void
|
||||
_h_spool(IndexTuple itup, HSpool *hspool)
|
||||
_h_spool(HSpool *hspool, ItemPointer self, Datum *values, bool *isnull)
|
||||
{
|
||||
tuplesort_putindextuple(hspool->sortstate, itup);
|
||||
tuplesort_putindextuplevalues(hspool->sortstate, hspool->index,
|
||||
self, values, isnull);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user