Understanding virtualxid && transactionid
对pg_locks视图中的virtualxid和transactionid字段感到困惑,经查阅资料,特此在此整理一下学习内容:
pg_locks Columns
| Name | Type | References | Description |
|---|---|---|---|
| locktype | text | Type of the lockable object: relation, extend, page, tuple, transactionid, virtualxid, object, userlock, or advisory | |
| database | oid | pg_database.oid | OID of the database in which the lock target exists, or zero if the target is a shared object, or null if the target is a transaction ID |
| relation | oid | pg_class.oid | OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation |
| page | integer | Page number targeted by the lock within the relation, or null if the target is not a relation page or tuple | |
| tuple | smallint | Tuple number targeted by the lock within the page, or null if the target is not a tuple | |
| virtualxid | text | Virtual ID of the transaction targeted by the lock, or null if the target is not a virtual transaction ID | |
| transactionid | xid | ID of the transaction targeted by the lock, or null if the target is not a transaction ID | |
| classid | oid | pg_class.oid | OID of the system catalog containing the lock target, or null if the target is not a general database object |
| objid | oid | any OID column | OID of the lock target within its system catalog, or null if the target is not a general database object |
| objsubid | smallint | Column number targeted by the lock (the classid and objid refer to the table itself), or zero if the target is some other general database object, or null if the target is not a general database object | |
| virtualtransaction | text | Virtual ID of the transaction that is holding or awaiting this lock | |
| pid | integer | Process ID of the server process holding or awaiting this lock, or null if the lock is held by a prepared transaction | |
| mode | text | Name of the lock mode held or desired by this process (see Section 13.3.1 and Section 13.2.3) | |
| granted | boolean | True if lock is held, false if lock is awaited | |
| fastpath | boolean | True if lock was taken via fast path, false if taken via main lock table |
查看src/include/storage/lock.h,可以看到对virtualxid结构的定义:
virtualxid不是针对pg实例整体来将的。
/*
* Top-level transactions are identified by VirtualTransactionIDs comprising
* the BackendId of the backend running the xact, plus a locally-assigned
* LocalTransactionId. These are guaranteed unique over the short term,
* but will be reused after a database restart; hence they should never
* be stored on disk.
*
* Note that struct VirtualTransactionId can not be assumed to be atomically
* assignable as a whole. However, type LocalTransactionId is assumed to
* be atomically assignable, and the backend ID doesn't change often enough
* to be a problem, so we can fetch or assign the two fields separately.
* We deliberately refrain from using the struct within PGPROC, to prevent
* coding errors from trying to use struct assignment with it; instead use
* GET_VXID_FROM_PGPROC().
*/
typedef struct
{
BackendId backendId; /* determined at backend startup */
LocalTransactionId localTransactionId; /* backend-local transaction
* id */
} VirtualTransactionId;
本地事务号和事务号又有什么分别呢?实际上一个是用来表示本地事务的,并且本地事务号不会存储在磁盘中,只存在于内存中。而事务号则是存储在磁盘中的,属于持久化的值。
/*
* GetNextLocalTransactionId --- allocate a new LocalTransactionId
*
* We split VirtualTransactionIds into two parts so that it is possible
* to allocate a new one without any contention for shared memory, except
* for a bit of additional overhead during backend startup/shutdown.
* The high-order part of a VirtualTransactionId is a BackendId, and the
* low-order part is a LocalTransactionId, which we assign from a local
* counter. To avoid the risk of a VirtualTransactionId being reused
* within a short interval, successive procs occupying the same backend ID
* slot should use a consecutive sequence of local IDs, which is implemented
* by copying nextLocalTransactionId as seen above.
*/
LocalTransactionId
GetNextLocalTransactionId(void)
{
LocalTransactionId result; /* loop to avoid returning InvalidLocalTransactionId at wraparound */
do
{
result = nextLocalTransactionId++;
} while (!LocalTransactionIdIsValid(result)); return result;
}
VirtualTransactionId *
GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode)
{
。。。。。。
swrd=# select * from pg_locks where locktype = 'virtualxid';
-[ RECORD 1 ]------+--------------
locktype | virtualxid
database |
relation |
page |
tuple |
virtualxid | 3/6510
transactionid |
classid |
objid |
objsubid |
virtualtransaction | 3/6510
pid | 20682
mode | ExclusiveLock
granted | t
fastpath | t swrd=#
As long as you query pg_locks, you in fact run a query and hence start a transaction. It needs to acquire AccessShareLock on pg_locks, for instance. That's why virtualxid is allocated.
参考:
http://blog.163.com/digoal@126/blog/static/16387704020156136857214
http://stackoverflow.com/questions/32996020/understanding-the-virtualxid-transaction-type-in-postgres
Understanding virtualxid && transactionid的更多相关文章
- Freezing Your Tuples Off 之 vacuum_freeze_min_age
The vacuum_freeze_min_age setting determines the youngest XID which will be changed to FrozenXID on ...
- PostgreSQL 监控数据库活动
监控数据库活动 1. 标准Unix 工具 [root@mysqlhq ~]# ps auxww | grep ^postgrespostgres 12106 0.0 0.0 340060 15064 ...
- PostgreSQL 锁机制浅析
锁机制在 PostgreSQL 里非常重要 (对于其他现代的 RDBMS 也是如此).对于数据库应用程序开发者(特别是那些涉及到高并发代码的程序员),需要对锁非常熟悉.对于某些问题,锁需要被重点关注与 ...
- Postgresql Useful SQL/Commands
Update records ' and a.subscriber_id=b.subscriber_id; Connections select count(*) from pg_stat_activ ...
- 仅4步,就可通过SQL进行分布式死锁的检测与消除
摘要:本文主要介绍在 GaussDB(DWS) 中,如何通过 SQL 语句,对分布式死锁进行检测和恢复. 分布式数仓应用场景中,我们经常遇到数据库系统 hang 住的问题,所谓 hang 是指虽然数据 ...
- 游标长时间open导致表无法vacuum问题
一.问题描述 用户在实际中可能会碰到类似以下 dead rows 无法 vacuum的问题,一个可能的原因是由于游标未结束的原因. test=# vacuum(verbose) t1; INFO: v ...
- KingbaseES V8R6 锁等待检测
背景 对于多数数据库,dba技能之一就是查找锁.锁的存在有效合理的在多并发场景下保证业务有序进行.下面我们看一下KingbaseESV8R6中查找阻塞的方法. 1.找到"被阻塞者" ...
- KingabseES 锁机制
KingabseES的锁机制 目录 KingabseES的锁机制 一.前言 二.锁机制 三.表级锁 ( Table-Level Locks ) 1.访问共享(ACCESS SHARE) 2.行共享(R ...
- GOOD MEETINGS CREATE SHARED UNDERSTANDING, NOT BRDS!
Deliverables and artifacts were a focal point of BA work during the early part of my career. If I ...
随机推荐
- ios创建bundle的图片资源文件(转)
在ios开发中为了方便管理资源文件,可以使用bundle的方式来进行管理,比如kkgridview里就是把所需的图片文件全部放在一个bundle来管理的 . 切记目前iOS中只允许使用bundle管理 ...
- GCD的多线程实现方式,线程和定时器混合使用
GCD (Grand Central Dispatch) 性能更好,追求性能的话 1.创建一个队列 //GCD的使用 //创建一个队列 dispatch_queue_t queue = dispatc ...
- 学会使用Ogitor
这几天在用Ogre读取Ogitor的场景,遇到了不少问题,在网上也找不到详细的说明,虽然读取Ogitor的场景对很多人来说太简单了,但对一些新手来说就有点难了,我刚开始就觉得是无从下手,因此简单的描述 ...
- CPP,MATLAB实现牛顿插值
牛顿插值法的原理,在维基百科上不太全面,具体可以参考这篇文章.同样贴出,楼主作为初学者认为好理解的代码. function p=Newton1(x1,y,x2) %p为多项式估计出的插值 syms x ...
- Linux下查看每个目录所占用空间大小的命令
cd到上级目录,然后输入一条命令即可查询每个子目录所占用的空间大小 du -h --max-depth=1 可以更改--max-depth参数的值,该参数表示查询子目录的层级,当前为1层
- Redis - 密码配置和主从复制
使用config set命令修改requirepass参数配置Redis密码config set requirepass password 也可以通过配置文件修改密码,重启后生效. 克隆虚拟机,分别运 ...
- android:windowSoftInputMode及其他部分属性用法
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 今天我们来讲讲android:windoSoftInputMode的用法,许多同学会为软键盘的弹出. ...
- 怎么在手机浏览器上访问电脑本地的文件,局域网内,自建WiFi也可以
首先,电脑要有Mysql+Apache+PHP环境,我直接用Wampsever,开启环境后手机和电脑要再同一个局域网内,然后电脑上打开win+R,输入cmd,再输入ipconfig,就可以看着这台的电 ...
- nginx+lua项目学习
1.启动nginx命令 /usr/local/ngx_openresty/nginx/sbin/nginx -p /usr/local/ngx_openresty/nginx/ -c /usr/loc ...
- eclipse 中文乱码
使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置编码 格式解决乱码问题.在Eclipse可以从几个层面设置编 ...