【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案

【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案

问题1:序列值不自动刷新问题

下面SQL只针对单库操作以及每个序列只绑定一张表的情况

复制代码
-- 自动生成的序列,设置序列值
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 auto_seq, --<<自动
                                (string_to_array(column_default, E'\''))[2]             as col_seq   --<<手动
                         from information_schema.columns
                         where table_schema not in ('pg_catalog', 'information_schema')
                           and column_default ILIKE 'nextval(%'
                           and table_schema = 'usc') t
--                    where auto_seq is not null
                   ),
     maxvals as (select table_schema,
                        table_name,
                        column_name,
                        auto_seq,
                        col_seq,
                        (xpath('/row/max/text()',
                               query_to_xml(format('select max(%I) as max from %I.%I', column_name, table_schema,
                                                   table_name),
                                            true, true, ''))
                            )[1]::text::bigint as max_val,
                                         (xpath('/row/cur/text()',
                               query_to_xml(format('select last_value as cur from %I.%I',table_schema, col_seq),
                                            true, true, ''))
                            )[1]::text::bigint as cur_val
                 from sequences)
select table_schema,
       table_name,
       column_name,
       auto_seq,
       col_seq,
       cur_val,
       coalesce(max_val, 0) as max_val,
       setval(col_seq, coalesce(max_val, 1)) --<<设置序列值
from maxvals;

问题2:

字符类型为空字符串迁移后会变成null排查,需要考虑大表问题,防止慢SQL

复制代码
-- 数据库迁移出现null/空字符串 排除
with t as (select table_schema,
       table_name,
       column_name,
       (xpath('/row/cnt/text()',
              query_to_xml(format(E'select count(*) as cnt from %I.%I where %I = \'\'', table_schema,
                                  table_name, column_name),
                           true, true, ''))
           )[1]::text::bigint as null_cnt
from information_schema.columns
where table_schema not in ('pg_catalog', 'information_schema')
  and table_schema = 'usc'
  and is_nullable = 'NO'
  and udt_name in ('varchar', 'text', 'bpchar'))
select * from t where null_cnt > 0;

问题3:

时间精度问题,导致时间范围查询失败。批量调整时间字段精度

复制代码
DO
$$
    DECLARE
        r RECORD;
    BEGIN
        FOR r IN
            select table_name, column_name, udt_name
            from information_schema.columns
            where table_schema not in ('pg_catalog', 'information_schema')
              and table_schema = 'usc'
              and udt_name in ('timestamp','timestamptz')
            LOOP
                EXECUTE 'ALTER TABLE ' || r.table_name || ' ALTER COLUMN ' || r.column_name || ' TYPE ' || r.udt_name ||
                        '(0)';
            END LOOP;
    END
$$;
相关推荐
程序员岳焱7 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
梦在深巷、8 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT_10248 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
Johny_Zhao9 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
祁思妙想10 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人10 小时前
1.MySQL之如何定位慢查询
数据库·mysql
叁沐11 小时前
MySQL 04 深入浅出索引(上)
mysql
q90854470311 小时前
MySQL 二进制日志binlog解析
mysql·binlog·binlog2sql·my2sql
码不停蹄的玄黓12 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突
帧栈13 小时前
mysql基础(一)快速上手篇
mysql