Skip to content

Commit 67bfcce

Browse files
committed
rewrite func
1 parent 1a5e53c commit 67bfcce

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pkg/cluster/database.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,41 @@ const (
134134
);
135135
END;
136136
$$ LANGUAGE plpgsql;
137+
138+
CREATE OR REPLACE FUNCTION exporter.update_pgbackrest_info()
139+
RETURNS VOID AS $$
140+
DECLARE
141+
last_entry_timestamp TIMESTAMP;
142+
record_count INT;
143+
BEGIN
144+
IF pg_is_in_recovery() THEN
145+
RAISE NOTICE 'Skipping pgbackrest_info update: running on a replica.';
146+
RETURN;
147+
END IF;
148+
149+
SELECT COUNT(*) INTO record_count
150+
FROM exporter.pgbackrestbackupinfo;
151+
152+
IF record_count > 0 THEN
153+
SELECT data_time INTO last_entry_timestamp
154+
FROM exporter.pgbackrestbackupinfo
155+
ORDER BY data_time DESC
156+
LIMIT 1;
157+
158+
IF last_entry_timestamp >= NOW() - INTERVAL '5 minutes' THEN
159+
RAISE NOTICE 'Skipping pgbackrest_info update: data does not need an update (last update at %).', last_entry_timestamp;
160+
RETURN;
161+
END IF;
162+
163+
DELETE FROM exporter.pgbackrestbackupinfo;
164+
END IF;
165+
166+
EXECUTE format(
167+
'COPY exporter.pgbackrestbackupinfo (data) FROM program ''pgbackrest info --output=json'' WITH (FORMAT text, DELIMITER ''|'')'
168+
);
169+
END;
170+
$$ LANGUAGE plpgsql;
171+
137172
`
138173
)
139174

0 commit comments

Comments
 (0)