PostgreSQL學(xué)習(xí)系列—pg_control之Database system identifier

[hgps@ps2 bin]$ pg_controldata ../data
pg_control version number:            942
Catalog version number:               201510051
Database system identifier:           6548580191788017147
Database cluster state:               in production
pg_control last modified:             Mon 21 May 2018 11:56:42 AM CST
Latest checkpoint location:           0/2797010
Prior checkpoint location:            0/17FAF50
Latest checkpoint's REDO location:    0/2797010
Latest checkpoint's REDO WAL file:    000000010000000000000002
Latest checkpoint's TimeLineID:       1
Latest checkpoint's PrevTimeLineID:   1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID:          0/1836
Latest checkpoint's NextOID:          24576
Latest checkpoint's NextMultiXactId:  1
Latest checkpoint's NextMultiOffset:  0
Latest checkpoint's oldestXID:        1825
Latest checkpoint's oldestXID's DB:   1
Latest checkpoint's oldestActiveXID:  0
Latest checkpoint's oldestMultiXid:   1
Latest checkpoint's oldestMulti's DB: 1
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint:            Mon 21 May 2018 11:54:48 AM CST
Fake LSN counter for unlogged rels:   0/1
Minimum recovery ending location:     0/0
Min recovery ending loc's timeline:   0
Backup start location:                0/0
Backup end location:                  0/0
End-of-backup record required:        no
wal_level setting:                    minimal
wal_log_hints setting:                off
max_connections setting:              300
max_worker_processes setting:         8
max_prepared_xacts setting:           0
max_locks_per_xact setting:           64
track_commit_timestamp setting:       off
Maximum data alignment:               8
Database block size:                  8192
Blocks per segment of large relation: 131072
WAL block size:                       8192
Bytes per WAL segment:                16777216
Maximum length of identifiers:        64
Maximum columns in an index:          32
Maximum size of a TOAST chunk:        1996
Size of a large-object chunk:         2048
Date/time type storage:               64-bit integers
Float4 argument passing:              by value
Float8 argument passing:              by value
Data page checksum version:           0
Data encryption:                      off

pg_control數(shù)據(jù)定義如下:

typedef struct ControlFileData
 {
     /*
      * Unique system identifier --- to ensure we match up xlog files with the
      * installation that produced them.
      */
     uint64      system_identifier;
 
     /*
      * Version identifier information.  Keep these fields at the same offset,
      * especially pg_control_version; they won't be real useful if they move
      * around.  (For historical reasons they must be 8 bytes into the file
      * rather than immediately at the front.)
      *
      * pg_control_version identifies the format of pg_control itself.
      * catalog_version_no identifies the format of the system catalogs.
      *
      * There are additional version identifiers in individual files; for
      * example, WAL logs contain per-page magic numbers that can serve as
      * version cues for the WAL log.
      */
     uint32      pg_control_version; /* PG_CONTROL_VERSION */
     uint32      catalog_version_no; /* see catversion.h */
 
     /*
      * System status data
      */
     DBState     state;          /* see enum above */
     pg_time_t   time;           /* time stamp of last pg_control update */
     XLogRecPtr  checkPoint;     /* last check point record ptr */
 
     CheckPoint  checkPointCopy; /* copy of last check point record */
 
     XLogRecPtr  unloggedLSN;    /* current fake LSN value, for unlogged rels */
 
     /*
      * These two values determine the minimum point we must recover up to
      * before starting up:
      *
      * minRecoveryPoint is updated to the latest replayed LSN whenever we
      * flush a data change during archive recovery. That guards against
      * starting archive recovery, aborting it, and restarting with an earlier
      * stop location. If we've already flushed data changes from WAL record X
      * to disk, we mustn't start up until we reach X again. Zero when not
      * doing archive recovery.
      *
      * backupStartPoint is the redo pointer of the backup start checkpoint, if
      * we are recovering from an online backup and haven't reached the end of
      * backup yet. It is reset to zero when the end of backup is reached, and
      * we mustn't start up before that. A boolean would suffice otherwise, but
      * we use the redo pointer as a cross-check when we see an end-of-backup
      * record, to make sure the end-of-backup record corresponds the base
      * backup we're recovering from.
      *
      * backupEndPoint is the backup end location, if we are recovering from an
      * online backup which was taken from the standby and haven't reached the
      * end of backup yet. It is initialized to the minimum recovery point in
      * pg_control which was backed up last. It is reset to zero when the end
      * of backup is reached, and we mustn't start up before that.
      *
      * If backupEndRequired is true, we know for sure that we're restoring
      * from a backup, and must see a backup-end record before we can safely
      * start up. If it's false, but backupStartPoint is set, a backup_label
      * file was found at startup but it may have been a leftover from a stray
      * pg_start_backup() call, not accompanied by pg_stop_backup().
      */
     XLogRecPtr  minRecoveryPoint;
     TimeLineID  minRecoveryPointTLI;
     XLogRecPtr  backupStartPoint;
     XLogRecPtr  backupEndPoint;
     bool        backupEndRequired;
 
     /*
      * Parameter settings that determine if the WAL can be used for archival
      * or hot standby.
      */
     int         wal_level;
     bool        wal_log_hints;
     int         MaxConnections;
     int         max_worker_processes;
     int         max_prepared_xacts;
     int         max_locks_per_xact;
     bool        track_commit_timestamp;
 
     /*
      * This data is used to check for hardware-architecture compatibility of
      * the database and the backend executable.  We need not check endianness
      * explicitly, since the pg_control version will surely look wrong to a
      * machine of different endianness, but we do need to worry about MAXALIGN
      * and floating-point format.  (Note: storage layout nominally also
      * depends on SHORTALIGN and INTALIGN, but in practice these are the same
      * on all architectures of interest.)
      *
      * Testing just one double value is not a very bulletproof test for
      * floating-point compatibility, but it will catch most cases.
      */
     uint32      maxAlign;       /* alignment requirement for tuples */
     double      floatFormat;    /* constant 1234567.0 */
 #define FLOATFORMAT_VALUE   1234567.0
 
     /*
      * This data is used to make sure that configuration of this database is
      * compatible with the backend executable.
      */
     uint32      blcksz;         /* data block size for this DB */
     uint32      relseg_size;    /* blocks per segment of large relation */
 
     uint32      xlog_blcksz;    /* block size within WAL files */
     uint32      xlog_seg_size;  /* size of each WAL segment */
 
     uint32      nameDataLen;    /* catalog name field width */
     uint32      indexMaxKeys;   /* max number of columns in an index */
 
     uint32      toast_max_chunk_size;   /* chunk size in TOAST tables */
     uint32      loblksize;      /* chunk size in pg_largeobject */
 
     /* flags indicating pass-by-value status of various types */
     bool        float4ByVal;    /* float4 pass-by-value? */
     bool        float8ByVal;    /* float8, int8, etc pass-by-value? */
 
     /* Are data pages protected by checksums? Zero if no checksum version */
     uint32      data_checksum_version;
 
     /*
      * Random nonce, used in authentication requests that need to proceed
      * based on values that are cluster-unique, like a SASL exchange that
      * failed at an early stage.
      */
     char        mock_authentication_nonce[MOCK_AUTH_NONCE_LEN];
 
     /* CRC of all above ... MUST BE LAST! */
     pg_crc32c   crc;
 } ControlFileData;

