ST_Relate
What is ST_Relate?
ST_Relate either returns the DE-9IM (Dimensionally Extended 9-Intersection Model) matrix describing the topological relationship between two geometries, or tests whether that relationship matches a pattern you supply. It is the most flexible primitive for topological predicates in PostGIS — every other topology predicate (ST_Contains, ST_Crosses, ST_Overlaps, ST_Touches, ST_Within) is a special case of a DE-9IM pattern.
1ST_Relate(geometry a, geometry b) → text
2ST_Relate(geometry a, geometry b, text intersectionMatrixPattern) → boolean
3ST_Relate(geometry a, geometry b, integer boundaryNodeRule) → textThe DE-9IM matrix is a 9-character string encoding the dimensions of the intersections of the interior, boundary, and exterior of each geometry. Each character is one of F (no intersection), 0, 1, 2, or * (any).
When would you use ST_Relate?
Use ST_Relate when no built-in predicate matches your exact topological requirement — for example, "line a passes through the interior of polygon b but does not share its boundary", or "the two polygons share only an edge and no area". It is also used in spatial QA to audit the full relationship matrix between geometries, and in cases where different OGC boundaryNodeRule values are needed to handle edge cases at polygon boundaries.
1-- Find lines that "cross through" a polygon without touching its boundary
2SELECT l.id
3FROM lines l, polygons p
4WHERE ST_Relate(l.geom, p.geom, 'T********');FAQs
What is the DE-9IM matrix?
A 9-character string describing the dimensions of pairwise intersections between the interior (I), boundary (B), and exterior (E) of two geometries: II IB IE BI BB BE EI EB EE. Each character is F, 0, 1, or 2. For example, T*F**F*** means "interiors intersect and exterior of a does not touch interior of b".
How do I use pattern matching?
Pass a 9-character pattern as the third argument. Valid pattern characters are T (intersection of any dimension), F (no intersection), 0/1/2 (exact dimension), and * (any). For example, T*F**F*** is the pattern for ST_Within.
Is ST_Relate index-accelerated?
Only when used as a predicate with certain pattern prefixes that imply intersection — and even then, it does not use the GiST index as directly as ST_Intersects. Prefer the specific predicate (ST_Contains, ST_Within, etc.) whenever one fits, because they are optimised for index use.
What is the boundaryNodeRule?
An integer controlling how geometric boundary is treated at endpoint-coincident line features. Rule 1 is OGC Mod-2 (default), 2 is Endpoint (all endpoints are boundary), 3 is MultiValent Endpoint, 4 is MonoValent Endpoint. Use non-default rules only in specialised topology-analysis workflows.