I've got regional data loaded into a PostGIS table and want to merge regions that share the same parent name (and ultimately output the data as KML.)  

This is my query (I'm using php):

    SELECT ST_AsTEXT(geom) FROM ST_UNION(ARRAY(SELECT geom FROM RegionsRaw WHERE (ParentName = '" . $parentName . "'))) AS geom;

When I run this script, I'm getting strange results: 

![map region][1]

It's pretty obvious that certain areas are a little messed up and I have a hunch that it's because some of the polygons I'm merging are next to each other (where I'm seeing success) while others are islands and separated from the rest (getting weird results).

Looking for some guidance from the community on this, thanks!


  [1]: https://i.sstatic.net/b7XA1.png