ST_AddPoint
What is ST_AddPoint?
ST_AddPoint is a PostGIS geometry editor that inserts a point into an existing LineString. It returns a new LineString with the point added either at a specific zero-based vertex index or appended at the end when the position is omitted or set to -1.
1ST_AddPoint(geometry linestring, geometry point) → geometry
2ST_AddPoint(geometry linestring, geometry point, integer position) → geometryThe function only works on LineString inputs — passing a MultiLineString or any other geometry type raises an error. The position index is zero-based and must be between 0 and the number of existing points.
When would you use ST_AddPoint?
Use ST_AddPoint when you need to programmatically extend or densify a linear feature — for instance, appending a new GPS fix to a recorded track, inserting a control point in a digitized road centreline to improve alignment, or splicing a new waypoint into a pipeline route at a known chainage. It is also useful in data-cleaning scripts that patch truncated lines by re-attaching a final vertex recovered from a separate source.
In combination with ST_LineLocatePoint and ST_LineSubstring, it lets you reconstruct edited linestrings where a new vertex must land at a specific fractional position along the original geometry.
FAQs
What index should I use to append a point?
Omit the position argument, or pass -1, to append the point at the end of the LineString. Passing 0 inserts the point as the new first vertex. Any index greater than the current vertex count will raise an "Invalid offset" error.
Does ST_AddPoint work on MultiLineStrings or Polygons?
No. ST_AddPoint accepts only a single LineString. To edit a MultiLineString, use ST_Dump to extract its component lines, edit the relevant one, then recombine with ST_Collect or ST_MakeLine. Polygons must be decomposed to their rings first.
Does the added point have to share the same dimension (Z/M) as the line?
Yes. If the LineString is 3D (has Z values), the added point should also carry a Z coordinate — otherwise the new vertex will receive NaN for missing ordinates. Use ST_Force3D, ST_Force2D or ST_Force4D to match dimensions before calling ST_AddPoint.
How is ST_AddPoint different from ST_SetPoint?
ST_AddPoint inserts a new vertex, increasing the total vertex count by one. ST_SetPoint replaces an existing vertex at a given index without changing the total count. Use the former to extend geometry, the latter to correct an existing coordinate.