One table has a series of grid squares in it, and the other has millions of voronoi polygons in it. I need to create a table that has the difference between the two of the them so that I effectively get the holes in the voronoi layer.
I've tried several things like this:
SELECT st_astext(ST_Difference(density_squares.geom,
st_collect(density_squares_voronoi.geom))) As new_geom
FROM density_squares_voronoi left JOIN density_squares ON
ST_Intersects(density_squares.geom,density_squares_voronoi.geom)
group by
density_squares.geom;
But I get one output gridsquare for every voronoi polygon instead of the one cropped polygon like I need. Will I need to union the voronoi layer first or something?
I had to crack on and get it finished, so I just wrote some .net code to run it as a batch process. However I'm now trying to union the data and that is also taking a long time. A screen shot is here of what it looks like

and the sql i'm trying to use is this:
Select st_union(density_square_islands.islands)
into density_square_islands_union
from density_square_islands
where st_touches(density_square_islands.islands,density_square_islands.islands)
You can see the individual squares within the cropped islands. I just need to union the squares within each island into a single polygons