Compare commits

..

10 Commits

Author SHA1 Message Date
Andrew Kane
89dc41ce2b Fixed crash with debugging disabled [skip ci] 2026-06-10 14:31:54 -07:00
Andrew Kane
06ab41c094 Removed extra whitespace [skip ci] 2026-06-10 13:49:39 -07:00
Andrew Kane
8a8bc90d6d Added support for amgetbatch for IVFFlat [skip ci] 2026-06-10 13:47:36 -07:00
Andrew Kane
91717c81b3 Improved batch size flexibility [skip ci] 2026-06-10 12:10:20 -07:00
Andrew Kane
98a7f94608 Added comment [skip ci] 2026-06-10 11:59:38 -07:00
Andrew Kane
7db3d67b2d Re-added check for iterative scans [skip ci] 2026-06-10 11:58:19 -07:00
Andrew Kane
1dd39f66de Minimized diff [skip ci] 2026-06-10 11:56:52 -07:00
Andrew Kane
cd431c51ca Removed unneeded check [skip ci] 2026-06-10 11:55:31 -07:00
Andrew Kane
73890f2f70 Fixed comment [skip ci] 2026-06-10 11:46:15 -07:00
Andrew Kane
18980387ec Added support for amgetbatch [skip ci] 2026-06-10 11:31:57 -07:00
11 changed files with 124 additions and 75 deletions

View File

@@ -97,7 +97,7 @@ jobs:
with:
postgres-version: ${{ matrix.postgres }}
- run: |
call "C:\Program Files\Microsoft Visual Studio\${{ matrix.os == 'windows-2025' && 18 || 2022 }}\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
nmake /NOLOGO /F Makefile.win && ^
nmake /NOLOGO /F Makefile.win install && ^
nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^

View File

@@ -1,7 +1,3 @@
## 0.8.3 (unreleased)
- Fixed performance regression with Hamming distance and Jaccard distance with Postgres 18
## 0.8.2 (2026-02-25)
- Fixed buffer overflow with parallel HNSW index build - [more info](https://github.com/pgvector/pgvector/issues/959)

View File

@@ -1329,7 +1329,7 @@ make clean && PG_CFLAGS="-DUSE_ASSERT_CHECKING" make && make install
To enable benchmarking:
```sh
make clean && PG_CFLAGS="-DHNSW_BENCH -DIVFFLAT_BENCH" make && make install
make clean && PG_CFLAGS="-DIVFFLAT_BENCH" make && make install
```
To show memory usage:

View File

@@ -31,12 +31,10 @@
#define BIT_TARGET_CLONES
#endif
/* Use built-ins when possible for Postgres < 19 for inlining */
#if PG_VERSION_NUM >= 190000
#define popcount64(x) pg_popcount64(x)
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_INT_64) || SIZEOF_LONG == 8)
/* Use built-ins when possible for inlining */
#if defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_INT_64)
#define popcount64(x) __builtin_popcountl(x)
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_LONG_INT_64) || SIZEOF_LONG_LONG == 8)
#elif defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_LONG_INT_64)
#define popcount64(x) __builtin_popcountll(x)
#elif !defined(_MSC_VER)
/* Fails to resolve with MSVC */

View File

