insertPendingBatchTx writes one webhook_subscriber_deliveries row per matched webhook in a single multi-row INSERT. Per-row ON CONFLICT (event_id, webhook_id) WHERE event_id IS NOT NULL AND replay_id IS NULL DO NOTHING swallows duplicate inserts that come from a multi-replica lease race (worker A in
(ctx context.Context, tx pgx.Tx, eventID string, webhookIDs []string, eventType string, messageID *string, envelope []byte)
| 376 | // in migration 028 — Postgres requires exact predicate matching for |
| 377 | // ON CONFLICT to bind to a partial index. |
| 378 | func insertPendingBatchTx(ctx context.Context, tx pgx.Tx, eventID string, webhookIDs []string, eventType string, messageID *string, envelope []byte) error { |
| 379 | if len(webhookIDs) == 0 { |
| 380 | return nil |
| 381 | } |
| 382 | |
| 383 | // Build multi-row VALUES list. pgx supports parameterized arrays, |
| 384 | // but the simplest portable shape is to fan out the parameters. |
| 385 | args := make([]any, 0, 5+len(webhookIDs)*2) |
| 386 | values := make([]string, 0, len(webhookIDs)) |
| 387 | args = append(args, eventID, eventType, messageID, envelope) |
| 388 | for i, whID := range webhookIDs { |
| 389 | // id, webhook_id placeholders. The other 4 fields are shared |
| 390 | // across all rows so reference them by their fixed indexes. |
| 391 | base := 4 + i*2 |
| 392 | values = append(values, fmt.Sprintf("($%d, $%d, $1, $2, $4, $3, 'pending')", base+1, base+2)) |
| 393 | args = append(args, generateDeliveryID(), whID) |
| 394 | } |
| 395 | |
| 396 | sql := `INSERT INTO webhook_subscriber_deliveries |
| 397 | (id, webhook_id, event_id, event_type, event_payload, message_id, status) |
| 398 | VALUES ` + strings.Join(values, ", ") + ` |
| 399 | ON CONFLICT (event_id, webhook_id) |
| 400 | WHERE event_id IS NOT NULL AND replay_id IS NULL |
| 401 | DO NOTHING` |
| 402 | |
| 403 | _, err := tx.Exec(ctx, sql, args...) |
| 404 | return err |
| 405 | } |
| 406 | |
| 407 | // generateDeliveryID mirrors the format used by the legacy publisher's |
| 408 | // dbInserter — whd_<32-hex>. The relay's blocker-fix commit moved |
no test coverage detected