Postgresql 中文操作指南
13.1. Introduction #
PostgreSQL 向开发者提供了一组用来管理并发数据访问的丰富工具。内部采用一个多版本模型(多版本并发控制,MVCC)来维护数据一致性。这意味着每一条 SQL 语句都会看到数据的快照(一个 database version),正如其在一段时间以前的状态,而无需访问基础数据的当前状态。这可防止语句查看由在同一数据行上执行更新的并发事务生成的不一致数据,从而为每个数据库会话提供 transaction isolation。MVCC 摒弃了传统数据库系统中的锁定方法,尽量减少锁定竞争,以在多用户环境中实现合理性能。
PostgreSQL provides a rich set of tools for developers to manage concurrent access to data. Internally, data consistency is maintained by using a multiversion model (Multiversion Concurrency Control, MVCC). This means that each SQL statement sees a snapshot of data (a database version) as it was some time ago, regardless of the current state of the underlying data. This prevents statements from viewing inconsistent data produced by concurrent transactions performing updates on the same data rows, providing transaction isolation for each database session. MVCC, by eschewing the locking methodologies of traditional database systems, minimizes lock contention in order to allow for reasonable performance in multiuser environments.
使用 MVCC 并发控制模型而不是锁定的主要优点在于:对数据进行查询(读取)而获取的锁与对数据进行写入而获取的锁不会发生冲突,因此读取绝不会阻塞写入,而写入也绝不会阻塞读取。即使通过使用一个创新的 Serializable Snapshot Isolation(SSI)级别提供了最严格的事务隔离级别,PostgreSQL 也会维持这一保证。
The main advantage of using the MVCC model of concurrency control rather than locking is that in MVCC locks acquired for querying (reading) data do not conflict with locks acquired for writing data, and so reading never blocks writing and writing never blocks reading. PostgreSQL maintains this guarantee even when providing the strictest level of transaction isolation through the use of an innovative Serializable Snapshot Isolation (SSI) level.
PostgreSQL 还为通常不需要完整事务隔离而选择明确管理具体冲突点的应用提供了表级和行级锁定工具。但是,正确使用 MVCC 通常会比使用锁定提供更好的性能。除此之外,应用定义的建议锁定提供了一种获取不与单一事务绑定的锁定的机制。
Table- and row-level locking facilities are also available in PostgreSQL for applications which don’t generally need full transaction isolation and prefer to explicitly manage particular points of conflict. However, proper use of MVCC will generally provide better performance than locks. In addition, application-defined advisory locks provide a mechanism for acquiring locks that are not tied to a single transaction.