1# mostly copied from ./timescaledb.nix which was copied from ./postgresql.nix
2# as it seemed unapproriate to test additional extensions for postgresql there.
3
4{ system ? builtins.currentSystem
5, config ? { }
6, pkgs ? import ../.. { inherit system config; }
7}:
8
9with import ../lib/testing-python.nix { inherit system pkgs; };
10with pkgs.lib;
11
12let
13 postgresql-versions = import ../../pkgs/servers/sql/postgresql pkgs;
14 # Test cases from https://docs.pgvecto.rs/use-cases/hybrid-search.html
15 test-sql = pkgs.writeText "postgresql-test" ''
16 CREATE EXTENSION vectors;
17
18 CREATE TABLE items (
19 id bigserial PRIMARY KEY,
20 content text NOT NULL,
21 embedding vectors.vector(3) NOT NULL -- 3 dimensions
22 );
23
24 INSERT INTO items (content, embedding) VALUES
25 ('a fat cat sat on a mat and ate a fat rat', '[1, 2, 3]'),
26 ('a fat dog sat on a mat and ate a fat rat', '[4, 5, 6]'),
27 ('a thin cat sat on a mat and ate a thin rat', '[7, 8, 9]'),
28 ('a thin dog sat on a mat and ate a thin rat', '[10, 11, 12]');
29 '';
30 make-postgresql-test = postgresql-name: postgresql-package: makeTest {
31 name = postgresql-name;
32 meta = with pkgs.lib.maintainers; {
33 maintainers = [ diogotcorreia ];
34 };
35
36 nodes.machine = { ... }:
37 {
38 services.postgresql = {
39 enable = true;
40 package = postgresql-package;
41 extraPlugins = ps: with ps; [
42 pgvecto-rs
43 ];
44 settings.shared_preload_libraries = "vectors";
45 };
46 };
47
48 testScript = ''
49 def check_count(statement, lines):
50 return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
51 statement, lines
52 )
53
54
55 machine.start()
56 machine.wait_for_unit("postgresql")
57
58 with subtest("Postgresql with extension vectors is available just after unit start"):
59 machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'vectors' AND default_version = '${postgresql-package.pkgs.pgvecto-rs.version}';", 1))
60
61 machine.succeed("sudo -u postgres psql -f ${test-sql}")
62
63 machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))
64
65 machine.shutdown()
66 '';
67
68 };
69 applicablePostgresqlVersions = filterAttrs (_: value: versionAtLeast value.version "14") postgresql-versions;
70in
71mapAttrs'
72 (name: package: {
73 inherit name;
74 value = make-postgresql-test name package;
75 })
76 applicablePostgresqlVersions