@@ -258,6 +258,11 @@ hnswvalidate(Oid opclassoid)
return true;
}
static void
hnswunguardbatch(IndexScanDesc scan, IndexScanBatch batch)
{
}
/*
* Define index handler
*
@@ -279,6 +284,7 @@ hnswhandler(PG_FUNCTION_ARGS)
.amconsistentequality = false,
.amconsistentordering = false,
.amcanbackward = false,
.amcanmarkpos = false,
.amcanunique = false,
.amcanmulticol = false,
.amoptionalkey = true,
@@ -311,11 +317,14 @@ hnswhandler(PG_FUNCTION_ARGS)
.amadjustmembers = NULL,
.ambeginscan = hnswbeginscan,
.amrescan = hnswrescan,
.amgettuple = hnswgettuple,
.amgettuple = NULL,
.amgetbatch = hnswgetbatch,
.amunguardbatch = hnswunguardbatch,
.amkillitemsbatch = NULL,
.amgettransform = NULL,
.amgetbitmap = NULL,
.amendscan = hnswendscan,
.ammarkpos = NULL,
.amrestrpos = NULL,
.amposreset = NULL,
.amestimateparallelscan = NULL,
.aminitparallelscan = NULL,
.amparallelrescan = NULL,

View File

@@ -18,10 +18,6 @@
#include "utils/sampling.h"
#include "vector.h"
#ifdef HNSW_BENCH
#include "portability/instr_time.h"
#endif
#if PG_VERSION_NUM >= 190000
typedef Pointer Item;
#endif
@@ -82,21 +78,6 @@ typedef Pointer Item;
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page))
#define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page))
#ifdef HNSW_BENCH
#define HnswBench(name, code) \
do { \
instr_time start; \
instr_time duration; \
INSTR_TIME_SET_CURRENT(start); \
(code); \
INSTR_TIME_SET_CURRENT(duration); \
INSTR_TIME_SUBTRACT(duration, start); \
elog(INFO, "%s: %.3f ms", name, INSTR_TIME_GET_MILLISEC(duration)); \
} while (0)
#else
#define HnswBench(name, code) (code)
#endif
#if PG_VERSION_NUM >= 150000
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
@@ -484,7 +465,7 @@ IndexBulkDeleteResult *hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResu
IndexBulkDeleteResult *hnswvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats);
IndexScanDesc hnswbeginscan(Relation index, int nkeys, int norderbys);
void hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys);
bool hnswgettuple(IndexScanDesc scan, ScanDirection dir);
IndexScanBatch hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir);
void hnswendscan(IndexScanDesc scan);
static inline HnswNeighborArray *

View File

@@ -3,6 +3,7 @@
#include <limits.h>
#include "access/genam.h"
#include "access/indexbatch.h"
#include "access/relscan.h"
#include "hnsw.h"
#include "lib/pairingheap.h"
@@ -135,6 +136,11 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
double maxMemory;
scan = RelationGetIndexScan(index, nkeys, norderbys);
scan->maxitemsbatch = hnsw_ef_search * HNSW_HEAPTIDS;
/* unused but must be > 0 */
scan->batch_index_opaque_static = MAXALIGN(1);
scan->batch_index_opaque_dyn = 0;
scan->batch_tuples_workspace = 0;
so = (HnswScanOpaque) palloc(sizeof(HnswScanOpaqueData));
so->typeInfo = HnswGetTypeInfo(index);
@@ -184,12 +190,13 @@ hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int no
}
/*
* Fetch the next tuple in the given scan
* Fetch the next batch in the given scan
*/
bool
hnswgettuple(IndexScanDesc scan, ScanDirection dir)
IndexScanBatch
hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
{
HnswScanOpaque so = (HnswScanOpaque) scan->opaque;
IndexScanBatch batch = indexam_util_alloc_batch(scan);
MemoryContext oldCtx = MemoryContextSwitchTo(so->tmpCtx);
/*
@@ -245,6 +252,7 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
HnswSearchCandidate *sc;
HnswElement element;
ItemPointer heaptid;
int nitems = 0;
if (list_length(so->w) == 0)
{
@@ -290,44 +298,64 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
break;
}
sc = llast(so->w);
element = HnswPtrAccess(base, sc->element);
/* Move to next element if no valid heap TIDs */
if (element->heaptidsLength == 0)
while (list_length(so->w) != 0)
{
so->w = list_delete_last(so->w);
sc = llast(so->w);
element = HnswPtrAccess(base, sc->element);
/* Mark memory as free for next iteration */
if (hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF)
/* Move to next element if no valid heap TIDs */
if (element->heaptidsLength == 0)
{
pfree(element);
pfree(sc);
so->w = list_delete_last(so->w);
/* Mark memory as free for next iteration */
if (hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF)
{
pfree(element);
pfree(sc);
}
continue;
}
heaptid = &element->heaptids[--element->heaptidsLength];
if (hnsw_iterative_scan == HNSW_ITERATIVE_SCAN_STRICT)
{
if (sc->distance < so->previousDistance)
continue;
so->previousDistance = sc->distance;
}
batch->items[nitems].tableTid = *heaptid;
batch->items[nitems].indexOffset = -1;
batch->items[nitems].tupleOffset = 0;
nitems++;
/* Keep batch size flexible */
if (nitems == scan->maxitemsbatch)
break;
}
/* Needed for strict iterative scans */
if (nitems == 0)
continue;
}
heaptid = &element->heaptids[--element->heaptidsLength];
if (hnsw_iterative_scan == HNSW_ITERATIVE_SCAN_STRICT)
{
if (sc->distance < so->previousDistance)
continue;
so->previousDistance = sc->distance;
}
MemoryContextSwitchTo(oldCtx);
scan->xs_heaptid = *heaptid;
scan->xs_recheck = false;
scan->xs_recheckorderby = false;
return true;
batch->firstItem = 0;
batch->lastItem = nitems - 1;
batch->dir = ForwardScanDirection;
return batch;
}
MemoryContextSwitchTo(oldCtx);
return false;
indexam_util_release_batch(scan, batch);
return NULL;
}
/*

View File

@@ -634,13 +634,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
/* Pass 1: Remove heap TIDs */
HnswBench("RemoveHeapTids", RemoveHeapTids(&vacuumstate));
RemoveHeapTids(&vacuumstate);
/* Pass 2: Repair graph */
HnswBench("RepairGraph", RepairGraph(&vacuumstate));
RepairGraph(&vacuumstate);
/* Pass 3: Mark as deleted */
HnswBench("MarkDeleted", MarkDeleted(&vacuumstate));
MarkDeleted(&vacuumstate);
FreeVacuumState(&vacuumstate);

