4

我有一个表“项目”,其中有一列“位置”。position 具有唯一且非空的约束。为了在位置 x 插入新行,我首先尝试增加后续项目的位置:

UPDATE items SET position = position + 1 WHERE position >= x;

这会导致违反唯一约束:

ERROR:  duplicate key value violates unique constraint

问题似乎是 PostgreSQL 执行更新的顺序。PostgreSQL < 9.0 中的唯一约束不可延迟,不幸的是,目前不能选择使用 9.0。此外,UPDATE 语句不支持 ORDER BY 子句,以下也不起作用(仍然重复键违规):

UPDATE items SET position = position + 1 WHERE id IN (
  SELECT id FROM items WHERE position >= x ORDER BY position DESC)

有人知道不涉及迭代代码中所有项目的解决方案吗?

4

4 回答 4

3

另一个表,具有多个唯一索引:

create table utest(id integer, position integer not null, unique(id, position));
test=# \d utest
      Table "public.utest"
  Column  |  Type   | Modifiers 
----------+---------+-----------
 id       | integer | 
 position | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id, "position")

一些数据:

insert into utest(id, position) select generate_series(1,3), 1;
insert into utest(id, position) select generate_series(1,3), 2;
insert into utest(id, position) select generate_series(1,3), 3;

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        2
  1 |        3
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

我创建了一个以正确顺序更新位置值的过程:

create or replace function update_positions(i integer, p integer) 
  returns void as $$
declare
  temprec record;
begin
  for temprec in 
    select * 
      from utest u 
      where id = i and position >= p 
      order by position desc 
  loop
    raise notice 'Id = [%], Moving % to %', 
      i, 
      temprec.position, 
      temprec.position+1;

    update utest 
      set position = position+1 
      where position=temprec.position and id = i;
  end loop;
end;
$$ language plpgsql;

一些测试:

test=# select * from update_positions(1, 2);
NOTICE:  Id = [1], Moving 3 to 4
NOTICE:  Id = [1], Moving 2 to 3
 update_positions 
------------------

(1 row)

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        3
  1 |        4
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

希望能帮助到你。

于 2010-10-24T10:18:49.020 回答
2


由于 PostgreSQL 支持全套事务性 DDL,您可以轻松地执行以下操作:

create table utest(id integer unique not null);
insert into utest(id) select generate_series(1,4);

该表现在看起来像这样:

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  1
  2
  3
  4
(4 rows)

现在整个魔术:

begin;
alter table utest drop constraint utest_id_key;
update utest set id = id + 1;
alter table utest add constraint utest_id_key unique(id);
commit;

之后我们有:

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  2
  3
  4
  5
(4 rows)

这个解决方案有一个缺点:它需要锁定整个表,但也许这不是问题。

于 2010-10-23T23:10:46.200 回答
0

不更改表和删除约束的变体:

UPDATE items t1 
SET    position = t2.position + 1 
FROM   (SELECT position
    FROM   items 
    ORDER  BY position DESC) t2 
WHERE t2.position >= x AND t1.position = t2.position

在线示例:http ://rextester.com/FAU54991

于 2018-09-19T08:24:08.233 回答
0

“更正”的解决方案可能是使约束DEFERRABLE

ALTER TABLE channels ADD CONSTRAINT 
channels_position_unique unique("position") 
DEFERRABLE INITIALLY IMMEDIATE

然后在递增时将该约束设置为 DEFERRED 并在完成后将其设置回 IMMEDIATE。

SET CONSTRAINTS channels_position_unique DEFERRED;
UPDATE channels SET position = position+1 
WHERE position BETWEEN 1 AND 10;
SET CONSTRAINTS channels_position_unique IMMEDIATE;
于 2016-02-11T13:39:18.977 回答