这使用的是PostgreSQL 10.4
我有一个分区表,设置为:
Table "public.foo_partitioned"
Column | Type | Collation | Nullable | Default
---------------------+-----------------------------+-----------+----------+----------------
bar_id | integer | | not null |
...
Partition key: LIST (bar_id)
Number of partitions: 10 (Use \d+ to list them.)bar_id上有一个引用表栏的外键。
当我尝试删除分区foo_partitioned_1时,如果bars上存在未完成的空闲事务查询,则删除会阻塞。这对我来说似乎非常奇怪,在对一个单独的表进行查询时,不允许删除此分区。我首先尝试分离分区,但也不起作用。我可以查询锁表并获得如下信息:
pid | usename | blocked_by | blocked_query
-------+-------------+------------+----------------------------
59897 | my_user | {59888} | DROP TABLE foo_partitioned_1当我运行以下命令时
select pgl.*, relname from pg_locks pgl join pg_class pgt on pgl.relation=pgt.oid where pid=59897;我得到了:
locktype | database | relation |...| virtualtransaction | pid | mode | granted | fastpath | relname
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+---------------------+---------+----------+-----------------------------------------
relation | 16401 | 2620 |...| 7/758 | 60266 | RowExclusiveLock | t | t | pg_trigger
relation | 16401 | 2608 |...| 7/758 | 60266 | RowExclusiveLock | t | t | pg_depend
relation | 16401 | 940755 |...| 7/758 | 60266 | AccessExclusiveLock | t | f | foo_partitioned
relation | 16401 | 941051 |...| 7/758 | 60266 | AccessExclusiveLock | t | f | uq_foo_partitioned_1
relation | 16401 | 742405 |...| 7/758 | 60266 | AccessExclusiveLock | f | f | bars
relation | 16401 | 2702 |...| 7/758 | 60266 | AccessShareLock | t | f | pg_trigger_oid_index
relation | 16401 | 941047 |...| 7/758 | 60266 | AccessShareLock | t | f | foo_partitioned_1
relation | 16401 | 941047 |...| 7/758 | 60266 | AccessExclusiveLock | t | f | foo_partitioned_1唯一未授予查询权限的锁是bars上的锁。然而,我完全不清楚为什么需要这个锁。
谢谢你所有的想法!
发布于 2018-06-09 05:08:27
这与分区无关。
外键在PostgreSQL中实现为触发器,当您删除分区时,必须删除实现(一半)外键的bars上的触发器。
现在删除触发器需要表上的ACCESS EXCLUSIVE锁,该锁将阻塞,直到表上的所有并发事务完成为止。
https://stackoverflow.com/questions/50767831
复制相似问题