From 62b40b986e2c6b3fd85f6b962a24ab93cea59922 Mon Sep 17 00:00:00 2001 From: Jo Asplin Date: Thu, 26 Sep 2024 09:53:51 +0200 Subject: [PATCH] Avoided doing cleanup in a transaction --- .../storagebackend/postgresql/postgresql.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/datastore/datastore/storagebackend/postgresql/postgresql.go b/datastore/datastore/storagebackend/postgresql/postgresql.go index 4cc490c3..a99cdeba 100644 --- a/datastore/datastore/storagebackend/postgresql/postgresql.go +++ b/datastore/datastore/storagebackend/postgresql/postgresql.go @@ -345,12 +345,7 @@ func cleanup(db *sql.DB) error { log.Println("db cleanup started") start := time.Now() - // start transaction - tx, err := db.Begin() - if err != nil { - return fmt.Errorf("db.Begin() failed: %v", err) - } - defer tx.Rollback() + var err error // --- BEGIN define removal functions ---------------------------------- @@ -362,7 +357,7 @@ func cleanup(db *sql.DB) error { WHERE (obstime_instant < to_timestamp(%d)) OR (obstime_instant > to_timestamp(%d)) `, loTime.Unix(), hiTime.Unix()) - _, err = tx.Exec(cmd) + _, err = db.Exec(cmd) if err != nil { return fmt.Errorf( "tx.Exec() failed when removing observations outside valid range: %v", err) @@ -382,7 +377,7 @@ func cleanup(db *sql.DB) error { ) `, tableName, tableName, fkName) - _, err = tx.Exec(cmd) + _, err = db.Exec(cmd) if err != nil { return fmt.Errorf( "tx.Exec() failed when removing unreferenced rows from %s: %v", tableName, err) @@ -415,11 +410,6 @@ func cleanup(db *sql.DB) error { // --- END apply removal functions ------------------------------------------ - // commit transaction - if err = tx.Commit(); err != nil { - return fmt.Errorf("tx.Commit() failed: %v", err) - } - log.Printf("db cleanup complete after %v", time.Since(start)) return nil