Skip to content

Commit ef9b676

Browse files
committed
More changes for extension schema name.
1 parent 975c98a commit ef9b676

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

collector/collect.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,19 +1992,19 @@ func (c *collector) getStatements(currdb string) {
19921992
// Collect based on pss version, not pg version. This allows for cases when
19931993
// postgres is upgraded, but not the extension.
19941994
if semver.Compare(version, "v1.11") >= 0 { // pg v17
1995-
c.getStatementsv111(currdb, schema)
1995+
c.getStatementsv111(schema)
19961996
} else if semver.Compare(version, "v1.10") >= 0 { // pg v15, pg v16
1997-
c.getStatementsv110(currdb, schema)
1997+
c.getStatementsv110(schema)
19981998
} else if semver.Compare(version, "v1.9") >= 0 { // pg v14
1999-
c.getStatementsv19(currdb, schema)
1999+
c.getStatementsv19(schema)
20002000
} else if semver.Compare(version, "v1.8") >= 0 { // pg v13
2001-
c.getStatementsv18(currdb, schema)
2001+
c.getStatementsv18(schema)
20022002
} else {
2003-
c.getStatementsPrev18(currdb, schema)
2003+
c.getStatementsPrev18(schema)
20042004
}
20052005
}
20062006

2007-
func (c *collector) getStatementsv111(currdb string, schema string) {
2007+
func (c *collector) getStatementsv111(schema string) {
20082008
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
20092009
defer cancel()
20102010

@@ -2022,10 +2022,11 @@ func (c *collector) getStatementsv111(currdb string, schema string) {
20222022
jit_emission_count, jit_emission_time, local_blk_read_time,
20232023
local_blk_write_time, jit_deform_count, jit_deform_time,
20242024
stats_since, minmax_stats_since
2025-
FROM %s.pg_stat_statements
2025+
FROM @schema@.pg_stat_statements
20262026
ORDER BY total_exec_time DESC
20272027
LIMIT $2`
2028-
rows, err := c.db.QueryContext(ctx, fmt.Sprintf(q, schema), c.sqlLength, c.stmtsLimit)
2028+
q = strings.Replace(q, "@schema@", schema, -1)
2029+
rows, err := c.db.QueryContext(ctx, q, c.sqlLength, c.stmtsLimit)
20292030
if err != nil {
20302031
log.Printf("warning: pg_stat_statements query failed: %v", err)
20312032
return
@@ -2074,7 +2075,7 @@ func (c *collector) getStatementsv111(currdb string, schema string) {
20742075
}
20752076
}
20762077

2077-
func (c *collector) getStatementsv110(currdb string, schema string) {
2078+
func (c *collector) getStatementsv110(schema string) {
20782079
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
20792080
defer cancel()
20802081

@@ -2090,10 +2091,11 @@ func (c *collector) getStatementsv110(currdb string, schema string) {
20902091
jit_functions, jit_generation_time, jit_inlining_count,
20912092
jit_inlining_time, jit_optimization_count, jit_optimization_time,
20922093
jit_emission_count, jit_emission_time
2093-
FROM %s.pg_stat_statements
2094+
FROM @schema@.pg_stat_statements
20942095
ORDER BY total_exec_time DESC
20952096
LIMIT $2`
2096-
rows, err := c.db.QueryContext(ctx, fmt.Sprintf(q, schema), c.sqlLength, c.stmtsLimit)
2097+
q = strings.Replace(q, "@schema@", schema, -1)
2098+
rows, err := c.db.QueryContext(ctx, q, c.sqlLength, c.stmtsLimit)
20972099
if err != nil {
20982100
log.Printf("warning: pg_stat_statements query failed: %v", err)
20992101
return
@@ -2135,7 +2137,7 @@ func (c *collector) getStatementsv110(currdb string, schema string) {
21352137
}
21362138
}
21372139

2138-
func (c *collector) getStatementsv19(currdb string, schema string) {
2140+
func (c *collector) getStatementsv19(schema string) {
21392141
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
21402142
defer cancel()
21412143

@@ -2148,10 +2150,11 @@ func (c *collector) getStatementsv19(currdb string, schema string) {
21482150
plans, total_plan_time, min_plan_time, max_plan_time,
21492151
stddev_plan_time, wal_records, wal_fpi, wal_bytes::bigint,
21502152
toplevel
2151-
FROM %s.pg_stat_statements
2153+
FROM @schema@.pg_stat_statements
21522154
ORDER BY total_exec_time DESC
21532155
LIMIT $2`
2154-
rows, err := c.db.QueryContext(ctx, fmt.Sprintf(q, schema), c.sqlLength, c.stmtsLimit)
2156+
q = strings.Replace(q, "@schema@", schema, -1)
2157+
rows, err := c.db.QueryContext(ctx, q, c.sqlLength, c.stmtsLimit)
21552158
if err != nil {
21562159
log.Printf("warning: pg_stat_statements query failed: %v", err)
21572160
return
@@ -2189,7 +2192,7 @@ func (c *collector) getStatementsv19(currdb string, schema string) {
21892192
}
21902193
}
21912194

2192-
func (c *collector) getStatementsv18(currdb string, schema string) {
2195+
func (c *collector) getStatementsv18(schema string) {
21932196
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
21942197
defer cancel()
21952198

@@ -2201,10 +2204,11 @@ func (c *collector) getStatementsv18(currdb string, schema string) {
22012204
temp_blks_written, blk_read_time, blk_write_time,
22022205
plans, total_plan_time, min_plan_time, max_plan_time,
22032206
stddev_plan_time, wal_records, wal_fpi, wal_bytes::bigint
2204-
FROM %s.pg_stat_statements
2207+
FROM @schema@.pg_stat_statements
22052208
ORDER BY total_exec_time DESC
22062209
LIMIT $2`
2207-
rows, err := c.db.QueryContext(ctx, fmt.Sprintf(q, schema), c.sqlLength, c.stmtsLimit)
2210+
q = strings.Replace(q, "@schema@", schema, -1)
2211+
rows, err := c.db.QueryContext(ctx, q, c.sqlLength, c.stmtsLimit)
22082212
if err != nil {
22092213
log.Printf("warning: pg_stat_statements query failed: %v", err)
22102214
return
@@ -2242,7 +2246,7 @@ func (c *collector) getStatementsv18(currdb string, schema string) {
22422246
}
22432247
}
22442248

2245-
func (c *collector) getStatementsPrev18(currdb string, schema string) {
2249+
func (c *collector) getStatementsPrev18(schema string) {
22462250
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
22472251
defer cancel()
22482252

@@ -2252,9 +2256,10 @@ func (c *collector) getStatementsPrev18(currdb string, schema string) {
22522256
local_blks_hit, local_blks_read, local_blks_dirtied,
22532257
local_blks_written, temp_blks_read, temp_blks_written,
22542258
blk_read_time, blk_write_time
2255-
FROM %s.pg_stat_statements
2259+
FROM @schema@.pg_stat_statements
22562260
ORDER BY total_time DESC
22572261
LIMIT $2`
2262+
q = strings.Replace(q, "@schema@", schema, -1)
22582263
rows, err := c.db.QueryContext(ctx, fmt.Sprintf(q, schema), c.sqlLength, c.stmtsLimit)
22592264
if err != nil {
22602265
// If we get an error about "min_time" we probably have an old (v1.2)

model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package pgmetrics
1919
// ModelSchemaVersion is the schema version of the "Model" data structure
2020
// defined below. It is in the "semver" notation. Version history:
2121
//
22+
// 1.18 - Add schema name for extensions
2223
// 1.17 - Raw log entries, Postgres 17 support
2324
// 1.16 - Postgres 16 support
2425
// 1.15 - Pgpool ReplicationDelaySeconds
@@ -38,7 +39,7 @@ package pgmetrics
3839
// 1.2 - more table and index attributes
3940
// 1.1 - added NotificationQueueUsage and Statements
4041
// 1.0 - initial release
41-
const ModelSchemaVersion = "1.17"
42+
const ModelSchemaVersion = "1.18"
4243

4344
// Model contains the entire information collected by a single run of
4445
// pgmetrics. It can be converted to and from json without loss of
@@ -524,10 +525,11 @@ type VacuumProgressBackend struct {
524525
type Extension struct {
525526
Name string `json:"name"`
526527
DBName string `json:"db_name"`
527-
SchemaName string `json:"schema_name"`
528528
DefaultVersion string `json:"default_version"`
529529
InstalledVersion string `json:"installed_version"`
530530
Comment string `json:"comment"`
531+
// following fields present only in schema 1.18 and later
532+
SchemaName string `json:"schema_name"`
531533
}
532534

533535
type Setting struct {

0 commit comments

Comments
 (0)