[25.05] postgresqlPackages.vectorchord: init at 0.4.2 (#429374)

This commit is contained in:
dotlambda
2025-09-13 09:49:51 -07:00
committed by GitHub
6 changed files with 346 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
# New rust versions should first go to staging.
# Things to check after updating:
# 1. Rustc should produce rust binaries on x86_64-linux, aarch64-linux and x86_64-darwin:
# i.e. nix-shell -p fd or @GrahamcOfBorg build fd on github
# This testing can be also done by other volunteers as part of the pull
# request review, in case platforms cannot be covered.
# 2. The LLVM version used for building should match with rust upstream.
# Check the version number in the src/llvm-project git submodule in:
# https://github.com/rust-lang/rust/blob/<version-tag>/.gitmodules
{
stdenv,
lib,
newScope,
callPackage,
pkgsBuildTarget,
pkgsBuildBuild,
pkgsBuildHost,
pkgsHostTarget,
pkgsTargetTarget,
makeRustPlatform,
wrapRustcWith,
llvmPackages_20,
llvm_20,
wrapCCWith,
overrideCC,
fetchpatch,
}@args:
let
llvmSharedFor =
pkgSet:
pkgSet.llvmPackages_20.libllvm.override (
{
enableSharedLibraries = true;
}
// lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
# Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM
stdenv = pkgSet.stdenv.override {
allowedRequisites = null;
cc = pkgSet.pkgsBuildHost.llvmPackages_20.clangUseLLVM;
};
}
);
in
import ./default.nix
{
rustcVersion = "1.88.0";
rustcSha256 = "sha256-OpdURDSEiuPRk9HWvIPW8ky4XCYa2V+VX95H7GTPz74=";
llvmSharedForBuild = llvmSharedFor pkgsBuildBuild;
llvmSharedForHost = llvmSharedFor pkgsBuildHost;
llvmSharedForTarget = llvmSharedFor pkgsBuildTarget;
# For use at runtime
llvmShared = llvmSharedFor pkgsHostTarget;
# Expose llvmPackages used for rustc from rustc via passthru for LTO in Firefox
llvmPackages =
if (stdenv.targetPlatform.useLLVM or false) then
callPackage (
{
pkgs,
bootBintoolsNoLibc ? if stdenv.targetPlatform.linker == "lld" then null else pkgs.bintoolsNoLibc,
bootBintools ? if stdenv.targetPlatform.linker == "lld" then null else pkgs.bintools,
}:
let
llvmPackages = llvmPackages_20;
setStdenv =
pkg:
pkg.override {
stdenv = stdenv.override {
allowedRequisites = null;
cc = pkgsBuildHost.llvmPackages_20.clangUseLLVM;
};
};
in
rec {
inherit (llvmPackages) bintools;
libunwind = setStdenv llvmPackages.libunwind;
llvm = setStdenv llvmPackages.llvm;
libcxx = llvmPackages.libcxx.override {
stdenv = stdenv.override {
allowedRequisites = null;
cc = pkgsBuildHost.llvmPackages_20.clangNoLibcxx;
hostPlatform = stdenv.hostPlatform // {
useLLVM = !stdenv.hostPlatform.isDarwin;
};
};
inherit libunwind;
};
clangUseLLVM = llvmPackages.clangUseLLVM.override { inherit libcxx; };
stdenv = overrideCC args.stdenv clangUseLLVM;
}
) { }
else
llvmPackages_20;
# Note: the version MUST be the same version that we are building. Upstream
# ensures that each released compiler can compile itself:
# https://github.com/NixOS/nixpkgs/pull/351028#issuecomment-2438244363
bootstrapVersion = "1.88.0";
# fetch hashes by running `print-hashes.sh ${bootstrapVersion}`
bootstrapHashes = {
i686-unknown-linux-gnu = "987738444da172dc2d8c9ab93c6178f0000ced44a3089013839e5916755c4844";
x86_64-unknown-linux-gnu = "ad6f0cc845e7fcca17fd451bafd2c04a7bbcb543f8f3ef5bc412fd1fef99ef7b";
x86_64-unknown-linux-musl = "8bea28e71582d1bb29031ea2783a6cf2be626fdbcec24b5bca75c85b9b3ca92d";
arm-unknown-linux-gnueabihf = "cd269f3d53c286c0d0b0014947a4023d86698eac96b456ce746260ef21eec6af";
armv7-unknown-linux-gnueabihf = "718b8110c8f8ea40282b4a0e2efa09c4a91472eec45739f3b37ecc03f2b53954";
aarch64-unknown-linux-gnu = "dbc75abc31d142eacf15e60d0e51c4f291539974221d217b80786756b0ce1d6b";
aarch64-unknown-linux-musl = "9ccb8f16656d2d4e412553ebaf13489198b915519873752dcebb886de50063c6";
x86_64-apple-darwin = "b36b0bfac17e0a1f6cc06b9fdc4e2131ad578b4122a67792236b58650ae4c5c8";
aarch64-apple-darwin = "dee921b9a41b1c3fbb088ad31dcca3b232de2cb89c268db75f40912eeaa474db";
powerpc64-unknown-linux-gnu = "b56e903c6e4d661b6025d45b2675c31d513db207dbd85929c1a25473129275e3";
powerpc64le-unknown-linux-gnu = "e1f16b2885237695f3cce7fc2f0128a938fc07462b076cb61bd2f06e5f8baf38";
riscv64gc-unknown-linux-gnu = "6a72741671555fad7ffaceeaa32510c877438087ae71901ccf4a2b03a76c8439";
s390x-unknown-linux-gnu = "498ec8be66b2c6d8bc77dd06e226d3cc7448bc508ebb9f6d7650db79350d0cb7";
loongarch64-unknown-linux-gnu = "d4cb16ce9e2f04a7c44efe0abe5fc6cf2b9084f349fac042070882300719cbde";
loongarch64-unknown-linux-musl = "b9c0c6ca12312dbf8ab80571816fc68b615628ae4fdd0f204c11b71264550b87";
x86_64-unknown-freebsd = "961de5d723b034c1308d2b4a4d710fe006fb87bdbf914d045c01a5df87a0b332";
};
selectRustPackage = pkgs: pkgs.rust_1_88;
}
(
builtins.removeAttrs args [
"llvmPackages_20"
"llvm_20"
"wrapCCWith"
"overrideCC"
"pkgsHostTarget"
"fetchpatch"
]
)

