Skip to content

Commit

Permalink
Removed unreferenced time series and geo points
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-asplin-met-no committed Sep 20, 2024
1 parent 20d4664 commit ad3aaf8
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions datastore/datastore/storagebackend/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,27 +461,78 @@ func getStringMdataFilter(filter map[string]*datastore.Strings, phVals *[]interf
// cleanup performs various cleanup tasks, like removing old observations from the database.
func cleanup(db *sql.DB) error {

var err error

// start transaction
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("db.Begin() failed: %v", err)
}
defer tx.Rollback()

// --- BEGIN define removal functions ----------------------------------

rmObsOutsideValidRange := func() error {

loTime, hiTime := common.GetValidTimeRange()
cmd := fmt.Sprintf(`
DELETE FROM observation
WHERE (obstime_instant < to_timestamp(%d))
OR (obstime_instant > to_timestamp(%d))
`, loTime.Unix(), hiTime.Unix())

_, err = tx.Exec(cmd)
if err != nil {
return fmt.Errorf(
"tx.Exec() failed when removing observations outside valid range: %v", err)
}

return nil
}

rmUnrefRows := func(tableName, fkName string) error {

cmd := fmt.Sprintf(`
DELETE FROM %s
WHERE id in (
SELECT id FROM %s t WHERE NOT EXISTS (
SELECT FROM observation obs WHERE %s = t.id
)
)
`, tableName, tableName, fkName)

_, err = tx.Exec(cmd)
if err != nil {
return fmt.Errorf(
"tx.Exec() failed when removing unreferenced rows from %s: %v", tableName, err)
}

return nil
}

// --- END define removal functions ----------------------------------

// --- BEGIN apply removal functions ------------------------------------------

// remove observations outside valid range
loTime, hiTime := common.GetValidTimeRange()
cmd := fmt.Sprintf(`
DELETE FROM observation
WHERE (obstime_instant < to_timestamp(%d))
OR (obstime_instant > to_timestamp(%d))
`, loTime.Unix(), hiTime.Unix())
_, err = tx.Exec(cmd)
err = rmObsOutsideValidRange()
if err != nil {
return fmt.Errorf("rmObsOutsideValidRange() failed: %v", err)
}

// remove time series that are no longer referenced by any observation
err = rmUnrefRows("time_series", "ts_id")
if err != nil {
return fmt.Errorf("rmUnrefRows(time_series) failed: %v", err)
}

// remove geo points that are no longer referenced by any observation
err = rmUnrefRows("geo_point", "geo_point_id")
if err != nil {
return fmt.Errorf("tx.Exec() failed: %v", err)
return fmt.Errorf("rmUnrefRows(geo_point) failed: %v", err)
}

// DELETE FROM time_series WHERE <no FK refs from observation anymore> ... TODO!
// DELETE FROM geo_points WHERE <no FK refs from observation anymore> ... TODO!
// --- END apply removal functions ------------------------------------------

// commit transaction
if err = tx.Commit(); err != nil {
Expand Down

0 comments on commit ad3aaf8

Please sign in to comment.