Billionmail/BillionMail · error

Failed to query existing abnormal recipients: %w

Error message

Failed to query existing abnormal recipients: %w

What it means

BatchUpsertAbnormalRecipients first SELECTs existing rows with WhereIn("recipient", recipients) to decide update-vs-insert. If that query fails, the error is wrapped as this message and the whole batch op aborts before any writes.

Source

Thrown at core/internal/service/abnormal_recipient/abnormal_recipient.go:100

	var recipient entity.AbnormalRecipient
	err := g.DB().Model("abnormal_recipient").Where("id", id).Scan(&recipient)
	if err != nil {
		return nil, fmt.Errorf("Failed to get exception recipient: %w", err)
	}
	return &recipient, nil
}

func BatchUpsertAbnormalRecipients(ctx context.Context, recipients []string, addType int, description string) error {
	now := time.Now().Unix()

	if len(recipients) == 0 {
		return nil
	}

	var existList []entity.AbnormalRecipient
	err := g.DB().Model("abnormal_recipient").WhereIn("recipient", recipients).Scan(&existList)
	if err != nil {
		return fmt.Errorf("Failed to query existing abnormal recipients: %w", err)
	}
	existMap := make(map[string]*entity.AbnormalRecipient)
	for _, r := range existList {
		existMap[r.Recipient] = &r
	}
	// 1. Update the existing one count+1
	for _, r := range existList {
		_, err := g.DB().Model("abnormal_recipient").Where("id", r.Id).Data(g.Map{
			"count":       r.Count + 1,
			"description": description,
			"add_type":    addType,
		}).Update()
		if err != nil {

			return fmt.Errorf("Failed to update abnormal recipient: %w", err)
		}

	}

View on GitHub (pinned to fc36c76c05)

Solutions

  1. Verify DB connectivity and schema (migrations).
  2. Chunk large recipient slices (e.g. 500 per batch) to avoid huge WHERE IN clauses.
  3. Retry transient connection errors with backoff.
  4. Inspect the wrapped pq/pgx error for the exact SQL failure.

Example fix

// before
err := abnormal_recipient.BatchUpsertAbnormalRecipients(ctx, allRecipients, 1, "Manually added")
// after
for chunk := range lo.Chunk(allRecipients, 500) {
	if err := abnormal_recipient.BatchUpsertAbnormalRecipients(ctx, chunk, 1, "Manually added"); err != nil {
		return err
	}
}
Defensive patterns

Strategy: retry

Validate before calling

// before calling the batch upsert
if len(recipients) == 0 { return nil }
if err := g.DB().PingMaster(); err != nil { return fmt.Errorf("db unavailable: %w", err) }

Try / catch

err := abnormal_recipient.BatchUpsertAbnormalRecipients(ctx, recs, addType, desc)
if err != nil {
	if isTransientDBError(err) { err = retry.Do(3, backoff, func() error { return abnormal_recipient.BatchUpsertAbnormalRecipients(ctx, recs, addType, desc) }) }
	if err != nil { return err }
}

Prevention

When it happens

Trigger: Calling BatchUpsertAbnormalRecipients with a dead/unconfigured DB connection, missing abnormal_recipient table, or an oversized WhereIn list hitting driver/statement limits.

Common situations: Bulk imports during a postgres restart; fresh environment without migrations; very large recipient batches causing query timeouts.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of Billionmail/BillionMail@fc36c76c05 (2026-09-05). Data as JSON: /api/errors/9252d51a849fce5d. Report an issue: GitHub.