@@ -394,11 +394,12 @@ LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, int *idx, Relation
394
394
* Get update index
395
395
*/
396
396
static int
397
- GetUpdateIndex (HnswElement element , HnswElement newElement , float distance , int m , int lm , int lc , Relation index , FmgrInfo * procinfo , Oid collation )
397
+ GetUpdateIndex (HnswElement element , HnswElement newElement , float distance , int m , int lm , int lc , Relation index , FmgrInfo * procinfo , Oid collation , MemoryContext updateCtx )
398
398
{
399
399
char * base = NULL ;
400
400
int idx = -1 ;
401
401
HnswNeighborArray * neighbors ;
402
+ MemoryContext oldCtx = MemoryContextSwitchTo (updateCtx );
402
403
403
404
/*
404
405
* Get latest neighbors since they may have changed. Do not lock yet since
@@ -426,6 +427,9 @@ GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int
426
427
HnswUpdateConnection (base , neighbors , newElement , distance , lm , & idx , index , procinfo , collation );
427
428
}
428
429
430
+ MemoryContextSwitchTo (oldCtx );
431
+ MemoryContextReset (updateCtx );
432
+
429
433
return idx ;
430
434
}
431
435
@@ -529,6 +533,14 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
<
8000
code>529 533
{
530
534
char * base = NULL ;
531
535
536
+ /* Use separate memory context to improve performance for larger vectors */
537
+ MemoryContext updateCtx = GenerationContextCreate (CurrentMemoryContext ,
538
+ "Hnsw insert update context" ,
539
+ #if PG_VERSION_NUM >= 150000
540
+ 128 * 1024 , 128 * 1024 ,
541
+ #endif
542
+ 128 * 1024 );
543
+
532
544
for (int lc = e -> level ; lc >= 0 ; lc -- )
533
545
{
534
546
int lm = HnswGetLayerM (m , lc );
@@ -540,7 +552,7 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
540
552
HnswElement neighborElement = HnswPtrAccess (base , hc -> element );
541
553
int idx ;
542
554
543
- idx = GetUpdateIndex (neighborElement , e , hc -> distance , m , lm , lc , index , procinfo , collation );
555
+ idx = GetUpdateIndex (neighborElement , e , hc -> distance , m , lm , lc , index , procinfo , collation , updateCtx );
544
556
545
557
/* New element was not selected as a neighbor */
546
558
if (idx == -1 )
0 commit comments