1

我有一张表,它在我的系统中由进程 A 大量更新。这是简化表:

db=# \d employee;
                                       Table "public.employee"
     Column      |            Type             | Collation | Nullable |                   Default
-----------------+-----------------------------+-----------+----------+---------------------------------------------
 id              | integer                     |           | not null | nextval('employee_id_seq'::regclass)
 name            | character varying           |           |          |

Indexes:
"employee_pkey" PRIMARY KEY, btree (id)

我有一个引用该表的表:

db=# \d employee_property;
                                       Table "public.employee_property"
     Column      |            Type             | Collation | Nullable |                   Default
-----------------+-----------------------------+-----------+----------+---------------------------------------------
 id              | integer                     |           | not null | nextval('employee_property_id_seq'::regclass)
 type            | character varying           |           |          |
 value           | character varying           |           |          |
 employee_id     | integer                     |           | not null |

Indexes:
    "employee_property_pkey" PRIMARY KEY, btree (id)
    "employee_property_employee_id_type_value_key" UNIQUE CONSTRAINT, btree (employee_id, type, value)
    "ix_employee_property_employee_id" btree (employee_id)
Foreign-key constraints:
   "employee_property_employee_id_fkey" FOREIGN KEY (employee_id) REFERENCES employee(employee_id) ON DELETE CASCADE DEFERRABLE

我试图了解我是否employee_property通过系统中的进程 B 大量更新表,它是否会导致一些锁或任何其他可能影响更新员工表的进程 A 的副作用?

4

1 回答 1

1

如果您在现有行的列中插入一行employee_property或更新该行,则会在新行所指的行上放置一个锁。employee_idFOR KEY SHAREemployee_id

此锁将阻止任何并发尝试删除引用的employee行或更新任何PRIMARY KEYUNIQUE列。employee对不修改键列的锁定行的更新将起作用,因为它们只需要FOR NO KEY UPDATE对行进行锁定,这与FOR KEY SHARE.

这样做的原因是 PostgreSQL 必须确保在修改的事务employee_property仍在进行时引用的行不会消失。仅仅检查引用的行employee是不够的,因为仍在进行中的事务的影响在事务本身之外是不可见的。

于 2018-12-18T16:14:02.953 回答