我正在尝试找出 postgres 可以在事务内部安全处理什么,但我无法在 中找到相关信息postgres 手册。到目前为止,我发现了以下内容:
-
UPDATE
、INSERT
和 DELTE
在事务内部完全支持,并在事务未完成时回滚
- < code>DROP TABLE 无法在事务内安全处理,并且可以通过
CREATE TABLE
撤消,因此会重新创建删除的表,但不会重新填充它
CREATE TABLE
也不是真正的事务化,并使用相应的DROP TABLE
撤消,
这是正确的吗?另外,我找不到任何有关处理 ALTER TABLE 和 TRUNCATE 的提示。这些是如何处理的?它们在交易中安全吗?不同类型的事务和不同版本的 postgres 之间的处理是否有差异?
I am trying find out what is postgres can handle safely inside of transaction, but I cannot find the relavant information in the postgres manual. So far I have found out the following:
UPDATE
, INSERT
and DELTE
are fully supported inside transactions and rolled back when the transaction is not finished
DROP TABLE
is not handled safely inside a transaction, and is undone with a CREATE TABLE
, thus recreates the dropped table but does not repopulate it
CREATE TABLE
is also not truly transactionized and is instead undone with a corresponding DROP TABLE
Is this correct? Also I could not find any hints as to the handling of ALTER TABLE
and TRUNCATE
. In what way are those handled and are they safe inside transactions? Is there a difference of the handling between different types of transactions and different versions of postgres?
发布评论
评论(2)
DROP TABLE
是事务性的。要撤消此操作,您需要发出ROLLBACK
而不是CREATE TABLE
。CREATE TABLE
也是如此(也可以使用 ROLLBACK 撤消)。ROLLBACK 始终是撤消事务的唯一正确方法 - 包括 ALTER TABLE 和 TRUNCATE。
Postgres 中唯一不具有事务性的是序列生成的数字(尽管 CREATE/ALTER/DROP SEQUENCE 本身是事务性的)。
DROP TABLE
is transactional. To undo this, you need to issue aROLLBACK
not aCREATE TABLE
. The same goes forCREATE TABLE
(which is also undone using ROLLBACK).ROLLBACK
is always the only correct way to undo a transaction - that includes ALTER TABLE and TRUNCATE.The only thing that is never transactional in Postgres are the numbers generated by a sequence (CREATE/ALTER/DROP SEQUENCE themselves are transactional though).
据我所知,所有这些命令都是事务感知的,除了
TRUNCATE ... RESTART IDENTITY
(甚至那个命令也是事务性的 自 9.1 起。)请参阅 并发控制和事务相关命令。
Best I'm aware all of these commands are transaction aware, except for
TRUNCATE ... RESTART IDENTITY
(and even that one is transactional since 9.1.)See the manual on concurrency control and transaction-related commands.