如何为 PostgreSQL 增加系统表字段

如何为 PostgreSQL 增加系统表字段,第1张

1为pg_database增加一个字段中此 datdummy,打开 /src/include/卖巧迅catalog/pg_database.h:

CATALOG(pg_database,1262) BKI_SHARED_RELATION BKI_ROWTYPE_OID(1248) BKI_SCHEMA_MACRO

{

NameDatadatname /* database name */宽基

Oid datdba/* owner of database */

int32 encoding /* character encoding */

NameDatadatcollate/* LC_COLLATE setting */

NameDatadatctype /* LC_CTYPE setting */

booldatistemplate /* allowed as CREATE DATABASE template? */

booldatallowconn /* new connections allowed? */

int32 datconnlimit /* max connections allowed (-1=no limit) */

Oid datlastsysoid /* highest OID to consider a system OID */

TransactionId datfrozenxid/* all Xids <this are frozen in this DB */

TransactionId datminmxid /* all multixacts in the DB are >= this */

Oid dattablespace /* default table space for this DB */

#ifdef CATALOG_VARLEN /* variable-length fields start here */

aclitem datacl[1] /* access permissions */

#endif

} FormData_pg_database

/* ----------------

* Form_pg_database corresponds to a pointer to a tuple with

* the format of pg_database relation.

* ----------------

*/

typedef FormData_pg_database *Form_pg_database

/* ----------------

* compiler constants for pg_database

* ----------------

*/

#define Natts_pg_database 13

#define Anum_pg_database_datname 1

#define Anum_pg_database_datdba2

#define Anum_pg_database_encoding 3

#define Anum_pg_database_datcollate4

#define Anum_pg_database_datctype 5

#define Anum_pg_database_datistemplate 6

#define Anum_pg_database_datallowconn 7

#define Anum_pg_database_datconnlimit 8

#define Anum_pg_database_datlastsysoid 9

#define Anum_pg_database_datfrozenxid 10

#define Anum_pg_database_datminmxid11

#define Anum_pg_database_dattablespace 12

#define Anum_pg_database_datacl13

它们最终会被脚本 genbki.pl 利用生成 postgresql.bki文件,用在 initdb 初始化 data cluster 时,有兴趣可以自己学习一下,PG编译系统也是一个充分展示强大 perl 语言的系统;

3、在 dattablespace 下增加新定义:

int8datdummy /* dummy column */

后边字段序号的定义也是很重要的,必须按顺序修改,也要记得属性数相应修改:

#define Natts_pg_database 14

...

#define Anum_pg_database_dattablespace 12

#define Anum_pg_database_datdummy 13

#define Anum_pg_database_datacl14

预定义的数据库必须也要修改,_null_ 前边增加 100的字段为 datdummy 数据:

DATA(insert OID = 1 ( template1 PGUID ENCODING "LC_COLLATE" "LC_CTYPE" t t -1 0 0 1 1663 100 _null_))

4、编译运行,会发生什么,编译正常,然后 initdb,竟然也神奇的通过了,难道这就好了吗?

Tip:Linux 下不停在一个目录下改代码,可能会遇到莫名的程序问题,试试 make clean

5、那么创建一个数据库试试看:CREATE DATABASE quanzl成功了,是不是感觉似乎还缺点什么?

datdummy的赋值,总不能 UPDATE pg_database SET datdummy = xxx 吧?

预订义的数据库比如template1,我们可以在catalog里边定义 BKI 脚本,比如上边的例子,给它一个初始值。程序里也必须有所改动才能成为可 *** 作属性;

6、参照语法修改,创建一个 CREATE DATABASE xxx DUMMY nnn语法,修改结构体 CreatedbStmt 增加新属性,语法分析阶段将此值读入,创建数据库时将它写入属性;

new_record[Anum_pg_database_datdummy - 1] = 1234

此部分代码在 src/backend/commands/dbcommands.c 中,自行阅读好了,写程序就这么简单。:)

用SQL语句添加删除修改字段、一些表与字段的基本 *** 作、数据库备份等,需带晌要的朋友可以参考下。

用SQL语句添加删除修改字段:

1.增加蠢孙锋字段。 

alter table docdsp add dspcode char(200)

2.删除字段。 

ALTER TABLE table_NAME DROP COLUMN column_NAME

3.修改字段类型 。

ALTER TABLE table_name ALTER COLUMN column_name new_data_type

数据表有两种含义,一是指数据库最重要的组成部分之一,二是指电子元件,电子芯片等的数据手册(datasheet)。

数据表一般为产品或资料提供一个详细具体的数据资料,方便人们使用和工作时能够清楚方便的获得相应的数凯槐据信息。

1. 数据库表中增加字段可以使芹团链用alter table 语句

如嫌孙需在表中添加列,请使用下或行列语法:

ALTER TABLE table_name

ADD column_name datatype

2. 示例

我们希望在表 "Persons" 中添加一个名为 "Birthday" 的新列。

我们使用下列 SQL 语句:

ALTER TABLE Persons

ADD Birthday date


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/bake/11991453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存