synapse=# explain delete from tg_msg where mxid in (select event_id as mxid from event_json where event_id=mxid limit 1); QUERY PLAN -------------------------------------------------------------------------------------------------------------------- Delete on tg_msg (cost=0.00..2794528.19 rows=0 width=0) -> Seq Scan on tg_msg (cost=0.00..2794528.19 rows=304956 width=6) Filter: (SubPlan 1) SubPlan 1 -> Limit (cost=0.55..8.57 rows=1 width=44) -> Index Only Scan using event_json_event_id_key on event_json (cost=0.55..8.57 rows=1 width=44) Index Cond: (event_id = tg_msg.mxid) (7 rows) synapse=# select mxid from tg_msg where not exists (select 1 from event_json where event_id=mxid limit 1) limit 1; mxid ---------------------------------------------- $---CbxAsVhRDNWV8Y_GGCnpk1EBN4sjTAGliMVlwG30 (1 row) synapse=# explain delete from tg_msg where not exists (select 1 from event_json where event_id=mxid limit 1) limit 1; ERROR: syntax error at or near "limit" LINE 1: ...lect 1 from event_json where event_id=mxid limit 1) limit 1; ^ synapse=# explain delete from tg_msg where not exists (select 1 from event_json where event_id=mxid limit 1); QUERY PLAN --------------------------------------------------------------------------------------- Delete on tg_msg (cost=626149.02..661674.33 rows=0 width=0) -> Hash Anti Join (cost=626149.02..661674.33 rows=1 width=12) Hash Cond: (tg_msg.mxid = event_json.event_id) -> Seq Scan on tg_msg (cost=0.00..11800.13 rows=609913 width=51) -> Hash (cost=604435.01..604435.01 rows=975201 width=50) -> Seq Scan on event_json (cost=0.00..604435.01 rows=975201 width=50) (6 rows) synapse=# delete from tg_msg where not exists (select 1 from event_json where event_id=mxid limit 1);