ST_NumGeometries
What is ST_NumGeometries?
ST_NumGeometries is a PostGIS function that returns the number of component geometries in a multi-geometry or GEOMETRYCOLLECTION. For non-multi simple geometries (single point, linestring, polygon), it returns 1.
ST_NumGeometries(geometry g) → integerRecent PostGIS versions accept any input including single geometries; older versions may return NULL for non-collections.
When would you use ST_NumGeometries?
Use ST_NumGeometries to iterate components by index (paired with ST_GeometryN), validate collection size, or filter rows with more than one component:
1SELECT id, ST_NumGeometries(geom) AS parts
2FROM regions
3WHERE GeometryType(geom) = 'MULTIPOLYGON'
4ORDER BY parts DESC;For bulk component iteration prefer ST_Dump, which is usually more idiomatic than a generate_series loop.
FAQs
What does ST_NumGeometries return for a single Point?
1 in modern PostGIS. This lets you write generic per-component code that works on both simple and multi types.
Does it recurse into nested collections?
No — it returns the top-level component count. A GEOMETRYCOLLECTION of MULTIPOLYGONs reports the number of top-level components, not the total number of polygons.
How do I iterate every component?
Either SELECT ST_GeometryN(g, generate_series(1, ST_NumGeometries(g))) or, more commonly, (ST_Dump(g)).geom. The ST_Dump form is usually cleaner.
What is the difference from ST_NPoints?
ST_NumGeometries counts components (parts). ST_NPoints counts vertices across all parts. Very different metrics.