1. 程式人生 > 資料庫 >Linux安裝redis(6.0.9)環境

Linux安裝redis(6.0.9)環境

  後端介面postgres.bki檔案是在編譯的過程中由/src/backend/catalog目錄下的指令碼程式genbki.sh讀取/src/include/catalog目錄下的以.h結尾的系統表定義檔案(包括系統表索引和TOAST表定義檔案)建立,並通常存放在安裝樹的share子目錄下

  在pg_*.h(星號表示對應系統表的名稱,每個這樣的標頭檔案對應一個系統表的結構定義)的標頭檔案中包含如下內容的定義:定義CATALOG巨集,用於以統一的模式去定義系統表的結構以及用以描述系統表的資料結構,如系統表pg_class的定義通過CATALOG(pg_class,1259)來表現。通過巨集DATA(x)和DESCR(x)來定義insert操作,這樣的Insert操作可能會有多個,用於定義系統表中的初始資料。以pg_class.h為例,其定義了系統relation關係表用於初始化關係表的內容(/src/include/catalog/pg_class.h)。

 1 #ifndef PG_CLASS_H
 2 #define PG_CLASS_H
 3 #include "catalog/genbki.h"
 4 /* pg_class definition.  cpp turns this into typedef struct FormData_pg_class */
 5 #define RelationRelationId    1259
 6 CATALOG(pg_class,1259) BKI_BOOTSTRAP
 7 {
 8     NameData    relname;        /* class name */
 9     Oid relnamespace;/*
OID of namespace containing this class */ 10 Oid reltype; /* OID of associated entry in pg_type */ 11 Oid relowner; /* class owner */ 12 Oid relam; /* index access method; 0 if not an index */ 13 Oid relfilenode; /* identifier of physical storage file */ 14 Oid reltablespace;/*
identifier of table space for relation */ 15 int4 relpages; /* # of blocks (not always up-to-date) */ 16 float4 reltuples; /* # of tuples (not always up-to-date) */ 17 Oid reltoastrelid; /* OID of toast table; 0 if none */ 18 Oid reltoastidxid; /* if toast table, OID of chunk_id index */ 19 bool relhasindex; /* T if has (or has had) any indexes */ 20 bool relisshared; /* T if shared across databases */ 21 bool relistemp; /* T if temporary relation */ 22 char relkind; /* see RELKIND_xxx constants below */ 23 int2 relnatts; /* number of user attributes */ 24 25 /* Class pg_attribute must contain exactly "relnatts" user attributes (with attnums ranging from 1 to relnatts) for this class. It may also contain entries with negative attnums for system attributes. */ 26 int2 relchecks; /* # of CHECK constraints for class */ 27 bool relhasoids; /* T if we generate OIDs for rows of rel */ 28 bool relhaspkey; /* has (or has had) PRIMARY KEY index */ 29 bool relhasrules; /* has (or has had) any rules */ 30 bool relhastriggers; /* has (or has had) any TRIGGERs */ 31 bool relhassubclass; /* has (or has had) derived classes */ 32 TransactionId relfrozenxid; /* all Xids < this are frozen in this rel */ 33 /* VARIABLE LENGTH FIELDS start here. These fields may be NULL, too. NOTE: these fields are not present in a relcache entry's rd_rel field. */ 34 aclitem relacl[1]; /* access permissions */ 35 text reloptions[1]; /* access-method-specific options */ 36 } FormData_pg_class; 37 38 /* Size of fixed part of pg_class tuples, not counting var-length fields */ 39 #define CLASS_TUPLE_SIZE (offsetof(FormData_pg_class,relfrozenxid) + sizeof(TransactionId)) 40 41 /* Form_pg_class corresponds to a pointer to a tuple with the format of pg_class relation. */ 42 typedef FormData_pg_class *Form_pg_class; 43 /* compiler constants for pg_class */ 44 #define Natts_pg_class 25 45 #define Anum_pg_class_relname 1 46 #define Anum_pg_class_relnamespace 2 47 #define Anum_pg_class_reltype 3 48 #define Anum_pg_class_relowner 4 49 #define Anum_pg_class_relam 5 50 #define Anum_pg_class_relfilenode 6 51 #define Anum_pg_class_reltablespace 7 52 #define Anum_pg_class_relpages 8 53 #define Anum_pg_class_reltuples 9 54 #define Anum_pg_class_reltoastrelid 10 55 #define Anum_pg_class_reltoastidxid 11 56 #define Anum_pg_class_relhasindex 12 57 #define Anum_pg_class_relisshared 13 58 #define Anum_pg_class_relistemp 14 59 #define Anum_pg_class_relkind 15 60 #define Anum_pg_class_relnatts 16 61 #define Anum_pg_class_relchecks 17 62 #define Anum_pg_class_relhasoids 18 63 #define Anum_pg_class_relhaspkey 19 64 #define Anum_pg_class_relhasrules 20 65 #define Anum_pg_class_relhastriggers 21 66 #define Anum_pg_class_relhassubclass 22 67 #define Anum_pg_class_relfrozenxid 23 68 #define Anum_pg_class_relacl 24 69 #define Anum_pg_class_reloptions 25 70 71 /* initial contents of pg_class NOTE: only "bootstrapped" relations need to be declared here. Be sure that the OIDs listed here match those given in their CATALOG macros, and that the relnatts values are correct. */ 72 73 /* Note: "3" in the relfrozenxid column stands for FirstNormalTransactionId */ 74 DATA(insert OID = 1247 ( pg_type PGNSP 71 PGUID 0 1247 0 0 0 0 0 f f f r 28 0 t f f f f 3 _null_ _null_ )); 75 DESCR(""); 76 DATA(insert OID = 1249 ( pg_attribute PGNSP 75 PGUID 0 1249 0 0 0 0 0 f f f r 18 0 f f f f f 3 _null_ _null_ )); 77 DESCR(""); 78 DATA(insert OID = 1255 ( pg_proc PGNSP 81 PGUID 0 1255 0 0 0 0 0 f f f r 25 0 t f f f f 3 _null_ _null_ )); 79 DESCR(""); 80 DATA(insert OID = 1259 ( pg_class PGNSP 83 PGUID 0 1259 0 0 0 0 0 f f f r 25 0 t f f f f 3 _null_ _null_ )); 81 DESCR(""); 82 83 #define RELKIND_INDEX 'i' /* secondary index */ 84 #define RELKIND_RELATION 'r' /* ordinary cataloged heap */ 85 #define RELKIND_SEQUENCE 'S' /* SEQUENCE relation */ 86 #define RELKIND_UNCATALOGED 'u' /* temporary heap */ 87 #define RELKIND_TOASTVALUE 't' /* moved off huge values */ 88 #define RELKIND_VIEW 'v' /* view */ 89 #define RELKIND_COMPOSITE_TYPE 'c' /* composite type */ 90 #endif /* PG_CLASS_H */

  模板資料庫template1是通過執行在bootstrap模式的postgres程式讀取postgres.bki檔案建立的。BKI檔案是一些用特殊語言寫的指令碼,這些指令碼使PostgreSQL後端能夠理解,且以特殊的bootstrap模式來執行之,這種模式允許在不存在系統表的零初始條件下執行資料庫函式,而普通的SQL命令要求系統表必須存在。因此BKI檔案僅用於初始化資料集簇。BKI命令格式如下:

