2013-11-26 46 views
3

我正在使用psycopg2从python访问postgresql数据库。当我尝试运行的最短路径pgrouting功能“pgr_astar”,我收到一条错误psycopg2.InterfaceError:连接已关闭/ pgr_astar

cur = db.cursor() 
psycopg2.InterfaceError: connection already closed 

基本上,所发生的事情是:当pgr_astar没有找到两点间的路径,它创建的崩溃数据库,并关闭到数据库的连接。

如何避免这种情况?

我试图函数与一个try /隔离除了和创建其自己的连接,因此,如果关闭没有问题:

conn = psycopg2.connect(...) 
try: 
    result = getRecords(conn,query_pgr_astar) 
    return float(result[0]['sum']) 
except: 
    return 500000000.0 
conn.close() 

但在这种情况下,我收到

cur.execute(query) 
psycopg2.DatabaseError: SSL SYSCALL error: EOF detected 

如何管理不可能的路径?

(我做了PGDB和老shortest_path_astar功能(不提供给我了)一样的,我没有问题)

PostgreSQL的日志:

2013-11-27 15:54:35 CET LOG: terminating any other active server processes 
2013-11-27 15:54:35 CET WARNING: terminating connection because of crash of another server process 
2013-11-27 15:54:35 CET DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory. 
2013-11-27 15:54:35 CET HINT: In a moment you should be able to reconnect to the database and repeat your command. 
2013-11-27 15:54:35 CET WARNING: terminating connection because of crash of another server process 
2013-11-27 15:54:35 CET DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory. 
2013-11-27 15:54:35 CET HINT: In a moment you should be able to reconnect to the database and repeat your command. 
2013-11-27 15:54:35 CET LOG: all server processes terminated; reinitializing 
2013-11-27 15:54:35 CET LOG: database system was interrupted; last known up at 2013-11-27 15:42:01 CET 
2013-11-27 15:54:35 CET LOG: database system was not properly shut down; automatic recovery in progress 
2013-11-27 15:54:35 CET LOG: record with zero length at 65/80875D60 
2013-11-27 15:54:35 CET LOG: redo is not required 
2013-11-27 15:54:35 CET LOG: autovacuum launcher started 
2013-11-27 15:54:35 CET LOG: database system is ready to accept connections 

我做的查询:

SELECT SUM(t2.length) FROM (SELECT id2 FROM pgr_astar('SELECT edge_id AS id, vertex_id1 AS source, vertex_id2 AS target, ' || '(CASE WHEN door = ''S'' THEN -1.0 ELSE (length * (CASE network WHEN ''0'' THEN 1.0 WHEN ''10'' THEN 1.2 WHEN ''20'' THEN 1.5 WHEN ''30'' THEN 2.0 ELSE 1.0 END) * (CASE type WHEN ''9.1'' THEN CASE WHEN level1 < level2 THEN 3.0 ELSE 2.0 END WHEN ''9.2'' THEN CASE WHEN level1 < level2 THEN 15.0 ELSE 12.0 END ELSE 1.0 END) + (CASE type WHEN ''9.3'' THEN 40.0 ELSE 0.0 END) ) END)::float8 AS cost, ' || '(CASE WHEN door_rev = ''S'' THEN -1.0 ELSE (length * (CASE network WHEN ''0'' THEN 1.0 WHEN ''10'' THEN 1.2 WHEN ''20'' THEN 1.5 WHEN ''30'' THEN 2.0 ELSE 1.0 END) * (CASE type WHEN ''9.1'' THEN CASE WHEN level1 < level2 THEN 3.0 ELSE 2.0 END WHEN ''9.2'' THEN CASE WHEN level1 < level2 THEN 15.0 ELSE 12.0 END ELSE 1.0 END) + (CASE type WHEN ''9.3'' THEN 40.0 ELSE 0.0 END) ) END)::float8 AS reverse_cost, ' || 'x1, y1, x2, y2 FROM edges', " + str(destination_route) + ", " + str(origin_route) + ", TRUE, TRUE)) as t1, edges as t2 where t1.id2 = t2.edge_id 

door ='S''表示门已关闭,在这种情况下,值为-1。

回答

3

我假设你可以在python中检查你的连接状态(我不使用python),所以你可以恢复并做一些合理的事情,比如重新连接。您可能需要休息几秒钟才能让数据库在重新连接之前从崩溃中恢复。

关于pgrouting问题,我希望看到一个简单的测试用例,可以在pgadmin或psql shell中重现此问题,并将其作为错误提交,以便我可以查看它。我们的代码不应该使服务器崩溃,但有时它确实会发生,我们需要解决这些问题。

+0

我在我的问题中添加了SQL查询。也许它可以帮助你。 – Antonin

+0

我问了一个单独的问题:[pgr_astar和不可能的边缘(例如,锁着的门)](http://stackoverflow.com/questions/20271054/pgr-astar-and-impossible-edges-eg-lockeddoors ) – Antonin

相关问题