From: itaispiegel (@itaispiegel) <noreply+itaispiegel@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #995: IVFFlat: CREATE INDEX on empty/small table fails with "memory required is N MB" in 0.8.4 (regression from 0.8.2)
Date: Wed, 01 Jul 2026 09:19:03 +0000
Message-ID: <gh-pgvector-pgvector-995@github.com> (raw)
## Summary
Since **0.8.4**, creating an IVFFlat index on an **empty (or small) table** fails with `memory required is N MB, maintenance_work_mem is 64 MB`, where the required memory is derived from the **worst-case** sample buffer (`lists × 50` vectors) regardless of how many rows the table actually has. The same statement **succeeds on 0.8.2** (with the usual `ivfflat index created with little data` notice).
This breaks the common pattern of creating the index in a schema migration *before* loading data (e.g. Rails/sea-orm/Flyway migrations, `CREATE TABLE` + `CREATE INDEX` in one step).
## Reproduction
```bash
docker run --rm -e POSTGRES_HOST_AUTH_METHOD=trust -d \
pgvector/pgvector:0.8.4-pg17 postgres -c maintenance_work_mem=64MB
psql -U postgres <<'SQL'
CREATE EXTENSION vector;
CREATE TABLE my_table (id int, embedding vector(1536)); -- empty table
CREATE INDEX ON my_table USING ivfflat (embedding vector_cosine_ops) WITH (lists = 300);
SQL
```
**0.8.4 (`0.8.4-pg17`):**
```
ERROR: memory required is 90 MB, maintenance_work_mem is 64 MB
```
**0.8.2 (`0.8.2-pg17`), identical statements:**
```
NOTICE: ivfflat index created with little data
DETAIL: This will cause low recall.
HINT: Drop the index until the table has more data.
CREATE INDEX
```
`90 MB ≈ lists × 50 × dim × 4 B = 300 × 50 × 1536 × 4` — i.e. the check sizes the full `maxSamples` buffer even though the table has 0 rows.
## Environment
- pgvector **0.8.4** (regression) vs **0.8.2** (works); same PostgreSQL 17.10 image (`pgvector/pgvector`), so the change is in pgvector, not Postgres.
- The check comes from `IvfflatCheckMemoryUsage` (`src/ivfutils.c`), now invoked up-front during build init in `src/ivfbuild.c`.
## Expected
Building an IVFFlat index on an empty/tiny table should not require worst-case memory. The pre-check should size the sample buffer by `Min(lists × 50, estimated_rows)` (or skip the hard error and fall back to the existing "little data" notice) when the table has few/no rows, so migration-time index creation keeps working.
## Notes
Discovered because the floating `pgvector/pgvector:pg17` tag rebuilt from 0.8.2 → 0.8.4 and this began failing our migration-time index build in CI. Happy to open a PR — see proposed fix.