Skip to content

PMM-7 Add PostgreSQL 18 support with new metrics and collectors #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## [Unreleased]

* [ENHANCEMENT] Add PostgreSQL 18 support:
* Add parallel worker activity metrics (`pg_stat_database_parallel_workers_to_launch`, `pg_stat_database_parallel_workers_launched`)
* Add vacuum/analyze timing metrics (`pg_stat_user_tables_total_vacuum_time`, `pg_stat_user_tables_total_autovacuum_time`, `pg_stat_user_tables_total_analyze_time`, `pg_stat_user_tables_total_autoanalyze_time`)
* Add enhanced checkpointer metrics (`pg_stat_bgwriter_checkpoints_done_total`, `pg_stat_bgwriter_slru_written_total`)
* Add `pg_stat_io` collector with byte statistics and WAL I/O activity tracking
* Add `pg_backend_stats` collector for per-backend I/O and WAL statistics
* Add enhanced `pg_backend_memory_contexts` collector with type and path columns
* [ENHANCEMENT] Update CI tested PostgreSQL versions to include PostgreSQL 18

## 0.15.0 / 2023-10-27

* [ENHANCEMENT] Add 1kB and 2kB units #915
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Prometheus exporter for PostgreSQL server metrics.

CI Tested PostgreSQL versions: `11`, `12`, `13`, `14`, `15`, `16`
CI Tested PostgreSQL versions: `11`, `12`, `13`, `14`, `15`, `16`, `18`

## Quick Start
This package is available for Docker:
Expand Down
216 changes: 216 additions & 0 deletions collector/pg_backend_memory_contexts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"database/sql"

"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

const backendMemoryContextsSubsystem = "backend_memory_contexts"

func init() {
registerCollector(backendMemoryContextsSubsystem, defaultDisabled, NewPGBackendMemoryContextsCollector)
}

type PGBackendMemoryContextsCollector struct {
log log.Logger
}

func NewPGBackendMemoryContextsCollector(config collectorConfig) (Collector, error) {
return &PGBackendMemoryContextsCollector{log: config.logger}, nil
}

var (
backendMemoryContextsTotalBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, backendMemoryContextsSubsystem, "total_bytes"),
"Total bytes allocated for memory context",
[]string{"pid", "name", "ident", "parent", "level", "type", "path"},
prometheus.Labels{},
)
backendMemoryContextsUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, backendMemoryContextsSubsystem, "used_bytes"),
"Used bytes in memory context",
[]string{"pid", "name", "ident", "parent", "level", "type", "path"},
prometheus.Labels{},
)
backendMemoryContextsFreeBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, backendMemoryContextsSubsystem, "free_bytes"),
"Free bytes in memory context",
[]string{"pid", "name", "ident", "parent", "level", "type", "path"},
prometheus.Labels{},
)
backendMemoryContextsFreeChunks = prometheus.NewDesc(
prometheus.BuildFQName(namespace, backendMemoryContextsSubsystem, "free_chunks"),
"Number of free chunks in memory context",
[]string{"pid", "name", "ident", "parent", "level", "type", "path"},
prometheus.Labels{},
)

// PostgreSQL 18+ query with type and path columns
backendMemoryContextsQuery18Plus = `
SELECT
pid,
name,
COALESCE(ident, '') as ident,
COALESCE(parent, '') as parent,
level,
total_bytes,
total_nblocks,
free_bytes,
free_chunks,
used_bytes,
type,
path
FROM pg_backend_memory_contexts
ORDER BY pid, name
`

// Pre-PostgreSQL 18 query without type and path columns
backendMemoryContextsQueryPre18 = `
SELECT
pid,
name,
COALESCE(ident, '') as ident,
COALESCE(parent, '') as parent,
level,
total_bytes,
total_nblocks,
free_bytes,
free_chunks,
used_bytes,
'' as type,
'' as path
FROM pg_backend_memory_contexts
ORDER BY pid, name
`
)

func (c *PGBackendMemoryContextsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
// pg_backend_memory_contexts was introduced in PostgreSQL 14
if instance.version.LT(semver.Version{Major: 14}) {
return nil
}

db := instance.getDB()

// Use version-specific query for PostgreSQL 18+
query := backendMemoryContextsQueryPre18
if instance.version.GTE(semver.Version{Major: 18}) {
query = backendMemoryContextsQuery18Plus
}

rows, err := db.QueryContext(ctx, query)
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var pid, name, ident, parent, contextType, path sql.NullString
var level, totalNblocks, freeChunks sql.NullInt64
var totalBytes, freeBytes, usedBytes sql.NullFloat64

err := rows.Scan(
&pid,
&name,
&ident,
&parent,
&level,
&totalBytes,
&totalNblocks,
&freeBytes,
&freeChunks,
&usedBytes,
&contextType,
&path,
)
if err != nil {
return err
}

pidLabel := "unknown"
if pid.Valid {
pidLabel = pid.String
}
nameLabel := "unknown"
if name.Valid {
nameLabel = name.String
}
identLabel := ""
if ident.Valid {
identLabel = ident.String
}
parentLabel := ""
if parent.Valid {
parentLabel = parent.String
}
levelLabel := "0"
if level.Valid {
levelLabel = string(rune(level.Int64 + '0'))
}
typeLabel := ""
if contextType.Valid {
typeLabel = contextType.String
}
pathLabel := ""
if path.Valid {
pathLabel = path.String
}

labels := []string{pidLabel, nameLabel, identLabel, parentLabel, levelLabel, typeLabel, pathLabel}

if totalBytes.Valid {
ch <- prometheus.MustNewConstMetric(
backendMemoryContextsTotalBytes,
prometheus.GaugeValue,
totalBytes.Float64,
labels...,
)
}

if usedBytes.Valid {
ch <- prometheus.MustNewConstMetric(
backendMemoryContextsUsedBytes,
prometheus.GaugeValue,
usedBytes.Float64,
labels...,
)
}

if freeBytes.Valid {
ch <- prometheus.MustNewConstMetric(
backendMemoryContextsFreeBytes,
prometheus.GaugeValue,
freeBytes.Float64,
labels...,
)
}

if freeChunks.Valid {
ch <- prometheus.MustNewConstMetric(
backendMemoryContextsFreeChunks,
prometheus.GaugeValue,
float64(freeChunks.Int64),
labels...,
)
}
}

return nil
}
Loading
Loading