diff --git a/pkg/tsdb/postgres/postgres.go b/pkg/tsdb/postgres/postgres.go index fdf09216e51..12270da2a48 100644 --- a/pkg/tsdb/postgres/postgres.go +++ b/pkg/tsdb/postgres/postgres.go @@ -170,6 +170,8 @@ func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *co rowCount := 0 timeIndex := -1 metricIndex := -1 + metricPrefix := false + metricPrefixValue := "" // check columns of resultset: a column named time is mandatory // the first text column is treated as metric name unless a column named metric is present @@ -179,6 +181,10 @@ func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *co timeIndex = i case "metric": metricIndex = i + // use metric column as prefix with multiple value columns + if len(columnNames) > 3 { + metricPrefix = true + } default: if metricIndex == -1 { switch columnTypes[i].DatabaseTypeName() { @@ -234,7 +240,11 @@ func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *co if metricIndex >= 0 { if columnValue, ok := values[metricIndex].(string); ok { - metric = columnValue + if metricPrefix { + metricPrefixValue = columnValue + } else { + metric = columnValue + } } else { return fmt.Errorf("Column metric must be of type char,varchar or text, got: %T %v", values[metricIndex], values[metricIndex]) } @@ -251,6 +261,8 @@ func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *co if metricIndex == -1 { metric = col + } else if metricPrefix { + metric = metricPrefixValue + " " + col } series, exist := pointsBySeries[metric] diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 4e60eee9e11..2222e512e32 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -77,7 +77,8 @@ export default class PostgresQuery { render(interpolate?) { let target = this.target; - if (!('table' in this.target)) { + // new query with no table set yet + if (!this.target.rawQuery && !('table' in this.target)) { return ''; }