0

After receiving a lot of help from this site I have finally created a set of functions that does the job I wanted. Thank you for that, but it seems there is one final issue, that of efficient memory usage. Here is the problem:
In postgreSQL 9.3, using postGIS 2.1 and pgRouting 2.0, I have created a function that calculates a route between 2 points using the pgrouting function pgr_trsp and returns a geometry (Linestring) value. Here is the code:

CREATE OR REPLACE FUNCTION fm_pgr2geom(edge1 integer, pos1 double precision, edge2 integer, pos2 double precision)
  RETURNS geometry AS
$BODY$
--We have to do a routing query. And declare a cursor for it
DECLARE resc CURSOR FOR
SELECT * FROM pgr_trsp (
    'SELECT * FROM th_2po_4pgr',
    $1, $2, $3, $4, false, true);
doline geometry[];
temp_point geometry;
geom geometry;
temp_rec RECORD;
n integer;
BEGIN

--Append all the edges
FOR temp_rec IN SELECT * FROM pgr_trsp (
    'SELECT * FROM th_2po_4pgr',
    $1, $2, $3, $4, false, true) LOOP
        doline := array_append(
        doline, (SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = temp_rec.id2));
END LOOP;
--Remove 1st and last edge
n := array_length (doline, 1);
doline := doline [2:n-1];
--Find startpoint and append to doline
doline := array_prepend(
    ST_LineInterpolatePoint((SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = $1),$2),doline);
--Append the endpoint
doline := array_append(
    doline,ST_LineInterpolatePoint((SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = $3),$4));
geom := ST_MakeLine(doline);
RETURN geom;
EXCEPTION
WHEN SQLSTATE 'XX000' THEN RETURN NULL;
WHEN SQLSTATE '38001' THEN RETURN NULL;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION fm_pgr2geom(integer, double precision, integer, double precision)
  OWNER TO postgres;

This function is used in another function to batch update a large table (800k+) with the geometry result. Here it is for reference:

CREATE OR REPLACE FUNCTION fm_seqrouting()
  RETURNS integer AS
$BODY$
--Declarations
DECLARE
    r record;
    i integer;
BEGIN
--CODE to calculate routes and update table
    i := 0;
FOR r IN 
    SELECT  veh_id
        ,dt
        ,map_edge_id                AS map_id1
        ,map_edge_pos               AS map_pos1
        ,lead(map_edge_id)      OVER w  AS map_id2
        ,lead(map_edge_pos)     OVER w  AS map_pos2
    FROM taxilocs
    WINDOW w AS (ORDER BY veh_id, dt)
    LOOP

        UPDATE taxilocs
            SET geom_route = fm_pgr2geom (r.map_id1,r.map_pos1,r.map_id2,r.map_pos2)
            WHERE r.veh_id = taxilocs.veh_id AND r.dt=taxilocs.dt;
        i := i + 1;

    END LOOP;
RETURN i;
END;
$BODY$
  LANGUAGE plpgsql;  

The exceptions are absolutely necessary, because they handle some cases where some data is missing from the aforementioned table, or the routing path cannot be found. However, it seems they are causing the problem, which is that the update query crashes after some minutes. The message I receive after some minutes of execution is:

ERROR: out of memory
SQL state: 53200
Detail: Failed on request of size 640000.

So the question is: How can I effectively use or recode this function to update the table I want? Any ideas?
Thank you in advance!

Petros Apotsos
  • 615
  • 2
  • 6
  • 13
  • What's the message you get when the query "crashes"? What version of PostgreSQL are you on? Where is the function doing the update that crashes? – Kuberchaun Dec 18 '13 at 13:57
  • @JustBob I updated my question with the information you asked. Maybe you can give me some hints now? – Petros Apotsos Dec 23 '13 at 15:12
  • Are there any index on your tables, can you provide the DDL? Is PostgreSQL compiled for 32bit or 64bit. I recall in the past folks having an issue like this on 32bit, but things working fine on 64bit. – Kuberchaun Dec 23 '13 at 16:19
  • @JustBob After some research, I found out that none of my tables have indexes. Since I am a fairly new user, can you give me some hints on how to choose indexes and how to define them? However, I feel that there is something "wrong" about how I have coded the exceptions. From my tests, it shows that the longer the query runs (that is the more times it enters the excpetion block), the heavier the memory load on the system is and I feel the repeating excpetions are the cause of it.Any clues on that? My system is Windows 7 64-bit – Petros Apotsos Jan 10 '14 at 08:21
  • A bit late, but I might face a similar problem. I calculate routes with `pgr_trsp`and try to insert them into a table. Have you solved it? – sequoia Oct 25 '19 at 10:14

1 Answers1

0

try to run the sql from the command line using below command psql -h < hostname> -d < dbname> -U < username> -p < portname> -f < filename>

Mahendran
  • 129
  • 1
  • 6