如何批量更新所有表的序列 ID postgreSQL

Muh*_*kur 2 sql postgresql database-sequence

我使用 TablePlus(SQL 客户端)将 Postgres SQL 文件导入到我的服务器,但在插入新行后,出现如下错误:

SQLSTATE[23505]:唯一违规:7 错误:重复键值违反唯一约束 \"users_pkey\" 详细信息:键 (id)=(1) 已经存在

我知道它是由序列值为 0 引起的,需要通过以下代码更新:

SELECT setval(_sequence_name_, max(id)) FROM _table_name_;
Run Code Online (Sandbox Code Playgroud)

但是如果我必须一一写入所有表序列(可能是数百个序列),则需要很多时间。那么如何一次更新所有序列呢?

a_h*_*ame 5

假设所有使用的序列都由各自的列拥有,例如通过一个serialidentity属性,您可以使用它来重置当前数据库中的所有(拥有的)序列。

with sequences as (
  select *
  from (
    select table_schema,
           table_name,
           column_name,
           pg_get_serial_sequence(format('%I.%I', table_schema, table_name), column_name) as col_sequence
    from information_schema.columns
    where table_schema not in ('pg_catalog', 'information_schema')
  ) t
  where col_sequence is not null
), maxvals as (
  select table_schema, table_name, column_name, col_sequence,
          (xpath('/row/max/text()',
             query_to_xml(format('select max(%I) from %I.%I', column_name, table_schema, table_name), true, true, ''))
          )[1]::text::bigint as max_val
  from sequences
) 
select table_schema, 
       table_name, 
       column_name, 
       col_sequence,
       coalesce(max_val, 0) as max_val,
       setval(col_sequence, coalesce(max_val, 1)) --<< this will change the sequence
from maxvals;
Run Code Online (Sandbox Code Playgroud)

第一部分选择列拥有的所有序列。然后第二部分query_to_xml()用于获取与该序列关联的列的最大值。然后最后的 SELECT 将该最大值应用于每个序列,使用setval().

您可能希望在没有setval()调用的情况下运行它,以查看是否一切都符合您的需要。