By digoal
Currently, Alibaba Cloud DTS does not support the migration of sequence values. When DTS is used to migrate data from a PostgreSQL database, you must synchronize the sequence values from the source database to the target database manually after the data migration is completed. Otherwise, the sequence values generated by the business that uses the sequence will return to the previous version, and UK PK generated by the sequence will also report conflicts.
For PostgreSQL 9.6 and earlier versions, the sample code for setting SQL for sequences obtained from the original database is listed below:
do language plpgsql $$
declare
nsp name;
rel name;
val int8;
begin
for nsp,rel in select nspname,relname from pg_class t2 , pg_namespace t3 where t2.relnamespace=t3.oid and t2.relkind='S'
loop
execute format($_$select last_value from %I.%I$_$, nsp, rel) into val;
raise notice '%',
format($_$select setval('%I.%I'::regclass, %s);$_$, nsp, rel, val+1);
end loop;
end;
$$;
NOTICE: select setval('public."Seq"'::regclass, 3);
NOTICE: select setval('"P12"."Seq"'::regclass, 4);
DO
For PostgreSQL 10 and later versions, the sample code for setting SQL for sequences obtained from the original database is listed below:
postgres=> select format($$
select setval('%s.%s'::regclass, %s);$$,
relnamespace::regnamespace::text,
oid::regclass::text,
coalesce(pg_sequence_last_value(oid),
(pg_sequence_parameters(oid)).start_value) + 1)
from pg_class where relkind='S';
format
---------------------------------------------
select setval('public.seq'::regclass, 103);
select setval('public."Seq"'::regclass, 3);
(2 rows)
Run the following commands in the target database:
select setval('public.seq'::regclass, 103);
select setval('public."Seq"'::regclass, 3);
Condition Index (Partial Index) in PostgreSQL and Use of Certain Indexes in Bypass
Alibaba Cloud Community - January 9, 2024
Apache Flink Community China - May 13, 2021
digoal - August 20, 2021
ApsaraDB - January 3, 2024
digoal - April 21, 2021
ApsaraDB - July 13, 2023
Migrate your legacy Oracle databases to Alibaba Cloud to save on long-term costs and take advantage of improved scalability, reliability, robust security, high performance, and cloud-native features.
Learn MoreMigrating to fully managed cloud databases brings a host of benefits including scalability, reliability, and cost efficiency.
Learn MoreSecure and easy solutions for moving you workloads to the cloud
Learn MoreAlibaba Cloud PolarDB for PostgreSQL is an in-house relational database service 100% compatible with PostgreSQL and highly compatible with the Oracle syntax.
Learn MoreMore Posts by digoal