3
3
module SafePgMigrations
4
4
module BlockingActivityLogger
5
5
SELECT_BLOCKING_QUERIES_SQL = <<~SQL . squish
6
- SELECT blocking_activity.query
6
+ SELECT blocking_activity.query, blocked_activity.xact_start as start
7
7
FROM pg_catalog.pg_locks blocked_locks
8
8
JOIN pg_catalog.pg_stat_activity blocked_activity
9
9
ON blocked_activity.pid = blocked_locks.pid
@@ -50,7 +50,7 @@ def log_blocking_queries(method)
50
50
blocking_queries_retriever_thread =
51
51
Thread . new do
52
52
sleep delay_before_logging ( method )
53
- SafePgMigrations . alternate_connection . query_values ( SELECT_BLOCKING_QUERIES_SQL % raw_connection . backend_pid )
53
+ SafePgMigrations . alternate_connection . query ( SELECT_BLOCKING_QUERIES_SQL % raw_connection . backend_pid )
54
54
end
55
55
56
56
yield
@@ -75,7 +75,7 @@ def log_blocking_queries(method)
75
75
"Statement was being blocked by the following #{ 'query' . pluralize ( queries . size ) } :" , true
76
76
)
77
77
SafePgMigrations . say '' , true
78
- queries . each { |query | SafePgMigrations . say " #{ query } " , true }
78
+ queries . each { |query , start_time | SafePgMigrations . say "#{ format_start_time start_time } : #{ query } " , true }
79
79
SafePgMigrations . say (
80
80
'Beware, some of those queries might run in a transaction. In this case the locking query might be ' \
81
81
'located elsewhere in the transaction' ,
@@ -86,5 +86,10 @@ def log_blocking_queries(method)
86
86
87
87
raise
88
88
end
89
+
90
+ def format_start_time ( start_time , reference_time = Time . now )
91
+ duration = ( reference_time - start_time ) . round
92
+ "transaction started #{ duration } #{ 'second' . pluralize ( duration ) } ago"
93
+ end
89
94
end
90
95
end
0 commit comments