2pc影响,发现和通知

/home/postgres/pg11/project/bin/pg_ctl -D /home/postgres/pg11/data -l ~/logfile start

postgres=# begin;
BEGIN
postgres=*# insert into t1(id) values(1);
INSERT 0 1
postgres=*# select t_ctid,t_infomask2,t_infomask,t_xmin,t_xmax,t_data from heap_page_items(get_raw_page('t1',0));
 t_ctid | t_infomask2 | t_infomask | t_xmin | t_xmax |   t_data   
--------+-------------+------------+--------+--------+------------
 (0,1)  |           1 |       2048 |    783 |      0 | \x01000000
(1 row)

postgres=*# prepare transaction 'test_2pc_trans';
PREPARE TRANSACTION
postgres=# select * from t1;
 id 
----
(0 rows)
一阶段提交后我们已经退出了事务块,而事务没有完全提交所以我们无法查询到事务块内插入的数据

查看
postgres=# SELECT * FROM pg_prepared_xacts;
 transaction |       gid       |           prepared            |  owner   | database 
-------------+-----------------+-------------------------------+----------+----------
       20901 | test_2pc_trans  | 2021-10-14 14:38:04.135845-04 | postgres | postgres
(3 rows)

rollback prepared 'test_2pc_trans';
postgres=# commit prepared 'test_2pc_trans';
COMMIT PREPARED
postgres=# select * from t1;
 id 
----
  1
(1 row)
二阶段提交后,2PC事务已完成,所以又重新获取到了数据
1)prepare transaction 'test_2pc_trans';逻辑
会写xlog
rmgr: Transaction len (rec/tot):    242/   242, tx:      20904, lsn: 0/3C0FFB68, prev 0/3C0FFB28, desc: PREPARE

2)commit prepared 'test_2pc_trans'; 逻辑

3)如果prepare挂了,启动怎么恢复

4)如果没收到commit,怎么处理

5)如果

残留prepared transaction影响

1)该事务ID可能会阻止vacuum

2)插入语句,prepare事务,可导致修改表一直阻塞住

postgres=# begin;
BEGIN
postgres=# insert into t1(id) values(1);
INSERT 0 1
postgres=# prepare transaction 'test_2pc_trans6';

alter table t1 add column b int;

发现和通知

PREPARE TRANSACTION 'abce_insert 1m';
PREPARE TRANSACTION 'abce_insert 1d';

select gid,prepared,regexp_replace(gid, '.* ', '') AS age
from pg_prepared_xacts
WHERE prepared + CAST(regexp_replace(gid, '.* ', '') AS INTERVAL) < NOW();
posted @ 2021-10-15 23:34  stupidstan2019  阅读(20)  评论(0编辑  收藏  举报