Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
my $node;
my @queries = ();
my @expected;
my $limit = 20;
my $dim = 52;
my $max = 2**$dim;
 
sub test_recall
{
    my ($probes, $min, $operator) = @_;
    my $correct = 0;
    my $total = 0;
 
    my $explain = $node->safe_psql("postgres", qq(
        SET enable_seqscan = off;
        SET ivfflat.probes = $probes;
        EXPLAIN ANALYZE SELECT i FROM tst ORDER BY v $operator $queries[0] LIMIT $limit;
    ));
    like($explain, qr/Index Scan using idx on tst/);
 
    for my $i (0 .. $#queries)
    {
        my $actual = $node->safe_psql("postgres", qq(
            SET enable_seqscan = off;
            SET ivfflat.probes = $probes;
            SELECT i FROM tst ORDER BY v $operator $queries[$i] LIMIT $limit;
        ));
        my @actual_ids = split("\n", $actual);
 
        my @expected_ids = split("\n", $expected[$i]);
        my %expected_set = map { $_ => 1 } @expected_ids;
 
        foreach (@actual_ids)
        {
            if (exists($expected_set{$_}))
            {
                $correct++;
            }
        }
 
        $total += $limit;
    }
 
    cmp_ok($correct / $total, ">=", $min, $operator);
}
 
# Initialize node
$node = PostgreSQL::Test::Cluster->new('node');
$node->init;
$node->start;
 
# Create table
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v bit($dim));");
$node->safe_psql("postgres",
    "INSERT INTO tst SELECT i, (random() * $max)::bigint::bit($dim) FROM generate_series(1, 100000) i;"
);
 
# Generate queries
for (1 .. 20)
{
    my $r = int(rand() * $max);
    push(@queries, "${r}::bigint::bit($dim)");
}
 
# Check each index type
my @operators = ("<~>");
my @opclasses = ("bit_hamming_ops");
 
for my $i (0 .. $#operators)
{
    my $operator = $operators[$i];
    my $opclass = $opclasses[$i];
 
    # Get exact results
    @expected = ();
    foreach (@queries)
    {
        my $res = $node->safe_psql("postgres", qq(
            WITH top AS (
                SELECT v $operator $_ AS distance FROM tst ORDER BY distance LIMIT $limit
            )
            SELECT i FROM tst WHERE (v $operator $_) <= (SELECT MAX(distance) FROM top)
        ));
        push(@expected, $res);
    }
 
    # Build index serially
    $node->safe_psql("postgres", qq(
        SET max_parallel_maintenance_workers = 0;
        CREATE INDEX idx ON tst USING ivfflat (v $opclass);
    ));
 
    # Test approximate results
    test_recall(1, 0.08, $operator);
    test_recall(10, 0.50, $operator);
 
    # Test probes equals lists
    test_recall(100, 1.00, $operator);
 
    $node->safe_psql("postgres", "DROP INDEX idx;");
 
    # Build index in parallel
    my ($ret, $stdout, $stderr) = $node->psql("postgres", qq(
        SET client_min_messages = DEBUG;
        SET min_parallel_table_scan_size = 1;
        CREATE INDEX idx ON tst USING ivfflat (v $opclass);
    ));
    is($ret, 0, $stderr);
    like($stderr, qr/using \d+ parallel workers/);
 
    # Test approximate results
    test_recall(1, 0.08, $operator);
    test_recall(10, 0.50, $operator);
 
    # Test probes equals lists
    test_recall(100, 1.00, $operator);
 
    $node->safe_psql("postgres", "DROP INDEX idx;");
}
 
done_testing();