Repairs an invalid geometry without dropping any vertexes.
Syntax
geometry ST_MakeValid(geometry input);Parameters
| Parameter | Description |
|---|---|
input | The geometry to repair. |
Description
ST_MakeValid repairs invalid geometry objects without removing vertexes. It only processes geometries that fail the OGC validity check — valid geometries are returned as-is.
Supported input types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, and combinations thereof.
Output type changes to expect:
If the input geometry has missing dimensions, the function returns a geometry of lower dimensions or a collection of lower-dimension geometries.
If the input is a self-intersecting polygon, the function may return a MultiPolygon.
Examples
Repair a self-intersecting polygon
This example shows how to check whether a geometry is valid, repair it, and verify the result.
Step 1: Check validity.
SELECT ST_IsValid(geom), ST_IsValidReason(geom)
FROM your_table
WHERE NOT ST_IsValid(geom);Step 2: Repair invalid geometries.
The following example repairs a bowtie-shaped (self-intersecting) polygon. ST_MakeValid splits it into two valid polygons and returns a MultiPolygon.
SELECT ST_AsText(ST_MakeValid(
ST_GeomFromText('POLYGON((0 0, 1 1, 1 0, 0 1, 0 0))')
));Output:
MULTIPOLYGON(((0.5 0.5,0 0,0 1,0.5 0.5)),((1 0,0.5 0.5,1 1,1 0)))Bulk repair in a table
To repair all invalid geometries in a table while preserving the originals for review:
-- Add a column to store the original invalid geometry
ALTER TABLE your_table
ADD COLUMN geom_invalid geometry DEFAULT NULL;
-- Repair invalid geometries and save originals
UPDATE your_table
SET geom = ST_MakeValid(geom),
geom_invalid = geom
WHERE NOT ST_IsValid(geom);
-- Review repaired rows and their original invalidity reason
SELECT ST_IsValidReason(geom_invalid)
FROM your_table
WHERE geom_invalid IS NOT NULL;What's next
ST_IsValid — check whether a geometry is valid
ST_IsValidReason — get the reason a geometry is invalid