Skip to content

Commit 3e10bd1

Browse files
committed
fix(bulk): batch bulk group inserts to avoid asyncpg 32767 param limit
1 parent 09b4fc9 commit 3e10bd1

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

app/db/crud/bulk.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ async def add_groups_to_users(db: AsyncSession, bulk_model: BulkGroup) -> tuple[
180180
if not new_rows:
181181
return [], count_effctive_users
182182

183-
await db.execute(users_groups_association.insert(), new_rows)
183+
# PostgreSQL asyncpg limits bind parameters to 32767 per query.
184+
# Each row has 2 columns (user_id, groups_id), so cap batches at 16000 rows.
185+
BATCH_SIZE = 16_000
186+
for i in range(0, len(new_rows), BATCH_SIZE):
187+
await db.execute(users_groups_association.insert(), new_rows[i : i + BATCH_SIZE])
184188
await db.commit()
185189

186190
# Return users that actually had groups added

0 commit comments

Comments
 (0)