Skip to content

Commit 6f63789

Browse files
committed
feat: allow setting limit in pg_stat_statements
Add `.limit` CLI flag to `stat_statements` collector to allow setting a custom number of queries to be returned. Signed-off-by: Cristian Greco <cristian@regolo.cc>
1 parent a95b518 commit 6f63789

File tree

2 files changed

+216
-25
lines changed

2 files changed

+216
-25
lines changed

collector/pg_stat_statements.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ import (
2424
"github.com/prometheus/client_golang/prometheus"
2525
)
2626

27-
const statStatementsSubsystem = "stat_statements"
27+
const (
28+
statStatementsSubsystem = "stat_statements"
29+
defaultStatementLimit = "100"
30+
)
2831

2932
var (
3033
includeQueryFlag *bool = nil
3134
statementLengthFlag *uint = nil
35+
statementLimitFlag *uint = nil
3236
)
3337

3438
func init() {
@@ -47,19 +51,26 @@ func init() {
4751
"Maximum length of the statement text.").
4852
Default("120").
4953
Uint()
54+
statementLimitFlag = kingpin.Flag(
55+
fmt.Sprint(collectorFlagPrefix, statStatementsSubsystem, ".limit"),
56+
"Maximum number of statements to return.").
57+
Default(defaultStatementLimit).
58+
Uint()
5059
}
5160

5261
type PGStatStatementsCollector struct {
5362
log *slog.Logger
5463
includeQueryStatement bool
5564
statementLength uint
65+
statementLimit uint
5666
}
5767

5868
func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) {
5969
return &PGStatStatementsCollector{
6070
log: config.logger,
6171
includeQueryStatement: *includeQueryFlag,
6272
statementLength: *statementLengthFlag,
73+
statementLimit: *statementLimitFlag,
6374
}, nil
6475
}
6576

@@ -126,9 +137,9 @@ const (
126137
FROM pg_stat_statements
127138
)
128139
ORDER BY seconds_total DESC
129-
LIMIT 100;`
140+
LIMIT %s;`
130141

131-
pgStatStatementsNewQuery = `SELECT
142+
pgStatStatementsQuery_PG13 = `SELECT
132143
pg_get_userbyid(userid) as user,
133144
pg_database.datname,
134145
pg_stat_statements.queryid,
@@ -148,7 +159,7 @@ const (
148159
FROM pg_stat_statements
149160
)
150161
ORDER BY seconds_total DESC
151-
LIMIT 100;`
162+
LIMIT %s;`
152163

153164
pgStatStatementsQuery_PG17 = `SELECT
154165
pg_get_userbyid(userid) as user,
@@ -170,7 +181,7 @@ const (
170181
FROM pg_stat_statements
171182
)
172183
ORDER BY seconds_total DESC
173-
LIMIT 100;`
184+
LIMIT %s;`
174185
)
175186

176187
func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
@@ -179,20 +190,24 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc
179190
case instance.version.GE(semver.MustParse("17.0.0")):
180191
queryTemplate = pgStatStatementsQuery_PG17
181192
case instance.version.GE(semver.MustParse("13.0.0")):
182-
queryTemplate = pgStatStatementsNewQuery
193+
queryTemplate = pgStatStatementsQuery_PG13
183194
default:
184195
queryTemplate = pgStatStatementsQuery
185196
}
186-
var querySelect = ""
197+
querySelect := ""
187198
if c.includeQueryStatement {
188199
querySelect = fmt.Sprintf(pgStatStatementQuerySelect, c.statementLength)
189200
}
190-
query := fmt.Sprintf(queryTemplate, querySelect)
201+
statementLimit := defaultStatementLimit
202+
if c.statementLimit > 0 {
203+
statementLimit = fmt.Sprintf("%d", c.statementLimit)
204+
}
205+
query := fmt.Sprintf(queryTemplate, querySelect, statementLimit)
191206

192207
db := instance.getDB()
193208
rows, err := db.QueryContext(ctx, query)
194209

195-
var presentQueryIds = make(map[string]struct{})
210+
presentQueryIds := make(map[string]struct{})
196211

197212
if err != nil {
198213
return err

0 commit comments

Comments
 (0)