我的数据库中的一切都在正常运行——读取、写入、大量活动。
然后我想在表格中添加一列foos
。该foos
表变得不可用。我退出了执行查询的代码并查看了系统中的锁。我发现下面的查询被锁定了 15 天。之后是我的表更改查询,然后是更多涉及该foos
表的查询。
什么会导致此查询卡住 15 天?这是在 9.1.3
select generate_report, b.count from
(select count(1), date_trunc('hour',f.event_happened_at) from
foos as f, bars as b
where age(f.event_happened_at) <= interval '24 hour' and f.id=b.foo_id and b.thing_type='Dog' and b.thing_id=26631
group by date_trunc('hour',f.event_happened_at)) as e
right join generate_report(date_trunc('hour',now()) - interval '24 hour',now(),interval '1 hour')
on generate_report = b.date_trunc
order by generate_report;
更新:信息来自pg_stat_activity
| 后端开始 | xact_start | 查询开始 | 等待 | --------+---------+----------+------------------+-- --------------+-----------------+----------------- -------- | 2012-11-19 18:38:40.029818+00 | 2012-11-19 18:38:40.145172+00 | 2012-11-19 18:38:40.145172+00 | f |
更新:解释的输出:
Merge Left Join (cost=14135.74..14138.08 rows=1000 width=16)
Merge Cond: (generate_report.generate_report = (date_trunc('hour'::text, f.event_happened_at)))
-> Sort (cost=12.97..13.47 rows=1000 width=8)
Sort Key: generate_report.generate_report
-> Function Scan on generate_report (cost=0.00..3.00 rows=1000 width=8)
-> Sort (cost=14122.77..14122.81 rows=67 width=16)
Sort Key: (date_trunc('hour'::text, f.event_happened_at))
-> HashAggregate (cost=14121.93..14122.17 rows=67 width=8)
-> Hash Join (cost=3237.14..14121.86 rows=67 width=8)
Hash Cond: (b.foo_id = f.id)
-> Index Scan using index_bars_on_thing_type_and_thing_id_and_baz on bars b (cost=0.00..10859.88 rows=10937 width=4)
Index Cond: (((thing_type)::text = 'Dog'::text) AND (thing_id = 26631))
-> Hash (cost=3131.42..3131.42 rows=30207 width=12)
-> Seq Scan on foos f (cost=0.00..3131.42 rows=30207 width=12)
Filter: (age((('now'::text)::date)::timestamp without time zone, event_happened_at) <= '24:00:00'::interval)