1. 问题描述

增加一个系统表,记录初始化db的时间,用户

  1. 创建系统表ux_record_initdb

2.1 选择oid

新增表结构中的oid要使用,表结构中未使用的oid,可以通过./uxdb-ng/uxdb-2.1/src/include/catalog目录下的unused_oids脚本查找出未使用的oid编码,在执行unused_oids的时候需要通过 chmod +x unused_oids 命令加上可执行权限,选择新增表的oid编码为2023,如下图

postgres数据库在代码中新增系统表

2.2 在uxdb-ng/uxdb-2.1/src/backend/catalog 的makefile 中添加相应的系统表头文件postgres数据库在代码中新增系统表

2.3 在include的 catalog目录下添加这张表的自定义

postgres数据库在代码中新增系统表

2.4 在catalog 的indexing.h 头文件中添加系统表的唯一性索引

索引id为空闲的oid

postgres数据库在代码中新增系统表

2.5 在syscache.h 的SysCacheIdentifier 中添加一个枚举值

修改SysCacheSize为INITDBOID + 1

postgres数据库在代码中新增系统表

2.6 在uxdb-ng/uxdb-2.1/src/backend/utils/cache 目录下的syscache.c 中cacheinfo数组中添加这张表的信息,并添加对应的头文件 #include "catalog/ux_record_initdb.h"

postgres数据库在代码中新增系统表

  1. RecordInitdbRelationId 为要缓存的表的oid
  2. RecordInitdnIndexId 要缓存的表上对应的唯一的索引oid
  3. 1为前面的索引的索引键个数
  4. Anum_ux_user_mapping_oid为对应的索引键为表中哪列,最多4个
  5. 2为hash桶个数,如果要缓存的元组多,不妨把这个桶个数设大点,提高效率

注意:这里在cacheinfo中新加的元素位置必须和SysCacheIdentifier中加的枚举位置相同,因为使用时就是以枚举作为数组下标引用

  1. 向系统表中插入数据

系统表的创建是在initdb中bootstrap_template1函数读取uxdb.bki中系统表创建信息进行创建的。

setup_initdb_time函数是向ux_record_initdb中插入数据的代码实现

oid的值为2.1中查询出来的未使用的oid值

postgres数据库在代码中新增系统表

 

在initialize_data_directory中调用 setup_initdb_time函数将插入的数据加载到post-bootstrap

  1. 验证结果
    1. 执行./initdb -W -D uxdblocal命令初始化实例
    2. 执行./ux_ctl -D uxdblocal -l logfile start启动uxdb数据库
    3. 执行./uxsql -d uxdb 启动uxsql

执行 select * from ux_tables where tablename =  ‘ux_record_initdb’;查询xu_record_initdb是否创建成功

执行 select * from ux_record_initdb;查询系统表中的数据信息,查询结果见下图

postgres数据库在代码中新增系统表

  1. 遇到的问题

图中的问题是向ux_record_initdb中插入数据的时候遇到的语法问题

原因:在出入数据的时候 数据值要放在单引号中

postgres数据库在代码中新增系统表

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2021-05-07
  • 2021-07-08
猜你喜欢
  • 2022-12-23
  • 2021-07-28
  • 2021-11-05
  • 2021-10-13
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
相关资源
相似解决方案