Skip to content

Commit

Permalink
Merge pull request #573 from cybertec-postgresql/make-distinct-dbname…
Browse files Browse the repository at this point in the history
…-maintenance-less-costly

[+] make unique dbname maintenance less costly for multi-daemon setups, fixes #525
  • Loading branch information
pashagolub committed Dec 20, 2022
2 parents 67dd676 + e55e237 commit 69588a0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pgwatch2/pgwatch2.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,9 @@ func InitAndTestMetricStoreConnection(connStr string, failOnErr bool) error {
break
}
}
metricDb.SetMaxIdleConns(1)
metricDb.SetMaxIdleConns(2)
metricDb.SetMaxOpenConns(2)
metricDb.SetConnMaxLifetime(time.Second * time.Duration(PG_CONN_RECYCLE_SECONDS))
metricDb.SetConnMaxLifetime(time.Second * 172800) // 2d
return nil
}

Expand Down Expand Up @@ -1513,6 +1513,7 @@ func AddDBUniqueMetricToListingTable(db_unique, metric string) error {

func UniqueDbnamesListingMaintainer(daemonMode bool) {
// due to metrics deletion the listing can go out of sync (a trigger not really wanted)
sql_get_advisory_lock := `SELECT pg_try_advisory_lock(1571543679778230000) AS have_lock` // 1571543679778230000 is just a random bigint
sql_top_level_metrics := `SELECT table_name FROM admin.get_top_level_metric_tables()`
sql_distinct := `
WITH RECURSIVE t(dbname) AS (
Expand All @@ -1534,6 +1535,17 @@ func UniqueDbnamesListingMaintainer(daemonMode bool) {
time.Sleep(time.Hour * 24)
}

log.Infof("Trying to get metricsDb listing maintaner advisory lock...") // to only have one "maintainer" in case of a "push" setup, as can get costly
lock, err := DBExecRead(metricDb, METRICDB_IDENT, sql_get_advisory_lock)
if err != nil {
log.Error("Getting metricsDb listing maintaner advisory lock failed:", err)
continue
}
if !(lock[0]["have_lock"].(bool)) {
log.Info("Skipping admin.all_distinct_dbname_metrics maintenance as another instance has the advisory lock...")
continue
}

log.Infof("Refreshing admin.all_distinct_dbname_metrics listing table...")
all_distinct_metric_tables, err := DBExecRead(metricDb, METRICDB_IDENT, sql_top_level_metrics)
if err != nil {
Expand Down

0 comments on commit 69588a0

Please sign in to comment.