我检查了pg_stat_activity.query_start列出了耗时超过2分钟的查询,并在输出中找到了一个简单的插入内容。该表中只有4条记录,因此我无法想象索引会使其变慢。
我无法弄清楚原因。有人可以建议。
INSERT INTO events(event_id, alert_type, payload, received_at) VALUES('21', 'IMMEDIATE', '{"title":"IMMEDIATE ALERT - Far Western","message":"test immediate message.","url":"https://www.test-example.com"}', 1553356287962) RETURNING id;
\d events;
Table "public.events"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('events_id_seq'::regclass)
event_id | text | | not null |
alert_type | alert_type | | |
payload | json | | |
received_at | bigint | | not null |
created_at | timestamp without time zone | | | now()
updated_at | timestamp without time zone | | | now()
Indexes:
"events_pkey" PRIMARY KEY, btree (id)
"events_event_id_received_at_key" UNIQUE CONSTRAINT, btree (event_id, received_at)
Referenced by:
TABLE "event_polygons" CONSTRAINT "events_id_fkey" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
TABLE "notifications" CONSTRAINT "events_id_fkey" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Triggers:
create_notifications AFTER INSERT ON events DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE create_notifications()
插入发生在事务中,并且具有关联的multiPolygons,这些多多边形将插入子表event_polygons
devTestSetup=> SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '1 minute';
pid | duration | query | state
-------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------
14877 | 00:04:09.142522 | commit | active
18019 | 00:01:07.521619 | INSERT INTO events(event_id, alert_type, payload, received_at) VALUES('1', 'IMMEDIATE', '{"title":"IMMEDIATE ALERT - Far North Coast","message":"test immediate message.","url":"https://www.test-example.com"}', 1553356287742) RETURNING id | active
17172 | 00:02:08.377769 | INSERT INTO events(event_id, alert_type, payload, received_at) VALUES('1', 'IMMEDIATE', '{"title":"IMMEDIATE ALERT - Far North Coast","message":"test immediate message.","url":"https://www.test-example.com"}', 1553356287742) RETURNING id | active
16070 | 00:03:08.764471 | commit | idle
16071 | 00:03:07.424489 | commit | idle
16072 | 00:03:08.764385 | commit | idle
16074 | 00:03:08.764946 | commit