红移中可能出现死锁

Possible deadlock in redshift

我有两个似乎互相阻塞的红移查询,所以我怀疑可能存在死锁

query1 是 ETL 管道中的查询

DROP TABLE IF EXISTS temp_table;
CREATE TABLE temp_table AS SELECT * FROM sometable;
BEGIN;
ALTER TABLE table_a RENAME TO temp_old_table;
ALTER TABLE temp_table RENAME TO table_a;
END;
DROP TABLE IF EXISTS temp_old_table;

query2 是临时查询;

select * from table_a;

query1 和 query2 运行 同时。不确定第一个查询 运行。但无论出于何种原因,这两个查询都会卡住。 这是 pg_locks 中的锁定情况: query2 在 table_a 上具有 AccessShareLock,被授予 true query1 正在等待 table_a 上的 AccessExclusiveLock,被授予 false

由于 query2 已经有 AccessShareLock,它应该可以继续前进,query1 也应该完成。

看起来可疑的地方是query1不是单个事务。它可能会尝试多次获取锁,而 query2 可能会在其间获取锁。这两个查询之间是否有任何可能发生死锁的情况?

来自How to Prevent Locks from Blocking Queries in Amazon Redshift

AccessShareLock: Acquired during UNLOAD, SELECT, UPDATE, or DELETE operations. AccessShareLock blocks only AccessExclusiveLock attempts. AccessShareLock doesn't block other sessions that are trying to read or write on the table.

When a table acquires a lock, the lock remains until you finish the transaction with COMMIT or ROLLBACK. Transactions that are waiting for locks can block subsequent transactions that are also waiting to acquire the same locks. This can lead to lock enqueuing on the cluster.

奇怪的是SELECT会导致阻塞

如果您使用的是 SQL 客户端,请打开自动提交。