pg_control文件首次創(chuàng)建是在src/backend/access/transam/xlog.c中的void BootStrapXLOG(void) 完成澎粟。

BootStrapXLOG(void)函數(shù)在系統(tǒng)安裝時(shí)僅執(zhí)行一次璧尸,負(fù)責(zé)創(chuàng)建pg_control文件以及初始化XLOG文件诗力。

Database system identifier(數(shù)據(jù)庫系統(tǒng)標(biāo)識(shí)符篷牌,內(nèi)部提示為sysid)撩轰,用于唯一識(shí)別Database Cluster,啟動(dòng)低千、備份或者恢復(fù)等過程中會(huì)校驗(yàn)pg_control中的Database system identifier與wal文件中的sysid是否相同掺栅。

sysid生成的算法為:

     uint64 sysidentifier;
     
     /*
      * Select a hopefully-unique system identifier code for this installation.
      * We use the result of gettimeofday(), including the fractional seconds
      * field, as being about as unique as we can easily get.  (Think not to
      * use random(), since it hasn't been seeded and there's no portable way
      * to seed it other than the system clock value...)  The upper half of the
      * uint64 value is just the tv_sec part, while the lower half contains the
      * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
      * PID for a little extra uniqueness.  A person knowing this encoding can
      * determine the initialization time of the installation, which could
      * perhaps be useful sometimes.
      */
     gettimeofday(&tv, NULL);
     sysidentifier = ((uint64) tv.tv_sec) << 32;
     sysidentifier |= ((uint64) tv.tv_usec) << 12;
     sysidentifier |= getpid() & 0xFFF;

pg_controldata顯示sysid時(shí),基于跨平臺(tái)展示的考慮誉帅,采用char進(jìn)行了轉(zhuǎn)換:

     /*
      * Format system_identifier and mock_authentication_nonce separately to
      * keep platform-dependent format code out of the translatable message
      * string.
      */
     snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
     ControlFile->system_identifier);

基于這種機(jī)制淀散,可以利用system identifier來推斷出數(shù)據(jù)庫群集創(chuàng)建時(shí)間:

SELECT to_timestamp(((6548580191788017147>>32)&(2^32 -1)::bigint));

      to_timestamp      
------------------------
 2018-04-26 10:35:41+08
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蚜锨,隨后出現(xiàn)的幾起案子档插,更是在濱河造成了極大的恐慌,老刑警劉巖亚再,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件郭膛,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡针余,警方通過查閱死者的電腦和手機(jī)饲鄙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來圆雁,“玉大人忍级,你說我怎么就攤上這事∥毙啵” “怎么了轴咱?”我有些...
    開封第一講書人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長烈涮。 經(jīng)常有香客問我朴肺,道長,這世上最難降的妖魔是什么坚洽? 我笑而不...
    開封第一講書人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任戈稿,我火速辦了婚禮,結(jié)果婚禮上讶舰,老公的妹妹穿的比我還像新娘鞍盗。我一直安慰自己需了,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開白布般甲。 她就那樣靜靜地躺著肋乍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪敷存。 梳的紋絲不亂的頭發(fā)上墓造,一...
    開封第一講書人閱讀 49,784評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音锚烦,去河邊找鬼觅闽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛挽牢,可吹牛的內(nèi)容都是我干的谱煤。 我是一名探鬼主播,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼禽拔,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼刘离!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起睹栖,我...
    開封第一講書人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤硫惕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后野来,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恼除,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年曼氛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了豁辉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡舀患,死狀恐怖徽级,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情聊浅,我是刑警寧澤餐抢,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站低匙,受9級(jí)特大地震影響旷痕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜顽冶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一欺抗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧强重,春花似錦绞呈、人聲如沸团滥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拱燃,卻和暖如春秉溉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背碗誉。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工召嘶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哮缺。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓弄跌,卻偏偏與公主長得像,于是被迫代替她去往敵國和親尝苇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子铛只,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容