CREATE TABLE lines
(
    id   serial PRIMARY KEY,
    geom geometry(LINESTRINGZ, 3763) NOT NULL
);

CREATE INDEX idx_lines_geom ON lines USING gist (geom);

INSERT INTO lines (geom)
VALUES ('01EA0300000600000000000000C8C4F0400AD7A370C98D064148E17A14AE8B77403333333307C5F0400AD7A370C98D064166666666668A774048E17A1406C5F0403E0AD7A3A68D064148E17A14AE8777406666666626C5F040D7A3703DA08D0641AE47E17A14567740295C8FC235C5F040295C8FC2BF8D0641713D0AD7A350774066666666FEC4F040B81E85EBD78D0641713D0AD7A3507740'), -- replace with the line below to have no error
       ('01020000A0B30E00000200000066666666FEC4F040B81E85EBD78D0641713D0AD7A3507740F6285C8FE6C4F0400AD7A370C98D06410000000000607740');



SELECT topology.droptopology('lines_topo')
WHERE exists(SELECT * FROM topology.topology WHERE name = 'lines_topo');
SELECT topology.createtopology('lines_topo', 3763, 0, TRUE);
SELECT topology.addtopogeometrycolumn('lines_topo', 'public', 'lines', 'topo', 'LINE');

DO
$$
    DECLARE
        id_line integer;
    BEGIN
        FOR id_line IN SELECT id FROM lines ORDER BY id
            LOOP
                BEGIN
                    UPDATE lines
                    SET topo = topology.totopogeom(geom, 'lines_topo', 1, 0)
                    WHERE id = id_line;
                EXCEPTION
                    WHEN OTHERS THEN
                        RAISE WARNING 'Loading of record % failed: %', id_line, sqlerrm;
                END;
            END LOOP;
    END
$$;
