暂无图片
暂无图片
暂无图片
暂无图片
暂无图片
在 PostgreSQL 中使用预备事务,你知道有哪些坑吗?.doc
12
6页
4次
2025-04-04
免费下载
PostgreSQL
中使用预备事务,你知道有哪些坑吗?
PostgreSQL
,但
在使用时,很容易出问题,有非常多的注意事项。
介绍
PostgreSQL
如果
PostgreSQL
使
“除正在一个理器则你应该
使用
PREPARE TRANSACTION
。如果你真的需要使用
也不明显。
使用预备事务的注意事项
首先,让我们来创建一个简单的预备事务:
begin;
create table t1 (a int);
insert into t1 values (1);
prepare transaction 'abc';
此时再与果您
回滚事务,就可以轻松验证这一点:
commit;
WARNING: there is no transaction in progress
这也意味着,在我们预备好事务前创建的 “
t1”
表对我们
来说是不可见的:
select * from t1;
ERROR: relation "t1" does not exist
LINE 1: select * from t1;
^
虽然我们不再处于任何可见的事务中,但由于我们预备好
的事务,在后台还持有锁:
select * from pg_locks
where database = (select oid from pg_database where datname =
'postgres')
and mode like '%Exclusive%';
locktype | database | relation | page | tuple | virtualxid | transactionid | classid
| objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
| waitstart
----------+----------+----------+------+-------+------------+---------------+---------+-------
+----------+--------------------+-----+---------------------+---------+----------+-----------
relation | 12969 | 24582 | | | | | | | |
-1/562 | | RowExclusiveLock | t | f |
relation | 12969 | 24582 | | | | | | | |
-1/562 | | AccessExclusiveLock | t | f |
(2 rows)
有一个
AccessExclusiveLock
锁,它是 “
t1”
表上的锁
RowExclusiveLock”
为 “
t1”
表是不可见的:
select relname from pg_class where oid = 24582;
relname
---------
(0 rows)
提交后,它是
于 “
t1”
的:
commit prepared 'abc';
select relname from pg_class where oid = 24582;
relname
---------
t1
(1 row)
select * from t1;
a
---
1
(1 row)
我们还可以通过再次查看锁来确认:
select * from pg_locks
where database = (select oid from pg_database where datname =
'postgres')
and mode like '%Exclusive%';
locktype | database | relation | page | tuple | virtualxid | transactionid | classid
| objid | objsubid | virtualtransaction | pid | mode | granted | fastpath |
waitstart
----------+----------+----------+------+-------+------------+---------------+---------+-------
+----------+--------------------+-----+------+---------+----------+-----------
(0 rows)
些锁以,事,
是这样,您将不会遇到任何问题。
让我们来创建另一个预备事务:
begin;
insert into t1 values(2);
prepare transaction 'abc';
先要:一个预
就会完全存储在磁盘上:
ls -la $PGDATA/pg_twophase/*
-rw------- 1 postgres postgres 212 Feb 26 11:24
/db/pgsql/data/pg_twophase/00000233
提交事务后,文件会消失:
commit prepared 'abc';
ls -la $PGDATA/pg_twophase/
drwx------ 2 postgres postgres 4096 Feb 26 11:26 .
of 6
免费下载
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文档的来源(墨天轮),文档链接,文档作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。