#author("2019-06-06T17:05:22+00:00","default:haikikyou","haikikyou")
#author("2019-06-20T14:44:03+00:00","default:haikikyou","haikikyou")
[[PostgreSQL/解析]]

#contents



* CLOG [#w78c0138]

- トランザクションのステータスを保持する。
- トランザクションの更新タイミングで共有メモリ上のバッファに書かれる。
- 共有メモリ上のバッファは、チェックポイントのタイミングで pg_xact下のファイルに更新される。
- clogは、8192byteのブロック(ページ)で管理される。また、clogは、トランザクションごとに2bitを消費する。1ページに格納されるトランザクションの状態数は以下となる。

 1 byte = 4トランザクション
 8192 byte x 4 = 32768 = 32k トランザクション

&ref(./clog.png,100%);


* トランザクションのステータス [#c1933c69]
|~ステータス|~値|h
|TRANSACTION_STATUS_IN_PROGRESS|0x00|
|TRANSACTION_STATUS_COMMITTED|0x01|
|TRANSACTION_STATUS_ABORTED|0x02|
|TRANSACTION_STATUS_SUB_COMMITTED|0x03|

&label(warn){参考};
- [[src/backend/access/transam/clog.c>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/transam/clog.c;h=8b7ff5b0c24b8a8d0139bbe60044204d94ac66d4;hb=refs/heads/REL_11_STABLE#l58]]

* 共有メモリ上の構造 [#g08eac32]

- コミットログは、1ページ8192kbで共有メモリ上のバッファに格納される。バッファのサイズは、スロットとして管理される。スロットのサイズは、以下で決まる。~
- コミットログは、1ページ8192byteで共有メモリ上のバッファに格納される。バッファのサイズは、スロットとして管理される。スロットのサイズは、以下で決まる。~
&label(warn){参考}; [[CLOGShmemBuffers()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/transam/clog.c;h=8b7ff5b0c24b8a8d0139bbe60044204d94ac66d4;hb=refs/heads/REL_11_STABLE#l666]]

#geshi(c){{{
/*
 * 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ファイル [#rb1627be]
* 共有メモリ上のトランザクションxidのコミットステータス位置 [#j830e60d]

- xidから、pageno、byteno、bshiftが求められる。
#geshi(c){{{
/* 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)
}}}

&label(warn){参考};
- [[src/backend/access/transam/clog.c>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/transam/clog.c;h=8b7ff5b0c24b8a8d0139bbe60044204d94ac66d4;hb=refs/heads/REL_11_STABLE#l58]]

''例:xid=1052757の場合''

#geshi{{{
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
}}}



* 参考リンク [#k86d1173]

- [[Hint Bits/ja>https://wiki.postgresql.org/wiki/Hint_Bits/ja]] - &size(11){&color(gray){on https://wiki.postgresql.org/wiki/Hint_Bits/ja};};


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP