pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
12+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-25 08:10 "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
0 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-01-25 08:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
It occurs to me that it's silly that we allocate an area based on `maintenance_work_mem`, even if the table is small. For example, if maintenance_work_mem is set to 10 GB, and the table has 1000 rows, clearly we don't need to allocate 10 GB.
There's some precedence for this in parallel VACUUM. It allocates an array to hold TIDs of dead tuples based on `maintenance_work_mem`, but it uses an upper limit on the worst case assumption that every page is full of tiny tuples, all deleted. See `dead_items_max_items`
It'd be nice t do something similar in HNSW build.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-25 08:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-01-25 08:28 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Makes sense to me. <strike>I think the max memory needed would be `total memory needed for a HNSW_MAX_DIM element * (relation pages + TOAST pages)`.</strike>
Edit: Nevermind, needs to be the opposite (max number of `dimension` vectors that could be present in those pages)
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-25 10:50 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-01-25 10:50 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
If we know the number of rows in the table, we can calculate the memory needed by # of rows * sizeof(vector with N dimensions). The space needed for neighbor lists depend on the level of each node, so there's some randomness there, but you can estimate it pretty accurately.
So the key problem is to determine the number of rows in the table. Vacuum uses # of page * MaxHeapTuplesPerPage for that. That's good for heapam, but if you're using a table AM extension, it might have different limits. As first cut, maybe check if the table is using the heapam and use that limit if so, and a maximum sized allocation for custom table AMs.
(relation pages + toast pages) * BLCKSZ / (size of vector with N dimensions) would indeed be a good estimate for anything that uses Postgres pages.
It isn't the end of the world if we slightly underestimate the memory needed, and switch to the on-disk build at the very end of the build. It's slower, but if we only do that for the last 1% of the table for example, it's not too bad.
Another idea is to start with a small allocation, maybe based on reltuples estimate, and reallocate a larger area if it fills up. That's a lot more complicated though.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-26 03:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-01-26 03:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Yeah, the initial idea sounds simpler. Do you want to take this, or should I?
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-26 16:56 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-01-26 16:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hmm, assuming MaxHeapTuplesPerPage gives a very high upper bound. Vectors are large, so in practice you can only fit a few of them on each page, but MaxHeapTuplesPerPage is 291. So assuming MaxHeapTuplesPerPage doesn't seem that useful.
`(relation pages + toast pages) * BLCKSZ / (size of vector with N dimensions)` can be badly wrong in the other direction, if the value is toast-compressed effectively.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-26 16:56 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-01-26 16:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Yeah, the initial idea sounds simpler. Do you want to take this, or should I?
I don't have any great ideas on this at the moment
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-26 17:02 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-01-26 17:02 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
BTW, did you consider using the `dsa.c` facility for the shared memory area? It's a pretty good fit for what we'd need, it can expand the shared memory area on the fly by managing a set of segments behind the scenes. And it has a DSA_ALLOC_NO_OOM flag, so back out gracefully if you run out of shared memory.
I suspect it's too slow for our purposes. You have to call dsa_get_address() to convert the relative pointer to a real pointer, and it has more overhead than the relptrs we use now. But maybe it's OK after all? I think the biggest problem in expanding the search is the cache misses, so that function call overhead might be hidden by the cache miss that you get anyway.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-01-26 21:53 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-01-26 21:53 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
It should be possible to check the column storage with `TupleDescAttr(tupdesc, attnum)->attstorage`. Existing vectors could have different storages, but it's probably not a common case.
I tried using DSA in https://github.com/pgvector/pgvector/issues/409#issuecomment-1894993783 ([hnsw-fast-build-dsa branch](https://github.com/pgvector/pgvector/compare/hnsw-fast-build...hnsw-fast-build-dsa)), but `DSA_ALLOC_NO_OOM` still throws an error when running out of shared memory (at least in Docker).
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-02-19 11:53 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: hlinnaka (@hlinnaka) @ 2024-02-19 11:53 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> I tried using DSA in https://github.com/pgvector/pgvector/issues/409#issuecomment-1894993783 ([hnsw-fast-build-dsa branch](https://github.com/pgvector/pgvector/compare/hnsw-fast-build...hnsw-fast-build-dsa)), but DSA_ALLOC_NO_OOM still throws an error when running out of shared memory (at least in Docker).
Huh, yeah, that's a bug. I started a thread on that on pgsql-hackers: https://www.postgresql.org/message-id/5efa4a5e-2b8b-42dd-80ed-f920718cf5c0%40iki.fi. I'll try to fix that.
Even if DSA_ALLOC_NO_OOM doesn't work, it would be more friendly to the rest of the system to allocate the memory as you go, instead of one giant allocation. Creating a large DSM segment which is largely unused, can blow away the OS caches.
(We ran into this with Neon, where we start with a small compute VM with little RAM, and hot-plug memory to scale it up as needed. When pgvector makes one large allocation, it fails because /dev/shm is too small. We could scale it up, but if the allocation has already failed, it's too late. We'll find some neon-specific workaround to this, but it seems that growing the allocation slowly would be better for everyone.)
So I resurrected the DSA approach, on top of current 'master': https://github.com/pgvector/pgvector/compare/master...hlinnaka:pgvector:use-dsa?expand=1. In my quick testing, the get_dsa_address() calls do show up in profile, using up to 4% of CPU time. We don't need all the bookkeeping of freed pages and segment in DSA code, so we could build something faster on top of DSA, or directly on top of DSM.
Before I spend more time polishing that, what do you think of switching to DSA in general, assuming the overhead is tolerable and the upstream bug with `DSA_ALLOC_NO_OOM` is fixed?
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-02-20 22:49 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-02-20 22:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks for working on getting that fixed.
I'm a bit hesitant to make a big change like this. Creating a large segment doesn't seem to immediately show up in memory usage, so I'm not sure of the actual impact to the system (but don't have a great understanding of it).
My main concern (besides the overhead) is code complexity. The current diff has a lot of changes. If there's a way to do this with very little changes outside of the pointer macros (and changing the type of `base`), this would be less of a concern.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-09-19 21:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-09-19 21:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Cleaning up stale issues, but can revisit in the future if needed.
^ permalink raw [nested|flat] 12+ messages in thread
* Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
@ 2024-09-19 21:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
10 siblings, 0 replies; 12+ messages in thread
From: ankane (@ankane) @ 2024-09-19 21:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 12+ messages in thread
end of thread, other threads:[~2024-09-19 21:10 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25 08:10 [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-01-25 08:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-01-25 10:50 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-01-26 03:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-01-26 16:56 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-01-26 16:56 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-01-26 17:02 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-01-26 21:53 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-02-19 11:53 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-02-20 22:49 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-19 21:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-19 21:10 ` "ankane (@ankane)" <noreply+ankane@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