Skip to content

Commit

Permalink
[DB-13678] Assess Migration command guardrails check for pgss extensi…
Browse files Browse the repository at this point in the history
…on properly loaded or not in source db

- doing SELECT query on pg_stat_statements view and comparing the error message
  • Loading branch information
sanyamsinghal committed Nov 28, 2024
1 parent 8731fc1 commit d09c2e6
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions yb-voyager/src/srcdb/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ const (
JOIN pg_namespace n ON e.extnamespace = n.oid
WHERE e.extname = 'pg_stat_statements'`

querySharedPreloadLibraries = `SELECT current_setting('shared_preload_libraries')`
queryCheckPgssLoaded = `SELECT query from pg_stat_statements LIMIT 1`

queryHasReadStatsPermission = `
SELECT pg_has_role(current_user, 'pg_read_all_stats', 'USAGE')`
Expand Down Expand Up @@ -1195,21 +1195,7 @@ func (pg *PostgreSQL) checkPgStatStatementsSetup() (string, error) {
}
}

// TODO: finalise the approach for detecting shared_preload_libraries
// 2. check if its properly installed/loaded
// To access "shared_preload_libraries" must be superuser or a member of pg_read_all_settings
// so trying a best effort here, if accessible then check otherwise it will fail during the gather metadata
// var sharedPreloadLibraries string
// err = pg.db.QueryRow(querySharedPreloadLibraries).Scan(&sharedPreloadLibraries)
// if err != nil {
// log.Warnf("failed to check if pg_stat_statements extension is properly loaded on source DB: %v", err)
// } else {
// if !slices.Contains(strings.Split(sharedPreloadLibraries, ","), PG_STAT_STATEMENTS) {
// return "pg_stat_statements is not loaded via shared_preload_libraries, required for detecting Unsupported Query Constructs", nil
// }
// }

// 3. User has permission to read from pg_stat_statements table
// 2. User has permission to read from pg_stat_statements table
var hasReadAllStats bool
err = pg.db.QueryRow(queryHasReadStatsPermission).Scan(&hasReadAllStats)
if err != nil {
Expand All @@ -1220,6 +1206,14 @@ func (pg *PostgreSQL) checkPgStatStatementsSetup() (string, error) {
return "User doesn't have permissions to read pg_stat_statements view, unsupported query constructs won't be detected/reported", nil
}

// To access "shared_preload_libraries" must be superuser or a member of pg_read_all_settings
// so instead of getting current_settings(), executing SELECT query on pg_stat_statements view
// 3. check if its properly installed/loaded without any extra permissions
_, err = pg.db.Exec(queryCheckPgssLoaded)
if err != nil && strings.Contains(err.Error(), "pg_stat_statements must be loaded via shared_preload_libraries") {
return "pg_stat_statements is not loaded via shared_preload_libraries, required for detecting Unsupported Query Constructs", nil
}

return "", nil
}

Expand Down

0 comments on commit d09c2e6

Please sign in to comment.