Introduction to PostgreSQL circle Data Type

PostgreSQL is a widely used open-source relational database that provides many different data types. One of its special data types is circle.

Syntax

The circle data type represents a circle on a plane. Its syntax is as follows:

CIRCLE(point, radius)

where point is a tuple (x, y) representing the coordinates of the center of the circle, and radius is a floating-point number representing the radius of the circle.

Use Cases

The circle data type is suitable for storing circular regions on a plane, such as search radius on a map, circumscribed circles of rectangles, etc. It can be used together with other geometric types such as points, lines, polygons, etc.

Examples

Here are two examples of using the circle data type.

Example 1

Suppose we have a circle with center coordinates (3, 4) and a radius of 5 on a plane, it can be represented using the following SQL statement:

SELECT CIRCLE '((3, 4), 5)';

The result of the query will be:

 circle
----------------
 <(3,4),5>
(1 row)

Example 2

Suppose we want to query for stores within a radius of 10 kilometers around a certain location. We can represent the coordinates of that location as a point and use the circle data type to represent a circle with center at that point and a radius of 10 kilometers. Here is an example query:

SELECT * FROM stores
WHERE CIRCLE '((10.0, 20.0), 10000)' @> point;

where stores is a table containing store information, and point is a variable of point type representing the coordinates of a certain location. The above query will return all stores that are within a distance of 10 kilometers from that location.

Conclusion

The circle data type is suitable for storing circular regions on a plane. It can be used together with other geometric types and can be used in various scenarios such as search radius on a map, circumscribed circles of rectangles, etc.