pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: guzhoudiaoke (@guzhoudiaoke) <noreply+guzhoudiaoke@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #635: Whether we should lock element when UpdateNeighborsInMemory ?
Date: Fri, 02 Aug 2024 10:05:51 +0000
Message-ID: <gh-pgvector-pgvector-635@github.com> (raw)

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


view thread (11+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-635@github.com>
Permalink:  ../gh-pgvector-pgvector-635@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