View File

@@ -175,6 +175,11 @@ ivfflatvalidate(Oid opclassoid)
return true;
}
static void
ivfflatunguardbatch(IndexScanDesc scan, IndexScanBatch batch)
{
}
/*
* Define index handler
*
@@ -196,6 +201,7 @@ ivfflathandler(PG_FUNCTION_ARGS)
.amconsistentequality = false,
.amconsistentordering = false,
.amcanbackward = false,
.amcanmarkpos = false,
.amcanunique = false,
.amcanmulticol = false,
.amoptionalkey = true,
@@ -228,11 +234,14 @@ ivfflathandler(PG_FUNCTION_ARGS)
.amadjustmembers = NULL,
.ambeginscan = ivfflatbeginscan,
.amrescan = ivfflatrescan,
.amgettuple = ivfflatgettuple,
.amgettuple = NULL,
.amgetbatch = ivfflatgetbatch,
.amunguardbatch = ivfflatunguardbatch,
.amkillitemsbatch = NULL,
.amgettransform = NULL,
.amgetbitmap = NULL,
.amendscan = ivfflatendscan,
.ammarkpos = NULL,
.amrestrpos = NULL,
.amposreset = NULL,
.amestimateparallelscan = NULL,
.aminitparallelscan = NULL,
.amparallelrescan = NULL,

View File

@@ -344,7 +344,7 @@ IndexBulkDeleteResult *ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteR
IndexBulkDeleteResult *ivfflatvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats);
IndexScanDesc ivfflatbeginscan(Relation index, int nkeys, int norderbys);
void ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys);
bool ivfflatgettuple(IndexScanDesc scan, ScanDirection dir);
IndexScanBatch ivfflatgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir);
void ivfflatendscan(IndexScanDesc scan);
#endif

View File

@@ -3,6 +3,7 @@
#include <float.h>
#include "access/genam.h"
#include "access/indexbatch.h"
#include "access/itup.h"
#include "access/relscan.h"
#include "access/tupdesc.h"
@@ -261,6 +262,11 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
MemoryContext oldCtx;
scan = RelationGetIndexScan(index, nkeys, norderbys);
scan->maxitemsbatch = 1000;
/* unused but must be > 0 */
scan->batch_index_opaque_static = MAXALIGN(1);
scan->batch_index_opaque_dyn = 0;
scan->batch_tuples_workspace = 0;
/* Get lists and dimensions from metapage */
IvfflatGetMetaPageInfo(index, &lists, &dimensions);
@@ -348,14 +354,16 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
}
/*
* Fetch the next tuple in the given scan
* Fetch the next batch in the given scan
*/
bool
ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
IndexScanBatch
ivfflatgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
{
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
IndexScanBatch batch = indexam_util_alloc_batch(scan);
ItemPointer heaptid;
bool isnull;
int nitems = 0;
/*
* Index can be used to scan backward, but Postgres doesn't support
@@ -393,17 +401,37 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
while (!tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL))
{
if (so->listIndex == so->maxProbes)
return false;
{
indexam_util_release_batch(scan, batch);
return NULL;
}
IvfflatBench("GetScanItems", GetScanItems(scan, so->value));
}
heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->mslot, 2, &isnull));
for (;;)
{
heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->mslot, 2, &isnull));
batch->items[nitems].tableTid = *heaptid;
batch->items[nitems].indexOffset = -1;
batch->items[nitems].tupleOffset = 0;
nitems++;
if (nitems == scan->maxitemsbatch)
break;
if (!tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL))
break;
}
scan->xs_heaptid = *heaptid;
scan->xs_recheck = false;
scan->xs_recheckorderby = false;
return true;
batch->firstItem = 0;
batch->lastItem = nitems - 1;
batch->dir = ForwardScanDirection;
return batch;
}
/*