If you want use Postgres you need create a SELECT
using ST_Union
geometry ST_Union(geometry set g1field);
geometry ST_Union(geometry g1, geometry g2);
geometry ST_Union(geometry[] g1_array);
You can try this to create one array
Concatenate multiple rows in an array with SQL on PostgreSQL
I try this one in my states tables and work ok
SELECT ST_Union(a_geom)
FROM (SELECT array_agg(e.geom) as a_geom
FROM mapas.estadosven_region e
) t
I make another test and looks like this also may work
SELECT ID, ST_Union(geom)
FROM test_dissolve_function t
WHERE ST_isValid(geom)='t'
GROUP BY ID;