简体   繁体   English

PostgreSQL是否针对只读事务运行一些性能优化

[英]Does PostgreSQL run some performance optimizations for read-only transactions

According to the reference documentation the READ ONLY transaction flag is useful other than allowing DEFERRABLE transactions? 根据参考文档 ,除了允许DEFERRABLE事务以外,READ ONLY事务标志还有用。

SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;

The DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. 除非事务也是可序列化的并且只能读取,否则DEFERRABLE事务属性无效。 When all three of these properties are selected for a transaction, the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a SERIALIZABLE transaction and without any risk of contributing to or being canceled by a serialization failure. 当为一个事务选择了所有这三个属性时,该事务在首次获取其快照时可能会阻塞,此后它可以在没有SERIALIZABLE事务的正常开销的情况下运行,并且没有任何有助于序列化或被序列化取消的风险失败。 This mode is well suited for long-running reports or backups. 此模式非常适合长时间运行的报告或备份。

Does the database engine runs other optimizations for read-only transactions? 数据库引擎是否对只读事务运行其他优化?

To sum up the comments from Nick Barnes and Craig Ringer in the question comments: 总结一下尼克·巴恩斯和克雷格·林格在问题评论中的评论:

  1. The READ_ONLY flag does not necessarily provide any optimization READ_ONLY标志不一定提供任何优化
  2. The main benefit of setting the READ_ONLY flag is to ensure that no tuple is going to be modified 设置READ_ONLY标志的主要好处是确保不会修改任何元组

Actually, it does. 实际上,确实如此。 Let me just cite source code comment here: 让我在这里引用源代码注释:

/*
 * 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.
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM