Skip to content

Add pgvector support #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: wasm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ OBJS += \
$(top_builddir)/src/pl/plpgsql/src/pl_handler.o \
$(top_builddir)/src/pl/plpgsql/src/pl_scanner.o

OBJS += \
$(top_builddir)/../pg_ext/pgvector/src/bitutils.o \
$(top_builddir)/../pg_ext/pgvector/src/bitvec.o \
$(top_builddir)/../pg_ext/pgvector/src/halfutils.o \
$(top_builddir)/../pg_ext/pgvector/src/halfvec.o \
$(top_builddir)/../pg_ext/pgvector/src/hnsw.o \
$(top_builddir)/../pg_ext/pgvector/src/hnswbuild.o \
$(top_builddir)/../pg_ext/pgvector/src/hnswscan.o \
$(top_builddir)/../pg_ext/pgvector/src/hnswutils.o \
$(top_builddir)/../pg_ext/pgvector/src/hnswvacuum.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfbuild.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfflat.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfinsert.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfkmeans.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfscan.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfutils.o \
$(top_builddir)/../pg_ext/pgvector/src/ivfvacuum.o \
$(top_builddir)/../pg_ext/pgvector/src/sparsevec.o \
$(top_builddir)/../pg_ext/pgvector/src/vector.o

postgres: $(OBJS)
$(CC) $(CFLAGS) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(LIBS) -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1 -lnodefs.js -lproxyfs.js -lidbfs.js -o $@

Expand Down
3 changes: 2 additions & 1 deletion src/backend/storage/lmgr/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ InitProcess(void)
* be careful and reinitialize its value here. (This is not strictly
* necessary anymore, but seems like a good idea for cleanliness.)
*/
PGSemaphoreReset(MyProc->sem);
// PGSemaphoreReset(MyProc->sem); // caused process to hang for some reason

/*
* Arrange to clean up at backend exit.
Expand All @@ -468,6 +468,7 @@ InitProcess(void)
* local state needed for LWLocks, and the deadlock checker.
*/
InitLWLockAccess();

InitDeadLockChecking();
}

Expand Down
244 changes: 244 additions & 0 deletions src/backend/utils/fmgr/dfmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "utils/hsearch.h"

#include "extensions/plpgsql.h"
#include "extensions/pgvector.h"

/* signatures for PostgreSQL-specific library init/fini functions */
typedef void (*PG_init_t) (void);
Expand Down Expand Up @@ -110,6 +111,222 @@ static const func_map function_map[] = {
{NULL, NULL}
};

