Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 4b31af1

Browse files
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
1 parent 4d5d32a commit 4b31af1

3 files changed

Lines changed: 5 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
*~
2-
/prometheus-postgresql-adapter
2+
/prometheus-postgresql-adapter*
33
/vendor
44
/version.properties
55
/.target_os
66
/.history
77
/.vscode
8+
/.idea
89

Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

postgresql/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Client struct {
7373
const (
7474
sqlCreateTmpTable = "CREATE TEMPORARY TABLE IF NOT EXISTS %s_tmp(sample prom_sample) ON COMMIT DELETE ROWS;"
7575
sqlCopyTable = "COPY \"%s\" FROM STDIN"
76-
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;"
76+
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;"
7777
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;"
7878
)
7979

@@ -256,7 +256,7 @@ func (c *Client) Write(samples model.Samples) error {
256256
}
257257

258258
if copyTable == fmt.Sprintf("%s_tmp", c.cfg.table) {
259-
stmtLabels, err := tx.Prepare(fmt.Sprintf(sqlInsertLabels, c.cfg.table, c.cfg.table))
259+
stmtLabels, err := tx.Prepare(fmt.Sprintf(sqlInsertLabels, c.cfg.table, c.cfg.table, c.cfg.table))
260260
if err != nil {
261261
log.Error("msg", "Error on preparing labels statement", "err", err)
262262
return err

0 commit comments

Comments
 (0)