红移中的句柄锁
handle locks in redshift
我有一个 python 脚本,它在 Redshift 中执行多个 sql 脚本(一个接一个)。这些 sql 脚本中的某些 table 可以多次查询。对于前。 Table t1 可以在一个脚本中选择,也可以在另一个脚本中 dropped/recreated。整个过程在一笔交易中 运行。现在,有时,我会收到检测到死锁的错误,整个事务都会回滚。如果 table 出现死锁,我想等待 table 被释放,然后重试 sql 执行。对于其他类型的错误,我想回滚事务。从文档中可以看出 table 锁直到事务结束才被释放。我想实现所有或没有数据更改(这是通过使用事务完成的),但也想处理死锁。关于如何实现这一点有什么建议吗?
我会在一个带有重试循环的事务中执行您所指的所有 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, knowing which cases require a rollback, and having an exponential backoff for retries抓一次。
我有一个 python 脚本,它在 Redshift 中执行多个 sql 脚本(一个接一个)。这些 sql 脚本中的某些 table 可以多次查询。对于前。 Table t1 可以在一个脚本中选择,也可以在另一个脚本中 dropped/recreated。整个过程在一笔交易中 运行。现在,有时,我会收到检测到死锁的错误,整个事务都会回滚。如果 table 出现死锁,我想等待 table 被释放,然后重试 sql 执行。对于其他类型的错误,我想回滚事务。从文档中可以看出 table 锁直到事务结束才被释放。我想实现所有或没有数据更改(这是通过使用事务完成的),但也想处理死锁。关于如何实现这一点有什么建议吗?
我会在一个带有重试循环的事务中执行您所指的所有 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, knowing which cases require a rollback, and having an exponential backoff for retries抓一次。