diff --git a/database/pgsql/feature.go b/database/pgsql/feature.go index 247d5701..a2f2abe8 100644 --- a/database/pgsql/feature.go +++ b/database/pgsql/feature.go @@ -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 { diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index 80c61784..22ca33b8 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -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)