22

根据参考文档,READ ONLY 事务标志除了允许 DEFERRABLE 事务之外还有用吗?

SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;

除非事务也是 SERIALIZABLE 和 READ ONLY,否则 DEFERRABLE 事务属性无效。当为事务选择所有这三个属性时,事务可能会在第一次获取其快照时阻塞,之后它能够在没有 SERIALIZABLE 事务的正常开销的情况下运行,并且没有任何有助于序列化或被序列化取消的风险失败。此模式非常适合长时间运行的报告或备份。

数据库引擎是否为只读事务运行其他优化?

4

2 回答 2

12

总结尼克巴恩斯和克雷格林格在问题评论中的评论:

  1. READ_ONLY 标志不一定提供任何优化
  2. 设置 READ_ONLY 标志的主要好处是确保不会修改任何元组
于 2017-01-06T19:43:52.170 回答
8

事实上,确实如此。让我在这里引用源代码注释:

/*
 * Check if we have just become "RO-safe". If we have, immediately release
 * all locks as they're not needed anymore. This also resets
 * MySerializableXact, so that subsequent calls to this function can exit
 * quickly.
 *
 * A transaction is flagged as RO_SAFE if all concurrent R/W transactions
 * commit without having conflicts out to an earlier snapshot, thus
 * ensuring that no conflicts are possible for this transaction.
 */
于 2018-09-09T00:11:12.147 回答