From 644066fac009c765618d2a9a69cafc12ef636e26 Mon Sep 17 00:00:00 2001 From: matthias Date: Tue, 19 Nov 2024 10:35:25 +0100 Subject: [PATCH] add function for pgbackrest-data --- pkg/cluster/database.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/database.go b/pkg/cluster/database.go index 274d224f..0d969f29 100644 --- a/pkg/cluster/database.go +++ b/pkg/cluster/database.go @@ -98,12 +98,40 @@ const ( CREATE EXTENSION IF NOT EXISTS pgnodemx with SCHEMA exporter; alter extension pgnodemx UPDATE; CREATE TABLE IF NOT EXISTS exporter.pgbackrestbackupinfo ( - name text NOT NULL, data jsonb NOT NULL, data_time timestamp with time zone DEFAULT now() NOT NULL ) WITH (autovacuum_analyze_scale_factor='0', autovacuum_vacuum_scale_factor='0', autovacuum_vacuum_threshold='2', autovacuum_analyze_threshold='2'); ALTER TABLE exporter.pgbackrestbackupinfo OWNER TO cpo_exporter; + + CREATE OR REPLACE FUNCTION exporter.update_pgbackrest_info() + RETURNS VOID AS $$ + DECLARE + last_entry_timestamp TIMESTAMP; + record_count INT; + BEGIN + SELECT COUNT(*) INTO record_count + FROM exporter.pgbackrestbackupinfo; + + IF record_count > 0 THEN + SELECT data_time INTO last_entry_timestamp + FROM exporter.pgbackrestbackupinfo + ORDER BY data_time DESC + LIMIT 1; + + IF last_entry_timestamp < NOW() - INTERVAL '5 minutes' THEN + DELETE FROM exporter.pgbackrestbackupinfo; + ELSE + RETURN; + END IF; + END IF; + + EXECUTE format( + 'COPY exporter.pgbackrestbackupinfo (data) FROM program ''pgbackrest info --output=json'' WITH (FORMAT text, DELIMITER ''|'')' + ); + END; + $$ LANGUAGE plpgsql; + ` )