pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: hlinnaka (@hlinnaka) <noreply+hlinnaka@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #434: Limit HNSW build's shared memory size for small tables
Date: Thu, 25 Jan 2024 10:50:31 +0000
Message-ID: <gh-pgvector-pgvector-434-c1909906860@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-434@github.com>
References: <gh-pgvector-pgvector-434@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.


view thread (12+ messages)  latest in thread

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