sraoss / pgsql-ivm

IVM (Incremental View Maintenance) development for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible deadlock in truncate scenerios

yugo-n opened this issue · comments

Reported by huyajun in https://www.postgresql.org/message-id/tencent_FCAF11BCA5003FD16BDDFDDA5D6A19587809%40qq.com

There is a risk of deadlock that is also acceptable for truncate scenarios.
The deadlock scene is as follows:

View definition: select * from base_a,base_b;

S1: truncate base_a; — only AccessExclusiveLock base_a and not run into after trigger
S2: insert into base_b; — The update has been completed and the incremental refresh is started in the after trigger,RowExclusive on base_b and ExclusiveLock on mv
S1: continue truncate mv, wait for AccessExclusiveLock on mv, wait for S2
S2: continue refresh mv, wait for AccessShardLock on base_a, wait for S1
So deadlock occurred

I confirm that this deadlock occurs when S2 starts before the before trigger is fired in S1. (Note that the lock on mv is acquired in BEFORE trigger instead of AFTER.)