sraoss / pgsql-ivm

IVM (Incremental View Maintenance) development for PostgreSQL
Other
129 stars 12 forks source link

Possible deadlock in truncate scenerios #147

Open yugo-n opened 2 years ago

yugo-n commented 2 years ago

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

yugo-n commented 2 years ago

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.)