Module: PgHero::Methods::Explain
- Included in:
- Database
- Defined in:
- lib/pghero/methods/explain.rb
Instance Method Summary collapse
-
#explain(sql) ⇒ Object
TODO remove in 4.0 note: this method is not affected by the explain option.
-
#explain_v2(sql, analyze: nil, verbose: nil, costs: nil, settings: nil, generic_plan: nil, buffers: nil, wal: nil, timing: nil, summary: nil, format: "text") ⇒ Object
TODO rename to explain in 4.0 note: this method is not affected by the explain option.
Instance Method Details
#explain(sql) ⇒ Object
TODO remove in 4.0 note: this method is not affected by the explain option
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pghero/methods/explain.rb', line 6 def explain(sql) sql = squish(sql) explanation = nil # use transaction for safety with_transaction(statement_timeout: (explain_timeout_sec * 1000).round, rollback: true) do if (sql.delete_suffix(";").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe? raise ActiveRecord::StatementInvalid, "Unsafe statement" end explanation = execute("EXPLAIN #{sql}").map { |v| v["QUERY PLAN"] }.join("\n") end explanation end |
#explain_v2(sql, analyze: nil, verbose: nil, costs: nil, settings: nil, generic_plan: nil, buffers: nil, wal: nil, timing: nil, summary: nil, format: "text") ⇒ Object
TODO rename to explain in 4.0 note: this method is not affected by the explain option
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pghero/methods/explain.rb', line 23 def explain_v2(sql, analyze: nil, verbose: nil, costs: nil, settings: nil, generic_plan: nil, buffers: nil, wal: nil, timing: nil, summary: nil, format: "text") = [] add_explain_option(, "ANALYZE", analyze) add_explain_option(, "VERBOSE", verbose) add_explain_option(, "SETTINGS", settings) add_explain_option(, "GENERIC_PLAN", generic_plan) add_explain_option(, "COSTS", costs) add_explain_option(, "BUFFERS", buffers) add_explain_option(, "WAL", wal) add_explain_option(, "TIMING", timing) add_explain_option(, "SUMMARY", summary) << "FORMAT #{explain_format(format)}" explain("(#{.join(", ")}) #{sql}") end |