nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

postgresqlPackages.vectorchord: init at 0.4.2

+264
+29
pkgs/servers/sql/postgresql/ext/vectorchord/0001-read-clang-flags-from-environment.diff
··· 1 + diff --git a/crates/simd/build.rs b/crates/simd/build.rs 2 + index 12ce198..aed5588 100644 3 + --- a/crates/simd/build.rs 4 + +++ b/crates/simd/build.rs 5 + @@ -17,17 +17,24 @@ use std::error::Error; 6 + 7 + fn main() -> Result<(), Box<dyn Error>> { 8 + println!("cargo::rerun-if-changed=cshim"); 9 + + println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS"); 10 + let target_arch = var("CARGO_CFG_TARGET_ARCH")?; 11 + match target_arch.as_str() { 12 + "aarch64" => { 13 + let mut build = cc::Build::new(); 14 + build.file("./cshim/aarch64.c"); 15 + + build.compiler("@clang@"); 16 + + // read env var set by rustPlatform.bindgenHook 17 + + build.try_flags_from_environment("BINDGEN_EXTRA_CLANG_ARGS").expect("the BINDGEN_EXTRA_CLANG_ARGS environment variable must be specified and UTF-8"); 18 + build.opt_level(3); 19 + build.compile("simd_cshim"); 20 + } 21 + "x86_64" => { 22 + let mut build = cc::Build::new(); 23 + build.file("./cshim/x86_64.c"); 24 + + build.compiler("@clang@"); 25 + + // read env var set by rustPlatform.bindgenHook 26 + + build.try_flags_from_environment("BINDGEN_EXTRA_CLANG_ARGS").expect("the BINDGEN_EXTRA_CLANG_ARGS environment variable must be specified and UTF-8"); 27 + build.opt_level(3); 28 + build.compile("simd_cshim"); 29 + }
+24
pkgs/servers/sql/postgresql/ext/vectorchord/0002-add-feature-flags.diff
··· 1 + diff --git a/crates/algorithm/src/lib.rs b/crates/algorithm/src/lib.rs 2 + index 853a280..f88acbf 100644 3 + --- a/crates/algorithm/src/lib.rs 4 + +++ b/crates/algorithm/src/lib.rs 5 + @@ -13,6 +13,7 @@ 6 + // Copyright (c) 2025 TensorChord Inc. 7 + 8 + #![feature(select_unpredictable)] 9 + +#![feature(let_chains)] 10 + #![allow(clippy::type_complexity)] 11 + 12 + mod build; 13 + diff --git a/src/lib.rs b/src/lib.rs 14 + index 654b4d1..2b11d03 100644 15 + --- a/src/lib.rs 16 + +++ b/src/lib.rs 17 + @@ -13,6 +13,7 @@ 18 + // Copyright (c) 2025 TensorChord Inc. 19 + 20 + #![allow(unsafe_code)] 21 + +#![feature(let_chains)] 22 + 23 + mod datatype; 24 + mod index;
+65
pkgs/servers/sql/postgresql/ext/vectorchord/0003-select_unpredictable-on-bool.diff
··· 1 + diff --git a/crates/algorithm/src/operator.rs b/crates/algorithm/src/operator.rs 2 + index 7de8d07..c496dcd 100644 3 + --- a/crates/algorithm/src/operator.rs 4 + +++ b/crates/algorithm/src/operator.rs 5 + @@ -672,7 +672,7 @@ impl Operator for Op<VectOwned<f32>, L2> { 6 + use std::iter::zip; 7 + let dims = vector.dims(); 8 + let t = zip(&code.1, centroid.slice()) 9 + - .map(|(&sign, &num)| std::hint::select_unpredictable(sign, num, -num)) 10 + + .map(|(&sign, &num)| sign.select_unpredictable(num, -num)) 11 + .sum::<f32>() 12 + / (dims as f32).sqrt(); 13 + let sum_of_x_2 = code.0.dis_u_2; 14 + @@ -763,7 +763,7 @@ impl Operator for Op<VectOwned<f32>, Dot> { 15 + use std::iter::zip; 16 + let dims = vector.dims(); 17 + let t = zip(&code.1, centroid.slice()) 18 + - .map(|(&sign, &num)| std::hint::select_unpredictable(sign, num, -num)) 19 + + .map(|(&sign, &num)| sign.select_unpredictable(num, -num)) 20 + .sum::<f32>() 21 + / (dims as f32).sqrt(); 22 + let sum_of_x_2 = code.0.dis_u_2; 23 + @@ -854,7 +854,7 @@ impl Operator for Op<VectOwned<f16>, L2> { 24 + use std::iter::zip; 25 + let dims = vector.dims(); 26 + let t = zip(&code.1, centroid.slice()) 27 + - .map(|(&sign, &num)| std::hint::select_unpredictable(sign, num, -num).to_f32()) 28 + + .map(|(&sign, &num)| sign.select_unpredictable(num, -num).to_f32()) 29 + .sum::<f32>() 30 + / (dims as f32).sqrt(); 31 + let sum_of_x_2 = code.0.dis_u_2; 32 + @@ -945,7 +945,7 @@ impl Operator for Op<VectOwned<f16>, Dot> { 33 + use std::iter::zip; 34 + let dims = vector.dims(); 35 + let t = zip(&code.1, centroid.slice()) 36 + - .map(|(&sign, &num)| std::hint::select_unpredictable(sign, num, -num).to_f32()) 37 + + .map(|(&sign, &num)| sign.select_unpredictable(num, -num).to_f32()) 38 + .sum::<f32>() 39 + / (dims as f32).sqrt(); 40 + let sum_of_x_2 = code.0.dis_u_2; 41 + diff --git a/crates/simd/src/rotate.rs b/crates/simd/src/rotate.rs 42 + index 7a211e5..0fcd955 100644 43 + --- a/crates/simd/src/rotate.rs 44 + +++ b/crates/simd/src/rotate.rs 45 + @@ -31,18 +31,17 @@ pub fn givens(lhs: &mut [f32], rhs: &mut [f32]) { 46 + pub mod flip { 47 + #[crate::multiversion("v4", "v3", "v2", "a2")] 48 + pub fn flip(bits: &[u64; 1024], result: &mut [f32]) { 49 + - use std::hint::select_unpredictable; 50 + let result: &mut [u32] = unsafe { std::mem::transmute(result) }; 51 + let (slice, remainder) = result.as_chunks_mut::<64>(); 52 + let n = slice.len(); 53 + assert!(n <= 1024); 54 + for i in 0..n { 55 + for j in 0..64 { 56 + - slice[i][j] ^= select_unpredictable((bits[i] & (1 << j)) != 0, 0x80000000, 0); 57 + + slice[i][j] ^= ((bits[i] & (1 << j)) != 0).select_unpredictable(0x80000000, 0); 58 + } 59 + } 60 + for j in 0..remainder.len() { 61 + - remainder[j] ^= select_unpredictable((bits[n] & (1 << j)) != 0, 0x80000000, 0); 62 + + remainder[j] ^= ((bits[n] & (1 << j)) != 0).select_unpredictable(0x80000000, 0); 63 + } 64 + } 65 + }
+146
pkgs/servers/sql/postgresql/ext/vectorchord/package.nix
··· 1 + { 2 + buildPgrxExtension, 3 + cargo-pgrx_0_14_1, 4 + clang, 5 + fetchFromGitHub, 6 + lib, 7 + nix-update-script, 8 + postgresql, 9 + postgresqlTestExtension, 10 + replaceVars, 11 + rust-jemalloc-sys, 12 + stdenv, 13 + }: 14 + let 15 + buildPgrxExtension' = buildPgrxExtension.override { 16 + # Upstream only works with a fixed version of cargo-pgrx for each release, 17 + # so we're pinning it here to avoid future incompatibility. 18 + cargo-pgrx = cargo-pgrx_0_14_1; 19 + }; 20 + 21 + # Follow upstream and use rust-jemalloc-sys on linux aarch64 and x86_64 22 + # Additionally, disable init exec TLS, since it causes issues with postgres. 23 + # https://github.com/tensorchord/VectorChord/blob/0.4.2/Cargo.toml#L43-L44 24 + useSystemJemalloc = 25 + stdenv.hostPlatform.isLinux && (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64); 26 + rust-jemalloc-sys' = ( 27 + rust-jemalloc-sys.override (old: { 28 + jemalloc = old.jemalloc.override { disableInitExecTls = true; }; 29 + }) 30 + ); 31 + in 32 + buildPgrxExtension' (finalAttrs: { 33 + inherit postgresql; 34 + 35 + pname = "vectorchord"; 36 + version = "0.4.2"; 37 + 38 + src = fetchFromGitHub { 39 + owner = "tensorchord"; 40 + repo = "vectorchord"; 41 + tag = finalAttrs.version; 42 + hash = "sha256-EdMuSNcWwCBsAY0e3d0WVug1KBWYWldvKStF6cf/uRs="; 43 + }; 44 + 45 + patches = [ 46 + # Tell the `simd` crate to use the flags from the rust bindgen hook 47 + (replaceVars ./0001-read-clang-flags-from-environment.diff { 48 + clang = lib.getExe clang; 49 + }) 50 + # Add feature flags needed for features not yet stabilised in rustc stable 51 + ./0002-add-feature-flags.diff 52 + # The select_predictable function has been moved from std::bool to std::hint before it has been stabilized. 53 + # This move isn't present in rustc 1.87, but upstream is using nightly so they have already updated their code. 54 + # This patch changes the code to use the function on std::bool instead. 55 + # See https://github.com/rust-lang/rust/pull/139726 56 + ./0003-select_unpredictable-on-bool.diff 57 + ]; 58 + 59 + buildInputs = lib.optionals (useSystemJemalloc) [ 60 + rust-jemalloc-sys' 61 + ]; 62 + 63 + useFetchCargoVendor = true; 64 + cargoHash = "sha256-8NwfsJn5dnvog3fexzLmO3v7/3+L7xtv+PHWfCCWoHY="; 65 + 66 + # Include upgrade scripts in the final package 67 + # https://github.com/tensorchord/VectorChord/blob/0.4.2/crates/make/src/main.rs#L224 68 + postInstall = '' 69 + cp sql/upgrade/* $out/share/postgresql/extension/ 70 + ''; 71 + 72 + env = { 73 + # Bypass rust nightly features not being available on rust stable 74 + RUSTC_BOOTSTRAP = 1; 75 + }; 76 + 77 + # This crate does not have the "pg_test" feature 78 + usePgTestCheckFeature = false; 79 + 80 + passthru = { 81 + updateScript = nix-update-script { }; 82 + 83 + tests.extension = postgresqlTestExtension { 84 + inherit (finalAttrs) finalPackage; 85 + withPackages = [ "pgvector" ]; # vectorchord depends on pgvector at runtime 86 + postgresqlExtraSettings = '' 87 + shared_preload_libraries = 'vchord' 88 + ''; 89 + 90 + sql = '' 91 + CREATE EXTENSION vchord CASCADE; 92 + 93 + CREATE TABLE items (id bigint PRIMARY KEY, embedding vector(3)); 94 + INSERT INTO items (id, embedding) VALUES 95 + (1, '[1,2,4]'), 96 + (2, '[1,2,5]'), 97 + (3, '[0,0,3]'), 98 + (4, '[0,0,2]'), 99 + (5, '[0,0,1]'); 100 + 101 + CREATE INDEX ON items USING vchordrq (embedding vector_l2_ops) WITH (options = $$ 102 + residual_quantization = true 103 + [build.internal] 104 + lists = [4096] 105 + spherical_centroids = false 106 + $$); 107 + 108 + SET vchordrq.probes = 1; 109 + ''; 110 + 111 + asserts = [ 112 + { 113 + query = "SELECT extversion FROM pg_extension WHERE extname = 'vchord'"; 114 + expected = "'${finalAttrs.version}'"; 115 + description = "Expected installed version to match the derivation's version"; 116 + } 117 + { 118 + query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 1"; 119 + expected = "1"; 120 + description = "Expected vector of row with ID=1 to have an euclidean distance from [1,2,3] of 1."; 121 + } 122 + { 123 + query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 2"; 124 + expected = "2"; 125 + description = "Expected vector of row with ID=2 to have an euclidean distance from [1,2,3] of 2."; 126 + } 127 + { 128 + query = "SELECT id FROM items ORDER BY embedding <-> '[2,3,7]' LIMIT 1"; 129 + expected = "2"; 130 + description = "Expected vector of row with ID=2 to be the closest to [2,3,7]."; 131 + } 132 + ]; 133 + }; 134 + }; 135 + 136 + meta = { 137 + changelog = "https://github.com/tensorchord/VectorChord/releases/tag/${finalAttrs.version}"; 138 + description = "Scalable, fast, and disk-friendly vector search in Postgres, the successor of pgvecto.rs"; 139 + homepage = "https://github.com/tensorchord/VectorChord"; 140 + license = lib.licenses.agpl3Only; # dual licensed with Elastic License v2 (ELv2) 141 + maintainers = with lib.maintainers; [ 142 + diogotcorreia 143 + ]; 144 + platforms = postgresql.meta.platforms; 145 + }; 146 + })