postgresql - 为什么txid会增加?

标签 postgresql psql

我有以下脚本:

select txid_current();

显示的txid为=001

begin;
insert into tab values(10,20); insert into values(20,40);
commit;

现在当我这样做时: 选择 txid_current();

txid 看到的是:004 为什么增加 2 即为什么 txid 增加 2 不应该增加 1 即 txid 应该是 003 不应该选择 txid_current() 显示 003 ?

有没有办法将003显示为当前txid()?

最佳答案

<强> Transaction

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it. A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block.

这意味着当您运行 select txid_current(); 时,您就处于事务中,并且在运行之后您将获得新的事务 ID。

begin;
select txid_current(); // 1
end;

begin;
insert into tab values(10,20); insert into values(20,40);
select txid_current(); // 2
commit;

begin;
select txid_current(); // 3
end;

关于postgresql - 为什么txid会增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611537/

相关文章:

sql - 逻辑变化存在与存在限制 1

sql - Postgresql:在更新时替换部分字符串

Postgresql - 无法识别的配置参数

postgresql - 如何使用 psql\copy 元命令忽略错误

mysql - 如何使用不同的 where 语句从同一列中选择值 - SQL

主键约束中的 Postgresql 用户定义类型

sql - 使用 JOIN 和限制构建 SQL 查询

postgresql - 强制失败的 mvn flyway :migrate to skip failed schema migration, 并尝试执行下一个模式

mysql - 如何在 PostgreSQL 中仅获取数据库的表名

python - 一种将 psql 表(或查询)作为文件(csv、json)直接导出到 AWS S3 的方法