diff --git a/src/ivfbuild.c b/src/ivfbuild.c
index e35c35215..fd5e0ba66 100644
--- a/src/ivfbuild.c
+++ b/src/ivfbuild.c
@@ -448,6 +448,17 @@ ComputeCenters(IvfflatBuildState * buildstate)
 	if (buildstate->heap == NULL)
 		numSamples = 1;
 
+	/*
+	 * An empty table has no rows to sample, so avoid allocating (and
+	 * requiring maintenance_work_mem for) the full sample buffer. Without
+	 * this, creating an ivfflat index on an empty table can fail with
+	 * "memory required is N MB, maintenance_work_mem is M MB" even though
+	 * nothing will be sampled - which breaks creating the index in a schema
+	 * migration before data is loaded.
+	 */
+	else if (RelationGetNumberOfBlocks(buildstate->heap) == 0)
+		numSamples = 1;
+
 	/* Sample rows */
 	buildstate->memoryUsed += VECTOR_ARRAY_SIZE(numSamples, buildstate->itemsize);
 	IvfflatCheckMemoryUsage(buildstate->memoryUsed);
diff --git a/test/expected/ivfflat_vector.out b/test/expected/ivfflat_vector.out
index aad8226c1..b1d283ac3 100644
--- a/test/expected/ivfflat_vector.out
+++ b/test/expected/ivfflat_vector.out
@@ -173,3 +173,12 @@ ERROR:  0 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32
 SET ivfflat.max_probes = 32769;
 ERROR:  32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768)
 DROP TABLE t;
+-- building on an empty table should not require worst-case maintenance_work_mem
+SET maintenance_work_mem = '1MB';
+CREATE TABLE t (val vector(1536));
+CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 100);
+NOTICE:  ivfflat index created with little data
+DETAIL:  This will cause low recall.
+HINT:  Drop the index until the table has more data.
+DROP TABLE t;
+RESET maintenance_work_mem;
diff --git a/test/sql/ivfflat_vector.sql b/test/sql/ivfflat_vector.sql
index 4c20d2faf..e4c585677 100644
--- a/test/sql/ivfflat_vector.sql
+++ b/test/sql/ivfflat_vector.sql
@@ -97,3 +97,10 @@ SET ivfflat.max_probes = 0;
 SET ivfflat.max_probes = 32769;
 
 DROP TABLE t;
+
+-- building on an empty table should not require worst-case maintenance_work_mem
+SET maintenance_work_mem = '1MB';
+CREATE TABLE t (val vector(1536));
+CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 100);
+DROP TABLE t;
+RESET maintenance_work_mem;
