如何批量更新所有表的sequence id postgresql

jrcvhitl  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(519)

我使用tableplus(sql客户端)将postgres sql文件导入到我的服务器,但插入新行后出现如下错误:
sqlstate[23505]:唯一冲突:7错误:重复的键值违反唯一约束\“users\u pkey\”详细信息:键(id)=(1)已存在
我知道这是由序列值为0引起的,需要通过下面的代码进行更新:

SELECT setval(_sequence_name_, max(id)) FROM _table_name_;

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

6l7fqoea

6l7fqoea1#

假设所有使用的序列都属于相应的列,例如通过 serial 或者 identity 属性,可以使用此属性重置当前数据库中的所有(拥有的)序列。

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;

第一部分选择列所拥有的所有序列。第二部分接着使用 query_to_xml() 获取与该序列关联的列的最大值。最后一个select使用 setval() .
你可能想在没有 setval() 先打电话看看是否一切都如你所需。

umuewwlo

umuewwlo2#

不能同时更新所有序列,因为每个序列可能包含与每个表相关的不同值。您必须从每个表中获取最大值并更新它。

SELECT setval(_sequence_name_, max(id)) FROM _table_name_;
9udxz4iz

9udxz4iz3#

由于@a \u horse \u with \u no \u name answer在我的情况下不起作用(可能是sql文件有问题),我修改了如下在我的情况下起作用的查询。

with sequences as (
  select *
  from (
    select table_schema,
           table_name,
           column_name,
           replace(replace(replace(column_default, '::regclass)', ''), '''', ''), 'nextval(', 'public.') as col_sequence
    from information_schema.columns
    where table_schema not in ('pg_catalog', 'information_schema') and column_default ILIKE 'nextval(%'
  ) 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;

我只是换衣服 pg_get_serial_sequence(format('%I.%I', table_schema, table_name), column_name) as col_sequencereplace(replace(replace(column_default, '::regclass)', ''), '''', ''), 'nextval(', 'public.') as col_sequence .
也许我的查询不太好,我应该使用regex而不是multiple replace。但对我来说这是100%的工作。

相关问题