Use memory context for inserts

This commit is contained in:
Andrew Kane
2023-01-10 02:53:58 -08:00
parent 7c65fd13c1
commit 13e0fa24fe

View File

@@ -4,6 +4,7 @@
#include "ivfflat.h" #include "ivfflat.h"
#include "storage/bufmgr.h" #include "storage/bufmgr.h"
#include "utils/memutils.h"
/* /*
* Find the list that minimizes the distance function * Find the list that minimizes the distance function
@@ -57,8 +58,11 @@ FindInsertPage(Relation rel, Datum *values, BlockNumber *insertPage, ListInfo *
* Insert a tuple into the index * Insert a tuple into the index
*/ */
static void static void
InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values) InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heapRel)
{ {
IndexTuple itup;
Datum value;
FmgrInfo *normprocinfo;
Buffer buf; Buffer buf;
Page page; Page page;
GenericXLogState *state; GenericXLogState *state;
@@ -67,11 +71,27 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values)
ListInfo listInfo; ListInfo listInfo;
BlockNumber originalInsertPage; BlockNumber originalInsertPage;
/* Detoast once for all calls */
value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
/* Normalize if needed */
normprocinfo = IvfflatOptionalProcInfo(rel, IVFFLAT_NORM_PROC);
if (normprocinfo != NULL)
{
if (!IvfflatNormValue(normprocinfo, rel->rd_indcollation[0], &value, NULL))
return;
}
/* Find the insert page - sets the page and list info */ /* Find the insert page - sets the page and list info */
FindInsertPage(rel, values, &insertPage, &listInfo); FindInsertPage(rel, values, &insertPage, &listInfo);
Assert(BlockNumberIsValid(insertPage)); Assert(BlockNumberIsValid(insertPage));
originalInsertPage = insertPage; originalInsertPage = insertPage;
/* Form tuple */
itup = index_form_tuple(RelationGetDescr(rel), &value, isnull);
itup->t_tid = *heap_tid;
/* Get tuple size */
itemsz = MAXALIGN(IndexTupleSize(itup)); itemsz = MAXALIGN(IndexTupleSize(itup));
Assert(itemsz <= BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(IvfflatPageOpaqueData))); Assert(itemsz <= BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(IvfflatPageOpaqueData)));
@@ -164,33 +184,28 @@ ivfflatinsert(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid,
,IndexInfo *indexInfo ,IndexInfo *indexInfo
) )
{ {
IndexTuple itup; MemoryContext oldCtx;
Datum value; MemoryContext insertCtx;
FmgrInfo *normprocinfo;
/* Skip nulls */ /* Skip nulls */
if (isnull[0]) if (isnull[0])
return false; return false;
/* Detoast once for all calls */ /*
value = PointerGetDatum(PG_DETOAST_DATUM(values[0])); * Use memory context since detoast, IvfflatNormValue, and
* index_form_tuple can allocate
*/
insertCtx = AllocSetContextCreate(CurrentMemoryContext,
"Ivfflat insert temporary context",
ALLOCSET_DEFAULT_SIZES);
oldCtx = MemoryContextSwitchTo(insertCtx);
/* Normalize if needed */ /* Insert tuple */
normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_NORM_PROC); InsertTuple(index, values, isnull, heap_tid, heap);
if (normprocinfo != NULL)
{
if (!IvfflatNormValue(normprocinfo, index->rd_indcollation[0], &value, NULL))
return false;
}
itup = index_form_tuple(RelationGetDescr(index), &value, isnull); /* Delete memory context */
itup->t_tid = *heap_tid; MemoryContextSwitchTo(oldCtx);
InsertTuple(index, itup, heap, &value); MemoryContextDelete(insertCtx);
pfree(itup);
/* Clean up if we allocated a new value */
if (value != values[0])
pfree(DatumGetPointer(value));
return false; return false;
} }