All Products
Search
Document Center

PolarDB:pg_sphere

Last Updated:Mar 28, 2026

pg_sphere is a PostgreSQL extension that adds spherical data types, operators, and functions to PolarDB for PostgreSQL (Compatible with Oracle). Use it to store and query positions on a sphere — such as geographic coordinates on Earth or star positions in the sky — and to calculate distances and areas between spherical objects.

Prerequisites

pg_sphere is supported on the following engine versions:

EngineMinimum revision version
PolarDB for PostgreSQL (Compatible with Oracle) 2.02.0.14.1.0
PolarDB for PostgreSQL (Compatible with Oracle) 1.01.1.27

To check the revision version of your cluster, run:

SHOW polar_version;

Install and remove the extension

To install pg_sphere:

CREATE EXTENSION pg_sphere;

To remove pg_sphere:

DROP EXTENSION pg_sphere;

Data types

pg_sphere provides the following spherical data types:

Data typeUse cases
POINT (spoint)Geographic sites on Earth, star positions on the sky sphere, positions on a planet
CIRCLE (scircle)Search areas around a location, round celestial clusters, positions with an undirected error radius
LINEGreat circle segments and arcs on a sphere
ELLIPSESElliptical regions on a sphere, such as nebulae or observation error ellipses
PATHSequences of connected arcs on a sphere
POLYGONArbitrary polygonal regions on a sphere
EULER TRANSFORMATIONRotations and coordinate system transformations on a sphere

Specify a spherical point

Three formats are accepted for spoint literals.

Longitude and latitude in radians:

SELECT spoint '(0.1,-0.2)';
spoint
--------------
 (0.1 , -0.2)
(1 row)

Longitude and latitude in degrees (append `d`):

SELECT spoint '( 10.1d, -90d)';
spoint
----------------------------------------
 (0.176278254451427 , -1.5707963267949)
(1 row)

Degrees, minutes, and seconds (DMS):

SELECT spoint '( 10d 12m 11.3s, -13d 14m)';
spoint
----------------------------------------
 (0.176278254451427 , -1.5707963267949)
(1 row)
In DMS notation, 1 degree is 1/360 of a full circle, 1 minute is 1/60 of a degree, and 1 second is 1/60 of a minute.

Constructors

Constructors build spherical data types from raw numeric values. They accept all seven data types: POINT, EULER TRANSFORMATION, CIRCLE, LINE, ELLIPSES, PATH, and POLYGON.

The following example constructs a spherical point at longitude 270°, latitude −30° by converting degrees to radians:

SELECT spoint (270.0 * pi() / 180.0, -30.0 * pi() / 180.0) AS spoint;
spoint
-----------------------------------------
 (4.71238898038469 , -0.523598775598299)
(1 row)

Operators

pg_sphere supports the following operator categories:

CategoryDescription
CastingConvert between spherical and other data types
EqualityTest whether two spherical objects are equal
Contain and overlapTest spatial containment and overlap relationships
Crossing of linesTest whether two spherical lines cross
Distance (<->)Calculate the angular distance between two spherical objects
Length and circumferenceMeasure arc length or circumference
CenterReturn the center point of a spherical object

Distance example

The <-> operator returns the angular distance in radians. The following example calculates the distance between two circles centered at (0°, 20°) and (0°, 40°), each with a radius of 2°, and converts the result to degrees:

SELECT 180 * ( scircle '<(0d,20d),2d>' <-> scircle '<(0d,40d),2d>' ) / pi() AS dist;
dist
------
   16
(1 row)

Functions

pg_sphere provides three categories of functions:

CategoryDescription
Area functionsCalculate the area of a spherical object
Path functionsOperate on spherical paths
Distance functionsCalculate distances between spherical objects

Area function

The following example calculates the area of a circle centered at the north pole (90°) with a radius of 60° and expresses the result as a multiple of π:

SELECT area( scircle '<(0d,90d),60d>' ) / pi() AS area;
 area
------
    1
(1 row)

Point coordinate functions

The long(spoint) function has been renamed to long_sphere(spoint) in this implementation.
SELECT long_sphere ( spoint '(10d,20d)' ) * 180.0 / pi() AS longitude;
longitude
----------
       10
(1 row)
SELECT lat ( spoint '(10d,20d)' ) * 180.0 / pi() AS latitude;
latitude
----------
      20
(1 row)

References

For the full list of pg_sphere data types, operators, and functions, see the pg_sphere documentation.