aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/insert_module.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-01-29 11:10:42 -0500
committerJonathan Amsterdam <jba@google.com>2021-01-29 22:10:09 +0000
commit786e2aadc606edda9f92233e991da90edec22ed0 (patch)
treee40151faa2fb02a6b6b02f52cca591e1fc4e06f4 /internal/postgres/insert_module.go
parent7588e3daea450e3c570039549f12b21f888a1f4e (diff)
downloadgo-x-pkgsite-786e2aadc606edda9f92233e991da90edec22ed0.tar.xz
internal/postgres: add logging to debug conflict duplicates
Add logging to the insertUnits function to see if we can understand why we occasionally see "ON CONFLICT DO UPDATE command cannot affect row a second time". According to https://pganalyze.com/docs/log-insights/app-errors/U126, it happens because we have duplicate conflict columns. For golang/go#43899 Change-Id: Idba3018708c3f5ee451e574b2c747929a5e2c30d Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/287793 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'internal/postgres/insert_module.go')
-rw-r--r--internal/postgres/insert_module.go18
1 files changed, 17 insertions, 1 deletions
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go
index 69e8e7ca..2d84be8d 100644
--- a/internal/postgres/insert_module.go
+++ b/internal/postgres/insert_module.go
@@ -407,7 +407,7 @@ func insertPaths(ctx context.Context, db *database.DB, m *internal.Module) (path
}
func insertUnits(ctx context.Context, db *database.DB, unitValues []interface{}) (pathIDToUnitID map[int]int, err error) {
- defer derrors.Wrap(&err, "insertUnits")
+ defer derrors.WrapAndReport(&err, "insertUnits")
// Insert data into the units table.
unitCols := []string{
@@ -422,6 +422,18 @@ func insertUnits(ctx context.Context, db *database.DB, unitValues []interface{})
uniqueUnitCols := []string{"path_id", "module_id"}
returningUnitCols := []string{"id", "path_id"}
+ // Check to see if any rows have the same path_id and module_id.
+ // For golang/go#43899.
+ conflictingValues := map[[2]interface{}]bool{}
+ for i := 0; i < len(unitValues); i += len(unitCols) {
+ key := [2]interface{}{unitValues[i], unitValues[i+1]}
+ if conflictingValues[key] {
+ log.Errorf(ctx, "insertUnits: %v occurs twice", key)
+ } else {
+ conflictingValues[key] = true
+ }
+ }
+
pathIDToUnitID = map[int]int{}
if err := db.BulkUpsertReturning(ctx, "units", unitCols, unitValues,
uniqueUnitCols, returningUnitCols, func(rows *sql.Rows) error {
@@ -432,6 +444,10 @@ func insertUnits(ctx context.Context, db *database.DB, unitValues []interface{})
pathIDToUnitID[pathID] = unitID
return nil
}); err != nil {
+ log.Errorf(ctx, "got error doing bulk upsert to units (see below); logging path_id, module_id for golang.org/issue/43899")
+ for i := 0; i < len(unitValues); i += len(unitCols) {
+ log.Errorf(ctx, "%v, %v", unitValues[i], unitValues[i+1])
+ }
return nil, err
}
return pathIDToUnitID, nil