bool pgvector_loaded = false;
static const func_map pgvector_func_map[] = {
{"sparsevec_in", sparsevec_in},
{"pg_finfo_sparsevec_in", pg_finfo_sparsevec_in},
{"sparsevec_out", sparsevec_out},
{"pg_finfo_sparsevec_out", pg_finfo_sparsevec_out},
{"sparsevec_typmod_in", sparsevec_typmod_in},
{"pg_finfo_sparsevec_typmod_in", pg_finfo_sparsevec_typmod_in},
{"sparsevec_recv", sparsevec_recv},
{"pg_finfo_sparsevec_recv", pg_finfo_sparsevec_recv},
{"sparsevec_send", sparsevec_send},
{"pg_finfo_sparsevec_send", pg_finfo_sparsevec_send},
{"sparsevec", sparsevec},
{"pg_finfo_sparsevec", pg_finfo_sparsevec},
{"vector_to_sparsevec", vector_to_sparsevec},
{"pg_finfo_vector_to_sparsevec", pg_finfo_vector_to_sparsevec},
{"halfvec_to_sparsevec", halfvec_to_sparsevec},
{"pg_finfo_halfvec_to_sparsevec", pg_finfo_halfvec_to_sparsevec},
{"sparsevec_l2_distance", sparsevec_l2_distance},
{"pg_finfo_sparsevec_l2_distance", pg_finfo_sparsevec_l2_distance},
{"sparsevec_l2_squared_distance", sparsevec_l2_squared_distance},
{"pg_finfo_sparsevec_l2_squared_distance", pg_finfo_sparsevec_l2_squared_distance},
{"sparsevec_inner_product", sparsevec_inner_product},
{"pg_finfo_sparsevec_inner_product", pg_finfo_sparsevec_inner_product},
{"sparsevec_negative_inner_product", sparsevec_negative_inner_product},
{"pg_finfo_sparsevec_negative_inner_product", pg_finfo_sparsevec_negative_inner_product},
{"sparsevec_cosine_distance", sparsevec_cosine_distance},
{"pg_finfo_sparsevec_cosine_distance", pg_finfo_sparsevec_cosine_distance},
{"sparsevec_l1_distance", sparsevec_l1_distance},
{"pg_finfo_sparsevec_l1_distance", pg_finfo_sparsevec_l1_distance},
{"sparsevec_l2_norm", sparsevec_l2_norm},
{"pg_finfo_sparsevec_l2_norm", pg_finfo_sparsevec_l2_norm},
{"sparsevec_l2_normalize", sparsevec_l2_normalize},
{"pg_finfo_sparsevec_l2_normalize", pg_finfo_sparsevec_l2_normalize},
{"sparsevec_lt", sparsevec_lt},
{"pg_finfo_sparsevec_lt", pg_finfo_sparsevec_lt},
{"sparsevec_le", sparsevec_le},
{"pg_finfo_sparsevec_le", pg_finfo_sparsevec_le},
{"sparsevec_eq", sparsevec_eq},
{"pg_finfo_sparsevec_eq", pg_finfo_sparsevec_eq},
{"sparsevec_ne", sparsevec_ne},
{"pg_finfo_sparsevec_ne", pg_finfo_sparsevec_ne},
{"sparsevec_ge", sparsevec_ge},
{"pg_finfo_sparsevec_ge", pg_finfo_sparsevec_ge},
{"sparsevec_gt", sparsevec_gt},
{"pg_finfo_sparsevec_gt", pg_finfo_sparsevec_gt},
{"sparsevec_cmp", sparsevec_cmp},
{"pg_finfo_sparsevec_cmp", pg_finfo_sparsevec_cmp},
{"hamming_distance", hamming_distance},
{"pg_finfo_hamming_distance", pg_finfo_hamming_distance},
{"jaccard_distance", jaccard_distance},
{"pg_finfo_jaccard_distance", pg_finfo_jaccard_distance},
{"vector_in", vector_in},
{"pg_finfo_vector_in", pg_finfo_vector_in},
{"vector_out", vector_out},
{"pg_finfo_vector_out", pg_finfo_vector_out},
{"vector_typmod_in", vector_typmod_in},
{"pg_finfo_vector_typmod_in", pg_finfo_vector_typmod_in},
{"vector_recv", vector_recv},
{"pg_finfo_vector_recv", pg_finfo_vector_recv},
{"vector_send", vector_send},
{"pg_finfo_vector_send", pg_finfo_vector_send},
{"vector", vector},
{"pg_finfo_vector", pg_finfo_vector},
{"array_to_vector", array_to_vector},
{"pg_finfo_array_to_vector", pg_finfo_array_to_vector},
{"vector_to_float4", vector_to_float4},
{"pg_finfo_vector_to_float4", pg_finfo_vector_to_float4},
{"halfvec_to_vector", halfvec_to_vector},
{"pg_finfo_halfvec_to_vector", pg_finfo_halfvec_to_vector},
{"l2_distance", l2_distance},
{"pg_finfo_l2_distance", pg_finfo_l2_distance},
{"vector_l2_squared_distance", vector_l2_squared_distance},
{"pg_finfo_vector_l2_squared_distance", pg_finfo_vector_l2_squared_distance},
{"inner_product", inner_product},
{"pg_finfo_inner_product", pg_finfo_inner_product},
{"vector_negative_inner_product", vector_negative_inner_product},
{"pg_finfo_vector_negative_inner_product", pg_finfo_vector_negative_inner_product},
{"cosine_distance", cosine_distance},
{"pg_finfo_cosine_distance", pg_finfo_cosine_distance},
{"vector_spherical_distance", vector_spherical_distance},
{"pg_finfo_vector_spherical_distance", pg_finfo_vector_spherical_distance},
{"l1_distance", l1_distance},
{"pg_finfo_l1_distance", pg_finfo_l1_distance},
{"vector_dims", vector_dims},
{"pg_finfo_vector_dims", pg_finfo_vector_dims},
{"vector_norm", vector_norm},
{"pg_finfo_vector_norm", pg_finfo_vector_norm},
{"l2_normalize", l2_normalize},
{"pg_finfo_l2_normalize", pg_finfo_l2_normalize},
{"vector_add", vector_add},
{"pg_finfo_vector_add", pg_finfo_vector_add},
{"vector_sub", vector_sub},
{"pg_finfo_vector_sub", pg_finfo_vector_sub},
{"vector_mul", vector_mul},
{"pg_finfo_vector_mul", pg_finfo_vector_mul},
{"vector_concat", vector_concat},
{"pg_finfo_vector_concat", pg_finfo_vector_concat},
{"binary_quantize", binary_quantize},
{"pg_finfo_binary_quantize", pg_finfo_binary_quantize},
{"subvector", subvector},
{"pg_finfo_subvector", pg_finfo_subvector},
{"vector_lt", vector_lt},
{"pg_finfo_vector_lt", pg_finfo_vector_lt},
{"vector_le", vector_le},
{"pg_finfo_vector_le", pg_finfo_vector_le},
{"vector_eq", vector_eq},
{"pg_finfo_vector_eq", pg_finfo_vector_eq},
{"vector_ne", vector_ne},
{"pg_finfo_vector_ne", pg_finfo_vector_ne},
{"vector_ge", vector_ge},
{"pg_finfo_vector_ge", pg_finfo_vector_ge},
{"vector_gt", vector_gt},
{"pg_finfo_vector_gt", pg_finfo_vector_gt},
{"vector_cmp", vector_cmp},
{"pg_finfo_vector_cmp", pg_finfo_vector_cmp},
{"vector_accum", vector_accum},
{"pg_finfo_vector_accum", pg_finfo_vector_accum},
{"vector_combine", vector_combine},
{"pg_finfo_vector_combine", pg_finfo_vector_combine},
{"vector_avg", vector_avg},
{"pg_finfo_vector_avg", pg_finfo_vector_avg},
{"sparsevec_to_vector", sparsevec_to_vector},
{"pg_finfo_sparsevec_to_vector", pg_finfo_sparsevec_to_vector},
{"ivfflathandler", ivfflathandler},
{"pg_finfo_ivfflathandler", pg_finfo_ivfflathandler},
{"halfvec_in", halfvec_in},
{"pg_finfo_halfvec_in", pg_finfo_halfvec_in},
{"halfvec_out", halfvec_out},
{"pg_finfo_halfvec_out", pg_finfo_halfvec_out},
{"halfvec_typmod_in", halfvec_typmod_in},
{"pg_finfo_halfvec_typmod_in", pg_finfo_halfvec_typmod_in},
{"halfvec_recv", halfvec_recv},
{"pg_finfo_halfvec_recv", pg_finfo_halfvec_recv},
{"halfvec_send", halfvec_send},
{"pg_finfo_halfvec_send", pg_finfo_halfvec_send},
{"halfvec", halfvec},
{"pg_finfo_halfvec", pg_finfo_halfvec},
{"array_to_halfvec", array_to_halfvec},
{"pg_finfo_array_to_halfvec", pg_finfo_array_to_halfvec},
{"halfvec_to_float4", halfvec_to_float4},
{"pg_finfo_halfvec_to_float4", pg_finfo_halfvec_to_float4},
{"vector_to_halfvec", vector_to_halfvec},
{"pg_finfo_vector_to_halfvec", pg_finfo_vector_to_halfvec},
{"halfvec_l2_distance", halfvec_l2_distance},
{"pg_finfo_halfvec_l2_distance", pg_finfo_halfvec_l2_distance},
{"halfvec_l2_squared_distance", halfvec_l2_squared_distance},
{"pg_finfo_halfvec_l2_squared_distance", pg_finfo_halfvec_l2_squared_distance},
{"halfvec_inner_product", halfvec_inner_product},
{"pg_finfo_halfvec_inner_product", pg_finfo_halfvec_inner_product},
{"halfvec_negative_inner_product", halfvec_negative_inner_product},
{"pg_finfo_halfvec_negative_inner_product", pg_finfo_halfvec_negative_inner_product},
{"halfvec_cosine_distance", halfvec_cosine_distance},
{"pg_finfo_halfvec_cosine_distance", pg_finfo_halfvec_cosine_distance},
{"halfvec_spherical_distance", halfvec_spherical_distance},
{"pg_finfo_halfvec_spherical_distance", pg_finfo_halfvec_spherical_distance},
{"halfvec_l1_distance", halfvec_l1_distance},
{"pg_finfo_halfvec_l1_distance", pg_finfo_halfvec_l1_distance},
{"halfvec_vector_dims", halfvec_vector_dims},
{"pg_finfo_halfvec_vector_dims", pg_finfo_halfvec_vector_dims},
{"halfvec_l2_norm", halfvec_l2_norm},
{"pg_finfo_halfvec_l2_norm", pg_finfo_halfvec_l2_norm},
{"halfvec_l2_normalize", halfvec_l2_normalize},
{"pg_finfo_halfvec_l2_normalize", pg_finfo_halfvec_l2_normalize},
{"halfvec_add", halfvec_add},
{"pg_finfo_halfvec_add", pg_finfo_halfvec_add},
{"halfvec_sub", halfvec_sub},
{"pg_finfo_halfvec_sub", pg_finfo_halfvec_sub},
{"halfvec_mul", halfvec_mul},
{"pg_finfo_halfvec_mul", pg_finfo_halfvec_mul},
{"halfvec_concat", halfvec_concat},
{"pg_finfo_halfvec_concat", pg_finfo_halfvec_concat},
{"halfvec_binary_quantize", halfvec_binary_quantize},
{"pg_finfo_halfvec_binary_quantize", pg_finfo_halfvec_binary_quantize},
{"halfvec_subvector", halfvec_subvector},
{"pg_finfo_halfvec_subvector", pg_finfo_halfvec_subvector},
{"halfvec_lt", halfvec_lt},
{"pg_finfo_halfvec_lt", pg_finfo_halfvec_lt},
{"halfvec_le", halfvec_le},
{"pg_finfo_halfvec_le", pg_finfo_halfvec_le},
{"halfvec_eq", halfvec_eq},
{"pg_finfo_halfvec_eq", pg_finfo_halfvec_eq},
{"halfvec_ne", halfvec_ne},
{"pg_finfo_halfvec_ne", pg_finfo_halfvec_ne},
{"halfvec_ge", halfvec_ge},
{"pg_finfo_halfvec_ge", pg_finfo_halfvec_ge},
{"halfvec_gt", halfvec_gt},
{"pg_finfo_halfvec_gt", pg_finfo_halfvec_gt},
{"halfvec_cmp", halfvec_cmp},
{"pg_finfo_halfvec_cmp", pg_finfo_halfvec_cmp},
{"halfvec_accum", halfvec_accum},
{"pg_finfo_halfvec_accum", pg_finfo_halfvec_accum},
{"halfvec_avg", halfvec_avg},
{"pg_finfo_halfvec_avg", pg_finfo_halfvec_avg},
{"sparsevec_to_halfvec", sparsevec_to_halfvec},
{"pg_finfo_sparsevec_to_halfvec", pg_finfo_sparsevec_to_halfvec},
{"l2_normalize", l2_normalize},
{"halfvec_l2_normalize", halfvec_l2_normalize},
{"sparsevec_l2_normalize", sparsevec_l2_normalize},
{"ivfflat_halfvec_support", ivfflat_halfvec_support},
{"pg_finfo_ivfflat_halfvec_support", pg_finfo_ivfflat_halfvec_support},
{"ivfflat_bit_support", ivfflat_bit_support},
{"pg_finfo_ivfflat_bit_support", pg_finfo_ivfflat_bit_support},
{"hnswhandler", hnswhandler},
{"pg_finfo_hnswhandler", pg_finfo_hnswhandler},
{"l2_normalize", l2_normalize},
{"halfvec_l2_normalize", halfvec_l2_normalize},
{"sparsevec_l2_normalize", sparsevec_l2_normalize},
{"hnsw_halfvec_support", hnsw_halfvec_support},
{"pg_finfo_hnsw_halfvec_support", pg_finfo_hnsw_halfvec_support},
{"hnsw_bit_support", hnsw_bit_support},
{"pg_finfo_hnsw_bit_support", pg_finfo_hnsw_bit_support},
{"hnsw_sparsevec_support", hnsw_sparsevec_support},
{"pg_finfo_hnsw_sparsevec_support", pg_finfo_hnsw_sparsevec_support},
{NULL, NULL},
};

