From 4b31af18c0f3fc467e0dc57ffe563fc6c10c9c5b Mon Sep 17 00:00:00 2001 From: niksa Date: Sat, 13 Oct 2018 11:53:28 +0200 Subject: [PATCH] Insert metric labels only if they are not already present to prevent primary key sequence growing on each data point insert. Due to concurrency we can still attempt insert but the effect of that should not be big --- .gitignore | 3 ++- Gopkg.lock | 2 +- postgresql/client.go | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 852cdf5..ead9e69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ *~ -/prometheus-postgresql-adapter +/prometheus-postgresql-adapter* /vendor /version.properties /.target_os /.history /.vscode +/.idea diff --git a/Gopkg.lock b/Gopkg.lock index 39dbf73..54d71d4 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -215,6 +215,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "e7dac0745b368eab3f4a3c695ecf4175450cf073e16cdd7b116cf41c9f64be0a" + inputs-digest = "f5312ede7b670bb664c97338901ed5ed758bfd04c7e665ff01d7d8a1695c2e73" solver-name = "gps-cdcl" solver-version = 1 diff --git a/postgresql/client.go b/postgresql/client.go index b1952e1..3bc6ee8 100644 --- a/postgresql/client.go +++ b/postgresql/client.go @@ -73,7 +73,7 @@ type Client struct { const ( sqlCreateTmpTable = "CREATE TEMPORARY TABLE IF NOT EXISTS %s_tmp(sample prom_sample) ON COMMIT DELETE ROWS;" sqlCopyTable = "COPY \"%s\" FROM STDIN" - sqlInsertLabels = "INSERT INTO %s_labels (metric_name, labels) SELECT prom_name(tmp.sample), prom_labels(tmp.sample) FROM %s_tmp tmp ON CONFLICT (metric_name, labels) DO NOTHING;" + sqlInsertLabels = "INSERT INTO %s_labels (metric_name, labels) SELECT tmp.prom_name, tmp.prom_labels FROM (SELECT prom_time(sample), prom_value(sample), prom_name(sample), prom_labels(sample) FROM %s_tmp) tmp LEFT JOIN %s_labels l ON tmp.prom_name=l.metric_name AND tmp.prom_labels=l.labels WHERE l.metric_name IS NULL ON CONFLICT (metric_name, labels) DO NOTHING;" sqlInsertValues = "INSERT INTO %s_values SELECT tmp.prom_time, tmp.prom_value, l.id FROM (SELECT prom_time(sample), prom_value(sample), prom_name(sample), prom_labels(sample) FROM %s_tmp) tmp INNER JOIN %s_labels l on tmp.prom_name=l.metric_name AND tmp.prom_labels=l.labels;" ) @@ -256,7 +256,7 @@ func (c *Client) Write(samples model.Samples) error { } if copyTable == fmt.Sprintf("%s_tmp", c.cfg.table) { - stmtLabels, err := tx.Prepare(fmt.Sprintf(sqlInsertLabels, c.cfg.table, c.cfg.table)) + stmtLabels, err := tx.Prepare(fmt.Sprintf(sqlInsertLabels, c.cfg.table, c.cfg.table, c.cfg.table)) if err != nil { log.Error("msg", "Error on preparing labels statement", "err", err) return err