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

Commit

Permalink
Insert metric labels only if they are not already present to prevent …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
niksajakovljevic committed Oct 17, 2018
1 parent 4d5d32a commit 4b31af1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
*~
/prometheus-postgresql-adapter
/prometheus-postgresql-adapter*
/vendor
/version.properties
/.target_os
/.history
/.vscode
/.idea

2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions postgresql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b31af1

Please sign in to comment.