pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
11+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-02 10:05 "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
0 siblings, 0 replies; 11+ messages in thread
From: guzhoudiaoke (@guzhoudiaoke) @ 2024-08-02 10:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The following crash was triggered when we created the hnsw index using pgvector in GPDB:
```c++
(gdb) bt
#0 0x0000000000b7e743 in pg_atomic_read_u32_impl (ptr=0x74) at ../../../../src/include/port/atomics/generic.h:48
#1 pg_atomic_read_u32 (ptr=0x74) at ../../../../src/include/port/atomics.h:247
#2 LWLockAttemptLock (mode=LW_EXCLUSIVE, lock=0x70) at lwlock.c:776
#3 LWLockAcquire () at lwlock.c:1213
#4 0x00007ff6a749a842 in UpdateNeighborsInMemory (m=100, e=<optimized out>, collation=0, procinfo=0x2d43630,
base=0x7ff6a74b7380 <Address 0x7ff6a74b7380 out of bounds>) at src/hnswbuild.c:397
#5 UpdateGraphInMemory (efConstruction=<optimized out>, buildstate=0x7ffc4d7f34a0, entryPoint=0x7ff6a7519fc8, m=100, element=<optimized out>, collation=0,
procinfo=0x2d43630) at src/hnswbuild.c:421
#6 InsertTupleInMemory (element=<optimized out>, buildstate=0x7ffc4d7f34a0) at src/hnswbuild.c:471
#7 InsertTuple (buildstate=<optimized out>, heaptid=<optimized out>, isnull=<optimized out>, values=<optimized out>, index=<optimized out>)
at src/hnswbuild.c:573
#8 BuildCallback () at src/hnswbuild.c:606
#9 0x00000000007c4abd in heapam_index_build_range_scan () at heapam_handler.c:1709
#10 0x00007ff6a749adbe in table_index_build_scan (scan=<optimized out>, callback_state=0x7ffc4d7f34a0, callback=0x7ff6a749a1b0 <BuildCallback>,
progress=false, allow_sync=true, index_info=0x2cfb138, index_rel=0x7ff6a6ff1e90, table_rel=0x7ff6a6ff8e20) at ../../src/include/access/tableam.h:1647
#11 HnswParallelScanAndInsert (heapRel=heapRel@entry=0x7ff6a6ff8e20, indexRel=indexRel@entry=0x7ff6a6ff1e90, hnswshared=hnswshared@entry=0x7ff6ab1b7380,
hnswarea=0x7ff6a74b7380 <Address 0x7ff6a74b7380 out of bounds>, progress=progress@entry=false) at src/hnswbuild.c:805
#12 0x00007ff6a749b660 in HnswParallelBuildMain (seg=<optimized out>, toc=0x7ff6a74b7000) at src/hnswbuild.c:870
#13 0x00000000008012c0 in ParallelWorkerMain () at parallel.c:1559
```
The crash is caused by the `neighborElement` is NULL in f4 UpdateNeighborsInMemory.
But when we add some log when we find `neighborElement` is NULL and regain the `neighborElement`, it's not NULL, and it may change.
We tried to understand the code of UpdateNeighborsInMemory, we think that we should require e->lock when we do `UpdateNeighborsInMemory`:
```c
/*
* Update neighbors
*/
static void
UpdateNeighborsInMemory(char *base, FmgrInfo *procinfo, Oid collation, HnswElement e, int m)
{
LWLockAcquire(&e->lock, LW_SHARED);
for (int lc = e->level; lc >= 0; lc--)
{
int lm = HnswGetLayerM(m, lc);
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e, lc);
for (int i = 0; i < neighbors->length; i++)
{
HnswCandidate *hc = &neighbors->items[i];
HnswElement neighborElement = HnswPtrAccess(base, hc->element);
/* Keep scan-build happy on Mac x86-64 */
Assert(neighborElement);
/* Use element for lock instead of hc since hc can be replaced */
LWLockAcquire(&neighborElement->lock, LW_EXCLUSIVE);
HnswUpdateConnection(base, e, hc, lm, lc, NULL, NULL, procinfo, collation);
LWLockRelease(&neighborElement->lock);
}
}
LWLockRelease(&e->lock);
}
```
because the `HnswElement e` has been added to Memory and may be neighbors of other elements, and thay may UpdateConnection of e during we `UpdateNeighborsInMemory` e.
But we are not know much about the code, so initiate this discussion.
---
Reproduction method:
1. create a table and insert some data
2. set max_parallel_maintenance_workers to 20;
3. CREATE INDEX hnsw_cosine_qa_data_bge_new ON qa_data_bge USING hnsw (embedding vector_cosine_ops) WITH (m = 100, ef_construction = 1000);
4. Ctrl-C stop `CREATE INDEX` after 5s if not crash, goto 3
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-02 21:08 ` "ankane (@ankane)" <noreply+ankane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: ankane (@ankane) @ 2024-08-02 21:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @guzhoudiaoke, thanks for reporting. I'm having trouble reproducing the crash, but it does seem possible depending on how the compiler decides to order the instructions (so I think additional locking is needed). A few questions:
1. How often are you seeing the crash?
2. What compiler (including the version) are you using to build the extension?
3. What flags are used to build `hnswbuild.c` and `vector.so` (from the `make` output)?
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-02 21:43 ` "ankane (@ankane)" <noreply+ankane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: ankane (@ankane) @ 2024-08-02 21:43 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Also, does the [hnsw-build-locking branch](https://github.com/pgvector/pgvector/compare/hnsw-build-locking) fix it for you?
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-04 09:40 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: guzhoudiaoke (@guzhoudiaoke) @ 2024-08-04 09:40 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Hi @guzhoudiaoke, thanks for reporting. I'm having trouble reproducing the crash, but it does seem possible depending on how the compiler decides to order the instructions (so I think additional locking is needed). A few questions:
>
> 1. How often are you seeing the crash?
In fact, the trigger probability is not high, we meet once, then some reproduction methods were tried.
We found that set `max_parallel_maintenance_workers ` to a bigger value such as 20 can increase recurrence probability.
And also we found it often triggered at the beginning of hnsw index creation, so we reproduce the problem by constantly interrupting and restart to create the index (use Ctrl-C to stop and restart create index).
> 2. What compiler (including the version) are you using to build the extension?
```
gcc (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
> 3. What flags are used to build `hnswbuild.c` and `vector.so` (from the `make` output)?
```
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-unused-but-set-variable -Werror=implicit-fallthrough=3 -Wno-format-truncation -Wno-stringop-truncation -g -O0 -g2 -Werror=uninitialized -Werror=implicit-function-declaration -march=haswell -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -Wno-error=maybe-uninitialized -fPIC -I. -I. -I../../deps/root/include -I../../src/include -D_GNU_SOURCE -I/usr/include/libxml2 -c -o src/hnswbuild.o src/hnswbuild.c -MMD -MP -MF .deps/hnswbuild.Po
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-unused-but-set-variable -Werror=implicit-fallthrough=3 -Wno-format-truncation -Wno-stringop-truncation -g -O0 -g2 -Werror=uninitialized -Werror=implicit-function-declaration -march=haswell -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -Wno-error=maybe-uninitialized -fPIC -shared -o vector.so src/bitutils.o src/bitvec.o src/halfutils.o src/halfvec.o src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/sparsevec.o src/vector.o -L../../src/port -L../../src/common -L../../deps/root/lib -Wl,--as-needed -lxerces-c -Wl,-rpath,'/data/liuruyi/matrixdb/lib',--enable-new-dtags
```
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-04 09:58 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: guzhoudiaoke (@guzhoudiaoke) @ 2024-08-04 09:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Also, does the [hnsw-build-locking branch](https://github.com/pgvector/pgvector/compare/hnsw-build-locking) fix it for you?
Thanks for the fix very much.
Previously we were able to reproduce it by interrupting and restarting within 5 times with max_parallel_maintenance_workers = 20. I tried to use this branch to interrupt and restart 30+ times, but it did not reproduce.
So I think this branch can fix it for me.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-04 18:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: ankane (@ankane) @ 2024-08-04 18:27 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-04 18:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: ankane (@ankane) @ 2024-08-04 18:29 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Great, thanks @guzhoudiaoke! Merged in the commit above.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-05 01:30 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: guzhoudiaoke (@guzhoudiaoke) @ 2024-08-05 01:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Great, thanks @guzhoudiaoke! Merged in the commit above.
Thanks for the quick fix.
One small question, the fix palloc and copy the neighbors without pfree, is the MemoryContext it using offen reset or destroy?
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-05 01:58 ` "ankane (@ankane)" <noreply+ankane@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: ankane (@ankane) @ 2024-08-05 01:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Yes, it's reset every tuple in `BuildCallback`.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2024-08-05 02:19 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: guzhoudiaoke (@guzhoudiaoke) @ 2024-08-05 02:19 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Yes, it's reset every tuple in `BuildCallback`.
Got it and thanks again.
^ permalink raw [nested|flat] 11+ messages in thread
* Re: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
@ 2025-04-09 02:24 ` "xiaokai-wang (@xiaokai-wang)" <noreply+xiaokai-wang@github.com>
9 siblings, 0 replies; 11+ messages in thread
From: xiaokai-wang (@xiaokai-wang) @ 2025-04-09 02:24 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @guzhoudiaoke could you please let me know which versions of GPDB and pgvector you are using? I'm encountering a compatibility issue while using pgvector(#817 ). Thank you!
^ permalink raw [nested|flat] 11+ messages in thread
end of thread, other threads:[~2025-04-09 02:24 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-02 10:05 [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ? "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
2024-08-02 21:08 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-02 21:43 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-04 09:40 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
2024-08-04 09:58 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
2024-08-04 18:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-04 18:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-05 01:30 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
2024-08-05 01:58 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-05 02:19 ` "guzhoudiaoke (@guzhoudiaoke)" <noreply+guzhoudiaoke@github.com>
2025-04-09 02:24 ` "xiaokai-wang (@xiaokai-wang)" <noreply+xiaokai-wang@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox