txid_current()如何工作? [英] How does txid_current() work?

查看:173
本文介绍了txid_current()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下psql语句:

I have the below psql statements:

假设:初始txid:a

Assumption :initial txid: a

select txid_current();
----------------------
a+1

begin;
insert into tab( v1,v2);
insert into tab (v3,v4);
commit;

select txid_current();
----------------------
a+3

为什么我将交易ID视为a + 3而不应该是a + 2?
txid_current如何工作?
有什么有效的方法可以让我仅返回当前的txid而无需额外的增量?

Why do I see the transaction ID as a+3 shouldn't it be a+2 ?? how does txid_current work? Is there any effective way where i could only return the current txid without the additional increment ?

推荐答案

要理解的重点:


  • 一切正在进行交易。如果您未使用 BEGIN COMMIT (或 ROLLBACK )仅针对该语句为您创建了一个。

  • Everything is in a transaction. If you don't explicitly create one with BEGIN and COMMIT (or ROLLBACK) one is created for you just for that statement.

只读 SELECT 不会获得完整的交易ID,他们只会获得虚拟交易ID。因此,即使这是一笔交易, SELECT 1; 或任何不会增加交易ID计数器的东西。

Read-only SELECTs don't get a full transaction ID, they only get a virtual transaction ID. So even though it's a transaction, SELECT 1; or whatever doesn't increment the transaction ID counter.

调用 txid_current() 强制(如果尚未分配)则分配事务ID。因此,只读事务现在将具有事务ID,而以前是不存在的。

Calling txid_current() forces the allocation of a transaction ID if one wasn't already allocated. So a read-only transaction will now have a transaction ID, where it previously wouldn't.

当然, txid也会在会话之间分配。实际上,如果数据库繁忙,上面的示例可能会得到t + 1的a + 1和a + 429。

Of course, txids are also allocated across sessions. In practice your example above might get txid's of a+1 and a+429 if the database is busy.

请勿在应用程序级别将事务ID用作任何东西。这是内部系统级别的字段。无论您想做什么,交易ID都不可能是正确的方法。

Do not use the transaction ID for anything at the application level. It is an internal system level field. Whatever you are trying to do, it's likely that the transaction ID is not the right way to do it.

这篇关于txid_current()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