首页 > 解决方案 > Redshift:如何修复由并发 MERGE 操作引起的可序列化隔离违规(1023)?

问题描述

我的用例是从 x 个 Lambda 函数中以增量方式实时提取、转换和加载数据。我希望多个 Lambda 函数能够同时运行,并且 Redshift 能够保持活动状态以进行读取查询。

由于 Redshift 不强制执行主键约束,因此我使用 aws 文档合并示例 - 替换现有行以强制执行唯一行的合并示例。当只有 1 个 lambda 函数实例正在运行时,此方法可以正常工作。

-- Start a new transaction
begin transaction;

-- Delete any rows from SALES that exist in STAGESALES, because they are updates
-- The join includes a redundant predicate to collocate on the distribution key 
-- A filter on saletime enables a range-restricted scan on SALES

delete from sales
using stagesales
where sales.salesid = stagesales.salesid
and sales.listid = stagesales.listid
and sales.saletime > '2008-11-30';

-- Insert all the rows from the staging table into the target table
insert into sales
select * from stagesales;

-- End transaction and commit
end transaction;

-- Drop the staging table
drop table stagesales;

但是一旦 > 1 lambda 函数同时运行并访问同一个表,我将收到:

"ERROR: 1023 DETAIL: Serializable isolation violation on table in Redshift" when performing operations in a transaction concurrently with another session. 

我应该如何修改这个示例以允许它在并发环境中运行?

标签: aws-lambdaamazon-redshift

解决方案


您遇到的问题是您有多个 lambda 函数同时在同一个表上执行 DML。Redshift 不支持不可序列化的并发事务,即尝试同时修改相同的数据。在这种情况下,Redshift 将中止一个或多个事务,以确保执行的所有 DML 都是可序列化的。

由于 Redshift 工作方式的这些限制,当扩展到多个 lambda 函数时,您当前的设计将无法正常工作。您需要设计一种管理 lambda 函数的方法,这样就不会在同一个表上同时运行有冲突的 DML 语句。目前尚不清楚您为什么要使用多个 lambda 函数来执行此操作,因此我无法评论替代方案的外观。


推荐阅读