ST_NRings
What is ST_NRings?
ST_NRings is a PostGIS function that returns the total number of rings in a POLYGON or MULTIPOLYGON, summing the exterior and interior rings across all component polygons.
ST_NRings(geometry g) → integerNon-polygonal input returns 0. Differs from ST_NumInteriorRings, which only counts holes of a single polygon.
When would you use ST_NRings?
Use ST_NRings when you need a single ring-count metric across a multipolygon — for example to spot over-complicated features, or to budget row counts when expanding rings via ST_DumpRings:
1SELECT id, ST_NRings(geom) AS total_rings
2FROM regions
3ORDER BY total_rings DESC
4LIMIT 10;Also useful in pipeline capacity planning — if a dataset has many rings per feature, downstream analyses that operate per ring will be expensive.
FAQs
Does ST_NRings count the exterior ring?
Yes. For a single polygon with no holes, ST_NRings = 1. For a polygon with two holes, ST_NRings = 3.
How is ST_NRings different from ST_NumInteriorRings?
ST_NumInteriorRings counts only interior rings of a single POLYGON and returns NULL for MULTIPOLYGON. ST_NRings counts all rings across a multi and returns 0 for non-polygons.
What does it return for non-polygon input?
Is ST_NRings fast?
Yes — it walks only the header structures, not the vertex data.