Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何在postgresql中解析锁定数据库上的更新查询?_Database_Postgresql_Acid - Fatal编程技术网

Database 如何在postgresql中解析锁定数据库上的更新查询?

Database 如何在postgresql中解析锁定数据库上的更新查询?,database,postgresql,acid,Database,Postgresql,Acid,使用postgresql,如果用户希望在数据库被锁定时添加新数据或更新数据库中的现有数据,则如何解析其事务?让我们考虑一下这个场景,如果我的理解在任何时候都错了,请指正我: 1. User 1 wants to batch update some records in the database. 2. The transaction from user 1 locks the database until all the updates are pushed. 3. User 2 wants t

使用postgresql,如果用户希望在数据库被锁定时添加新数据或更新数据库中的现有数据,则如何解析其事务?让我们考虑一下这个场景,如果我的理解在任何时候都错了,请指正我:

1. User 1 wants to batch update some records in the database.
2. The transaction from user 1 locks the database until all the updates are pushed.
3. User 2 wants to update something in the database, or insert some new data to the database while it is locked.
4. Based on what MVCC denotes, user 2 is shown the pre-lock version of the database.
5. User 2 inserts or updates the data.
6. User one finishes pushing its transaction and releases the database.
7. There are two versions of database now, the data is resolved.

如何解决步骤7中的问题?我在某个地方读到,它将获取带有最新全球时间戳的数据。但我如何才能确定这是它应该保存的数据呢?如果来自用户2的数据的优先级高于用户1,但是来自用户2的事务在用户1之前完成,那么如何解析此优先级?谢谢您的帮助。

您不能在PostgreSQL中锁定数据库,但可以以独占方式锁定表。这是您应该永远不要做的事情,因为这是不必要的,并且会损害并发性和系统维护过程(autovacuum)

在事务期间,您修改或删除的每一行都将被自动锁定。这不会影响并发会话对其他行的修改


如果某个事务试图修改已被另一个事务锁定的行,则第二个事务将被阻止,并且必须等待第一个事务完成。这样就避免了您描述的场景。

PostgreSQL没有数据库级锁,因此编写的问题毫无意义。因此,修改锁定行的事务将等待锁释放。那么在什么情况下会发生回滚呢?当您发出
rollback
命令或数据库会话在事务仍然打开时终止时,就会发生回滚。