@@ -24,11 +24,15 @@ import (
24
24
"github.com/prometheus/client_golang/prometheus"
25
25
)
26
26
27
- const statStatementsSubsystem = "stat_statements"
27
+ const (
28
+ statStatementsSubsystem = "stat_statements"
29
+ defaultStatementLimit = "100"
30
+ )
28
31
29
32
var (
30
33
includeQueryFlag * bool = nil
31
34
statementLengthFlag * uint = nil
35
+ statementLimitFlag * uint = nil
32
36
)
33
37
34
38
func init () {
@@ -47,19 +51,26 @@ func init() {
47
51
"Maximum length of the statement text." ).
48
52
Default ("120" ).
49
53
Uint ()
54
+ statementLimitFlag = kingpin .Flag (
55
+ fmt .Sprint (collectorFlagPrefix , statStatementsSubsystem , ".limit" ),
56
+ "Maximum number of statements to return." ).
57
+ Default (defaultStatementLimit ).
58
+ Uint ()
50
59
}
51
60
52
61
type PGStatStatementsCollector struct {
53
62
log * slog.Logger
54
63
includeQueryStatement bool
55
64
statementLength uint
65
+ statementLimit uint
56
66
}
57
67
58
68
func NewPGStatStatementsCollector (config collectorConfig ) (Collector , error ) {
59
69
return & PGStatStatementsCollector {
60
70
log : config .logger ,
61
71
includeQueryStatement : * includeQueryFlag ,
62
72
statementLength : * statementLengthFlag ,
73
+ statementLimit : * statementLimitFlag ,
63
74
}, nil
64
75
}
65
76
@@ -126,9 +137,9 @@ const (
126
137
FROM pg_stat_statements
127
138
)
128
139
ORDER BY seconds_total DESC
129
- LIMIT 100 ;`
140
+ LIMIT %s ;`
130
141
131
- pgStatStatementsNewQuery = `SELECT
142
+ pgStatStatementsQuery_PG13 = `SELECT
132
143
pg_get_userbyid(userid) as user,
133
144
pg_database.datname,
134
145
pg_stat_statements.queryid,
@@ -148,7 +159,7 @@ const (
148
159
FROM pg_stat_statements
149
160
)
150
161
ORDER BY seconds_total DESC
151
- LIMIT 100 ;`
162
+ LIMIT %s ;`
152
163
153
164
pgStatStatementsQuery_PG17 = `SELECT
154
165
pg_get_userbyid(userid) as user,
@@ -170,7 +181,7 @@ const (
170
181
FROM pg_stat_statements
171
182
)
172
183
ORDER BY seconds_total DESC
173
- LIMIT 100 ;`
184
+ LIMIT %s ;`
174
185
)
175
186
176
187
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
179
190
case instance .version .GE (semver .MustParse ("17.0.0" )):
180
191
queryTemplate = pgStatStatementsQuery_PG17
181
192
case instance .version .GE (semver .MustParse ("13.0.0" )):
182
- queryTemplate = pgStatStatementsNewQuery
193
+ queryTemplate = pgStatStatementsQuery_PG13
183
194
default :
184
195
queryTemplate = pgStatStatementsQuery
185
196
}
186
- var querySelect = ""
197
+ querySelect : = ""
187
198
if c .includeQueryStatement {
188
199
querySelect = fmt .Sprintf (pgStatStatementQuerySelect , c .statementLength )
189
200
}
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 )
191
206
192
207
db := instance .getDB ()
193
208
rows , err := db .QueryContext (ctx , query )
194
209
195
- var presentQueryIds = make (map [string ]struct {})
210
+ presentQueryIds : = make (map [string ]struct {})
196
211
197
212
if err != nil {
198
213
return err
0 commit comments