ST_NumInteriorRings — Returns the number of interior rings (holes) of a Polygon.
integer ST_NumInteriorRings(geometry a_polygon);
Return the number of interior rings of a polygon geometry. Return NULL if the geometry is not a polygon.
This method implements the SQL/MM specification.
SQL-MM 3: 8.2.5
Changed: 2.0.0 - in prior versions it would allow passing a MULTIPOLYGON, returning the number of interior rings of first POLYGON.
Count the interior rings of a Polygon.
SELECT gid, field1, field2, ST_NumInteriorRings(geom) AS numholes FROM sometable;
For a MultiPolygon, dump its component Polygons and sum their interior ring counts.
SELECT gid, field1, field2, SUM(ST_NumInteriorRings(geom)) AS numholes
FROM (
SELECT gid, field1, field2, (ST_Dump(geom)).geom AS geom
FROM sometable
) AS foo
GROUP BY gid, field1, field2;