diff --git a/contrib/pgvector/Makefile b/contrib/pgvector/Makefile index ff26f56cf41ac8ced78a267071519b0c8760ca71..caf60291db7ad8f3bd53f572521fe77b22f9d57c 100644 --- a/contrib/pgvector/Makefile +++ b/contrib/pgvector/Makefile @@ -42,9 +42,19 @@ sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql -PG_CONFIG ?= pg_config +ifdef USE_PGXS +PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) +else +subdir = contrib/pgvector +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +override CXXFLAGS :=$(filter-out -fstack-protector, $(CFLAGS)) -fstack-protector-all -Wl,-z,relro,-z,now -fPIC +override CPPFLAGS +=$(filter-out -fstack-protector, $(CFLAGS)) -fstack-protector-all -Wl,-z,relro,-z,now -fPIC # for Mac ifeq ($(PROVE),) diff --git a/contrib/pgvector/sql/vector--0.1.0--0.1.1.sql b/contrib/pgvector/sql/vector--0.1.0--0.1.1.sql index 959a0d72261a64af629e0c9be78767337743c26d..1ca2be29ea7bc613709a1e91f2ef8c411dba92a1 100644 --- a/contrib/pgvector/sql/vector--0.1.0--0.1.1.sql +++ b/contrib/pgvector/sql/vector--0.1.0--0.1.1.sql @@ -11,29 +11,29 @@ ALTER TYPE vector SET ( RECEIVE = vector_recv, SEND = vector_send ); -- functions -ALTER FUNCTION vector_in(cstring, oid, integer) PARALLEL SAFE; -ALTER FUNCTION vector_out(vector) PARALLEL SAFE; -ALTER FUNCTION vector_typmod_in(cstring[]) PARALLEL SAFE; -ALTER FUNCTION vector_recv(internal, oid, integer) PARALLEL SAFE; -ALTER FUNCTION vector_send(vector) PARALLEL SAFE; -ALTER FUNCTION l2_distance(vector, vector) PARALLEL SAFE; -ALTER FUNCTION inner_product(vector, vector) PARALLEL SAFE; -ALTER FUNCTION cosine_distance(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_dims(vector) PARALLEL SAFE; -ALTER FUNCTION vector_norm(vector) PARALLEL SAFE; -ALTER FUNCTION vector_add(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_sub(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_lt(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_le(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_eq(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_ne(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_ge(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_gt(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_cmp(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_l2_squared_distance(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_negative_inner_product(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector_spherical_distance(vector, vector) PARALLEL SAFE; -ALTER FUNCTION vector(vector, integer, boolean) PARALLEL SAFE; -ALTER FUNCTION array_to_vector(integer[], integer, boolean) PARALLEL SAFE; -ALTER FUNCTION array_to_vector(real[], integer, boolean) PARALLEL SAFE; -ALTER FUNCTION array_to_vector(double precision[], integer, boolean) PARALLEL SAFE; +ALTER FUNCTION vector_in(cstring, oid, integer) ; +ALTER FUNCTION vector_out(vector) ; +ALTER FUNCTION vector_typmod_in(cstring[]) ; +ALTER FUNCTION vector_recv(internal, oid, integer) ; +ALTER FUNCTION vector_send(vector) ; +ALTER FUNCTION l2_distance(vector, vector) ; +ALTER FUNCTION inner_product(vector, vector) ; +ALTER FUNCTION cosine_distance(vector, vector) ; +ALTER FUNCTION vector_dims(vector) ; +ALTER FUNCTION vector_norm(vector) ; +ALTER FUNCTION vector_add(vector, vector) ; +ALTER FUNCTION vector_sub(vector, vector) ; +ALTER FUNCTION vector_lt(vector, vector) ; +ALTER FUNCTION vector_le(vector, vector) ; +ALTER FUNCTION vector_eq(vector, vector) ; +ALTER FUNCTION vector_ne(vector, vector) ; +ALTER FUNCTION vector_ge(vector, vector) ; +ALTER FUNCTION vector_gt(vector, vector) ; +ALTER FUNCTION vector_cmp(vector, vector) ; +ALTER FUNCTION vector_l2_squared_distance(vector, vector) ; +ALTER FUNCTION vector_negative_inner_product(vector, vector) ; +ALTER FUNCTION vector_spherical_distance(vector, vector) ; +ALTER FUNCTION vector(vector, integer, boolean) ; +ALTER FUNCTION array_to_vector(integer[], integer, boolean) ; +ALTER FUNCTION array_to_vector(real[], integer, boolean) ; +ALTER FUNCTION array_to_vector(double precision[], integer, boolean) ; diff --git a/contrib/pgvector/sql/vector--0.1.6--0.1.7.sql b/contrib/pgvector/sql/vector--0.1.6--0.1.7.sql index fcd32f45a90a323e9caf2b19ce69ca5c9e1d4007..35baa394747ce2c1d6487028fc64a533966e222b 100644 --- a/contrib/pgvector/sql/vector--0.1.6--0.1.7.sql +++ b/contrib/pgvector/sql/vector--0.1.6--0.1.7.sql @@ -2,7 +2,7 @@ \echo Use "ALTER EXTENSION vector UPDATE TO '0.1.7'" to load this file. \quit CREATE FUNCTION array_to_vector(numeric[], integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE CAST (numeric[] AS vector) WITH FUNCTION array_to_vector(numeric[], integer, boolean) AS IMPLICIT; diff --git a/contrib/pgvector/sql/vector--0.1.7--0.1.8.sql b/contrib/pgvector/sql/vector--0.1.7--0.1.8.sql index 5a387a76b6d2867775c4f081f56ee47b71d4663a..c668ad555ea9846bf6c56b17e1a2051a49aca8ed 100644 --- a/contrib/pgvector/sql/vector--0.1.7--0.1.8.sql +++ b/contrib/pgvector/sql/vector--0.1.7--0.1.8.sql @@ -2,7 +2,7 @@ \echo Use "ALTER EXTENSION vector UPDATE TO '0.1.8'" to load this file. \quit CREATE FUNCTION vector_to_float4(vector, integer, boolean) RETURNS real[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE CAST (vector AS real[]) WITH FUNCTION vector_to_float4(vector, integer, boolean) AS IMPLICIT; diff --git a/contrib/pgvector/sql/vector--0.3.2--0.4.0.sql b/contrib/pgvector/sql/vector--0.3.2--0.4.0.sql index 3652664777c0fcc2f6e4f4e9b64dd5ebc8470a31..8b26dd8eda2669b8c7dc58f4129f56f72c1e27ac 100644 --- a/contrib/pgvector/sql/vector--0.3.2--0.4.0.sql +++ b/contrib/pgvector/sql/vector--0.3.2--0.4.0.sql @@ -5,13 +5,13 @@ ALTER TYPE vector SET (STORAGE = extended); CREATE FUNCTION vector_accum(double precision[], vector) RETURNS double precision[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_avg(double precision[]) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_combine(double precision[], double precision[]) RETURNS double precision[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE AGGREGATE avg(vector) ( SFUNC = vector_accum, diff --git a/contrib/pgvector/sql/vector.sql b/contrib/pgvector/sql/vector.sql index 6188e2eafb12197d7ffab25f19eb17dfa7ba3a3f..97603841c24f70214ed225ef34a43a5e438b700a 100644 --- a/contrib/pgvector/sql/vector.sql +++ b/contrib/pgvector/sql/vector.sql @@ -6,19 +6,19 @@ CREATE TYPE vector; CREATE FUNCTION vector_in(cstring, oid, integer) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_out(vector) RETURNS cstring - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_typmod_in(cstring[]) RETURNS integer - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_recv(internal, oid, integer) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_send(vector) RETURNS bytea - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE TYPE vector ( INPUT = vector_in, @@ -32,66 +32,66 @@ CREATE TYPE vector ( -- functions CREATE FUNCTION l2_distance(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION inner_product(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION cosine_distance(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_dims(vector) RETURNS integer - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_norm(vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_add(vector, vector) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_sub(vector, vector) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; -- private functions CREATE FUNCTION vector_lt(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_le(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_eq(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_ne(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_ge(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_gt(vector, vector) RETURNS bool - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_cmp(vector, vector) RETURNS int4 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_l2_squared_distance(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_negative_inner_product(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_spherical_distance(vector, vector) RETURNS float8 - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_accum(double precision[], vector) RETURNS double precision[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_avg(double precision[]) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_combine(double precision[], double precision[]) RETURNS double precision[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; -- aggregates @@ -99,30 +99,29 @@ CREATE AGGREGATE avg(vector) ( SFUNC = vector_accum, STYPE = double precision[], FINALFUNC = vector_avg, - COMBINEFUNC = vector_combine, - INITCOND = '{0}', - PARALLEL = SAFE + CFUNC = vector_combine, + INITCOND = '{0}' ); -- cast functions CREATE FUNCTION vector(vector, integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION array_to_vector(integer[], integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION array_to_vector(real[], integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION array_to_vector(double precision[], integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION array_to_vector(numeric[], integer, boolean) RETURNS vector - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; CREATE FUNCTION vector_to_float4(vector, integer, boolean) RETURNS real[] - AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT ; -- casts @@ -211,12 +210,47 @@ CREATE OPERATOR > ( -- access method +CREATE FUNCTION ivfflatbuild(internal, internal, internal) RETURNS internal + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatbuildempty(internal) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatinsert(internal, internal, internal, internal, internal, internal) RETURNS boolean + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatbulkdelete(internal, internal, internal, internal) RETURNS internal + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatvacuumcleanup(internal, internal) RETURNS internal + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatcostestimate(internal, internal, internal, internal, internal, internal, internal) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatoptions(internal, internal) RETURNS internal + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatvalidate(internal) RETURNS boolean + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatbeginscan(internal, internal, internal) RETURNS internal + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatrescan(internal, internal, internal, internal, internal) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatgettuple(internal, internal) RETURNS boolean + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION ivfflatendscan(internal) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C; + CREATE FUNCTION ivfflathandler(internal) RETURNS index_am_handler AS 'MODULE_PATHNAME' LANGUAGE C; CREATE ACCESS METHOD ivfflat TYPE INDEX HANDLER ivfflathandler; -COMMENT ON ACCESS METHOD ivfflat IS 'ivfflat index access method'; -- opclasses diff --git a/contrib/pgvector/src/ivfbuild.c b/contrib/pgvector/src/ivfbuild.c index f74a821afa45f3d9648720dccfaf0e7e7f907a95..f60a8f0fb1f7ebd9fa099f3cf022f22713432043 100644 --- a/contrib/pgvector/src/ivfbuild.c +++ b/contrib/pgvector/src/ivfbuild.c @@ -5,7 +5,7 @@ #include "catalog/index.h" #include "ivfflat.h" #include "miscadmin.h" -#include "storage/bufmgr.h" +#include "storage/buf/bufmgr.h" #include "utils/memutils.h" #if PG_VERSION_NUM >= 140000 @@ -14,8 +14,8 @@ #include "pgstat.h" #endif -#if PG_VERSION_NUM >= 120000 #include "access/tableam.h" +#if PG_VERSION_NUM >= 120000 #include "commands/progress.h" #else #define PROGRESS_CREATEIDX_SUBPHASE 0 @@ -23,8 +23,13 @@ #define PROGRESS_CREATEIDX_TUPLES_DONE 0 #endif +#if PG_VERSION_NUM >= 110000 #include "catalog/pg_operator_d.h" #include "catalog/pg_type_d.h" +#else +#include "catalog/pg_operator.h" +#include "catalog/pg_type.h" +#endif #if PG_VERSION_NUM >= 130000 #define CALLBACK_ITEM_POINTER ItemPointer tid @@ -38,107 +43,6 @@ #define UpdateProgress(index, val) ((void)val) #endif -/* - * Add sample - */ -static void -AddSample(Datum *values, IvfflatBuildState * buildstate) -{ - VectorArray samples = buildstate->samples; - int targsamples = samples->maxlen; - - /* Detoast once for all calls */ - Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0])); - - /* - * Normalize with KMEANS_NORM_PROC since spherical distance function - * expects unit vectors - */ - if (buildstate->kmeansnormprocinfo != NULL) - { - if (!IvfflatNormValue(buildstate->kmeansnormprocinfo, buildstate->collation, &value, buildstate->normvec)) - return; - } - - if (samples->length < targsamples) - { - VectorArraySet(samples, samples->length, DatumGetVector(value)); - samples->length++; - } - else - { - if (buildstate->rowstoskip < 0) - buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples); - - if (buildstate->rowstoskip <= 0) - { -#if PG_VERSION_NUM >= 150000 - int k = (int) (targsamples * sampler_random_fract(&buildstate->rstate.randstate)); -#else - int k = (int) (targsamples * sampler_random_fract(buildstate->rstate.randstate)); -#endif - - Assert(k >= 0 && k < targsamples); - VectorArraySet(samples, k, DatumGetVector(value)); - } - - buildstate->rowstoskip -= 1; - } -} - -/* - * Callback for sampling - */ -static void -SampleCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, - bool *isnull, bool tupleIsAlive, void *state) -{ - IvfflatBuildState *buildstate = (IvfflatBuildState *) state; - MemoryContext oldCtx; - - /* Skip nulls */ - if (isnull[0]) - return; - - /* Use memory context since detoast can allocate */ - oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx); - - /* Add sample */ - AddSample(values, state); - - /* Reset memory context */ - MemoryContextSwitchTo(oldCtx); - MemoryContextReset(buildstate->tmpCtx); -} - -/* - * Sample rows with same logic as ANALYZE - */ -static void -SampleRows(IvfflatBuildState * buildstate) -{ - int targsamples = buildstate->samples->maxlen; - BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap); - - buildstate->rowstoskip = -1; - - BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt()); - - reservoir_init_selection_state(&buildstate->rstate, targsamples); - while (BlockSampler_HasMore(&buildstate->bs)) - { - BlockNumber targblock = BlockSampler_Next(&buildstate->bs); - -#if PG_VERSION_NUM >= 120000 - table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo, - false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL); -#else - IndexBuildHeapRangeScan(buildstate->heap, buildstate->index, buildstate->indexInfo, - false, true, targblock, 1, SampleCallback, (void *) buildstate, NULL); -#endif - } -} - /* * Add tuple to sort */ @@ -206,7 +110,7 @@ AddTupleToSort(Relation index, ItemPointer tid, Datum *values, IvfflatBuildState */ static void BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, - bool *isnull, bool tupleIsAlive, void *state) + const bool *isnull, bool tupleIsAlive, void *state) { IvfflatBuildState *buildstate = (IvfflatBuildState *) state; MemoryContext oldCtx; @@ -239,14 +143,14 @@ GetNextTuple(Tuplesortstate *sortstate, TupleDesc tupdesc, TupleTableSlot *slot, Datum value; bool isnull; - if (tuplesort_gettupleslot(sortstate, true, false, slot, NULL)) + if (tuplesort_gettupleslot(sortstate, true, slot, NULL)) { - *list = DatumGetInt32(slot_getattr(slot, 1, &isnull)); - value = slot_getattr(slot, 3, &isnull); + *list = DatumGetInt32(tableam_tslot_getattr(slot, 1, &isnull)); + value = heap_slot_getattr(slot, 3, &isnull); /* Form the index tuple */ *itup = index_form_tuple(tupdesc, &value, &isnull); - (*itup)->t_tid = *((ItemPointer) DatumGetPointer(slot_getattr(slot, 2, &isnull))); + (*itup)->t_tid = *((ItemPointer) DatumGetPointer(tableam_tslot_getattr(slot, 2, &isnull))); } else *list = -1; @@ -260,7 +164,6 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum) { Buffer buf; Page page; - GenericXLogState *state; int list; IndexTuple itup = NULL; /* silence compiler warning */ BlockNumber startPage; @@ -289,7 +192,7 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum) CHECK_FOR_INTERRUPTS(); buf = IvfflatNewBuffer(index, forkNum); - IvfflatInitRegisterPage(index, &buf, &page, &state); + IvfflatInitRegisterPage(index, &buf, &page); startPage = BufferGetBlockNumber(buf); @@ -299,7 +202,7 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum) /* Check for free space */ itemsz = MAXALIGN(IndexTupleSize(itup)); if (PageGetFreeSpace(page) < itemsz) - IvfflatAppendPage(index, &buf, &page, &state, forkNum); + IvfflatAppendPage(index, &buf, &page, forkNum); /* Add the item */ if (PageAddItem(page, (Item) itup, itemsz, InvalidOffsetNumber, false, false) == InvalidOffsetNumber) @@ -314,10 +217,10 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum) insertPage = BufferGetBlockNumber(buf); - IvfflatCommitBuffer(buf, state); + IvfflatCommitBuffer(buf); /* Set the start and insert pages */ - IvfflatUpdateList(index, state, buildstate->listInfo[i], insertPage, InvalidBlockNumber, startPage, forkNum); + IvfflatUpdateList(index, buildstate->listInfo[i], insertPage, InvalidBlockNumber, startPage, forkNum); } } @@ -373,7 +276,7 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In #endif buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions); - buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists); + buildstate->listInfo = (ListInfo*)palloc(sizeof(ListInfo) * buildstate->lists); /* Reuse for each tuple */ buildstate->normvec = InitVector(buildstate->dimensions); @@ -430,18 +333,6 @@ ComputeCenters(IvfflatBuildState * buildstate) /* Sample rows */ /* TODO Ensure within maintenance_work_mem */ buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions); - if (buildstate->heap != NULL) - { - SampleRows(buildstate); - - if (buildstate->samples->length < buildstate->lists) - { - ereport(NOTICE, - (errmsg("ivfflat index created with little data"), - errdetail("This will cause low recall."), - errhint("Drop the index until the table has more data."))); - } - } /* Calculate centers */ IvfflatBench("k-means", IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers)); @@ -458,11 +349,10 @@ CreateMetaPage(Relation index, int dimensions, int lists, ForkNumber forkNum) { Buffer buf; Page page; - GenericXLogState *state; IvfflatMetaPage metap; buf = IvfflatNewBuffer(index, forkNum); - IvfflatInitRegisterPage(index, &buf, &page, &state); + IvfflatInitRegisterPage(index, &buf, &page); /* Set metapage data */ metap = IvfflatPageGetMeta(page); @@ -473,7 +363,7 @@ CreateMetaPage(Relation index, int dimensions, int lists, ForkNumber forkNum) ((PageHeader) page)->pd_lower = ((char *) metap + sizeof(IvfflatMetaPageData)) - (char *) page; - IvfflatCommitBuffer(buf, state); + IvfflatCommitBuffer(buf); } /* @@ -486,16 +376,15 @@ CreateListPages(Relation index, VectorArray centers, int dimensions, int i; Buffer buf; Page page; - GenericXLogState *state; OffsetNumber offno; Size itemsz; IvfflatList list; itemsz = MAXALIGN(IVFFLAT_LIST_SIZE(dimensions)); - list = palloc(itemsz); + list = (IvfflatList)palloc(itemsz); buf = IvfflatNewBuffer(index, forkNum); - IvfflatInitRegisterPage(index, &buf, &page, &state); + IvfflatInitRegisterPage(index, &buf, &page); for (i = 0; i < lists; i++) { @@ -506,7 +395,7 @@ CreateListPages(Relation index, VectorArray centers, int dimensions, /* Ensure free space */ if (PageGetFreeSpace(page) < itemsz) - IvfflatAppendPage(index, &buf, &page, &state, forkNum); + IvfflatAppendPage(index, &buf, &page, forkNum); /* Add the item */ offno = PageAddItem(page, (Item) list, itemsz, InvalidOffsetNumber, false, false); @@ -518,7 +407,7 @@ CreateListPages(Relation index, VectorArray centers, int dimensions, (*listInfo)[i].offno = offno; } - IvfflatCommitBuffer(buf, state); + IvfflatCommitBuffer(buf); pfree(list); } @@ -590,13 +479,13 @@ static void CreateEntryPages(IvfflatBuildState * buildstate, ForkNumber forkNum) { AttrNumber attNums[] = {1}; - Oid sortOperators[] = {Int4LessOperator}; + Oid sortOperators[] = {INT4LTOID}; Oid sortCollations[] = {InvalidOid}; bool nullsFirstFlags[] = {false}; UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_SORT); - buildstate->sortstate = tuplesort_begin_heap(buildstate->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, maintenance_work_mem, NULL, false); + buildstate->sortstate = tuplesort_begin_heap(buildstate->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, u_sess->attr.attr_memory.maintenance_work_mem, NULL, false); /* Add tuples to sort */ if (buildstate->heap != NULL) @@ -637,7 +526,7 @@ BuildIndex(Relation heap, Relation index, IndexInfo *indexInfo, * Build the index for a logged table */ IndexBuildResult * -ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo) +ivfflatbuild_internal(Relation heap, Relation index, IndexInfo *indexInfo) { IndexBuildResult *result; IvfflatBuildState buildstate; @@ -655,7 +544,7 @@ ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo) * Build the index for an unlogged table */ void -ivfflatbuildempty(Relation index) +ivfflatbuildempty_internal(Relation index) { IndexInfo *indexInfo = BuildIndexInfo(index); IvfflatBuildState buildstate; diff --git a/contrib/pgvector/src/ivfflat.c b/contrib/pgvector/src/ivfflat.c index ef926a872d8dbeb11a3c08157729b1fada3b2755..5aaae0334ab90b6e8ffcef8b6f59ab0c7ce6d72a 100644 --- a/contrib/pgvector/src/ivfflat.c +++ b/contrib/pgvector/src/ivfflat.c @@ -8,6 +8,7 @@ #include "utils/guc.h" #include "utils/selfuncs.h" #include "utils/spccache.h" +#include "securec.h" #if PG_VERSION_NUM >= 120000 #include "commands/progress.h" @@ -62,19 +63,15 @@ ivfflatbuildphasename(int64 phasenum) * Estimate the cost of an index scan */ static void -ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, +ivfflatcostestimate_internal(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, - Selectivity *indexSelectivity, double *indexCorrelation, - double *indexPages) + Selectivity *indexSelectivity, double *indexCorrelation) { GenericCosts costs; int lists; double ratio; double spc_seq_page_cost; Relation indexRel; -#if PG_VERSION_NUM < 120000 - List *qinfos; -#endif /* Never use index without order */ if (path->indexorderbys == NULL) @@ -83,7 +80,6 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, *indexTotalCost = DBL_MAX; *indexSelectivity = 0; *indexCorrelation = 0; - *indexPages = 0; return; } @@ -108,8 +104,8 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, #if PG_VERSION_NUM >= 120000 genericcostestimate(root, path, loop_count, &costs); #else - qinfos = deconstruct_indexquals(path); - genericcostestimate(root, path, loop_count, qinfos, &costs); + genericcostestimate(root, path, loop_count, costs.numIndexTuples, &costs.indexStartupCost, + &costs.indexTotalCost, &costs.indexSelectivity, &costs.indexCorrelation); #endif get_tablespace_page_costs(path->indexinfo->reltablespace, NULL, &spc_seq_page_cost); @@ -141,14 +137,13 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, *indexTotalCost = costs.indexTotalCost; *indexSelectivity = costs.indexSelectivity; *indexCorrelation = costs.indexCorrelation; - *indexPages = costs.numIndexPages; } /* * Parse and validate the reloptions */ static bytea * -ivfflatoptions(Datum reloptions, bool validate) +ivfflatoptions_internal(Datum reloptions, bool validate) { static const relopt_parse_elt tab[] = { {"lists", RELOPT_TYPE_INT, offsetof(IvfflatOptions, lists)}, @@ -165,7 +160,7 @@ ivfflatoptions(Datum reloptions, bool validate) IvfflatOptions *rdopts; options = parseRelOptions(reloptions, validate, ivfflat_relopt_kind, &numoptions); - rdopts = allocateReloptStruct(sizeof(IvfflatOptions), options, numoptions); + rdopts = (IvfflatOptions *)allocateReloptStruct(sizeof(IvfflatOptions), options, numoptions); fillRelOptions((void *) rdopts, sizeof(IvfflatOptions), options, numoptions, validate, tab, lengthof(tab)); @@ -177,11 +172,27 @@ ivfflatoptions(Datum reloptions, bool validate) * Validate catalog entries for the specified operator class */ static bool -ivfflatvalidate(Oid opclassoid) +ivfflatvalidate_internal(Oid opclassoid) { return true; } +extern "C" { + Datum ivfflathandler(PG_FUNCTION_ARGS); + Datum ivfflatbuild(PG_FUNCTION_ARGS); + Datum ivfflatbuildempty(PG_FUNCTION_ARGS); + Datum ivfflatinsert(PG_FUNCTION_ARGS); + Datum ivfflatbulkdelete(PG_FUNCTION_ARGS); + Datum ivfflatvacuumcleanup(PG_FUNCTION_ARGS); + Datum ivfflatcostestimate(PG_FUNCTION_ARGS); + Datum ivfflatoptions(PG_FUNCTION_ARGS); + Datum ivfflatvalidate(PG_FUNCTION_ARGS); + Datum ivfflatbeginscan(PG_FUNCTION_ARGS); + Datum ivfflatrescan(PG_FUNCTION_ARGS); + Datum ivfflatgettuple(PG_FUNCTION_ARGS); + Datum ivfflatendscan(PG_FUNCTION_ARGS); +} + /* * Define index handler * @@ -209,8 +220,7 @@ ivfflathandler(PG_FUNCTION_ARGS) amroutine->amstorage = false; amroutine->amclusterable = false; amroutine->ampredlocks = false; - amroutine->amcanparallel = false; - amroutine->amcaninclude = false; + #if PG_VERSION_NUM >= 130000 amroutine->amusemaintenanceworkmem = false; /* not used during VACUUM */ amroutine->amparallelvacuumoptions = VACUUM_OPTION_PARALLEL_BULKDEL; @@ -218,34 +228,183 @@ ivfflathandler(PG_FUNCTION_ARGS) amroutine->amkeytype = InvalidOid; /* Interface functions */ - amroutine->ambuild = ivfflatbuild; - amroutine->ambuildempty = ivfflatbuildempty; - amroutine->aminsert = ivfflatinsert; - amroutine->ambulkdelete = ivfflatbulkdelete; - amroutine->amvacuumcleanup = ivfflatvacuumcleanup; - amroutine->amcanreturn = NULL; /* tuple not included in heapsort */ - amroutine->amcostestimate = ivfflatcostestimate; - amroutine->amoptions = ivfflatoptions; - amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */ -#if PG_VERSION_NUM >= 120000 - amroutine->ambuildphasename = ivfflatbuildphasename; -#endif - amroutine->amvalidate = ivfflatvalidate; -#if PG_VERSION_NUM >= 140000 - amroutine->amadjustmembers = NULL; -#endif - amroutine->ambeginscan = ivfflatbeginscan; - amroutine->amrescan = ivfflatrescan; - amroutine->amgettuple = ivfflatgettuple; - amroutine->amgetbitmap = NULL; - amroutine->amendscan = ivfflatendscan; - amroutine->ammarkpos = NULL; - amroutine->amrestrpos = NULL; - - /* Interface functions to support parallel index scans */ - amroutine->amestimateparallelscan = NULL; - amroutine->aminitparallelscan = NULL; - amroutine->amparallelrescan = NULL; + errno_t rc; + rc = strcpy_s(amroutine->ambuildfuncname, NAMEDATALEN, "ivfflatbuild"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->ambuildemptyfuncname, NAMEDATALEN, "ivfflatbuildempty"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->aminsertfuncname, NAMEDATALEN, "ivfflatinsert"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->ambulkdeletefuncname, NAMEDATALEN, "ivfflatbulkdelete"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amvacuumcleanupfuncname, NAMEDATALEN, "ivfflatvacuumcleanup"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amcostestimatefuncname, NAMEDATALEN, "ivfflatcostestimate"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amoptionsfuncname, NAMEDATALEN, "ivfflatoptions"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amvalidatefuncname, NAMEDATALEN, "ivfflatvalidate"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->ambeginscanfuncname, NAMEDATALEN, "ivfflatbeginscan"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amrescanfuncname, NAMEDATALEN, "ivfflatrescan"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amgettuplefuncname, NAMEDATALEN, "ivfflatgettuple"); + securec_check(rc, "\0", "\0"); + rc = strcpy_s(amroutine->amendscanfuncname, NAMEDATALEN, "ivfflatendscan"); + securec_check(rc, "\0", "\0"); PG_RETURN_POINTER(amroutine); } + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatbuild); +Datum +ivfflatbuild(PG_FUNCTION_ARGS) +{ + Relation heap = (Relation)PG_GETARG_POINTER(0); + Relation index = (Relation)PG_GETARG_POINTER(1); + IndexInfo *indexinfo = (IndexInfo *)PG_GETARG_POINTER(2); + IndexBuildResult *result = ivfflatbuild_internal(heap, index, indexinfo); + + PG_RETURN_POINTER(result); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatbuildempty); +Datum +ivfflatbuildempty(PG_FUNCTION_ARGS) +{ + Relation index = (Relation)PG_GETARG_POINTER(0); + ivfflatbuildempty_internal(index); + + PG_RETURN_VOID(); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatinsert); +Datum +ivfflatinsert(PG_FUNCTION_ARGS) +{ + Relation rel = (Relation)PG_GETARG_POINTER(0); + Datum * values = (Datum *)PG_GETARG_POINTER(1); + bool *isnull = (bool *)PG_GETARG_POINTER(2); + ItemPointer ht_ctid = (ItemPointer)PG_GETARG_POINTER(3); + Relation heaprel = (Relation)PG_GETARG_POINTER(4); + IndexUniqueCheck checkunique = (IndexUniqueCheck)PG_GETARG_INT32(5); + bool result = ivfflatinsert_internal(rel, values, isnull, ht_ctid, heaprel, checkunique); + + PG_RETURN_BOOL(result); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatbulkdelete); +Datum +ivfflatbulkdelete(PG_FUNCTION_ARGS) +{ + IndexVacuumInfo *info = (IndexVacuumInfo *)PG_GETARG_POINTER(0); + IndexBulkDeleteResult *volatile stats = (IndexBulkDeleteResult *)PG_GETARG_POINTER(1); + IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback)PG_GETARG_POINTER(2); + void *callback_state = (void *)PG_GETARG_POINTER(3); + stats = ivfflatbulkdelete_internal(info, stats, callback, callback_state); + + PG_RETURN_POINTER(stats); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatvacuumcleanup); +Datum +ivfflatvacuumcleanup(PG_FUNCTION_ARGS) +{ + IndexVacuumInfo *info = (IndexVacuumInfo *)PG_GETARG_POINTER(0); + IndexBulkDeleteResult *stats = (IndexBulkDeleteResult *)PG_GETARG_POINTER(1); + stats = ivfflatvacuumcleanup_internal(info, stats); + + PG_RETURN_POINTER(stats); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatcostestimate); +Datum +ivfflatcostestimate(PG_FUNCTION_ARGS) +{ + PlannerInfo *root = (PlannerInfo *)PG_GETARG_POINTER(0); + IndexPath *path = (IndexPath *)PG_GETARG_POINTER(1); + double loopcount = (double)PG_GETARG_FLOAT8(2); + Cost *startupcost = (Cost *)PG_GETARG_POINTER(3); + Cost *totalcost = (Cost *)PG_GETARG_POINTER(4); + Selectivity *selectivity = (Selectivity *)PG_GETARG_POINTER(5); + double *correlation = (double *)PG_GETARG_POINTER(6); + ivfflatcostestimate_internal(root, path, loopcount, startupcost, totalcost, selectivity, correlation); + + PG_RETURN_VOID(); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatoptions); +Datum +ivfflatoptions(PG_FUNCTION_ARGS) +{ + Datum reloptions = PG_GETARG_DATUM(0); + bool validate = PG_GETARG_BOOL(1); + bytea *result = ivfflatoptions_internal(reloptions, validate); + + if (NULL != result) + PG_RETURN_BYTEA_P(result); + + PG_RETURN_NULL(); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatvalidate); +Datum +ivfflatvalidate(PG_FUNCTION_ARGS) +{ + Oid opclassoid = PG_GETARG_OID(0); + bool result = ivfflatvalidate_internal(opclassoid); + + PG_RETURN_BOOL(result); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatbeginscan); +Datum +ivfflatbeginscan(PG_FUNCTION_ARGS) +{ + Relation rel = (Relation)PG_GETARG_POINTER(0); + int nkeys = PG_GETARG_INT32(1); + int norderbys = PG_GETARG_INT32(2); + IndexScanDesc scan = ivfflatbeginscan_internal(rel, nkeys, norderbys); + + PG_RETURN_POINTER(scan); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatrescan); +Datum +ivfflatrescan(PG_FUNCTION_ARGS) +{ + IndexScanDesc scan = (IndexScanDesc)PG_GETARG_POINTER(0); + ScanKey scankey = (ScanKey)PG_GETARG_POINTER(1); + int nkeys = PG_GETARG_INT32(2); + ScanKey orderbys = (ScanKey)PG_GETARG_POINTER(3); + int norderbys = PG_GETARG_INT32(4); + ivfflatrescan_internal(scan, scankey, nkeys, orderbys, norderbys); + + PG_RETURN_VOID(); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatgettuple); +Datum +ivfflatgettuple(PG_FUNCTION_ARGS) +{ + IndexScanDesc scan = (IndexScanDesc)PG_GETARG_POINTER(0); + ScanDirection direction = (ScanDirection)PG_GETARG_INT32(1); + + if (NULL == scan) + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid arguments for function ivfflatgettuple"))); + + bool result = ivfflatgettuple_internal(scan, direction); + + PG_RETURN_BOOL(result); +} + +PGDLLEXPORT PG_FUNCTION_INFO_V1(ivfflatendscan); +Datum +ivfflatendscan(PG_FUNCTION_ARGS) +{ + IndexScanDesc scan = (IndexScanDesc)PG_GETARG_POINTER(0); + ivfflatendscan_internal(scan); + + PG_RETURN_VOID(); +} diff --git a/contrib/pgvector/src/ivfflat.h b/contrib/pgvector/src/ivfflat.h index 5bd7622caa1d1a28e7fe9c330c07e3884d8155dd..8f6f8b600a9101e79fa19e0a5a8b4c6f63950e3b 100644 --- a/contrib/pgvector/src/ivfflat.h +++ b/contrib/pgvector/src/ivfflat.h @@ -3,15 +3,11 @@ #include "postgres.h" -#if PG_VERSION_NUM < 110000 -#error "Requires PostgreSQL 11+" -#endif - -#include "access/generic_xlog.h" #include "access/reloptions.h" #include "nodes/execnodes.h" #include "port.h" /* for strtof() and random() */ -#include "utils/sampling.h" + +#include "lib/pairingheap.h" #include "utils/tuplesort.h" #include "vector.h" @@ -80,7 +76,7 @@ extern int ivfflat_probes; /* Exported functions */ -PGDLLEXPORT void _PG_init(void); +extern "C" void _PG_init(void); typedef struct VectorArrayData { @@ -138,11 +134,6 @@ typedef struct IvfflatBuildState int *listCounts; #endif - /* Sampling */ - BlockSamplerData bs; - ReservoirStateData rstate; - int rowstoskip; - /* Sorting */ Tuplesortstate *sortstate; TupleDesc tupdesc; @@ -224,27 +215,22 @@ void IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers); FmgrInfo *IvfflatOptionalProcInfo(Relation rel, uint16 procnum); bool IvfflatNormValue(FmgrInfo *procinfo, Oid collation, Datum *value, Vector * result); int IvfflatGetLists(Relation index); -void IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum); -void IvfflatCommitBuffer(Buffer buf, GenericXLogState *state); -void IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state, ForkNumber forkNum); +void IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum); +void IvfflatCommitBuffer(Buffer buf); +void IvfflatAppendPage(Relation index, Buffer *buf, Page *page, ForkNumber forkNum); Buffer IvfflatNewBuffer(Relation index, ForkNumber forkNum); void IvfflatInitPage(Buffer buf, Page page); -void IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state); +void IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page); /* Index access methods */ -IndexBuildResult *ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo); -void ivfflatbuildempty(Relation index); -bool ivfflatinsert(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heap, IndexUniqueCheck checkUnique -#if PG_VERSION_NUM >= 140000 - ,bool indexUnchanged -#endif - ,IndexInfo *indexInfo -); -IndexBulkDeleteResult *ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state); -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); -void ivfflatendscan(IndexScanDesc scan); +IndexBuildResult *ivfflatbuild_internal(Relation heap, Relation index, IndexInfo *indexInfo); +void ivfflatbuildempty_internal(Relation index); +bool ivfflatinsert_internal(Relation index, Datum *values, const bool *isnull, ItemPointer heap_tid, Relation heap, IndexUniqueCheck checkUnique); +IndexBulkDeleteResult *ivfflatbulkdelete_internal(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, const void *callback_state); +IndexBulkDeleteResult *ivfflatvacuumcleanup_internal(IndexVacuumInfo *info, IndexBulkDeleteResult *stats); +IndexScanDesc ivfflatbeginscan_internal(Relation index, int nkeys, int norderbys); +void ivfflatrescan_internal(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys); +bool ivfflatgettuple_internal(IndexScanDesc scan, ScanDirection dir); +void ivfflatendscan_internal(IndexScanDesc scan); #endif diff --git a/contrib/pgvector/src/ivfinsert.c b/contrib/pgvector/src/ivfinsert.c index 8761f6a564808e9b91e11c3eb69e5fdb46da193b..cef64290e27775889fb66ede8ac3d9be63daa619 100644 --- a/contrib/pgvector/src/ivfinsert.c +++ b/contrib/pgvector/src/ivfinsert.c @@ -3,7 +3,7 @@ #include #include "ivfflat.h" -#include "storage/bufmgr.h" +#include "storage/buf/bufmgr.h" #include "utils/memutils.h" /* @@ -62,14 +62,13 @@ FindInsertPage(Relation rel, Datum *values, BlockNumber *insertPage, ListInfo * * Insert a tuple into the index */ static void -InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heapRel) +InsertTuple(Relation rel, Datum *values, const bool *isnull, ItemPointer heap_tid, Relation heapRel) { IndexTuple itup; Datum value; FmgrInfo *normprocinfo; Buffer buf; Page page; - GenericXLogState *state; Size itemsz; BlockNumber insertPage = InvalidBlockNumber; ListInfo listInfo; @@ -105,8 +104,7 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel buf = ReadBuffer(rel, insertPage); LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); - state = GenericXLogStart(rel); - page = GenericXLogRegisterBuffer(state, buf, 0); + page = BufferGetPage(buf); if (PageGetFreeSpace(page) >= itemsz) break; @@ -115,8 +113,6 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel if (BlockNumberIsValid(insertPage)) { - /* Move to next page */ - GenericXLogAbort(state); UnlockReleaseBuffer(buf); } else @@ -135,7 +131,7 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel /* Add a new page */ newbuf = IvfflatNewBuffer(rel, MAIN_FORKNUM); - newpage = GenericXLogRegisterBuffer(state, newbuf, GENERIC_XLOG_FULL_IMAGE); + newpage = BufferGetPage(newbuf); /* Init new page */ IvfflatInitPage(newbuf, newpage); @@ -149,7 +145,6 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel /* Commit */ MarkBufferDirty(newbuf); MarkBufferDirty(buf); - GenericXLogFinish(state); /* Unlock extend relation lock as early as possible */ UnlockReleaseBuffer(metabuf); @@ -158,9 +153,8 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel UnlockReleaseBuffer(buf); /* Prepare new buffer */ - state = GenericXLogStart(rel); buf = newbuf; - page = GenericXLogRegisterBuffer(state, buf, 0); + page = BufferGetPage(buf); break; } } @@ -169,24 +163,19 @@ InsertTuple(Relation rel, Datum *values, bool *isnull, ItemPointer heap_tid, Rel if (PageAddItem(page, (Item) itup, itemsz, InvalidOffsetNumber, false, false) == InvalidOffsetNumber) elog(ERROR, "failed to add index item to \"%s\"", RelationGetRelationName(rel)); - IvfflatCommitBuffer(buf, state); + IvfflatCommitBuffer(buf); /* Update the insert page */ if (insertPage != originalInsertPage) - IvfflatUpdateList(rel, state, listInfo, insertPage, originalInsertPage, InvalidBlockNumber, MAIN_FORKNUM); + IvfflatUpdateList(rel, listInfo, insertPage, originalInsertPage, InvalidBlockNumber, MAIN_FORKNUM); } /* * Insert a tuple into the index */ bool -ivfflatinsert(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, - Relation heap, IndexUniqueCheck checkUnique -#if PG_VERSION_NUM >= 140000 - ,bool indexUnchanged -#endif - ,IndexInfo *indexInfo -) +ivfflatinsert_internal(Relation index, Datum *values, const bool *isnull, ItemPointer heap_tid, + Relation heap, IndexUniqueCheck checkUnique) { MemoryContext oldCtx; MemoryContext insertCtx; diff --git a/contrib/pgvector/src/ivfkmeans.c b/contrib/pgvector/src/ivfkmeans.c index eb94de0e3cfbb8d7636ddca9a4e7dbc906907699..0f8ddefa4d9370ca59d80987d151043042ebd159 100644 --- a/contrib/pgvector/src/ivfkmeans.c +++ b/contrib/pgvector/src/ivfkmeans.c @@ -22,7 +22,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low double sum; double choice; Vector *vec; - float *weight = palloc(samples->length * sizeof(float)); + float *weight = (float *)palloc(samples->length * sizeof(float)); int numCenters = centers->maxlen; int numSamples = samples->length; @@ -212,11 +212,11 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers) /* Check memory requirements */ /* Add one to error message to ceil */ - if (totalSize > (Size) maintenance_work_mem * 1024L) + if (totalSize > (Size) u_sess->attr.attr_memory.maintenance_work_mem * 1024L) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("memory required is %zu MB, maintenance_work_mem is %d MB", - totalSize / (1024 * 1024) + 1, maintenance_work_mem / 1024))); + totalSize / (1024 * 1024) + 1, u_sess->attr.attr_memory.maintenance_work_mem / 1024))); /* Ensure indexing does not overflow */ if (numCenters * numCenters > INT_MAX) @@ -229,13 +229,13 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers) /* Allocate space */ /* Use float instead of double to save memory */ - centerCounts = palloc(centerCountsSize); - closestCenters = palloc(closestCentersSize); - lowerBound = palloc_extended(lowerBoundSize, MCXT_ALLOC_HUGE); - upperBound = palloc(upperBoundSize); - s = palloc(sSize); - halfcdist = palloc_extended(halfcdistSize, MCXT_ALLOC_HUGE); - newcdist = palloc(newcdistSize); + centerCounts = (int *)palloc(centerCountsSize); + closestCenters = (int *)palloc(closestCentersSize); + lowerBound = (float *)palloc_extended(lowerBoundSize, MCXT_ALLOC_HUGE); + upperBound = (float *)palloc(upperBoundSize); + s = (float *)palloc(sSize); + halfcdist = (float *)palloc_extended(halfcdistSize, MCXT_ALLOC_HUGE); + newcdist = (float *)palloc(newcdistSize); newCenters = VectorArrayInit(numCenters, dimensions); for (j = 0; j < numCenters; j++) diff --git a/contrib/pgvector/src/ivfscan.c b/contrib/pgvector/src/ivfscan.c index fa3961b03e41c363fdf517c24246cee21a1b757f..6945429a9e8d83621516b1fcb06fbeab35a233f4 100644 --- a/contrib/pgvector/src/ivfscan.c +++ b/contrib/pgvector/src/ivfscan.c @@ -3,13 +3,19 @@ #include #include "access/relscan.h" +#include "access/tableam.h" #include "ivfflat.h" #include "miscadmin.h" #include "pgstat.h" -#include "storage/bufmgr.h" +#include "storage/buf/bufmgr.h" +#if PG_VERSION_NUM >= 110000 #include "catalog/pg_operator_d.h" #include "catalog/pg_type_d.h" +#else +#include "catalog/pg_operator.h" +#include "catalog/pg_type.h" +#endif /* * Compare list distances @@ -186,13 +192,13 @@ GetScanItems(IndexScanDesc scan, Datum value) * Prepare for an index scan */ IndexScanDesc -ivfflatbeginscan(Relation index, int nkeys, int norderbys) +ivfflatbeginscan_internal(Relation index, int nkeys, int norderbys) { IndexScanDesc scan; IvfflatScanOpaque so; int lists; AttrNumber attNums[] = {1}; - Oid sortOperators[] = {Float8LessOperator}; + Oid sortOperators[] = {FLOAT8LTOID}; Oid sortCollations[] = {InvalidOid}; bool nullsFirstFlags[] = {false}; int probes = ivfflat_probes; @@ -224,7 +230,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) TupleDescInitEntry(so->tupdesc, (AttrNumber) 3, "indexblkno", INT4OID, -1, 0); /* Prep sort */ - so->sortstate = tuplesort_begin_heap(so->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, work_mem, NULL, false); + so->sortstate = tuplesort_begin_heap(so->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, u_sess->attr.attr_memory.work_mem, NULL, false); #if PG_VERSION_NUM >= 120000 so->slot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsMinimalTuple); @@ -243,7 +249,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) * Start or restart an index scan */ void -ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys) +ivfflatrescan_internal(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys) { IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque; @@ -266,7 +272,7 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int * Fetch the next tuple in the given scan */ bool -ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) +ivfflatgettuple_internal(IndexScanDesc scan, ScanDirection dir) { IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque; @@ -313,10 +319,10 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) pfree(DatumGetPointer(value)); } - if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL)) + if (tuplesort_gettupleslot(so->sortstate, true, so->slot, NULL)) { - ItemPointer tid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull)); - BlockNumber indexblkno = DatumGetInt32(slot_getattr(so->slot, 3, &so->isnull)); + ItemPointer tid = (ItemPointer) DatumGetPointer(tableam_tslot_getattr(so->slot, 2, &so->isnull)); + BlockNumber indexblkno = DatumGetInt32(tableam_tslot_getattr(so->slot, 3, &so->isnull)); #if PG_VERSION_NUM >= 120000 scan->xs_heaptid = *tid; @@ -335,7 +341,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) */ so->buf = ReadBuffer(scan->indexRelation, indexblkno); - scan->xs_recheckorderby = false; return true; } @@ -346,7 +351,7 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) * End a scan and release resources */ void -ivfflatendscan(IndexScanDesc scan) +ivfflatendscan_internal(IndexScanDesc scan) { IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque; diff --git a/contrib/pgvector/src/ivfutils.c b/contrib/pgvector/src/ivfutils.c index 7cf6fe65db5b3461a2aa32d16d1eca6cd42ad852..bb983835dd556e70c9bad9aaaa3e564f2c4d9b70 100644 --- a/contrib/pgvector/src/ivfutils.c +++ b/contrib/pgvector/src/ivfutils.c @@ -1,7 +1,7 @@ #include "postgres.h" #include "ivfflat.h" -#include "storage/bufmgr.h" +#include "storage/buf/bufmgr.h" #include "vector.h" /* @@ -10,12 +10,12 @@ VectorArray VectorArrayInit(int maxlen, int dimensions) { - VectorArray res = palloc(sizeof(VectorArrayData)); + VectorArray res = (VectorArray)palloc(sizeof(VectorArrayData)); res->length = 0; res->maxlen = maxlen; res->dim = dimensions; - res->items = palloc_extended(maxlen * VECTOR_SIZE(dimensions), MCXT_ALLOC_ZERO | MCXT_ALLOC_HUGE); + res->items = (Vector *)palloc_extended(maxlen * VECTOR_SIZE(dimensions), MCXT_ALLOC_ZERO | MCXT_ALLOC_HUGE); return res; } @@ -129,10 +129,9 @@ IvfflatInitPage(Buffer buf, Page page) * Init and register page */ void -IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state) +IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page) { - *state = GenericXLogStart(index); - *page = GenericXLogRegisterBuffer(*state, *buf, GENERIC_XLOG_FULL_IMAGE); + *page = BufferGetPage(*buf); IvfflatInitPage(*buf, *page); } @@ -140,10 +139,9 @@ IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogStat * Commit buffer */ void -IvfflatCommitBuffer(Buffer buf, GenericXLogState *state) +IvfflatCommitBuffer(Buffer buf) { MarkBufferDirty(buf); - GenericXLogFinish(state); UnlockReleaseBuffer(buf); } @@ -153,11 +151,11 @@ IvfflatCommitBuffer(Buffer buf, GenericXLogState *state) * The order is very important!! */ void -IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state, ForkNumber forkNum) +IvfflatAppendPage(Relation index, Buffer *buf, Page *page, ForkNumber forkNum) { /* Get new buffer */ Buffer newbuf = IvfflatNewBuffer(index, forkNum); - Page newpage = GenericXLogRegisterBuffer(*state, newbuf, GENERIC_XLOG_FULL_IMAGE); + Page newpage = BufferGetPage(newbuf); /* Update the previous buffer */ IvfflatPageGetOpaque(*page)->nextblkno = BufferGetBlockNumber(newbuf); @@ -168,13 +166,11 @@ IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **st /* Commit */ MarkBufferDirty(*buf); MarkBufferDirty(newbuf); - GenericXLogFinish(*state); /* Unlock */ UnlockReleaseBuffer(*buf); - *state = GenericXLogStart(index); - *page = GenericXLogRegisterBuffer(*state, newbuf, GENERIC_XLOG_FULL_IMAGE); + *page = BufferGetPage(newbuf); *buf = newbuf; } @@ -182,7 +178,7 @@ IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **st * Update the start or insert page of a list */ void -IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, +IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum) { @@ -193,8 +189,7 @@ IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, buf = ReadBufferExtended(index, forkNum, listInfo.blkno, RBM_NORMAL, NULL); LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); - state = GenericXLogStart(index); - page = GenericXLogRegisterBuffer(state, buf, 0); + page = BufferGetPage(buf); list = (IvfflatList) PageGetItem(page, PageGetItemId(page, listInfo.offno)); if (BlockNumberIsValid(insertPage) && insertPage != list->insertPage) @@ -216,10 +211,9 @@ IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, /* Only commit if changed */ if (changed) - IvfflatCommitBuffer(buf, state); + IvfflatCommitBuffer(buf); else { - GenericXLogAbort(state); UnlockReleaseBuffer(buf); } } diff --git a/contrib/pgvector/src/ivfvacuum.c b/contrib/pgvector/src/ivfvacuum.c index f9725f7a44db8048141a0a878274778c1b9ee9d9..64ee59259ceb1b5b488f16aba5fa8fd55a745a87 100644 --- a/contrib/pgvector/src/ivfvacuum.c +++ b/contrib/pgvector/src/ivfvacuum.c @@ -2,14 +2,15 @@ #include "commands/vacuum.h" #include "ivfflat.h" -#include "storage/bufmgr.h" +#include "storage/buf/bufmgr.h" + /* * Bulk delete tuples from the index */ IndexBulkDeleteResult * -ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, - IndexBulkDeleteCallback callback, void *callback_state) +ivfflatbulkdelete_internal(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, + IndexBulkDeleteCallback callback, const void *callback_state) { Relation index = info->index; Buffer cbuf; @@ -25,7 +26,6 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, BlockNumber nextblkno = IVFFLAT_HEAD_BLKNO; BlockNumber searchPage; BlockNumber insertPage; - GenericXLogState *state; OffsetNumber coffno; OffsetNumber cmaxoffno; OffsetNumber offno; @@ -77,8 +77,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, */ LockBufferForCleanup(buf); - state = GenericXLogStart(index); - page = GenericXLogRegisterBuffer(state, buf, 0); + page = BufferGetPage(buf); maxoffno = PageGetMaxOffsetNumber(page); ndeletable = 0; @@ -89,7 +88,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offno)); htup = &(itup->t_tid); - if (callback(htup, callback_state)) + if (callback(htup, (void *)callback_state, InvalidOid, InvalidBktId)) { deletable[ndeletable++] = offno; stats->tuples_removed++; @@ -110,10 +109,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, /* Delete tuples */ PageIndexMultiDelete(page, deletable, ndeletable); MarkBufferDirty(buf); - GenericXLogFinish(state); } - else - GenericXLogAbort(state); UnlockReleaseBuffer(buf); } @@ -127,7 +123,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, if (BlockNumberIsValid(insertPage)) { listInfo.offno = coffno; - IvfflatUpdateList(index, state, listInfo, insertPage, InvalidBlockNumber, InvalidBlockNumber, MAIN_FORKNUM); + IvfflatUpdateList(index, listInfo, insertPage, InvalidBlockNumber, InvalidBlockNumber, MAIN_FORKNUM); } } } @@ -141,7 +137,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, * Clean up after a VACUUM operation */ IndexBulkDeleteResult * -ivfflatvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) +ivfflatvacuumcleanup_internal(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) { Relation rel = info->index; diff --git a/contrib/pgvector/src/vector.c b/contrib/pgvector/src/vector.c index 394a478542c82f535904012050ac7c5cec3fd574..e62cae0121f98052fde78cec49da15e236a16c21 100644 --- a/contrib/pgvector/src/vector.c +++ b/contrib/pgvector/src/vector.c @@ -25,10 +25,42 @@ #endif #define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1) -#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1)) + +#define CreateStateDatums(dim) (Datum *)palloc(sizeof(Datum) * (dim + 1)) PG_MODULE_MAGIC; +extern "C" { + Datum vector_in(PG_FUNCTION_ARGS); + Datum vector_out(PG_FUNCTION_ARGS); + Datum vector_typmod_in(PG_FUNCTION_ARGS); + Datum vector_recv(PG_FUNCTION_ARGS); + Datum vector_send(PG_FUNCTION_ARGS); + Datum vector(PG_FUNCTION_ARGS); + Datum array_to_vector(PG_FUNCTION_ARGS); + Datum vector_to_float4(PG_FUNCTION_ARGS); + Datum l2_distance(PG_FUNCTION_ARGS); + Datum vector_l2_squared_distance(PG_FUNCTION_ARGS); + Datum inner_product(PG_FUNCTION_ARGS); + Datum vector_negative_inner_product(PG_FUNCTION_ARGS); + Datum cosine_distance(PG_FUNCTION_ARGS); + Datum vector_spherical_distance(PG_FUNCTION_ARGS); + Datum vector_dims(PG_FUNCTION_ARGS); + Datum vector_norm(PG_FUNCTION_ARGS); + Datum vector_add(PG_FUNCTION_ARGS); + Datum vector_sub(PG_FUNCTION_ARGS); + Datum vector_lt(PG_FUNCTION_ARGS); + Datum vector_le(PG_FUNCTION_ARGS); + Datum vector_eq(PG_FUNCTION_ARGS); + Datum vector_ne(PG_FUNCTION_ARGS); + Datum vector_ge(PG_FUNCTION_ARGS); + Datum vector_gt(PG_FUNCTION_ARGS); + Datum vector_cmp(PG_FUNCTION_ARGS); + Datum vector_accum(PG_FUNCTION_ARGS); + Datum vector_combine(PG_FUNCTION_ARGS); + Datum vector_avg(PG_FUNCTION_ARGS); +} + /* * Ensure same dimensions */ @@ -252,7 +284,7 @@ vector_out(PG_FUNCTION_ARGS) int n; #if PG_VERSION_NUM < 120000 - int ndig = FLT_DIG + extra_float_digits; + int ndig = FLT_DIG + u_sess->attr.attr_common.extra_float_digits; if (ndig < 1) ndig = 1; diff --git a/contrib/whale/plugin_utils/adt/selfuncs.cpp b/contrib/whale/plugin_utils/adt/selfuncs.cpp index a83b5e3fd50a6d92f4da96f620e8982f2f789712..55b3eb32875b730b0ce7865a831de05eb1935149 100644 --- a/contrib/whale/plugin_utils/adt/selfuncs.cpp +++ b/contrib/whale/plugin_utils/adt/selfuncs.cpp @@ -6441,7 +6441,7 @@ static List* add_predicate_to_quals(IndexOptInfo* index, List* indexQuals, Plann * visited. If numIndexTuples is not 0 then it is used as the estimate, * otherwise we compute a generic estimate. */ -static void genericcostestimate(PlannerInfo* root, IndexPath* path, double loop_count, double numIndexTuples, +void genericcostestimate(PlannerInfo* root, IndexPath* path, double loop_count, double numIndexTuples, Cost* indexStartupCost, Cost* indexTotalCost, Selectivity* indexSelectivity, double* indexCorrelation) { IndexOptInfo* index = path->indexinfo;