LineStrings can be sliced not only using other geometries, but also based on a fraction of their length. This is done by the ST_LineSubstring function. It accepts three arguments: an input geometry, which must be a LineString (a MultiLineString must be merged using ST_LineMerge), the starting fraction, and the ending fraction. For example, we can extract the first half of the Odra river as follows:
SELECT ST_LineSubstring(
(SELECT ST_LineMerge(ST_Collect(wkb_geometry)) FROM lines WHERE name='Odra' AND waterway='river'),
0,
0.5
)

When distance values are needed, the ST_Length function can be used to calculate the necessary fraction. For example, say that railway line No. 177 has been scheduled for repair between the 13th and 24th kilometer. How do we draw this section on a map?
WITH line_geom AS (SELECT ST_LineMerge((SELECT wkb_geometry FROM multilinestrings WHERE osm_id = '4581657')))
SELECT ST_LineSubstring(
line_geom.st_linemerege,
13000 / ST_Length(line_geom.st_linemerge),
24000 / ST_Length(line_geom.st_linemerge)
) FROM line_geom;
The WITH clause is called a common table expression (CTE). It allows us to store the geometry in question as a virtual table after merging as a virtual table, so we don't have to type SELECT ST_LineMerge... and its ID over and over again.
The geometry is stored in the EPSG:32633 coordinate system, so its units are meters. Therefore, we need to multiply the kilometer values by 1,000. The result looks like the following:
