• kingbase表或者索引坏块问题解决办法


    数据库查询启动报错

    报错信息   无法在文件"base/12345/123"中读取块0: No error
    
    • 1

    一、 可能是表损坏,或者是索引损坏。base后的第一个数字是数据库的oid,第二个数字是表的relfilenode值
    1.首先判断是哪个数据库中出了问题,连接任意数据库,执行:结果可以看到是哪个库出的问题。

    #12345是报错base/12345/123中的值
    根据报错知道是12345下的123出错了,查出是哪个数据库出问题
    select datname from pg_database where oid = '12345';
    
    • 1
    • 2
    • 3

    如果知道是哪个表,也可以根据表名来查询:

    select oid, relname,relfilenode from pg_class where relname='表名';
    
    或者
    
    select oid,datname from pg_database where datname='表名';
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、查询是哪个表发生了异常【123为报错的表的oid】

    select relname,relkind from pg_class where relfilenode = '123';  
    
    • 1

    如果查询结果中 relkind = r,表示损坏的是表

    比如:

    tb_test, r

    relname = tb_test:表示损坏的表是tb_test

    如果查询结果中relkind = i,表示损坏的是一个索引

    需要注意,损坏的可能是普通索引,也可能是主键或唯一键。如果索引的名称中有“_pkey”等很可能属于主键,而名称中含有 “key”则很可能属于唯一键。如果表名是“pg”开头的,则说明它是系统表。

    pgclass.relkind 的值的几种:
    r: 表示ordinary table(普通表);
    i: 表示index(索引);
    S: 表示sequence(序列);
    V: 表示view(视图);
    m: 表示materialized view(物化视图);
    c: 表示composite type(复合类型);
    t: 表示TOAST table(TOAST 表);
    f: 表示foreign table(外部表)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3、修复方法
    如果坏的是表的话

    set zero_damaged_pages = on;
    
    vacuum tb_test;
    
    reindex table tb_test;
    
    select relname ,relkind from pg_class where relfilenode='123';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果坏的是普通索引的话
    set zero_damaged_pages = on;
    reindex index 索引名;
    select relname ,relkind from pg_class where relfilenode=‘123’;

    如果损坏的是主键或唯一键,首先要找到它所在的表,以tb_1为例:

    select tablename,indexname from pg_indexes where indexname = 'tb_1';
    
    • 1

    查询结果:

    tb1, tb_1

    然后获取索引的定义:

    select pg_get_constraintdef((select oid from pg_constraint where conname = ' tb_dept_pkey '));
    
    • 1

    查询结果:PRIMARY KEY (tb_id)

    然后重新创建这个约束:

    Alter table drop constriant tb_1;
    
    Alter table add constraint tb_1 PRIMARY KEY (tb_id);
    
    • 1
    • 2
    • 3
  • 相关阅读:
    鸿蒙HarmonyOS实战-Stage模型(线程模型)
    “谈谈如何理解索引?谈谈如何理解事物?”我来带你模拟面试~
    Python自学笔记11-函数的定义和调用
    国信证券笔试题总分120分
    【C++ Primer Plus】第10章 对象和类
    springMVC的简单数据绑定
    Verilog功能模块——同步FIFO
    Mybatis plus中的逻辑删除源码跟踪
    css知识学习系列(17)-每天10个知识点
    灵性图书馆:好书推荐-《当下的力量》
  • 原文地址:https://blog.csdn.net/tomcat_lsh/article/details/126051808