@@ -31,7 +31,11 @@ const (
3131
3232 // Query Explain prefixes.
3333 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.
3539
3640 // locksTitle shows locks for a single query analyzed with EXPLAIN.
3741 // locksTitle = "*Query heavy locks:*\n".
@@ -82,7 +86,7 @@ func Explain(ctx context.Context, msgSvc connection.Messenger, command *platform
8286 return errors .Wrap (err , "failed to run explain without execution" )
8387 }
8488
85- explainAnalyze , err := querier .DBQueryWithResponse (ctx , tx , queryExplainAnalyze + command .Query )
89+ explainAnalyze , err := querier .DBQueryWithResponse (ctx , tx , analyzePrefix ( session . DBVersion ) + command .Query )
8690 if err != nil {
8791 return err
8892 }
@@ -191,6 +195,16 @@ func Explain(ctx context.Context, msgSvc connection.Messenger, command *platform
191195 return nil
192196}
193197
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+
194208func observeLocks (ctx context.Context , db * pgxpool.Pool , txPID int ) ([][]string , error ) {
195209 observeConn , err := getConn (ctx , db )
196210 if err != nil {
0 commit comments