Skip to content

Parallel index builds for HNSW #409

Closed

Description

@ankane

Hi all, support for in-memory, parallel index builds is now available in the hnsw-fast-build branch 🎉

A few benchmarks from my local machine with the SIFT 1M dataset (128 dimensions):

code version processes build time
0.5.1 1 415 sec
master 1 309 sec
branch 2 184 sec
branch 4 107 sec
branch 8 83 sec

A few useful settings are:

SET max_parallel_maintenance_workers = 3; -- plus leader (default is 2)
SET maintenance_work_mem = '8GB';         -- memory to use
SET client_min_messages = DEBUG;          -- prints worker information

For a high number of workers, you may also need to increase max_parallel_workers (default is 8).

Please test it out (in a non-production environment) and share any feedback. Aiming for a release (0.5.2) at the end of January if all goes well.

Activity

  1. jkatz commented on Jan 16, 2024

    @jkatz
    Contributor

    @ankane Awesome!

    I'm running a series of tests, but I waned to share a very early result. Here is my test info:

    Dataset: 10MM 1,536-dim randomly generated normalized vectors
    Instance: r7gd.16xlarge (64 vCPU, 512GB RAM)
    Storage: NVMe
    Build parameters:

    • m: 16
    • ef_construction: 100
      PostgreSQL configuration of relevance:
    • shared_buffers: 128GB
    • maintenance_work_mem: 128GB
    • max_parallel_maintenance_workers: 63 (with leader, so this will be 64)
    • max_wal_size: 20GB
    • wal_compression: zstd

    Results:

    This looks really promising! There are a few more tests I plan to run:

    • "Regression" tests against ANN Benchmark to ensure recall is not skewed.
    • Running with different values of max_parallel_maintenance_workers to understand the performance profile
    • A few tests with different vector sizes. At least 128 and 1536, but I'd like to also try 384 and 768.

    Anyway, this looks awesome!

  2. jkatz commented on Jan 16, 2024

    @jkatz
    Contributor

    For completeness, the second test finished in 3h7m35s (11255652.916 ms). So for this run, hnsw-fast-build-branch showed a 7.39x speedup!

  3. ankane commented on Jan 17, 2024

    @ankane
    MemberAuthor

    Running into an issue with Docker if anyone has ideas.

    Docker has a default shared memory limit of 64 MB (shm_size). When trying to allocate more shared memory, CREATE INDEX fails with ERROR: could not resize shared memory segment. Ideally, it would fallback to a serial build, but I'm having trouble getting this to happen.

    It initially fails on the InitializeParallelDSM call. One way around this is to create a minimal DSA, then try to allocate from that (hnsw-fast-build-dsa branch). However, it fails with the same error on dsa_allocate_extended, even when DSA_ALLOC_NO_OOM is set.

    Script to repro

    CREATE EXTENSION vector;
    CREATE TABLE tst (i int4, v vector(3));
    INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 10000) i;
    SET client_min_messages = DEBUG;
    SET maintenance_work_mem = '128MB';
    SET min_parallel_table_scan_size = 1;
    CREATE INDEX idx ON tst USING hnsw (v vector_l2_ops);
  4. pashkinelfe commented on Jan 17, 2024

    @pashkinelfe
    Contributor

    I did testing on a set of OpenAI embeddings (900K embeddings, 1536 dimensions). The results are really impressive: for construction ef=100 I have 20 times speedup for 64 workers (including leader) compared to building in one process and 27 times speedup compared to version 5.1.0!
    image

    On a second plot the same results in log scale. Here I also added (inverse) linear build time in a dotted line which is a theoretical maximum of parallelizing with no concurrency overhead.

    image

    Testing was on c7g.metal with settings:

    shared_buffers = 50000MB
    maintenance_work_mem = 30000MB
    max_parallel_maintenance_workers = {0-63}
    wal_level=minimal
    max_wal_size = 10GB
    autovacuum = off
    full_page_writes = off
    fsync = off
    
    create unlogged table emb (id serial, vector vector(1536), primary key (id));
    alter table emb alter column vector set storage plain;
    CREATE INDEX emb_hnsw ON emb USING hnsw (vector vector_ip_ops) WITH (m = 16, ef_construction = {32,64,100})
    

    I can't wait when the branch is merged into the release. A killer feature indeed!

  5. jkatz commented on Jan 17, 2024

    @jkatz
    Contributor

    I've been running tests on my random 10MM 1536-dim dataset. It's taking some time (I didn't automate it well), but here's roughly where I'm at (same settings as #409 (comment)):

    Workers master (min) hnsw-fast-build (min) Speedup
    2 243
    4 130
    8 524 74 7.13
    16 279 46 6.11
    32 201 30 6.72
    64 188 25 7.39

    I think the win is pretty demonstrable, even if there's still room for improvement. I'm going to pivot to run the ANN Benchmarks on the 64 worker load to check for any recall regressions.

  6. hlinnaka commented on Jan 17, 2024

    @hlinnaka
    Contributor

    Cool, thanks for the testing @jkatz! I wonder what's the bottleneck that prevents it from scaling linearly as you add worker. Would it be possible to run perf record --call-graph=lbr -p <pid of one of the worker processes> while the test is running with >= 64 workers? Running it for a short duration like 10 s - 20 s should be enough.

  7. jkatz commented on Jan 17, 2024

    @jkatz
    Contributor

    @hlinnaka I can do that. I'll get a few: 64 and 96 workers.

  8. jkatz commented on Jan 18, 2024

    @jkatz
    Contributor

    @hlinnaka Actually, I can't do that on my current instance as I'm using a Graviton 3 (ARM) processor. I can set up an instance with an Intel processor in the interim.

    From the Graviton, here's the dwarf:

    +  100.00%     0.00%  postgres  vector.so          [.] HnswParallelBuildMain
    +  100.00%     0.00%  postgres  vector.so          [.] HnswParallelScanAndInsert
    +  100.00%     0.00%  postgres  vector.so          [.] table_index_build_scan (inlined)
    +   99.90%     0.00%  postgres  postgres           [.] 0x0000aaaacf177e97
    +   99.86%     0.00%  postgres  postgres           [.] ParallelWorkerMain
    +   99.85%     0.00%  postgres  postgres           [.] StartBackgroundWorker
    +   99.76%     0.00%  postgres  postgres           [.] 0x0000aaaacf47ac93
    +   99.72%     0.00%  postgres  postgres           [.] _start
    +   99.72%     0.00%  postgres  libc.so.6          [.] __libc_start_main
    +   99.72%     0.00%  postgres  libc.so.6          [.] 0x0000ffffb00873fb
    +   99.72%     0.00%  postgres  postgres           [.] main
    +   99.72%     0.00%  postgres  postgres           [.] PostmasterMain
    +   99.72%     0.00%  postgres  postgres           [.] 0x0000aaaacf47ccd7
    +   99.39%     0.00%  postgres  vector.so          [.] BuildCallback (inlined)
    +   99.36%     0.00%  postgres  vector.so          [.] InsertTuple (inlined)
    +   92.51%     0.02%  postgres  vector.so          [.] HnswInsertElement
    +   87.62%    17.40%  postgres  vector.so          [.] HnswSearchLayer
    +   66.14%     0.16%  postgres  postgres           [.] FunctionCall2Coll
    +   61.67%     0.00%  postgres  vector.so          [.] GetCandidateDistance (inlined)
    +   45.54%    41.36%  postgres  vector.so          [.] vector_negative_inner_product
    +   21.17%     0.00%  postgres  vector.so          [.] AddToVisited (inlined)
    +   21.15%     0.00%  postgres  vector.so          [.] offsethash_insert_hash (inlined)
    +   20.45%    18.82%  postgres  vector.so          [.] vector_negative_inner_product
    +   10.29%     0.10%  postgres  vector.so          [.] SelectNeighbors
    +    8.11%     3.11%  postgres  vector.so          [.] CheckElementCloser
    +    7.18%     0.00%  postgres  vector.so          [.] HnswGetDistance (inlined)
    +    5.80%     0.00%  postgres  vector.so          [.] WriteElementInMemory (inlined)
    +    5.77%     0.00%  postgres  vector.so          [.] HnswUpdateNeighborPagesInMemory (inlined)
    +    5.69%     5.68%  postgres  postgres           [.] pg_detoast_datum
    +    5.69%     0.00%  postgres  vector.so          [.] HnswUpdateConnection (inlined)
    +    5.33%     0.00%  postgres  vector.so          [.] offsethash_insert_hash_internal (inlined)
    +    5.31%     5.31%  postgres  vector.so          [.] offsethash_insert_hash_internal.isra.0
    

    And here's the fp:

    +  100.00%     0.00%  postgres  postgres           [.] _start
    +  100.00%     0.00%  postgres  libc.so.6          [.] __libc_start_main
    +  100.00%     0.00%  postgres  libc.so.6          [.] 0x0000ffffb00873fc
    +  100.00%     0.00%  postgres  postgres           [.] main
    +  100.00%     0.00%  postgres  postgres           [.] PostmasterMain
    +  100.00%     0.00%  postgres  postgres           [.] 0x0000aaaacf47ccd8
    +  100.00%     0.00%  postgres  postgres           [.] HandleStartupProcInterrupts
    +  100.00%     0.00%  postgres  postgres           [.] StartBackgroundWorker
    +  100.00%     0.00%  postgres  postgres           [.] ParallelWorkerMain
    +  100.00%     0.00%  postgres  vector.so          [.] HnswParallelBuildMain
    +  100.00%     0.00%  postgres  vector.so          [.] HnswParallelScanAndInsert
    +   99.94%     0.00%  postgres  postgres           [.] 0x0000aaaacf177e98
    +   99.50%     0.12%  postgres  vector.so          [.] BuildCallback.lto_priv.0
    +   94.71%     0.02%  postgres  vector.so          [.] HnswInsertElement
    +   90.51%    19.43%  postgres  vector.so          [.] HnswSearchLayer
    +   66.32%     0.15%  postgres  postgres           [.] FunctionCall2Coll
    +   66.17%    59.47%  postgres  vector.so          [.] vector_negative_inner_product
    +    7.75%     0.07%  postgres  vector.so          [.] SelectNeighbors
    +    6.59%     6.58%  postgres  postgres           [.] pg_detoast_datum
    +    6.19%     2.60%  postgres  vector.so          [.] CheckElementCloser
    +    4.75%     4.61%  postgres  vector.so          [.] offsethash_insert_hash_internal.isra.0
    
  9. jkatz commented on Jan 18, 2024

    @jkatz
    Contributor

    @hlinnaka Hm, turns out I can't get lbr either from the r7i. Does this give you enough info, or do you want me to try on a different machine?

  10. hlinnaka commented on Jan 18, 2024

    @hlinnaka
    Contributor

    Yeah that's good, thanks @jkatz.

    I don't see a smoking gun in the profile, it looks similar to what I'm seeing without parallelism. Doesn't seem to be lock contention.

  11. hlinnaka commented on Jan 18, 2024

    @hlinnaka
    Contributor

    Docker has a default shared memory limit of 64 MB (shm_size). When trying to allocate more shared memory, CREATE INDEX fails with ERROR: could not resize shared memory segment. Ideally, it would fallback to a serial build, but I'm having trouble getting this to happen.

    Yeah, that's unfortunate. AFAICS there is no equivalent of MCXT_ALLOC_NO_OOM for the shared memory allocation.

    The HNSW build isn't doing anything special, this is a general Postgres issue. Most parallel operations in Postgres need only a small amount of memory for coordinating the workers, but e.g. with parallel vacuum:

    postgres=# create table foo (i int) with (fillfactor=10);
    CREATE TABLE
    postgres=# insert into foo select g from generate_series(1, 10000000) g;
    INSERT 0 10000000
    postgres=# create index foo_idx_1 on foo(i);
    CREATE INDEX
    postgres=# create index foo_idx_2 on foo(i);
    CREATE INDEX
    postgres=# delete from foo where i <= 1000;
    DELETE 1000
    postgres=# vacuum (parallel 2) foo;
    ERROR:  could not resize shared memory segment "/PostgreSQL.3249931004" to 67128640 bytes: No space left on device
    
  12. pashkinelfe commented on Jan 18, 2024

    @pashkinelfe
    Contributor

    @hlinnaka The graphs for time spent in calls for building index with ef=100 in 61 processes (including leader) on 64-core Graviton3 (c7g.metal). Gathered by interrupting and recording gdb stacks (using https://github.com/jasonrohrer/wallClockProfiler).

    My attention came to AddToVisited. There should be some locks inside hash inserts.
    Worker:
    pgvector-60
    Leader:
    pgvector-60-leader

    Non-recompressed svg's (clickable call graph) pgvector-60-time-in-calls.zip

  13. hlinnaka commented on Jan 18, 2024

    @hlinnaka
    Contributor

    @hlinnaka The graphs for time spent in calls for building index with ef=100 in 61 processes (including leader) on 64-core Graviton3 (c7g.metal). Gathered by interrupting and recording gdb stacks (using https://github.com/jasonrohrer/wallClockProfiler).

    Cool, thanks! This profile also looks similar to what I'm seeing on a non-parallel build.

    My attention came to AddToVisited. There should be some locks inside hash inserts.

    The hash table is local to the search, so it doesn't need locking.

    AddToVisited shows up as a hot spot in a single-process build too. It's because of CPU cache misses coming from this line:

    		pointerhash_insert_hash(v.pointers, (uintptr_t) hc->element, hc->element->hash, found);
    

    The hc->element->hash access is very often a cache miss. That's pretty much unavoidable, I'm afraid. The HNSW algorithm is very heavy on random memory access, and when expanding the search through the graph, this line happens to be the first access of the each neighbor.

    We could avoid that access here by recalculating the hash, instead of reading it from hc->element->hash. But it doesn't help: you then suffer the same CPU cache the next time you follow the pointer, right after AddToVisited.

    I spent some time on this at #386. The first commit there helps a little, by reorganizing the code around AddToVisited so that CPU cache prefetching is more effective for those memory accesses.

  14. jkatz commented on Jan 18, 2024

    @jkatz
    Contributor

    I ran an ANN Benchmark run I did overnight. I'll likely add details over a series of comments.

    First and most importantly (IMV) is recall impact. Overall, it's negligible, and any swings over 1% were positive(!). For this test, I had two machines running at the commits mentioned in the table, with the following setup:

    • shared_buffers: 128GB
    • work_mem: 64MB
    • maintenance_work_mem: 128GB
    • max_parallel_maintenance_workers: 63 (with leader, so this will be 64)
    • max_wal_size: 20GB
    • wal_compression: zstd

    Here is the run below. (Note the sort is lexical, so you'll see the ef_search in odd orders; this is due to me hacking this up in Excel, I'll work to make it nicer).

    Test Run HEAD@1ff9ab51 hnsw-fast-build@d801a843 Delta
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=10) 0.84631 0.84776 0.17%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=120) 0.99109 0.99106 0.00%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=20) 0.92248 0.92409 0.17%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=200) 0.99516 0.99464 -0.05%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=40) 0.96565 0.96604 0.04%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=400) 0.99747 0.99739 -0.01%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=80) 0.98552 0.98537 -0.02%
    dbpedia-openai-1000k-angular PGVector(m=16, ef_construction=200, ef_search=800) 0.99865 0.99878 0.01%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=10) 0.89639 0.89038 -0.67%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=120) 0.99402 0.99418 0.02%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=20) 0.94972 0.94849 -0.13%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=200) 0.99634 0.9965 0.02%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=40) 0.97833 0.97827 -0.01%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=400) 0.99811 0.99806 -0.01%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=80) 0.991 0.9909 -0.01%
    dbpedia-openai-1000k-angular PGVector(m=24, ef_construction=200, ef_search=800) 0.9991 0.99908 0.00%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=10) 0.5021 0.5131 2.14%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=120) 0.9182 0.9211 0.31%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=20) 0.647 0.6505 0.54%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=200) 0.9569 0.9583 0.15%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=40) 0.7763 0.7821 0.74%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=400) 0.9845 0.9862 0.17%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=80) 0.8743 0.8827 0.95%
    gist-960-euclidean PGVector(m=16, ef_construction=200, ef_search=800) 0.9939 0.9953 0.14%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=10) 0.5831 0.5931 1.69%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=120) 0.9548 0.9582 0.35%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=20) 0.7247 0.7248 0.01%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=200) 0.9788 0.9793 0.05%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=40) 0.8442 0.8472 0.35%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=400) 0.9941 0.9938 -0.03%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=80) 0.9259 0.9301 0.45%
    gist-960-euclidean PGVector(m=24, ef_construction=200, ef_search=800) 0.9977 0.9976 -0.01%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=10) 0.52271 0.52689 0.79%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=120) 0.85966 0.86036 0.08%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=20) 0.6431 0.64734 0.65%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=200) 0.89737 0.89685 -0.06%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=40) 0.7426 0.74888 0.84%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=400) 0.93564 0.93459 -0.11%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=80) 0.82281 0.82495 0.26%
    glove-100-angular PGVector(m=16, ef_construction=200, ef_search=800) 0.96347 0.96248 -0.10%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=10) 0.60296 0.60303 0.01%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=120) 0.8985 0.89952 0.11%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=20) 0.7133 0.71362 0.04%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=200) 0.93058 0.93113 0.06%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=40) 0.80102 0.8012 0.02%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=400) 0.96285 0.96265 -0.02%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=80) 0.86887 0.87032 0.17%
    glove-100-angular PGVector(m=24, ef_construction=200, ef_search=800) 0.98321 0.98357 0.04%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=10) 0.80462 0.80483 0.03%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=120) 0.99452 0.99454 0.00%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=20) 0.89851 0.89988 0.15%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=200) 0.99836 0.99838 0.00%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=40) 0.95716 0.95871 0.16%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=400) 0.99977 0.99985 0.01%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=80) 0.98686 0.98726 0.04%
    glove-25-angular PGVector(m=16, ef_construction=200, ef_search=800) 0.99996 0.99997 0.00%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=10) 0.86404 0.86389 -0.02%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=120) 0.99769 0.99764 -0.01%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=20) 0.93721 0.93725 0.00%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=200) 0.99949 0.99951 0.00%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=40) 0.97698 0.97689 -0.01%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=400) 0.99995 0.99998 0.00%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=80) 0.99377 0.99393 0.02%
    glove-25-angular PGVector(m=24, ef_construction=200, ef_search=800) 1 1 0.00%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=10) 0.96449 0.96639 0.20%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=120) 0.99948 0.99963 0.02%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=20) 0.9885 0.98936 0.09%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=200) 0.99977 0.99981 0.00%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=40) 0.99706 0.99727 0.02%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=400) 0.99986 0.99989 0.00%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=80) 0.99908 0.99927 0.02%
    mnist-784-euclidean PGVector(m=16, ef_construction=200, ef_search=800) 0.99998 0.99999 0.00%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=10) 0.98002 0.97944 -0.06%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=120) 0.99981 0.99984 0.00%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=20) 0.99308 0.99295 -0.01%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=200) 0.99993 0.99996 0.00%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=40) 0.99813 0.99832 0.02%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=400) 0.99999 0.99998 0.00%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=80) 0.99955 0.99956 0.00%
    mnist-784-euclidean PGVector(m=24, ef_construction=200, ef_search=800) 0.99999 0.99998 0.00%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=10) 0.7686 0.77472 0.79%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=120) 0.99276 0.99365 0.09%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=20) 0.87765 0.88309 0.62%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=200) 0.99772 0.99797 0.03%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=40) 0.94837 0.95219 0.40%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=400) 0.99956 0.99971 0.02%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=80) 0.984 0.98536 0.14%
    sift-128-euclidean PGVector(m=16, ef_construction=200, ef_search=800) 0.99993 0.99996 0.00%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=10) 0.82679 0.83335 0.79%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=120) 0.99664 0.99686 0.02%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=20) 0.91699 0.92059 0.39%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=200) 0.99902 0.9992 0.02%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=40) 0.96964 0.9714 0.18%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=400) 0.99985 0.99989 0.00%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=80) 0.9916 0.99241 0.08%
    sift-128-euclidean PGVector(m=24, ef_construction=200, ef_search=800) 0.99998 1 0.00%
  15. ankane commented on Jan 19, 2024

    @ankane
    MemberAuthor

    Due to not wanting to break Docker on a patch release (users who have increased maintenance_work_mem will need to either increase shm_size or set max_parallel_maintenance_workers to 0 for index builds to work), I'm leaning towards making this 0.6.0 instead of 0.5.2.

    A few other items that could also be included:

    • change vector storage to EXTERNAL - external branch
    • (maybe) include vectors with zero norm in indexes for cosine distance - index-zero-norm branch
    • drop support for Postgres 11 (EOL)

    Another option is to place this behind SET hnsw.enable_parallel_build = on; (then default to on in 0.6.0), but think it's better for users to make it the default earlier and not introduce a new GUC variable.

  16. ankane commented on Jan 23, 2024

    @ankane
    MemberAuthor

    Hi all, this is now merged 🎉

    Huge thanks @hlinnaka for reviewing and the code contributions (especially around locking and documenting the build process) and @jkatz and @pashkinelfe for benchmarking, testing, and profiling.

    The current plan is to release 0.6.0 early next week.

  17. pashkinelfe commented on Jan 23, 2024

    @pashkinelfe
    Contributor

    @ankane don't you think of something like MAIN storage to use out-of-line (i.e. TOAST) storage only as a last resort? Unfortunately, it will try to compress vectors inline, but maybe this will not add much burden (I haven't checked yet).

  18. ankane commented on Jan 23, 2024

    @ankane
    MemberAuthor

    Vectors don't really compress well (unless they have duplicate elements), so I think it's best to skip compression and just TOAST when needed.

    (external still stores inline if less than TOAST_TUPLE_THRESHOLD)

  19. pashkinelfe commented on Jan 23, 2024

    @pashkinelfe
    Contributor

    @ankane yes, that's it. My main line of thinking is to avoid TOASTing rows with vectors if their size is between 2 and 8 Kb. The user could avoid this setting TOAST_TUPLE_THRESHOLD to 8Kb but this could not be made default for type vector. Maybe at best setting TOAST_TUPLE_THRESHOLD value could be mentioned in readme as a performance-tuning action.

  20. ankane commented on Jan 24, 2024

    @ankane
    MemberAuthor

    @pashkinelfe The easiest way to make sure it's stored inline is to set:

    ALTER TABLE items ALTER COLUMN embedding SET STORAGE PLAIN;

    It'd be good to benchmark the difference between that and EXTERNAL before adding to the readme.

  21. pashkinelfe commented on Jan 26, 2024

    @pashkinelfe
    Contributor

    I propose mentioning toast_tuple_target in documentation: #437

  22. ankane commented on Mar 12, 2024

    @ankane
    MemberAuthor

    Just fyi: found an issue with lock contention that was impacting performance for higher numbers of parallel workers (thanks @jkatz). Fixed in 3ea2ce8.

  23. hlinnaka commented on Mar 12, 2024

    @hlinnaka
    Contributor

    Just fyi: found an issue with lock contention that was impacting performance for higher numbers of parallel workers (thanks @jkatz). Fixed in 3ea2ce8.

    Can you give some more details on the issue?

  24. ankane commented on Mar 12, 2024

    @ankane
    MemberAuthor

    Trying to get an exclusive lock on entryLock can cause the process to wait for a bit when there are a lot of shared locks. This shows up in pg_stat_activity and uneven tuple counts between processes.

    Before

    DEBUG:  worker processed 169722 tuples
    DEBUG:  leader processed 137032 tuples
    DEBUG:  worker processed 163898 tuples
    DEBUG:  worker processed 81592 tuples
    DEBUG:  worker processed 83258 tuples
    DEBUG:  worker processed 76006 tuples
    DEBUG:  worker processed 166818 tuples
    DEBUG:  worker processed 121674 tuples
    

    After

    DEBUG:  worker processed 125230 tuples
    DEBUG:  worker processed 124586 tuples
    DEBUG:  worker processed 125300 tuples
    DEBUG:  worker processed 123516 tuples
    DEBUG:  worker processed 126308 tuples
    DEBUG:  worker processed 125440 tuples
    DEBUG:  leader processed 124502 tuples
    DEBUG:  worker processed 125118 tuples
    
  25. hlinnaka commented on Mar 12, 2024

    @hlinnaka
    Contributor

    So the problem was that LWLocks are not fair. If there's a constant stream of processes acquiring and releasing the lock in shared mode, one process that's waiting to acquire it in exclusive mode might never get it. By briefly acquiring another lock in exclusive mode, you force the shared-lockers to pause when there's an exclusive-locker waiting. I see.

  26. ankane commented on Mar 12, 2024

    @ankane
    MemberAuthor

    Yeah, exactly. We want entryLock to be write-preferring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions