postgresql Tuple之t_ctid

翻译

当一个tuple保存到硬盘的时候,它的t_ctid被初始化为它的TID(位置信息),
如果tuple被更新过,则t_ctid被改变为最新的tuple 替换版本。

因此,如果XMAX无效时或者t_ctid指向它自己(如果XMAX有效,此时,tuple要么是被锁定的,要么是被删除的),
tuple表示此行的最新版本。这种情况下我们可以追随t_ctid找到此行最新的版本。

小心,VACUUM可能会再擦除c_tid指向的旧版本指针之前擦除新版本,
所以,当我们要追随t_ctid寻找新版本之前,需要检查一下引用槽是否为空或者引用了一个unrelate tuple
以及XMIN与XMAX是否相等来确定我们并没有引用到一个被VACUUM释放的tuple,
如果上述检查返回失败,我们可以假设这个tuple没有延伸的版本。

t_ctid有时候会用来保存一个特殊的插入标记,而不是一个TID,当tuple被插入的时候,
我们会设置一个特殊的标记,直到插入者确定将此tuple插入到硬盘,
所以,当事务正在运行,或者失败时,我们只应该看到一个包含XMAX的tuple,
如果插入被确定的时候标记就会被替换成实际的TID,注意,仅仅在插入的时候我们能看到标记,因为它仅仅被用作插入操作。

原文

/* A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
 * is initialized with its own TID (location).  If the tuple is ever updated,
 * its t_ctid is changed to point to the replacement version of the tuple.
 * Thus, a tuple is the latest version of its row iff XMAX is invalid or
 * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
 * either locked or deleted).  One can follow the chain of t_ctid links
 * to find the newest version of the row.  Beware however that VACUUM might
 * erase the pointed-to (newer) tuple before erasing the pointing (older)
 * tuple.  Hence, when following a t_ctid link, it is necessary to check
 * to see if the referenced slot is empty or contains an unrelated tuple.
 * Check that the referenced tuple has XMIN equal to the referencing tuple's
 * XMAX to verify that it is actually the descendant version and not an
 * unrelated tuple stored into a slot recently freed by VACUUM.  If either
 * check fails, one may assume that there is no live descendant version.
 *
 * t_ctid is sometimes used to store a speculative insertion token, instead
 * of a real TID.  A speculative token is set on a tuple that's being
 * inserted, until the inserter is sure that it wants to go ahead with the
 * insertion.  Hence a token should only be seen on a tuple with an XMAX
 * that's still in-progress, or invalid/aborted.  The token is replaced with
 * the tuple's real TID when the insertion is confirmed.  One should never
 * see a speculative insertion token while following a chain of t_ctid links,
 * because they are not used on updates, only insertions.

猜你喜欢

转载自blog.csdn.net/akakakak250/article/details/55049768