(1) create [bootstrap] [shared_relation] [without_Oids] tablename tableOid (name1 = type1 [, name2=type2, ...]) 建立一個名為tablename並且OID為tableOid的表,表的欄位在圓括弧中給出。Bootstrap模式支援下列欄位型別:bool、bytea、char、name、int2、int4、regproc、regclass、regtype、text、Oid、tid、xid、cid、int2vector、Oidvector、_int4(陣列)、_text(陣列)、_Oid(陣列)、_char(陣列)、_aclitem(陣列)。只有在建立完pg_type並且填充了合適的資料之後才可以建立包含其他型別欄位的表。如果指定了bootstrap選項,那麼將只建立表而不向pg_class等系統表裡面插入任何記錄。因此這樣的表將無法被普通的SQL操作訪問,直到系統表中的初始記錄用硬方法(BKI的insert命令)插入。這個選項用於建立諸如pg_class等核心的系統表。如果指定了shared_relation選項,那麼該表將作為整個資料集簇的共享表建立(比如建立pg_database)。如果指定了without_Oids選項,則該表將不會具有OID欄位。

(2) open tablename 開啟一個名為tablename的表,準備插入資料。在BKI被執行時,每一個時刻都只有一個表被開啟,因此使用open命令時,任何當前已經開啟的表都會被關閉。

(3) insert [OID = Oid_value] (value1 value2 ...) 以value1、value2等作為欄位值插入一個元組,如果該表具有OID屬性,則把Oid_value作為被插入元組的OID。如果Oid_value為零或者省略了OID關鍵字,將為該插入元組分配系統中可用的下一個OID。

(4) close [tablename] 關閉當前被開啟的表。由於每一時刻只有一個表被開啟,因此可以省略tablename。

(5) declare [unique] index indexname indexOid on tablename using amname (opclass1 name1 [, ...]) 在名為tablename的表上用amname訪問方法建立一個OID為indexOid且名為indexname的索引。索引屬性是tablename表的name1、name2等屬性,在屬性上使用的操作符類分別是opclass1、opclass2等。該命令僅僅是建立一個索引的結構,索引的內容(即索引項)並不由該命令填充。

(6) declare toast toasttableOid toastindexOid on tablename 為名為tablename的表建立一個TOAST表。該TOAST表的OID為toasttableOid,其索引的OID為toastindexOid。與declare index命令一樣,該命令也不會填充TOAST表的索引。

(7) build indices 對使用5和6兩種命令建立的索引進行內容填充。

  在整個原始碼被編譯時,genbki.sh指令碼會被呼叫,它將從每一個pg_*.h檔案中讀取系統表定義、系統表的初始化資料、系統表上的索引等資訊,然後分別將其轉換為對應的BKI命令,最後將所有BKI命令寫入postgres.bki檔案中,該檔案的內容如下:一個create bootstrap命令,用於建立其中一個關鍵表;一個或多個insert命令,用於填充步驟1建立的關鍵表中的資料;一個close命令,用於關閉步驟1建立的關鍵表;重複步驟1-3建立和填充其他關鍵表;一個不帶bootstrap選項的create命令,用於建立一個非關鍵表;一個open命令,開啟非關鍵表;一個或多個insert命令,填充非關鍵表所需要的資料;一個close命令,關閉上面開啟的非關鍵表;重複建立其他非關鍵表;一個或多個declare index命令,用於定義索引;一個build indices命令,用於實際建立上一步所定義的索引。