CLOG †
- トランザクションのステータスを保持する。
- トランザクションの更新タイミングで共有メモリ上のバッファに書かれる。
- 共有メモリ上のバッファは、チェックポイントのタイミングで pg_xact下のファイルに更新される。
- clogは、8192byteのブロック(ページ)で管理される。また、clogは、トランザクションごとに2bitを消費する。1ページに格納されるトランザクションの状態数は以下となる。
1 byte = 4トランザクション 8192 byte x 4 = 32768 = 32k トランザクション
トランザクションのステータス †
ステータス | 値 |
---|---|
TRANSACTION_STATUS_IN_PROGRESS | 0x00 |
TRANSACTION_STATUS_COMMITTED | 0x01 |
TRANSACTION_STATUS_ABORTED | 0x02 |
TRANSACTION_STATUS_SUB_COMMITTED | 0x03 |
参考
共有メモリ上の構造 †
- コミットログは、1ページ8192byteで共有メモリ上のバッファに格納される。バッファのサイズは、スロットとして管理される。スロットのサイズは、以下で決まる。
参考 CLOGShmemBuffers()
/* * Number of shared CLOG buffers. * * On larger multi-processor systems, it is possible to have many CLOG page * requests in flight at one time which could lead to disk access for CLOG * page if the required page is not found in memory. Testing revealed that we * can get the best performance by having 128 CLOG buffers, more than that it * doesn't improve performance. * * Unconditionally keeping the number of CLOG buffers to 128 did not seem like * a good idea, because it would increase the minimum amount of shared memory * required to start, which could be a problem for people running very small * configurations. The following formula seems to represent a reasonable * compromise: people with very low values for shared_buffers will get fewer * CLOG buffers as well, and everyone else will get 128. */ Size CLOGShmemBuffers(void) { return Min(128, Max(4, NBuffers / 512)); }
例えば、shared_buffers = 1024Mとした場合、NBuffersは131072となるが、Min(128, ...)となるため、128となる。
よって、共有メモリ上のバッファにのる最大のトランザクション数は以下となる。
8192 * 4 * 128 = 4162048
CLOGファイル †
共有メモリ上のトランザクションxidのコミットステータス位置 †
- xidから、pageno、byteno、bshiftが求められる。
/* We need two bits per xact, so four xacts fit in a byte */ #define CLOG_BITS_PER_XACT 2 #define CLOG_XACTS_PER_BYTE 4 #define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE) #define CLOG_XACT_BITMASK ((1 << CLOG_BITS_PER_XACT) - 1) #define TransactionIdToPage(xid) ((xid) / (TransactionId) CLOG_XACTS_PER_PAGE) #define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) #define TransactionIdToByte(xid) (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE) #define TransactionIdToBIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
参考
例:xid=1052757の場合
CLOG_XACTS_PER_PAGE = 8192 * 4 = 32768 pageno = TransactionIdToPage(xid) = 1052757 / 32768 = 32 byteno = TransactionIdToByte(xid) = TransactionIdToPgIndex(xid) / 4 = (1052757 % 32768) / 4 = 1045 bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT = (1052757 % 4) * 2 = 2