View File

@@ -75,4 +75,9 @@ in
cargoHash = "sha256-pnMxWWfvr1/AEp8DvG4awig8zjdHizJHoZ5RJA8CL08=";
};
cargo-pgrx_0_14_1 = generic {
version = "0.14.1";
hash = "sha256-oMToAhKkRiCyC8JYS0gmo/XX3QVcVtF5mUV0aQjd+p8=";
cargoHash = "sha256-RawGAQGtG2QVDCMbwjmUEaH6rDeRiBvvJsGCY8wySw0=";
};
}

View File

@@ -0,0 +1,29 @@
diff --git a/crates/simd/build.rs b/crates/simd/build.rs
index 12ce198..aed5588 100644
--- a/crates/simd/build.rs
+++ b/crates/simd/build.rs
@@ -17,17 +17,24 @@ use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo::rerun-if-changed=cshim");
+ println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS");
let target_arch = var("CARGO_CFG_TARGET_ARCH")?;
match target_arch.as_str() {
"aarch64" => {
let mut build = cc::Build::new();
build.file("./cshim/aarch64.c");
+ build.compiler("@clang@");
+ // read env var set by rustPlatform.bindgenHook
+ build.try_flags_from_environment("BINDGEN_EXTRA_CLANG_ARGS").expect("the BINDGEN_EXTRA_CLANG_ARGS environment variable must be specified and UTF-8");
build.opt_level(3);
build.compile("simd_cshim");
}
"x86_64" => {
let mut build = cc::Build::new();
build.file("./cshim/x86_64.c");
+ build.compiler("@clang@");
+ // read env var set by rustPlatform.bindgenHook
+ build.try_flags_from_environment("BINDGEN_EXTRA_CLANG_ARGS").expect("the BINDGEN_EXTRA_CLANG_ARGS environment variable must be specified and UTF-8");
build.opt_level(3);
build.compile("simd_cshim");
}

View File

@@ -0,0 +1,24 @@
diff --git a/crates/algorithm/src/lib.rs b/crates/algorithm/src/lib.rs
index 853a280..f88acbf 100644
--- a/crates/algorithm/src/lib.rs
+++ b/crates/algorithm/src/lib.rs
@@ -13,6 +13,7 @@
// Copyright (c) 2025 TensorChord Inc.
#![feature(select_unpredictable)]
+#![feature(let_chains)]
#![allow(clippy::type_complexity)]
mod build;
diff --git a/src/lib.rs b/src/lib.rs
index 654b4d1..2b11d03 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,6 +13,7 @@
// Copyright (c) 2025 TensorChord Inc.
#![allow(unsafe_code)]
+#![feature(let_chains)]
mod datatype;
mod index;

View File

@@ -0,0 +1,143 @@
{
buildPgrxExtension,
cargo-pgrx_0_14_1,
clang,
fetchFromGitHub,
lib,
nix-update-script,
postgresql,
postgresqlTestExtension,
replaceVars,
rust-jemalloc-sys,
rustPackages_1_88,
stdenv,
}:
let
buildPgrxExtension' = buildPgrxExtension.override {
# Upstream only works with a fixed version of cargo-pgrx for each release,
# so we're pinning it here to avoid future incompatibility.
cargo-pgrx = cargo-pgrx_0_14_1;
inherit (rustPackages_1_88) rustPlatform;
};
# Follow upstream and use rust-jemalloc-sys on linux aarch64 and x86_64
# Additionally, disable init exec TLS, since it causes issues with postgres.
# https://github.com/tensorchord/VectorChord/blob/0.4.2/Cargo.toml#L43-L44
useSystemJemalloc =
stdenv.hostPlatform.isLinux && (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64);
rust-jemalloc-sys' = (
rust-jemalloc-sys.override (old: {
jemalloc = old.jemalloc.override { disableInitExecTls = true; };
})
);
in
buildPgrxExtension' (finalAttrs: {
inherit postgresql;
pname = "vectorchord";
version = "0.4.2";
src = fetchFromGitHub {
owner = "tensorchord";
repo = "vectorchord";
tag = finalAttrs.version;
hash = "sha256-EdMuSNcWwCBsAY0e3d0WVug1KBWYWldvKStF6cf/uRs=";
};
patches = [
# Tell the `simd` crate to use the flags from the rust bindgen hook
(replaceVars ./0001-read-clang-flags-from-environment.diff {
clang = lib.getExe clang;
})
# Add feature flags needed for features not yet stabilised in rustc stable
./0002-add-feature-flags.diff
];
buildInputs = lib.optionals (useSystemJemalloc) [
rust-jemalloc-sys'
];
useFetchCargoVendor = true;
cargoHash = "sha256-8NwfsJn5dnvog3fexzLmO3v7/3+L7xtv+PHWfCCWoHY=";
# Include upgrade scripts in the final package
# https://github.com/tensorchord/VectorChord/blob/0.4.2/crates/make/src/main.rs#L224
postInstall = ''
cp sql/upgrade/* $out/share/postgresql/extension/
'';
env = {
# Bypass rust nightly features not being available on rust stable
RUSTC_BOOTSTRAP = 1;
};
# This crate does not have the "pg_test" feature
usePgTestCheckFeature = false;
passthru = {
updateScript = nix-update-script { };
tests.extension = postgresqlTestExtension {
inherit (finalAttrs) finalPackage;
withPackages = [ "pgvector" ]; # vectorchord depends on pgvector at runtime
postgresqlExtraSettings = ''
shared_preload_libraries = 'vchord'
'';
sql = ''
CREATE EXTENSION vchord CASCADE;
CREATE TABLE items (id bigint PRIMARY KEY, embedding vector(3));
INSERT INTO items (id, embedding) VALUES
(1, '[1,2,4]'),
(2, '[1,2,5]'),
(3, '[0,0,3]'),
(4, '[0,0,2]'),
(5, '[0,0,1]');
CREATE INDEX ON items USING vchordrq (embedding vector_l2_ops) WITH (options = $$
residual_quantization = true
[build.internal]
lists = [4096]
spherical_centroids = false
$$);
SET vchordrq.probes = 1;
'';
asserts = [
{
query = "SELECT extversion FROM pg_extension WHERE extname = 'vchord'";
expected = "'${finalAttrs.version}'";
description = "Expected installed version to match the derivation's version";
}
{
query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 1";
expected = "1";
description = "Expected vector of row with ID=1 to have an euclidean distance from [1,2,3] of 1.";
}
{
query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 2";
expected = "2";
description = "Expected vector of row with ID=2 to have an euclidean distance from [1,2,3] of 2.";
}
{
query = "SELECT id FROM items ORDER BY embedding <-> '[2,3,7]' LIMIT 1";
expected = "2";
description = "Expected vector of row with ID=2 to be the closest to [2,3,7].";
}
];
};
};
meta = {
changelog = "https://github.com/tensorchord/VectorChord/releases/tag/${finalAttrs.version}";
description = "Scalable, fast, and disk-friendly vector search in Postgres, the successor of pgvecto.rs";
homepage = "https://github.com/tensorchord/VectorChord";
license = lib.licenses.agpl3Only; # dual licensed with Elastic License v2 (ELv2)
maintainers = with lib.maintainers; [
diogotcorreia
];
platforms = postgresql.meta.platforms;
};
})

View File

@@ -6046,6 +6046,9 @@ with pkgs;
rust_1_86 = callPackage ../development/compilers/rust/1_86.nix {
llvm_19 = llvmPackages_19.libllvm;
};
rust_1_88 = callPackage ../development/compilers/rust/1_88.nix {
llvm_20 = llvmPackages_20.libllvm;
};
rust = rust_1_86;
mrustc = callPackage ../development/compilers/mrustc { };
@@ -6055,6 +6058,7 @@ with pkgs;
};
rustPackages_1_86 = rust_1_86.packages.stable;
rustPackages_1_88 = rust_1_88.packages.stable;
rustPackages = rustPackages_1_86;
inherit (rustPackages)
@@ -6100,6 +6104,7 @@ with pkgs;
cargo-pgrx_0_12_0_alpha_1
cargo-pgrx_0_12_5
cargo-pgrx_0_12_6
cargo-pgrx_0_14_1
;
cargo-pgrx = cargo-pgrx_0_12_6;