Skip to content

Commit 6edf774

Browse files
committed
feat: query_plan is available whether a current transaction exists or not
1 parent 7b11136 commit 6edf774

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/appmap/handler/rails/sql_handler.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ class << self
4545
def examine(payload, sql:)
4646
return unless (examiner = build_examiner)
4747

48-
if AppMap.explain_queries? && examiner.in_transaction? && examiner.database_type == :postgres
48+
in_transaction = examiner.in_transaction?
49+
50+
if AppMap.explain_queries? && examiner.database_type == :postgres
4951
if sql =~ /\A(SELECT|INSERT|UPDATE|DELETE|WITH)/i
5052
savepoint_established = \
5153
begin
52-
examiner.execute_query 'SAVEPOINT appmap_sql_examiner'
54+
tx_query = in_transaction ? 'SAVEPOINT appmap_sql_examiner' : 'BEGIN TRANSACTION'
55+
examiner.execute_query tx_query
5356
true
5457
rescue
5558
# Probably: Sequel::DatabaseError: PG::InFailedSqlTransaction
56-
byebug
5759
warn $!
5860
false
5961
end
@@ -62,13 +64,12 @@ def examine(payload, sql:)
6264
plan = nil
6365
begin
6466
plan = examiner.execute_query(%(EXPLAIN #{sql}))
65-
rescue
66-
warn "Exception occurred explaining query: #{$!}"
67-
examiner.execute_query 'ROLLBACK TO SAVEPOINT appmap_sql_examiner'
68-
end
69-
if plan
7067
payload[:query_plan] = plan.map { |line| line[:'QUERY PLAN'] }.join("\n")
71-
examiner.execute_query 'RELEASE SAVEPOINT appmap_sql_examiner'
68+
rescue
69+
warn "(appmap) Error explaining query: #{$!}"
70+
ensure
71+
tx_query = in_transaction ? 'ROLLBACK TO SAVEPOINT appmap_sql_examiner' : 'ROLLBACK'
72+
examiner.execute_query tx_query
7273
end
7374
end
7475
end

0 commit comments

Comments
 (0)