Alright, I can't test it on larger data set so it might not be fastest but should work.
If the polygons don't intersect (seems to be what you want) this will work. With
ST_INTERSECTION(<line>,<polygon>) we can get lines inside of polygons (one line per row), with
ST_Difference(<line>,ST_UNION(<polygon>)) we can get a geometry colection that holds pieces of line that don't fit inside any of the polygons. So now we just have to put those two results in one format and
SELECT ST_INTERSECTION(line,polygon) as inter
line AS l,
poly AS c
diff AS (
SELECT ST_Difference(l.line,ST_UNION(polygon)) geom FROM
line AS l
JOIN poly AS p ON
GROUP BY l.line)
FROM diff AS d
generate_series(1,ST_NumGeometries(geom)) as n FROM diff) n
If the polygons do intersect...I'll try tommorow.