/*
* Load the specified dynamic-link library file, and look for a function
Expand Down Expand Up @@ -146,6 +363,20 @@ load_external_function(const char *filename, const char *funcname,
}
}

/* PGVector Static Dispatch */
if (strcmp(filename, "$libdir/vector") == 0) {
if (!pgvector_loaded) {
_PG_init();
pgvector_loaded = true;
}

for (int i = 0; pgvector_func_map[i].name != NULL; i++) {
if (strcmp(funcname, pgvector_func_map[i].name) == 0) {
return pgvector_func_map[i].function;
}
}
}

char *fullname;
void *lib_handle;
void *retval;
Expand Down Expand Up @@ -196,6 +427,13 @@ load_file(const char *filename, bool restricted)
}
return;
}
if (strcmp(filename, "$libdir/vector") == 0) {
if (!pgvector_loaded) {
_PG_init();
pgvector_loaded = true;
}
return;
}

char *fullname;

Expand Down Expand Up @@ -231,6 +469,12 @@ lookup_external_function(void *filehandle, const char *funcname)
}
}

for (int i = 0; pgvector_func_map[i].name != NULL; i++) {
if (strcmp(funcname, pgvector_func_map[i].name) == 0) {
return pgvector_func_map[i].function;
}
}

return dlsym(filehandle, funcname);
}

Expand Down
3 changes: 3 additions & 0 deletions src/include/extensions/pglite_ext_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define PGlite_FUNCTION_INFO_V1(sym) \
extern Datum sym(FunctionCallInfo fcinfo); \
extern const Pg_finfo_record *pg_finfo_##sym(void)
Loading