pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #851: update ElementData to include index tuple
2+ messages / 1 participants
[nested] [flat]

* [pgvector/pgvector] PR #851: update ElementData to include index tuple
@ 2025-06-04 12:48 "collinforsyth (@collinforsyth)" <noreply+collinforsyth@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: collinforsyth (@collinforsyth) @ 2025-06-04 12:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>



inline patch 1: src/hnsw.h, src/hnswbuild.c, src/hnswinsert.c and 1 more (+152/-15 lines) download inline-1.patchdiff --git a/src/hnsw.h b/src/hnsw.h index 5102bfb5a..ab108a962 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -60,7 +60,8 @@ #define HNSW_MAX_SIZE (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(HnswPageOpaqueData)) - sizeof(ItemIdData)) #define HNSW_TUPLE_ALLOC_SIZE BLCKSZ -#define HNSW_ELEMENT_TUPLE_SIZE(size) MAXALIGN(offsetof(HnswElementTupleData, data) + (size)) +#define HNSW_ELEMENT_TUPLE_SIZE(size, index_tuple_size) \ + MAXALIGN(offsetof(HnswElementTupleData, data) + (size) + (index_tuple_size)) #define HNSW_NEIGHBOR_TUPLE_SIZE(level, m) MAXALIGN(offsetof(HnswNeighborTupleData, indextids) + ((level) + 2) * (m) * sizeof(ItemPointerData)) #define HNSW_NEIGHBOR_ARRAY_SIZE(lm) (offsetof(HnswNeighborArray, items) + sizeof(HnswCandidate) * (lm)) @@ -121,9 +122,11 @@ typedef enum HnswIterativeScanMode HNSW_ITERATIVE_SCAN_STRICT } HnswIterativeScanMode; +/* Forward declarations */ typedef struct HnswElementData HnswElementData; typedef struct HnswNeighborArray HnswNeighborArray; +/* Pointer declarations */ #define HnswPtrDeclare(type, relptrtype, ptrtype) \ relptr_declare(type, relptrtype); \ typedef union { type *ptr; relptrtype relptr; } ptrtype @@ -135,6 +138,7 @@ HnswPtrDeclare(HnswNeighborArray, HnswNeighborArrayRelptr, HnswNeighborArrayPtr) HnswPtrDeclare(HnswNeighborArrayPtr, HnswNeighborsRelptr, HnswNeighborsPtr); HnswPtrDeclare(char, DatumRelptr, DatumPtr); +/* Now define the actual structures */ struct HnswElementData { HnswElementPtr next; @@ -144,12 +148,14 @@ struct HnswElementData uint8 deleted; uint8 version; uint32 hash; + uint16 index_tuple_size; /* Size of the IndexTuple data */ HnswNeighborsPtr neighbors; BlockNumber blkno; OffsetNumber offno; OffsetNumber neighborOffno; BlockNumber neighborPage; DatumPtr value; + IndexTuple indexTuple; /* IndexTuple for INCLUDE columns (in-memory only) */ LWLock lock; }; @@ -333,8 +339,9 @@ typedef struct HnswElementTupleData uint8 version; ItemPointerData heaptids[HNSW_HEAPTIDS]; ItemPointerData neighbortid; - uint16 unused; + uint16 index_tuple_size; /* Size of the IndexTuple data */ Vector data; + /* IndexTuple data follows the vector data */ } HnswElementTupleData; typedef HnswElementTupleData * HnswElementTuple; @@ -436,6 +443,10 @@ void HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool void HnswLoadElement(HnswElement element, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance); bool HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support); void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element); +void HnswSetElementTupleIndexTuple(HnswElementTuple etup, IndexTuple itup); +IndexTuple HnswElementTupleGetIndexTuple(HnswElementTuple etup); +bool HnswElementTupleHasIndexTuple(HnswElementTuple etup); +IndexTuple HnswFormIndexTuple(Relation index, Datum *values, bool *isnull, const HnswTypeInfo *typeInfo, HnswSupport *support); void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support); bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc); void HnswInitLockTranche(void); diff --git a/src/hnswbuild.c b/src/hnswbuild.c index b667478b6..3c9598ace 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -65,6 +65,12 @@ #include "utils/wait_event.h" #endif +#include "access/genam.h" +#include "access/generic_xlog.h" +#include "access/relation.h" +#include "access/xlog.h" +#include "storage/lmgr.h" + #define PARALLEL_KEY_HNSW_SHARED UINT64CONST(0xA000000000000001) #define PARALLEL_KEY_HNSW_AREA UINT64CONST(0xA000000000000002) #define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000003) @@ -167,6 +173,7 @@ CreateGraphPages(HnswBuildState * buildstate) Size etupSize; Size ntupSize; Size combinedSize; + Size vectorSize; Pointer valuePtr = HnswPtrAccess(base, element->value); /* Update iterator */ @@ -176,7 +183,8 @@ CreateGraphPages(HnswBuildState * buildstate) MemSet(etup, 0, HNSW_TUPLE_ALLOC_SIZE); /* Calculate sizes */ - etupSize = HNSW_ELEMENT_TUPLE_SIZE(VARSIZE_ANY(valuePtr)); + vectorSize = VARSIZE_ANY(valuePtr); + etupSize = HNSW_ELEMENT_TUPLE_SIZE(vectorSize, 0); /* No IndexTuple data during initial build */ ntupSize = HNSW_NEIGHBOR_TUPLE_SIZE(element->level, buildstate->m); combinedSize = etupSize + ntupSize + sizeof(ItemIdData); diff --git a/src/hnswinsert.c b/src/hnswinsert.c index a5fac4eda..0c959f0c2 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -1,13 +1,15 @@ #include "postgres.h" - -#include <math.h> - +#include "access/genam.h" #include "access/generic_xlog.h" -#include "hnsw.h" +#include "access/relation.h" +#include "access/xlog.h" +#include "catalog/index.h" #include "storage/bufmgr.h" #include "storage/lmgr.h" -#include "utils/datum.h" #include "utils/memutils.h" +#include "hnsw.h" +#include <math.h> +#include "utils/datum.h" /* * Get the insert page @@ -158,7 +160,8 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B char *base = NULL; /* Calculate sizes */ - etupSize = HNSW_ELEMENT_TUPLE_SIZE(VARSIZE_ANY(HnswPtrAccess(base, e->value))); + Size vectorSize = VARSIZE_ANY(HnswPtrAccess(base, e->value)); + etupSize = HNSW_ELEMENT_TUPLE_SIZE(vectorSize, e->index_tuple_size); ntupSize = HNSW_NEIGHBOR_TUPLE_SIZE(e->level, m); combinedSize = etupSize + ntupSize + sizeof(ItemIdData); maxSize = HNSW_MAX_SIZE; diff --git a/src/hnswutils.c b/src/hnswutils.c index c52d2c78a..cd7f9f6bd 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -2,17 +2,26 @@ #include <math.h> +#include "access/genam.h" #include "access/generic_xlog.h" +#include "access/itup.h" +#include "access/relation.h" +#include "access/xlog.h" +#include "catalog/index.h" +#include "storage/bufmgr.h" +#include "storage/lmgr.h" +#include "utils/datum.h" +#include "utils/memutils.h" +#include "utils/memdebug.h" + +#include "hnsw.h" + #include "catalog/pg_type.h" #include "catalog/pg_type_d.h" #include "common/hashfn.h" #include "fmgr.h" -#include "hnsw.h" #include "lib/pairingheap.h" #include "sparsevec.h" -#include "storage/bufmgr.h" -#include "utils/datum.h" -#include "utils/memdebug.h" #include "utils/rel.h" #if PG_VERSION_NUM < 170000 @@ -257,6 +266,8 @@ HnswInitElement(char *base, ItemPointer heaptid, int m, double ml, int maxLevel, HnswInitNeighbors(base, element, m, allocator); HnswPtrStore(base, element->value, (Pointer) NULL); + element->indexTuple = NULL; /* Initialize IndexTuple pointer */ + element->index_tuple_size = 0; /* Initialize IndexTuple size */ return element; } @@ -283,6 +294,8 @@ HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno) element->offno = offno; HnswPtrStore(base, element->neighbors, (HnswNeighborArrayPtr *) NULL); HnswPtrStore(base, element->value, (Pointer) NULL); + element->indexTuple = NULL; /* Initialize IndexTuple pointer */ + element->index_tuple_size = 0; /* Initialize IndexTuple size */ return element; } @@ -428,11 +441,13 @@ void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element) { Pointer valuePtr = HnswPtrAccess(base, element->value); + Size valueSize = VARSIZE_ANY(valuePtr); etup->type = HNSW_ELEMENT_TUPLE_TYPE; etup->level = element->level; etup->deleted = 0; etup->version = element->version; + for (int i = 0; i < HNSW_HEAPTIDS; i++) { if (i < element->heaptidsLength) @@ -440,7 +455,23 @@ HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element) else ItemPointerSetInvalid(&etup->heaptids[i]); } - memcpy(&etup->data, valuePtr, VARSIZE_ANY(valuePtr)); + + /* Copy vector data */ + memcpy(&etup->data, valuePtr, valueSize); + + /* Embed IndexTuple data if present */ + if (element->indexTuple != NULL) + { + Size indexTupleSize = IndexTupleSize(element->indexTuple); + char *indexTuplePtr = (char *)&etup->data + valueSize; + + memcpy(indexTuplePtr, element->indexTuple, indexTupleSize); + etup->index_tuple_size = indexTupleSize; + } + else + { + etup->index_tuple_size = 0; /* No IndexTuple data */ + } } /* @@ -506,10 +537,36 @@ HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHe if (loadVec) { char *base = NULL; - Datum value = datumCopy(PointerGetDatum(&etup->data), false, -1); + Size vectorSize = VARSIZE_ANY(&etup->data); + Datum value = datumCopy(PointerGetDatum(&etup->data), false, vectorSize); HnswPtrStore(base, element->value, DatumGetPointer(value)); } + + /* Store IndexTuple size and load IndexTuple data if present */ + element->index_tuple_size = etup->index_tuple_size; + + if (etup->index_tuple_size > 0) + { + IndexTuple itup = HnswElementTupleGetIndexTuple(etup); + + if (itup != NULL) + { + /* Copy IndexTuple to local memory */ + Size indexTupleSize = IndexTupleSize(itup); + + element->indexTuple = palloc(indexTupleSize); + memcpy(element->indexTuple, itup, indexTupleSize); + } + else + { + element->indexTuple = NULL; + } + } + else + { + element->indexTuple = NULL; + } } /* @@ -1420,3 +1477,61 @@ hnsw_sparsevec_support(PG_FUNCTION_ARGS) PG_RETURN_POINTER(&typeInfo); } + +/* + * Get pointer to IndexTuple data in element tuple + */ +IndexTuple +HnswElementTupleGetIndexTuple(HnswElementTuple etup) +{ + if (etup->index_tuple_size == 0) + return NULL; + + /* IndexTuple data follows vector data */ + return (IndexTuple)((char *)&etup->data + VARSIZE_ANY(&etup->data)); +} + +/* + * Check if element tuple has IndexTuple data + */ +bool +HnswElementTupleHasIndexTuple(HnswElementTuple etup) +{ + return etup->index_tuple_size > 0; +} + +/* + * Form IndexTuple from column values + */ +IndexTuple +HnswFormIndexTuple(Relation index, Datum *values, bool *isnull, const HnswTypeInfo *typeInfo, HnswSupport *support) +{ + TupleDesc tupleDesc = RelationGetDescr(index); + IndexTuple itup; + + /* Only create IndexTuple if there are included columns beyond the vector column */ + if (tupleDesc->natts <= 1) + return NULL; + + /* Use PostgreSQL's index_form_tuple to create the IndexTuple */ + itup = index_form_tuple(tupleDesc, values, isnull); + + return itup; +} + +/* + * Set IndexTuple data in element tuple + */ +void +HnswSetElementTupleIndexTuple(HnswElementTuple etup, IndexTuple itup) +{ + if (itup == NULL) + { + etup->index_tuple_size = 0; + return; + } + + /* Copy IndexTuple after vector data */ + memcpy((char *)&etup->data + VARSIZE_ANY(&etup->data), itup, IndexTupleSize(itup)); + etup->index_tuple_size = IndexTupleSize(itup); +}
^ permalink raw [nested|flat] 2+ messages in thread

* Re: [pgvector/pgvector] PR #851: update ElementData to include index tuple
@ 2025-06-04 12:48 "collinforsyth (@collinforsyth)" <noreply+collinforsyth@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: collinforsyth (@collinforsyth) @ 2025-06-04 12:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@collinforsyth closed this.

^ permalink  raw  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-06-04 12:48 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-06-04 12:48 [pgvector/pgvector] PR #851: update ElementData to include index tuple "collinforsyth (@collinforsyth)" <noreply+collinforsyth@github.com>
2025-06-04 12:48 Re: [pgvector/pgvector] PR #851: update ElementData to include index tuple "collinforsyth (@collinforsyth)" <noreply+collinforsyth@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox