Postgresql 从AccessExclusiveLock锁定表中选择表
我正在centos-5.8上的pgsql-9.1上测试postgresql锁机制 我想知道的是,如何从事务中由于Postgresql 从AccessExclusiveLock锁定表中选择表,postgresql,Postgresql,我正在centos-5.8上的pgsql-9.1上测试postgresql锁机制 我想知道的是,如何从事务中由于未限制而被独占锁定的表中进行选择 案件如下 第1课时 postgres=# create table test_table(col1 char); CREATE TABLE postgres=# begin; BEGIN postgres=# insert into test_table values('1'); INSERT 0 1 --uncommited 在第2课时 postg
未限制而被独占锁定的表中进行选择
案件如下
第1课时
postgres=# create table test_table(col1 char);
CREATE TABLE
postgres=# begin;
BEGIN
postgres=# insert into test_table values('1');
INSERT 0 1
--uncommited
在第2课时
postgres=# alter table test_table add column c2 char;
-- It has been locked....
在第3课时
postgres=# select t.relname, l.locktype,page,pid,virtualtransaction,mode,granted from pg_locks l, pg_stat_all_tables t where l.relation=t.relid order by relation asc;
relname | locktype | page | pid | virtualtransaction | mode | granted
--------------+----------+------+------+--------------------+---------------------+---------
pg_class | relation | | 9940 | 2/715754 | AccessShareLock | t
pg_index | relation | | 9940 | 2/715754 | AccessShareLock | t
pg_namespace | relation | | 9940 | 2/715754 | AccessShareLock | t
test_table | relation | | 9660 | 9/671042 | RowExclusiveLock | t
test_table | relation | | 9639 | 7/707191 | AccessExclusiveLock | f
(5 rows)
postgres=# select col1 from test_table;
--It's not possible to select from exclusively locked table
我需要的是得到关系的大小,不管发生了什么锁定。
我正在研究设置事务隔离级别
,但到目前为止,我还没有找到任何解决方案
任何建议都将不胜感激
提前感谢。您的表没有以独占访问方式锁定,它在已授予的AccessShareLock后面有一个AccessExclusiveLock挂起但未授予。虽然它确实具有大致相同的效果,但新的AccessShareLock队列在它后面。理论上,他们可以跳转队列并获得许可,但这有可能导致AccessExclusiveLock请求不足,因此无法实现
如果一个估计值足够好,你可以这样做
select reltuples from pg_class where oid='test_table'::regclass;
否则,唯一的选择就是杀死持有表锁的两个进程中的一个,要么是持有AccessShare的进程,要么是想要AccessExclusive的进程。(或者破解PostgreSQL源代码。)