diff --git a/NEWS.md b/NEWS.md index 17709b83d9..4b3c655365 100644 --- a/NEWS.md +++ b/NEWS.md @@ -97,6 +97,7 @@ Development - Fix error while rolling back a user migration from one cloud to another [#16421](https://github.com/CartoDB/cartodb/pull/16421) - Add retry if a timeout is thrown when swapping the tables related with a sync process [#16430](https://github.com/CartoDB/cartodb/pull/16430) - Add AUTODETECT_SIZE_LIMIT to ogr2ogr process when guessing CSV file column types [#16431](https://github.com/CartoDB/cartodb/pull/16431) +- Log pg locks if there is any problem during a sync table import process [#16432](https://github.com/CartoDB/cartodb/pull/16432) 4.45.0 (2021-04-14) ------------------- diff --git a/app/models/synchronization/adapter.rb b/app/models/synchronization/adapter.rb index b71305d580..ccc523d3ad 100644 --- a/app/models/synchronization/adapter.rb +++ b/app/models/synchronization/adapter.rb @@ -11,7 +11,7 @@ module CartoDB THE_GEOM = 'the_geom'.freeze OVERWRITE_ERROR = 2013 - def initialize(table_name, runner, database, user, overviews_creator, synchronization_id) + def initialize(table_name, runner, database, user, overviews_creator, synchronization_id, logger = nil) @table_name = table_name @runner = runner @database = database @@ -25,6 +25,7 @@ module CartoDB ) @error_code = nil @synchronization_id = synchronization_id + @logger = logger end def run(&tracker) @@ -393,6 +394,18 @@ module CartoDB end rescue Exception => exception if exception.message.include?('canceling statement due to statement timeout') + # Check if the table has any lock + lock = user.in_database(as: :superuser).fetch(%Q{ + SELECT pid, state, usename, query, query_start + FROM pg_stat_activity + WHERE pid in ( + SELECT pid FROM pg_locks l + JOIN pg_class t ON l.relation = t.oid + AND t.relkind = 'r' + WHERE t.relname IN ('#{table_name}') + ); + }).first + @logger.append_and_store "Transaction timed out as the table is blocked by query #{lock[:query]}. Retrying in 60 seconds..." if @logger && lock.present? sleep(60) # wait 60 seconds and retry the swap database.transaction do rename(table_name, temporary_name) if exists?(table_name) diff --git a/app/models/synchronization/member.rb b/app/models/synchronization/member.rb index a5cd0a8d10..e0a8bc7e89 100644 --- a/app/models/synchronization/member.rb +++ b/app/models/synchronization/member.rb @@ -192,7 +192,7 @@ module CartoDB database = user.in_database overviews_creator = CartoDB::Importer2::Overviews.new(runner, user) - importer = CartoDB::Synchronization::Adapter.new(name, runner, database, user, overviews_creator, id) + importer = CartoDB::Synchronization::Adapter.new(name, runner, database, user, overviews_creator, id, @log) importer.run self.ran_at = Time.now