从postgres中的几条记录更新序列字段的增量

update increment of serial field from several records in postgres

我想更改所有使用顺序 ID 的列的增量值。我该怎么做?

我尝试了以下两种方法,都失败了:

UPDATE information_schema.sequences SET increment=1;

这不起作用,因为information_schema.sequences它是一个视图。

我也尝试过使用 ALTER SEQUENCE,在末尾添加一个 FROM,例如:

ALTER SEQUENCE A.sequence_name INCREMENT BY 2 FROM (SELECT * FROM information_schema.sequences) AS A

但是我得到一个语法错误。

我该怎么做?

尝试使用 docs

中的 setval

ALTER SEQUENCE... RESTART WITH... 描述 here

编辑了两次

示例如何更改拥有模式中的所有序列:

do
$$
declare
i record;
begin
for i in (select * from information_schema.sequences) loop
execute $e$ALTER SEQUENCE $e$||i.sequence_name||$e$ INCREMENT BY 2$e$;
end loop;
end;
$$
;
DO