Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: deal with lower version pg_stat_statements (#7052)

rebelice27f63219d734
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index 3b71424045..fecf4ab649 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -254,6 +254,18 @@ func (driver *Driver) getVersion(ctx context.Context) (string, error) {
return version, nil
}
+func (driver *Driver) getPGStatStatementsVersion(ctx context.Context) (string, error) {
+ query := "select extversion from pg_extension where extname = 'pg_stat_statements'"
+ var version string
+ if err := driver.db.QueryRowContext(ctx, query).Scan(&version); err != nil {
+ if err == sql.ErrNoRows {
+ return "", common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return "", util.FormatErrorWithQuery(err, query)
+ }
+ return version, nil
+}
+
// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres
// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.
func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index de08d19b8f..4e64504aa7 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -681,13 +681,16 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, _ time.Time) (map[strin
}
result := make(map[string]*storepb.SlowQueryStatistics)
- version, err := driver.getVersion(ctx)
+ version, err := driver.getPGStatStatementsVersion(ctx)
if err != nil {
return nil, err
}
var query string
- // Postgres 13 changed the column names of pg_stat_statements.
- if version >= "130000" {
+ // pg_stat_statements version 1.8 changed the column names of pg_stat_statements.
+ // version is a string in the form of "major.minor".
+ // We need to check if the major version is greater than or equal to 1 and the minor version is greater than or equal to 8.
+ versions := strings.Split(version, ".")
+ if len(versions) == 2 && ((versions[0] == "1" && versions[1] >= "8") || versions[0] > "1") {
query = `
SELECT
pg_database.datname,