简体   繁体   English

PostgreSQL事务锁

[英]PostgreSQL transaction lock

I'm using pg, pg-native for my project, I use transaction something like below code, I'm not familiar with database, 我在项目中使用的是pg,pg-native,我使用的是类似下面代码的事务,我对数据库不熟悉,
https://github.com/brianc/node-postgres/wiki/Transactions https://github.com/brianc/node-postgres/wiki/Transactions
1. Does this lock table or only lock the row? 1.该锁定表还是仅锁定行?
2. The lock does it means diable Create , Update , Delete in CRUD . 2.锁定确实意味着可以在CRUD CreateUpdateDelete but still can be Read ? 但还是可以Read if so, the data be select, is it the data before this change (before BEGIN )? 如果是这样,则选择数据,是该更改之前( BEGIN之前)的数据吗?

"pg": "^4.5.1", "pg-native": "^1.10.0", “ pg”:“ ^ 4.5.1”,“ pg-native”:“ ^ 1.10.0”,

var pg = require('pg').native;
...

var rollback = function(dbClient, response) {
  dbClient.query('ROLLBACK', function() {
    dbClient.end();
    // reject(response);
  });
};

dbClient.query('BEGIN', function(error, result) {
  ...

dbClient.query('COMMIT', dbClient.end.bind(dbClient));
  ...

Read the documentation for the complete picture. 阅读文档以获取完整图片。 It is worth the effort. 值得付出努力。

PostgreSQL has different levels of locking: table locks and row locks (I won't go into advisory loks). PostgreSQL有不同级别的锁定:表锁定和行锁定(我不会进入咨询性讨论)。

Table locks can be taken explicitly with the LOCK command, but that's something you don't do normally. 可以使用LOCK命令显式地获取表锁,但这是您通常不做的事情。 The normal case are implicit table locks that are taken when you access a table – for example, when you read or write data in a table, a lock will be taken that prevents concurrent users from ALTER or DROP on the table. 通常情况是在访问表时采用隐式表锁–例如,当您在表中读取或写入数据时,将采用一种锁,以防止并发用户对表进行ALTERDROP操作。

Row locks are taken by DML statements ( INSERT , UPDATE and DELETE ) as well as by SELECT ... FOR SHARE/UPDATE . 行锁由DML语句( INSERTUPDATEDELETE )以及SELECT ... FOR SHARE/UPDATE They prevent concurrent modification of the locked row. 它们防止同时修改锁定的行。

All locks are released at the end of a transaction. 所有锁在事务结束时释放。 If you didn't explicitly start a transaction, each statement runs in its own transaction, and locks won't outlast the duration of the single statement. 如果您未明确启动事务,则每个语句都将在其自己的事务中运行,并且锁定不会超过单个语句的持续时间。 If you start a transaction explicitly with BEGIN or START TRANSACTION , it lasts until you COMMIT or ROLLBACK . 如果使用BEGINSTART TRANSACTION显式START TRANSACTION ,则该START TRANSACTION将持续到COMMITROLLBACK为止。

One important design concept of PostgreSQL (and any other database that uses multiversion concurrency control ) is that readers never block writers and vice versa. PostgreSQL(以及使用多版本并发控制的任何其他数据库)的一个重要设计概念是读者永远不会阻止作者,反之亦然。 This is facilitated by keeping old versions of the data around, and readers are served with data that are valid at their snapshot (roughly, the consistent view of the database valid at statement or transaction start). 通过保留旧版本的数据可以方便地进行此操作,并且为读取器提供在其快照有效的数据(大致上,在语句或事务开始时有效的数据库一致视图)。 These old rows are eventually cleaned up by the autovacuum background process. 这些旧行最终将通过自动真空后台处理进行清理。

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

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