我將執行所有你是指在一個事務中與重試循環的SQL。下面是我用來處理併發問題和重試(爲了簡潔起見,僞碼)的邏輯。我沒有系統無限期地等待鎖釋放。相反,我會在應用程序中通過重試來處理它。
begin transaction
while not successful and count < 5
try
execute sql
commit
except
if error code is '40P01' or '55P03'
# Deadlock or lock not available
sleep a random time (200 ms to 1 sec) * number of retries
else if error code is '40001' or '25P02'
# "In failed sql transaction" or serialized transaction failure
rollback
sleep a random time (200 ms to 1 sec) * number of retries
begin transaction
else if error message is 'There is no active transaction'
sleep a random time (200 ms to 1 sec) * number of retries
begin transaction
increment count
關鍵零部件捕捉每一個type of error,知道哪些情況下需要回滾,並且具有exponential backoff for retries。
如果我理解正確,你建議在每個sql腳本之後提交,並只回滾出錯的腳本。當出現死鎖錯誤時,是否無法確保所有sql腳本在一個事務下運行? – stech
我建議你在一個事務中運行所有的SQL(我的'execute sql'行)。如果它死鎖或失敗,則重試整個事情。您也可能對保存點感興趣,但我從未使用過它們,也不知道它是否可用於redshift:https://www.postgresql.org/docs/current/static/sql-savepoint.html –
忽略我對使用保存點的評論。回滾到保存點不紅移可供選擇:http://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-functions.html –