I have a table with approximately 2.1 million tuples. Which has latitude and longitude columns. I am trying to convert this into a geographic type (point with SRID).
The function(procedure) I have written, works fine when I limit the entries (say : SELECT id,longitude,latitude FROM list_of_location limit 50).
CREATE OR REPLACE FUNCTION convertlatlon() RETURNS VOID AS $$
DECLARE rec RECORD;
BEGIN
FOR rec IN SELECT id,longitude,latitude FROM list_of_location
LOOP
UPDATE list_of_location SET location= concat('SRID=4326;POINT(',rec.longitude,' ',rec.latitude,')') WHERE id=rec.id;
END LOOP;
END;
$$ LANGUAGE 'plpgsql' ;
- When I try to run it on the entire table, PostgreSQL seems to do nothing. Have waited for an hour and a half.
- Consumes 99% of CPU on the core it is running.
- Does not spring any other instance of PostgreSQL to utilize other cores(since the request is from a single user?).
- Is this because of locks(row level)?
- How to circumvent this?
P.S. I am pretty sure this will get closed as off topic. But, I have to look for answers.
I don’t know what would cause it, but it sounds like there may be locks acquired in this operation. You can verify this easily though:
will tell you what locks are currently taken. And
will tell you, if there are locks, which transactions are sitting there idle waiting for a lock to be released. That should point you on the right direction.
This is how postgres works. A single session will spin off one backend. A single query does not do any sort multi-process or concurrent operation.
Why are you looping? I think you can do this in one shot. Why not do:
(assuming that gives you the correct result)