All Products
Search
Document Center

Hologres:PostGIS spatial functions

Last Updated:Mar 17, 2026

PostGIS extends Hologres with spatial data types, functions, and operators. Use it to store, query, and analyze geographic data — from bounding box filters and proximity searches to polygon intersection checks and distance calculations.

Hologres supports PostGIS 3.0.0. In Hologres V1.3 and later, most PostGIS functions run on the Hologres Query Engine (HQE) developed by Alibaba Cloud, which delivers better query performance. Earlier versions fall back to the PostgreSQL Query Engine (PQE).

Limits

  • Fields of the geometry or geography data type cannot be used as primary keys. Use an integer surrogate key instead.

  • Spatial indexes are not supported. For large datasets, apply bounding box pre-filters (such as ST_MakeBox2D + ST_Covers) before more expensive spatial predicates to reduce the scan scope.

  • In Hologres instances earlier than V1.3, all PostGIS functions run on PQE. Performance may be lower than HQE.

Install the PostGIS extension

Run the following statement as a superuser to install PostGIS in a database. Installation is per-database — repeat this step for each database that needs spatial support.

CREATE EXTENSION IF NOT EXISTS postgis;
PostGIS cannot be installed in the pg_catalog schema.

To verify the installation, run:

SELECT postgis_full_version();

A successful install returns a version string such as POSTGIS="3.0.0 ...".

To remove the extension:

DROP EXTENSION postgis;
Important

Do not use DROP EXTENSION postgis CASCADE. The CASCADE option drops all dependent objects — including PostGIS data, roaring bitmap data, Proxima data, binary log data, BSI data, and metadata such as tables, views, and server data — and cannot be undone.

Create and query a geometry table

PostGIS supports two spatial data types in Hologres: geometry (planar/Cartesian coordinates) and geography (spherical longitude/latitude coordinates). For details on the geography type, see the PostGIS geography documentation.

The geometry type is more commonly used. The following steps show how to create a geometry table and run spatial queries.

1. Create a geometry table

When creating a table, you can specify a geometry subtype. Supported subtypes: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon.

Without a subtype:

CREATE TABLE holo_gis_1 (
  id   INT,
  geom geometry,
  PRIMARY KEY (id)
);

With a subtype and spatial reference system identifier (SRID):

CREATE TABLE holo_gis_2 (
  id   INT,
  geom geometry(point, 4326),
  PRIMARY KEY (id)
);

In this example, the subtype is Point and the SRID is 4326 (WGS 84). If no SRID is specified, the default is 0. For more information about SRIDs, see the PostGIS documentation.

2. Insert spatial data

-- Without SRID
INSERT INTO holo_gis_1 VALUES (1, ST_GeomFromText('point(116 39)'));

-- With SRID 4326
INSERT INTO holo_gis_2 VALUES (1, ST_GeomFromText('point(116 39)', 4326));

For more information about spatial functions, see Spatial functions.

3. Query spatial data

After inserting data, you can run the following types of queries.

Rectangular range query

Returns all points within a bounding box.

Without SRID:

SELECT st_astext(geom)
FROM holo_gis_1
WHERE ST_Covers(
  ST_MakeBox2D(ST_Point(116, 39), ST_Point(117, 40)),
  geom
);

With SRID:

SELECT st_astext(geom)
FROM holo_gis_2
WHERE ST_Covers(
  ST_SetSRID(ST_MakeBox2D(ST_Point(116, 39), ST_Point(117, 40)), 4326),
  geom
);

Both queries return:

st_astext
-------------
POINT(116 39)

Polygon intersection check

To find points inside or on the boundary of a polygon, use ST_Covers. The syntax is the same as the rectangular range query above — ST_MakeBox2D produces a rectangular polygon that includes its boundary in the coverage test.

Without SRID:

SELECT st_astext(geom)
FROM holo_gis_1
WHERE ST_Covers(
  ST_MakeBox2D(ST_Point(116, 39), ST_Point(117, 40)),
  geom
);

With SRID:

SELECT st_astext(geom)
FROM holo_gis_2
WHERE ST_Covers(
  ST_SetSRID(ST_MakeBox2D(ST_Point(116, 39), ST_Point(117, 40)), 4326),
  geom
);

Both queries return:

st_astext
-------------
POINT(116 39)

Spatial functions

PostGIS provides spatial functions to convert and analyze geometry values. The function syntax uses the following parameters:

  • geom: a geometry value or an expression that evaluates to geometry

  • precision: INTEGER; coordinate precision in the range 1–20; default 15

  • index: INTEGER; a 1-based index unless otherwise noted

  • srid: INTEGER; a spatial reference system identifier

