What I need is to unite intersecting polygons in a single geometry and leave others without change.
I am able to do this using the following query, which unites all of the polygons into a single geometry and then dumps it to the separate ones.
INSERT INTO unioned(geom) SELECT ST_MULTI((ST_Dump(ST_Union(geom))).geom) from footprints;
The problem is that I have more than a million rows in a database and
ST_Union operation never ends. Is there any way to achieve the same without joining all of the geometries?