{"record":{"id":"ea3af1472b3f24bf","repo":"Billionmail/BillionMail","slug":"batch-insert-mailbox-failed-batch-d-d-w","errorCode":null,"errorMessage":"Batch insert mailbox failed (batch %d-%d): %w","messagePattern":"Batch insert mailbox failed \\(batch (.+?)-(.+?)\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/internal/service/mail_boxes/mail_boxes.go","lineNumber":332,"sourceCode":"\t\t//if err != nil {\n\t\t//\treturn fmt.Errorf(\"Check the domain for a failure: %w\", err)\n\t\t//}\n\t\t//if domainExists.IsEmpty() {\n\t\t//\treturn fmt.Errorf(\" %s Not present or activated\", domain)\n\t\t//}\n\n\t\tconst batchSize = 100\n\t\tfor i := 0; i < len(mailboxes); i += batchSize {\n\t\t\tend := i + batchSize\n\t\t\tif end > len(mailboxes) {\n\t\t\t\tend = len(mailboxes)\n\t\t\t}\n\n\t\t\tbatch := mailboxes[i:end]\n\n\t\t\t_, err := tx.Model(\"mailbox\").Ctx(ctx).InsertIgnore(batch)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"Batch insert mailbox failed (batch %d-%d): %w\", i, end-1, err)\n\t\t\t}\n\n\t\t\tfor j := i; j < end; j++ {\n\t\t\t\tcreatedEmails = append(createdEmails, emailList[j])\n\t\t\t}\n\n\t\t}\n\n\t\t// If the limit is enabled, asynchronous will initialize the maildir and maildirsize for batch-created email accounts, and limit the concurrency\n\t\tif quotaActive != 1 || quota <= 0 {\n\t\t\treturn nil\n\t\t}\n\n\t\tgo func(mbs []v1.Mailbox) {\n\t\t\tsem := make(chan struct{}, 10)\n\t\t\tfor i := range mbs {\n\t\t\t\tmb := mbs[i]\n\t\t\t\tsem <- struct{}{}","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/Billionmail/BillionMail/blob/fc36c76c050c3775c5e899faf7403cf0262d2744/core/internal/service/mail_boxes/mail_boxes.go#L314-L350","documentation":"Thrown inside the BatchAdd transaction when the batched InsertIgnore of mailboxes into the 'mailbox' table fails. The message includes the batch index range so you can tell which slice of the insert failed. Any insert error (constraint violation, connection loss, SQL syntax) aborts the whole transaction via the wrapped error returned from the closure.","triggerScenarios":"BatchAddMailbox with a large count split into batches, where one tx.Model(\"mailbox\").InsertIgnore(batch) fails — e.g. duplicate unique key not ignored by the DB, connection drop mid-transaction, or table schema mismatch.","commonSituations":"Postgres connection pool exhaustion under load; unique constraint conflicts on username/local_part when InsertIgnore semantics differ from expectations; oversized batch payloads.","solutions":["Read the wrapped %w cause to see the exact SQL error","Check for conflicting existing rows (duplicate username/local_part) in the mailbox table","Verify database connectivity and connection-pool limits during large batch operations","Reduce the batch size or count and retry the BatchAdd call"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// pre-check duplicates\nfor _, em := range emailList { if exists(em) { skip or rename } }","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"Batch insert mailbox failed\") {\n    // inspect wrapped SQL error, fix cause, retry batch\n}","preventionTips":["Pre-check for duplicate usernames/local_parts before batch insert","Ensure adequate DB connection pool size for large batches","Keep batch sizes moderate and monitor DB health during bulk ops"],"tags":["go","database","insert","transaction"],"backgroundTag":"database-insert-failed","analyzedSha":"fc36c76c050c3775c5e899faf7403cf0262d2744","analyzedAt":"2026-09-05T21:28:54.019Z","contentChangedAt":"2026-09-05T21:28:54.019Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}