The Required engine column indicates which query engine executes each function:

EngineWhen it appliesPerformance
HQE (Hologres Query Engine)Hologres V1.3 and laterHigher — optimized for analytical workloads
PQE (PostgreSQL Query Engine)All versions; required for certain functionsLower — compatibility mode

For the complete PostGIS function specification, see the PostGIS reference documentation.

Geometry constructors

All functions in this group require HQE in Hologres V1.3.

FunctionSyntaxReturnsDescription
ST_LineFromMultiPointST_LineFromMultiPoint(geom)GEOMETRYCreates a linestring from a multipoint geometry, preserving point order. The returned geometry has the same SRID as the input.
ST_MakeEnvelopeST_MakeEnvelope(xmin, ymin, xmax, ymax)
ST_MakeEnvelope(xmin, ymin, xmax, ymax, srid)
GEOMETRY (POINT, LINESTRING, or POLYGON)Creates a geometry from corner coordinates. Returns a point if the coordinates collapse to a point, a linestring if they form a line, or a polygon otherwise. If an SRID is provided, the returned geometry uses that SRID.
ST_MakeLineST_MakeLine(geom1, geom2)GEOMETRY (LINESTRING)Creates a linestring from two input geometries.
ST_MakePointST_MakePoint(x, y)GEOMETRY (POINT)Creates a point from coordinate values.
ST_PointST_Point(x, y)GEOMETRY (POINT)Creates a point from coordinate values.
ST_PolygonST_Polygon(linestring, srid)GEOMETRY (POLYGON)Creates a polygon whose exterior ring is the input linestring, with the given SRID.

Geometry accessors

All functions in this group require HQE in Hologres V1.3.

FunctionSyntaxReturnsDescription
GeometryTypeGeometryType(geom)VARCHARReturns the subtype name of the input geometry as a string.
ST_BoundaryST_Boundary(geom)GEOMETRYReturns the boundary of the input geometry. An empty geometry returns the input as-is; a point or non-empty multipoint returns an empty geometry collection; a linestring returns a multipoint of its boundary points; a polygon without interior rings returns a closed linestring; a polygon with interior rings or a multipolygon returns a multilinestring of all boundary rings.
ST_DimensionST_Dimension(geom)INTEGERReturns the intrinsic dimension of the geometry subtype.
ST_EnvelopeST_Envelope(geom)GEOMETRYReturns the minimum bounding box of the input geometry. Returns a point if the box degenerates to a point, a two-point linestring if it is one-dimensional, or a clockwise-oriented polygon otherwise. The returned geometry has the same SRID as the input.
ST_ExteriorRingST_ExteriorRing(geom)GEOMETRY (LINESTRING)Returns the exterior ring of a polygon as a closed linestring.
ST_GeometryNST_GeometryN(geom, index)GEOMETRYReturns the geometry at the given 1-based index. For simple geometries (point, linestring, polygon) with index 1, returns the geometry itself; otherwise returns null. For collections, returns the element at the index.
ST_GeometryTypeST_GeometryType(geom)VARCHARReturns the subtype name of the input geometry as a string.
ST_InteriorRingNST_InteriorRingN(geom, index)GEOMETRY (LINESTRING)Returns the interior ring of a polygon at the given index position as a closed linestring.
ST_IsClosedST_IsClosed(geom)BOOLEANReturns true if the geometry is closed. A point or multipoint is always closed. A linestring is closed when its start and end points coincide. A polygon is closed when all rings are non-empty and their start and end points coincide.
ST_IsCollectionST_IsCollection(geom)BOOLEANReturns true if the geometry is a GEOMETRYCOLLECTION, MULTIPOINT, MULTILINESTRING, or MULTIPOLYGON.
ST_IsEmptyST_IsEmpty(geom)BOOLEANReturns true if the geometry contains no points.
ST_IsPolygonCCWST_IsPolygonCCW(geom)BOOLEANReturns true if the input polygon is oriented counterclockwise. Also returns true for points, linestrings, multipoints, and multilinestrings, and for geometry collections where all elements are counterclockwise.
ST_IsSimpleST_IsSimple(geom)BOOLEANReturns true if the geometry has no anomalous geometric points such as self-intersections.
ST_NPointsST_NPoints(geom)INTEGERReturns the number of points in the geometry.
ST_NRingsST_NRings(geom)INTEGERReturns the number of rings in the geometry.
ST_NumGeometriesST_NumGeometries(geom)INTEGERReturns the number of elements in a geometry collection.
ST_NumInteriorRingsST_NumInteriorRings(geom)INTEGERReturns the number of interior rings in a polygon.
ST_NumPointsST_NumPoints(geom)INTEGERReturns the number of points in the geometry.
ST_PointNST_PointN(geom, index)GEOMETRY (POINT)Returns the point at the given index in a linestring. Negative index values count from the end: -1 returns the last point.
ST_PointsST_Points(geom)GEOMETRY (MULTIPOINT)Returns all non-empty points in the geometry as a multipoint. Duplicate points, including ring start and end points, are preserved.
ST_StartPointST_StartPoint(geom)GEOMETRYReturns the first point of a linestring. The returned geometry has the same SRID as the input.
ST_XST_X(point)DOUBLEReturns the X coordinate of a point.
ST_YST_Y(point)DOUBLEReturns the Y coordinate of a point.

