PostgreSQL 等价于 SQLServer 的 NoLock 提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2394565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:26:32  来源:igfitidea点击:

PostgreSQL Equivalent of SQLServer's NoLock Hint

postgresqlnolock

提问by Cerin

In SQLServer, you can use syntax "(nolock)" to ensure the query doesn't lock the table or isn't blocked by other queries locking the same table. e.g.

在 SQLServer 中,您可以使用语法“(nolock)”来确保查询不会锁定表或不会被锁定同一表的其他查询阻塞。例如

SELECT * FROM mytable (nolock) WHERE id = blah

What's the equivalent syntax in Postgres? I found some documentation on table locking in PG (http://www.postgresql.org/docs/8.1/interactive/sql-lock.html), but it all seems geared at how to locka table, not ensure it's not locked.

Postgres 中的等效语法是什么?我在 PG ( http://www.postgresql.org/docs/8.1/interactive/sql-lock.html) 中找到了一些关于表锁定的文档,但这似乎都是针对如何锁定表,而不是确保它没有被锁定.

回答by Frank Heikens

A SELECT doesn't lock any table in PostgreSQL, unless you want a lock:

SELECT 不会锁定 PostgreSQL 中的任何表,除非您想要锁定:

SELECT * FROM tablename FOR UPDATE;

PostgreSQL uses MVCCto minimize lock contention in order to allow for reasonable performance in multiuser environments. Readers do not conflict with writers nor other readers.

PostgreSQL 使用MVCC来最小化锁争用,以便在多用户环境中实现合理的性能。读者不与作者或其他读者发生冲突。

回答by Matthew Wood

I've done some research and it appears that the NOLOCKhint in SQL Server is roughly the same as READ UNCOMMITTEDtransaction isolation level. In PostgreSQL, you can set READ UNCOMMITTED, but it silently upgrades the level to READ COMMITTED. READ UNCOMMITTEDis not supported.

我做了一些研究,似乎NOLOCKSQL Server中的提示与 READUNCOMMITTED事务隔离级别大致相同。在 PostgreSQL 中,您可以设置READ UNCOMMITTED,但它会默默地将级别升级到READ COMMITTED. READ UNCOMMITTED不支持。

PostgreSQL 8.4 documentation for Transaction Isolation: http://www.postgresql.org/docs/8.4/static/transaction-iso.html

PostgreSQL 8.4 事务隔离文档:http: //www.postgresql.org/docs/8.4/static/transaction-iso.html