@@ -31,7 +31,11 @@ const (
31
31
32
32
// Query Explain prefixes.
33
33
queryExplain = "EXPLAIN (FORMAT TEXT) "
34
- queryExplainAnalyze = "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) "
34
+ queryExplainAnalyze = "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON %s) "
35
+ settingsExplain = ", SETTINGS TRUE"
36
+
37
+ postgresNumDiv = 10000 // Divider to get version from server_version_num.
38
+ pgVersion12 = 12 // Explain Settings are available starting with Postgres 12.
35
39
36
40
// locksTitle shows locks for a single query analyzed with EXPLAIN.
37
41
// locksTitle = "*Query heavy locks:*\n".
@@ -82,7 +86,7 @@ func Explain(ctx context.Context, msgSvc connection.Messenger, command *platform
82
86
return errors .Wrap (err , "failed to run explain without execution" )
83
87
}
84
88
85
- explainAnalyze , err := querier .DBQueryWithResponse (ctx , tx , queryExplainAnalyze + command .Query )
89
+ explainAnalyze , err := querier .DBQueryWithResponse (ctx , tx , analyzePrefix ( session . DBVersion ) + command .Query )
86
90
if err != nil {
87
91
return err
88
92
}
@@ -191,6 +195,16 @@ func Explain(ctx context.Context, msgSvc connection.Messenger, command *platform
191
195
return nil
192
196
}
193
197
198
+ func analyzePrefix (dbVersionNum int ) string {
199
+ settingsValue := ""
200
+
201
+ if (dbVersionNum / postgresNumDiv ) >= pgVersion12 {
202
+ settingsValue = settingsExplain
203
+ }
204
+
205
+ return fmt .Sprintf (queryExplainAnalyze , settingsValue )
206
+ }
207
+
194
208
func observeLocks (ctx context.Context , db * pgxpool.Pool , txPID int ) ([][]string , error ) {
195
209
observeConn , err := getConn (ctx , db )
196
210
if err != nil {
0 commit comments