Geometry editors

All functions in this group require HQE in Hologres V1.3.

FunctionSyntaxReturnsDescription
ST_AddPointST_AddPoint(geom1, geom2)GEOMETRYReturns a linestring with a point added to it.
ST_MultiST_Multi(geom)GEOMETRY (MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, or GEOMETRYCOLLECTION)Converts a geometry to its corresponding multi-type. If the input is already a multi-type or collection, returns a copy.
ST_RemovePointST_RemovePoint(geom, index)GEOMETRYReturns a linestring with the point at the given zero-based index removed. The returned geometry has the same SRID as the input.
ST_ReverseST_Reverse(geom)GEOMETRYReverses the vertex order of a linear or areal geometry. For a point or multipoint, returns a copy. For a collection, reverses vertices for each element.
ST_SetPointST_SetPoint(geom1, index, geom2)GEOMETRYReturns a linestring with the point at the given index replaced by the input point's coordinates.

Geometry validation

FunctionSyntaxReturnsRequired engineDescription
ST_IsValidST_IsValid(geom)BOOLEANPQEReturns true if the geometry is valid according to the OGC specification.

Spatial reference system functions

All functions in this group require HQE in Hologres V1.3.

FunctionSyntaxReturnsDescription
ST_SetSRIDST_SetSRID(geom, srid)GEOMETRYReturns the input geometry with its SRID updated to the given value. Does not reproject the coordinates.
ST_SRIDST_SRID(geom)INTEGERReturns the SRID of the input geometry.

Geometry input

FunctionSyntaxReturnsRequired engineDescription
ST_GeomFromTextST_GeomFromText(wkt_string)
ST_GeomFromText(wkt_string, srid)
GEOMETRYPQEConstructs a geometry from its well-known text (WKT) representation.

Geometry output

All functions in this group require HQE in Hologres V1.3.

FunctionSyntaxReturnsDescription
ST_AsBinaryST_AsBinary(geom)BYTEAReturns the well-known binary (WKB) representation of the geometry, encoded as a hex string using ASCII characters 09 and AF.
ST_AsEWKBST_AsEWKB(geom)BYTEAReturns the extended well-known binary (EWKB) representation of the geometry.
ST_AsEWKTST_AsEWKT(geom)VARCHARReturns the extended well-known text (EWKT) representation of the geometry.
ST_AsGeoJSONST_AsGeoJSON(geom)
ST_AsGeoJSON(geom, precision)
VARCHARReturns the GeoJSON representation of the geometry.
ST_AsTextST_AsText(geom)
ST_AsText(geom, precision)
VARCHARReturns the WKT representation of the geometry.

Spatial relationship predicates

Most functions in this group run on PQE. ST_DWithin_S2 runs on HQE (Hologres V2.0.8 and later).

FunctionSyntaxReturnsRequired engineDescription
ST_ContainsST_Contains(geom1, geom2)BOOLEANPQEReturns true if every point of geom2 is in geom1 and their interiors intersect. Equivalent to ST_Within(geom2, geom1).
ST_ContainsProperlyST_ContainsProperly(geom1, geom2)BOOLEANPQEReturns true if both geometries are non-empty and all points of geom2 lie in the interior (not the boundary) of geom1.
ST_CoveredByST_CoveredBy(geom1, geom2)BOOLEANPQEReturns true if every point of geom1 is in geom2. Equivalent to ST_Covers(geom2, geom1).
ST_CoversST_Covers(geom1, geom2)BOOLEANPQEReturns true if every point of geom2 is in geom1. Equivalent to ST_CoveredBy(geom2, geom1).
ST_CrossesST_Crosses(geom1, geom2)BOOLEANN/AReturns true if the two geometries intersect.
ST_DisjointST_Disjoint(geom1, geom2)BOOLEANN/AReturns true if the two geometries share no points.
ST_DWithinST_DWithin(geom1, geom2, threshold)BOOLEANPQEReturns true if the Euclidean distance between the two geometries does not exceed the threshold.
ST_DWithin_S2ST_DWithin_S2(x1, y1, x2, y2, threshold)BOOLEANHQE (V2.0.8)Returns true if the spherical distance between two geographic locations is less than or equal to threshold (unit: meters). Parameters in order: longitude of Location 1, latitude of Location 1, longitude of Location 2, latitude of Location 2, distance threshold. Input values cannot be constants.
ST_EqualsST_Equals(geom1, geom2)BOOLEANPQEReturns true if the two geometries have equal point sets and their interiors intersect.
ST_IntersectsST_Intersects(geom1, geom2)BOOLEANPQEReturns true if the two geometries share at least one point.
ST_TouchesST_Touches(geom1, geom2)BOOLEANPQEReturns true if the two geometries touch — they intersect but share no interior points.
ST_WithinST_Within(geom1, geom2)BOOLEANPQEReturns true if every point of geom1 is in geom2 and their interiors intersect. Equivalent to ST_Contains(geom2, geom1).

