Skip to content

Instantly share code, notes, and snippets.

@kmikmy
Last active October 28, 2015 02:10
Show Gist options
  • Save kmikmy/13805cf7b99387391bfd to your computer and use it in GitHub Desktop.
Save kmikmy/13805cf7b99387391bfd to your computer and use it in GitHub Desktop.
主要な構造体

WALに関する主要な構造体

xlog.h

XLogRecord

ソースコード

typedef struct XLogRecord
{
    uint32      xl_tot_len;     /* total len of entire record */
    TransactionId xl_xid;       /* xact id */
    uint32      xl_len;         /* total len of rmgr data */
    uint8       xl_info;        /* flag bits, see below */
    RmgrId      xl_rmid;        /* resource manager for this record */
    /* 2 bytes of padding here, initialize to zero */
    XLogRecPtr  xl_prev;        /* ptr to previous record in log */
    pg_crc32    xl_crc;         /* CRC for this record */

    /* If MAXALIGN==8, there are 4 wasted bytes here */

    /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */

} XLogRecord;

概要

ログレコード(固定長のヘッダ部分)

その他

実際はこのあとにデータが続いて一つのログレコードになっている

/*
 * The overall layout of an XLOG record is:
 *      Fixed-size header (XLogRecord struct)
 *      rmgr-specific data
 *      BkpBlock
 *      backup block data
 *      BkpBlock
 *      backup block data
 *      ...
 
 */

XLogRecData

ソースコード

typedef struct XLogRecData
{
    char       *data;           /* start of rmgr data to include */
    uint32      len;            /* length of rmgr data to include */
    Buffer      buffer;         /* buffer associated with data, if any */
    bool        buffer_std;     /* buffer has standard pd_lower/pd_upper */
    struct XLogRecData *next;   /* next struct in chain, or NULL */
} XLogRecData;

概要

XLogInsert()はXLogRecDataのチェーンを受け取って受け取る。

dataフィールドがログレコードのデータへのポインタになっていて、 XLogInsert()でdataをWALバッファにコピーしている。

BkpBlock

ソースコード

typedef struct BkpBlock
{
    RelFileNode node;           /* relation containing block */
    ForkNumber  fork;           /* fork within the relation */
    BlockNumber block;          /* block number */
    uint16      hole_offset;    /* number of bytes before "hole" */
    uint16      hole_length;    /* number of bytes in "hole" */

    /* ACTUAL BLOCK DATA FOLLOWS AT END OF STRUCT */
} BkpBlock;

概要

ログレコードのデータの一部。更新するフィールドの数だけBkpBlockがある(?)

その他

一つのログレコードにBkpBlockが一つ以上含まれる。 Backup Blockのデータがこの後に続く。

xlog.c

XLogCtlData

ソースコード

長いのでリンク

https://www.hpcs.cs.tsukuba.ac.jp/~kamiya/postgresql_9_4_5/S/114.html#L482

概要

XLOGで使われる全ての制御変数を含む構造体

その他

ソースコード中では以下のように記述されており、関数内部から変数XLogCtlを通してアクセスする。

static XLogCtlData *XLogCtl = NULL;

XLogCtlなどXLOGの共有メモリ変数の初期化は

関数XLOGShmemInit()

https://www.hpcs.cs.tsukuba.ac.jp/~kamiya/postgresql_9_4_5/S/114.html#L4835

で行われる。

XLogCtlInsert

ソースコード

長いのでリンク

https://www.hpcs.cs.tsukuba.ac.jp/~kamiya/postgresql_9_4_5/S/114.html#L482

概要

XLogInsert関数で使う制御情報を含む構造体

主要なフィールド

  • uint64 CurrBytePos:WALバッファの末尾の位置(次のレコードはこの位置に追加される)

  • uint64 PrevBytePos:ひとつ前のレコードの開始位置(次のレコードのprev-linkにコピーされる)

  • WALInsertLockPadded *WALInsertLocks:WALInsertLock

その他

以下のようにアクセスされる。

&XLogCtl->Insert;

テンプレート

ソースコード

概要

その他

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment