PostgreSQL 9.3长时间运行的查询

时间:2015-03-19 12:11:33

标签: sql postgresql locking database-administration

 select vesseltrav0_.id as id1_0_, vesseltrav0_.created as created2_0_, vesseltrav0_.lastUpdated as lastUpda3_0_, vesseltrav0_.geoFenceId as geoFence4_0_, 
        vesseltrav0_.geoFenceName as geoFence5_0_, vesseltrav0_.mmsi as mmsi6_0_, vesseltrav0_.position as position7_0_, vesseltrav0_.timestamp as timestam8_0_, 
        vesseltrav0_.vesselEnteredTime as vesselEn9_0_, vesseltrav0_.vesselExitTime as vesselE10_0_, vesseltrav0_.vesselId as vesselI11_0_ 
 from VESSEL_ENTERED_GEOFENCE vesseltrav0_ where vesseltrav0_.geoFenceId=$1 and vesseltrav0_.vesselId=$2

我有一个守护进程将记录记录到数据库并定期更新它们,但它似乎每小时一次或两次这个相当无辜的SQL只会拒绝完成。

CREATE TABLE public.vessel_entered_geofence
(
  id bigint NOT NULL,
  geofenceid character varying(36),
  geofencename character varying(128),
  mmsi character varying(32),
  vesselenteredtime timestamp without time zone,
  vesselexittime timestamp without time zone,
  vesselid character varying(36),
  created timestamp without time zone,
  lastupdated timestamp without time zone,
  "position" geometry(Point,4326),
  "timestamp" timestamp without time zone,
  CONSTRAINT vessel_entered_geofence_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

CREATE INDEX vessel_entered_geofence_idx
  ON public.vessel_entered_geofence
  USING btree
  (mmsi COLLATE pg_catalog."default");

CREATE INDEX vessel_entered_geofence_gid_idx
ON public.vessel_entered_geofence
USING btree
(geofenceid COLLATE pg_catalog."default");

CREATE INDEX vessel_entered_geofence_vid_idx
ON public.vessel_entered_geofence
USING btree
(vesselid COLLATE pg_catalog."default");

当我在SQL上运行解释计划时,它使用索引并手动运行SQL会很快返回一个答案。所有连接都处于空闲状态(编辑:它们都是"在事务中空闲")并且它们不等待任何事情(根据pg_stats_activity)。

select * from pg_stat_activity order by backend_start

enter image description here http://i.stack.imgur.com/0pp8X.png

select a.*, b.relname from pg_locks a left outer join pg_class b on a.relation = b.oid

enter image description here https://puu.sh/gGE9n/96da69d341.png

我不确定下一步要确定问题是什么,它们似乎是空闲的,没有等待任何事情,并且桌​​子上没有应该导致它们挂起的锁(除非我&# 39;读错了)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

想想我终于解决了这个问题。它缺少Java代码中的begin / commit。

entityManager.getTransaction().begin();
//perform query
entityManager.getTransaction().commit();

即使查询只包含一个select语句,如果你没有将它封装在一个事务中,它也会挂起。