Skip to content

Commit a5f7d69

Browse files
author
Andrey Kozlov
committed
Merge branch 'pg_repl-error' of github.com:mblp/postgres_exporter into pg_repl-error
2 parents 11cfc43 + 987bc50 commit a5f7d69

6 files changed

+49
-14
lines changed

.github/workflows/golangci-lint.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ jobs:
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Checkout repository
21-
uses: actions/checkout@v3
21+
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
2222
- name: install Go
23-
uses: actions/setup-go@v3
23+
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
2424
with:
2525
go-version: 1.20.x
2626
- name: Install snmp_exporter/generator dependencies
2727
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
2828
if: github.repository == 'prometheus/snmp_exporter'
2929
- name: Lint
30-
uses: golangci/golangci-lint-action@v3.4.0
30+
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
3131
with:
3232
version: v1.54.2

collector/pg_stat_activity_autovacuum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var (
4545
statActivityAutovacuumQuery = `
4646
SELECT
4747
SPLIT_PART(query, '.', 2) AS relname,
48-
EXTRACT(xact_start) AS timestamp_seconds
48+
EXTRACT(EPOCH FROM xact_start) AS timestamp_seconds
4949
FROM
5050
pg_catalog.pg_stat_activity
5151
WHERE

collector/pg_stat_user_tables.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ var (
150150
[]string{"datname", "schemaname", "relname"},
151151
prometheus.Labels{},
152152
)
153+
statUserTablesTotalSize = prometheus.NewDesc(
154+
prometheus.BuildFQName(namespace, userTableSubsystem, "size_bytes"),
155+
"Total disk space used by this table, in bytes, including all indexes and TOAST data",
156+
[]string{"datname", "schemaname", "relname"},
157+
prometheus.Labels{},
158+
)
153159

154160
statUserTablesQuery = `SELECT
155161
current_database() datname,
@@ -173,7 +179,8 @@ var (
173179
vacuum_count,
174180
autovacuum_count,
175181
analyze_count,
176-
autoanalyze_count
182+
autoanalyze_count,
183+
pg_total_relation_size(relid) as total_size
177184
FROM
178185
pg_stat_user_tables`
179186
)
@@ -191,10 +198,10 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
191198
for rows.Next() {
192199
var datname, schemaname, relname sql.NullString
193200
var seqScan, seqTupRead, idxScan, idxTupFetch, nTupIns, nTupUpd, nTupDel, nTupHotUpd, nLiveTup, nDeadTup,
194-
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount sql.NullInt64
201+
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, totalSize sql.NullInt64
195202
var lastVacuum, lastAutovacuum, lastAnalyze, lastAutoanalyze sql.NullTime
196203

197-
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount); err != nil {
204+
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount, &totalSize); err != nil {
198205
return err
199206
}
200207

@@ -419,6 +426,17 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
419426
autoanalyzeCountMetric,
420427
datnameLabel, schemanameLabel, relnameLabel,
421428
)
429+
430+
totalSizeMetric := 0.0
431+
if totalSize.Valid {
432+
totalSizeMetric = float64(totalSize.Int64)
433+
}
434+
ch <- prometheus.MustNewConstMetric(
435+
statUserTablesTotalSize,
436+
prometheus.GaugeValue,
437+
totalSizeMetric,
438+
datnameLabel, schemanameLabel, relnameLabel,
439+
)
422440
}
423441

424442
if err := rows.Err(); err != nil {

collector/pg_stat_user_tables_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
7171
"vacuum_count",
7272
"autovacuum_count",
7373
"analyze_count",
74-
"autoanalyze_count"}
74+
"autoanalyze_count",
75+
"total_size"}
7576
rows := sqlmock.NewRows(columns).
7677
AddRow("postgres",
7778
"public",
@@ -94,7 +95,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
9495
11,
9596
12,
9697
13,
97-
14)
98+
14,
99+
15)
98100
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
99101
ch := make(chan prometheus.Metric)
100102
go func() {
@@ -170,7 +172,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
170172
"vacuum_count",
171173
"autovacuum_count",
172174
"analyze_count",
173-
"autoanalyze_count"}
175+
"autoanalyze_count",
176+
"total_size"}
174177
rows := sqlmock.NewRows(columns).
175178
AddRow("postgres",
176179
nil,
@@ -193,6 +196,7 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
193196
nil,
194197
nil,
195198
nil,
199+
nil,
196200
nil)
197201
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
198202
ch := make(chan prometheus.Metric)

collector/pg_stat_walreceiver.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ var (
107107
trim(both '''' from substring(conninfo from 'host=([^ ]*)')) as upstream_host,
108108
slot_name,
109109
status,
110-
(receive_start_lsn- '0/0') % (2^52)::bigint as receive_start_lsn,
110+
(receive_start_lsn- '0/0') %% (2^52)::bigint as receive_start_lsn,
111111
%s
112112
receive_start_tli,
113113
received_tli,
114114
extract(epoch from last_msg_send_time) as last_msg_send_time,
115115
extract(epoch from last_msg_receipt_time) as last_msg_receipt_time,
116-
(latest_end_lsn - '0/0') % (2^52)::bigint as latest_end_lsn,
116+
(latest_end_lsn - '0/0') %% (2^52)::bigint as latest_end_lsn,
117117
extract(epoch from latest_end_time) as latest_end_time,
118118
substring(slot_name from 'repmgr_slot_([0-9]*)') as upstream_node
119119
FROM pg_catalog.pg_stat_wal_receiver
@@ -127,14 +127,16 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
127127
return err
128128
}
129129

130-
defer hasFlushedLSNRows.Close()
131130
hasFlushedLSN := hasFlushedLSNRows.Next()
132131
var query string
133132
if hasFlushedLSN {
134133
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "(flushed_lsn - '0/0') % (2^52)::bigint as flushed_lsn,\n")
135134
} else {
136135
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
137136
}
137+
138+
hasFlushedLSNRows.Close()
139+
138140
rows, err := db.QueryContext(ctx, query)
139141
if err != nil {
140142
return err

collector/pg_xlog_location.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package collector
1616
import (
1717
"context"
1818

19+
"github.com/blang/semver/v4"
1920
"github.com/go-kit/log"
21+
"github.com/go-kit/log/level"
2022
"github.com/prometheus/client_golang/prometheus"
2123
)
2224

@@ -50,8 +52,17 @@ var (
5052
`
5153
)
5254

53-
func (PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
55+
func (c PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
5456
db := instance.getDB()
57+
58+
// xlog was renmaed to WAL in PostgreSQL 10
59+
// https://wiki.postgresql.org/wiki/New_in_postgres_10#Renaming_of_.22xlog.22_to_.22wal.22_Globally_.28and_location.2Flsn.29
60+
after10 := instance.version.Compare(semver.MustParse("10.0.0"))
61+
if after10 >= 0 {
62+
level.Warn(c.log).Log("msg", "xlog_location collector is not available on PostgreSQL >= 10.0.0, skipping")
63+
return nil
64+
}
65+
5566
rows, err := db.QueryContext(ctx,
5667
xlogLocationQuery)
5768

0 commit comments

Comments
 (0)