在执行UPDATE查询时,我们收到以下错误消息:
原文链接:https://www.f2er.com/postgresql/192114.htmlERROR: duplicate key value violates unique constraint "tableA_pkey" DETAIL: Key (id)=(47470) already exists.
但是,我们的UPDATE查询不会影响主键.这是一个简化版本:
UPDATE tableA AS a SET items = ( SELECT array_to_string( array( SELECT b.value FROM tableB b WHERE b.a_id = b.id GROUP BY b.name ),',' ) ) WHERE a.end_at BETWEEN now() AND now() - interval '1 day';
我们确保主键序列已经同步:
\d tableA_id_seq
哪个产生:
Column | Type | Value ---------------+---------+-------------------------- sequence_name | name | tableA_id_seq last_value | bigint | 50364 start_value | bigint | 1 increment_by | bigint | 1 max_value | bigint | 9223372036854775807 min_value | bigint | 1 cache_value | bigint | 1 log_cnt | bigint | 0 is_cycled | boolean | f is_called | boolean | t
寻找最大表索引:
select max(id) from tableA;
我们的价值较低:
max ------- 50363 (1 row)
你有没有想过为什么会这样的行为?如果我们排除有问题的ID,它就可以了.
另一个奇怪的地方是将以前的UPDATE替换为:
UPDATE tableA AS a SET items = ( SELECT array_to_string( array( SELECT b.value FROM tableB b WHERE b.a_id = b.id GROUP BY b.name ),' ) ) WHERE a.id = 47470;
它运作良好.我们错过了什么吗?
编辑:触发器
我在这个表上没有用户定义的触发器:
SELECT t.tgname,c.relname FROM pg_trigger t JOIN pg_class c ON t.tgrelid = c.oid WHERE c.relname = 'tableA' AND t.tgisinternal = false ;
哪个不返回任何行.