Measurement functions

FunctionSyntaxReturnsRequired engineDescription
ST_AngleST_Angle(geom1, geom2, geom3)
ST_Angle(geom1, geom2, geom3, geom4)
DOUBLEPQEReturns the clockwise angle in radians in the range [0, 2π). With three points, measures the rotation from P1 to P3 around P2. With four points, measures the angle between directed lines P1–P2 and P3–P4; returns null if P1 equals P2 or P3 equals P4.
ST_AreaST_Area(geom)DOUBLEHQE (V1.3)Returns the Cartesian area of the geometry in the same units as the coordinate system. Returns 0 for points, linestrings, and their multi-types. For collections, returns the sum of all element areas.
ST_AzimuthST_Azimuth(point1, point2)DOUBLEHQE (V1.3)Returns the north-based Cartesian azimuth defined by two points.
ST_DistanceST_Distance(geom1, geom2)DOUBLEHQE (V1.3)Returns the spherical central angle between two geometries.
ST_Distance_Sphere_S2ST_Distance_Sphere_S2(x1, y1, x2, y2)DOUBLEHQE (V2.0.8)Returns the spherical distance between two geographic locations in meters. Parameters in order: longitude of Location 1, latitude of Location 1, longitude of Location 2, latitude of Location 2. Valid latitude range: [−90, +90]. Valid longitude range: [−180, +180]. Input values cannot be constants.
ST_LengthST_Length(geom)DOUBLEHQE (V1.3)Returns the Cartesian length of a linear geometry in the same units as the coordinate system. Returns 0 for points, multipoints, and areal geometries. For collections, returns the total length.
ST_PerimeterST_Perimeter(geom)DOUBLEHQE (V1.3)Returns the Cartesian perimeter (boundary length) of an areal geometry in the same units as the coordinate system. Returns 0 for points, multipoints, and linear geometries. For collections, returns the sum of all element perimeters.

Overlay function

FunctionSyntaxReturnsRequired engineDescription
ST_IntersectionST_Intersection(geom1, geom2)GEOMETRYHQE (V1.3)Returns the geometric intersection of two geometries.

Geometry processing functions

FunctionSyntaxReturnsRequired engineDescription
ST_BufferST_Buffer(geography, float8)GEOMETRYPQEReturns the geometry representing all points within the given distance from the input geography.
ST_ConvexHullST_ConvexHull(geom)GEOMETRYHQE (V1.3)Returns the convex hull of all non-empty points in the input geometry.
ST_SimplifyST_Simplify(geom, tolerance)GEOMETRYHQE (V1.3)Returns a simplified copy of the geometry using the Ramer-Douglas-Peucker algorithm with the given tolerance. Topology may not be preserved.

Bounding box functions

All functions in this group run on PQE.

FunctionSyntaxReturnsDescription
ST_XMaxST_XMax(geom)DOUBLEReturns the maximum X coordinate of the geometry.
ST_XMinST_XMin(geom)DOUBLEReturns the minimum X coordinate of the geometry.
ST_YMaxST_YMax(geom)DOUBLEReturns the maximum Y coordinate of the geometry.
ST_YMinST_YMin(geom)DOUBLEReturns the minimum Y coordinate of the geometry.

Linear referencing function

FunctionSyntaxReturnsRequired engineDescription
ST_LineInterpolatePointST_LineInterpolatePoint(geom, fraction)GEOMETRY (POINT)HQE (V1.3)Returns the point at a fractional distance along the line, measured from the start. For example, fraction=0.5 returns the midpoint.

Best practices for using spatial functions

For end-to-end examples of common geographic analysis patterns, see Use spatial functions to query data.