Skip to content

Commit 0c149a8

Browse files
0xgoudapashagolub
authored andcommitted
move maintenance lock acquisition logic to admin.try_get_maintenance_lock()
1 parent 5ed6968 commit 0c149a8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

internal/sinks/postgres.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ func (pgw *PostgresWriter) DeleteOldPartitions() {
509509
func (pgw *PostgresWriter) MaintainUniqueSources() {
510510
logger := log.GetLogger(pgw.ctx)
511511

512-
sqlGetAdvisoryLock := `SELECT pg_try_advisory_lock(1571543679778230000) AS have_lock` // 1571543679778230000 is just a random bigint
513512
sqlTopLevelMetrics := `SELECT table_name FROM admin.get_top_level_metric_tables()`
514513
sqlDistinct := `
515514
WITH RECURSIVE t(dbname) AS (
@@ -528,13 +527,13 @@ func (pgw *PostgresWriter) MaintainUniqueSources() {
528527
RETURNING *`
529528

530529
var lock bool
531-
logger.Infof("Trying to get admin.all_distinct_dbname_metrics maintainer advisory lock...") // to only have one "maintainer" in case of a "push" setup, as can get costly
532-
if err := pgw.sinkDb.QueryRow(pgw.ctx, sqlGetAdvisoryLock).Scan(&lock); err != nil {
533-
logger.Error("Getting admin.all_distinct_dbname_metrics maintainer advisory lock failed:", err)
530+
logger.Infof("Trying to get maintenance advisory lock...") // to only have one "maintainer" in case of a "push" setup, as can get costly
531+
if err := pgw.sinkDb.QueryRow(pgw.ctx, "SELECT admin.try_get_maintenance_lock();").Scan(&lock); err != nil {
532+
logger.Error("Getting maintenance advisory lock failed:", err)
534533
return
535534
}
536535
if !lock {
537-
logger.Info("Skipping admin.all_distinct_dbname_metrics maintenance as another instance has the advisory lock...")
536+
logger.Info("Skipping maintenance as another instance has the advisory lock...")
538537
return
539538
}
540539

internal/sinks/sql/admin_functions.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,14 @@ BEGIN
139139
RETURN i;
140140
END;
141141
$SQL$ LANGUAGE plpgsql;
142+
143+
CREATE OR REPLACE FUNCTION admin.try_get_maintenance_lock(
144+
OUT have_lock BOOLEAN
145+
)
146+
AS
147+
$SQL$
148+
BEGIN
149+
-- 1571543679778230000 is just a random bigint
150+
SELECT pg_try_advisory_lock(1571543679778230000) INTO have_lock;
151+
END;
152+
$SQL$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)