Skip to content

Commit

Permalink
database: Find the FeatureVersion we try to insert before doing any lock
Browse files Browse the repository at this point in the history
This commit is issued in order to limit the bottleneck that the
exclusive database lock on Vulnerability_Affects_FeautreVersion
introduces, when we inserting FeatureVersions. This slowdowns a bit
the FeatureVersion insertion on a mostly empty database but should
increase a lot the throughput and parallelism on a populated database.
  • Loading branch information
Quentin-M committed Mar 3, 2016
1 parent 5a716f9 commit 9b191fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions database/pgsql/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion)

featureVersion.Feature.ID = featureID

// Try to find the FeatureVersion.
//
// In a populated database, the likelihood of the FeatureVersion already being there is high.
// If we can find it here, we then avoid using a transaction and locking the database.
err = pgSQL.QueryRow(searchFeatureVersion, featureID, &featureVersion.Version).
Scan(&featureVersion.ID)
if err != nil && err != sql.ErrNoRows {
return 0, handleError("searchFeatureVersion", err)
}
if err == nil {
if pgSQL.cache != nil {
pgSQL.cache.Add(cacheIndex, featureVersion.ID)
}

return featureVersion.ID, nil
}

// Begin transaction.
tx, err := pgSQL.Begin()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const (
UNION
SELECT id FROM new_feature`

searchFeatureVersion = `
SELECT id FROM FeatureVersion WHERE feature_id = $1 AND version = $2`

soiFeatureVersion = `
WITH new_featureversion AS (
INSERT INTO FeatureVersion(feature_id, version)
Expand Down

0 comments on commit 9b191fb

Please sign in to comment.