我正在使用带有Postgres后端的SQLAlchemy来执行批量插入或更新。为了提高性能,我尝试每千行左右只提交一次:trans=engine.begin()fori,recinenumerate(records):ifi%1000==0:trans.commit()trans=engine.begin()try:inserter.execute(...)exceptsa.exceptions.SQLError:my_table.update(...).execute()trans.commit()但是,这是行不通的。似乎当INSERT失败时,它会使事情处于一种奇怪的状态,从而阻止U