Postgresql 中文操作指南

9.20. Range/Multirange Functions and Operators #

有关范围类型的概述,请参阅 Section 8.17

See Section 8.17 for an overview of range types.

Table 9.55 显示可用于范围类型的专有运算符。 Table 9.56 显示可用于多范围类型的专有运算符。除了这些之外, Table 9.1 中所示的通常比较运算符适用于范围和多范围类型。比较运算符首先按范围下限排序,仅当这些下限相等时,它们才会比较上限。多范围运算符比较每个范围,直至遇到不相等的范围为止。这通常不会产生有用的总体排序,但会提供运算符来允许在范围内构造唯一索引。

Table 9.55 shows the specialized operators available for range types. Table 9.56 shows the specialized operators available for multirange types. In addition to those, the usual comparison operators shown in Table 9.1 are available for range and multirange types. The comparison operators order first by the range lower bounds, and only if those are equal do they compare the upper bounds. The multirange operators compare each range until one is unequal. This does not usually result in a useful overall ordering, but the operators are provided to allow unique indexes to be constructed on ranges.

Table 9.55. Range Operators

Operator

Description

Example(s)

anyrange @> anyrangeboolean

Does the first range contain the second?

int4range(2,4) @> int4range(2,3)t

anyrange @> anyelementboolean

Does the range contain the element?

'[2011-01-01,2011-03-01)'::tsrange @> '2011-01-10'::timestampt

anyrange <@ anyrangeboolean

Is the first range contained by the second?

int4range(2,4) <@ int4range(1,7)t

anyelement <@ anyrangeboolean

Is the element contained in the range?

42 <@ int4range(1,7)f

anyrange && anyrangeboolean

Do the ranges overlap, that is, have any elements in common?

int8range(3,7) && int8range(4,12)t

anyrange << anyrangeboolean

Is the first range strictly left of the second?

int8range(1,10) << int8range(100,110)t

anyrange >> anyrangeboolean

Is the first range strictly right of the second?

int8range(50,60) >> int8range(20,30)t

anyrange &< anyrangeboolean

Does the first range not extend to the right of the second?

int8range(1,20) &< int8range(18,20)t

anyrange &> anyrangeboolean

Does the first range not extend to the left of the second?

int8range(7,20) &> int8range(5,10)t

anyrange _-

-_ anyrangeboolean

Are the ranges adjacent?

_numrange(1.1,2.2) -

- numrange(2.2,3.3)_ → t

anyrange + anyrangeanyrange

Computes the union of the ranges. The ranges must overlap or be adjacent, so that the union is a single range (but see range_merge()).

numrange(5,15) + numrange(10,20)[5,20)

anyrange * anyrangeanyrange

Computes the intersection of the ranges.

int8range(5,15) * int8range(10,20)[10,15)

anyrange - anyrangeanyrange

Computes the difference of the ranges. The second range must not be contained in the first in such a way that the difference would not be a single range.

int8range(5,15) - int8range(10,20)[5,10)

Table 9.56. Multirange Operators

Operator

Description

Example(s)

anymultirange @> anymultirangeboolean

Does the first multirange contain the second?

'{[2,4)}'::int4multirange @> '{[2,3)}'::int4multiranget

anymultirange @> anyrangeboolean

Does the multirange contain the range?

'{[2,4)}'::int4multirange @> int4range(2,3)t

anymultirange @> anyelementboolean

Does the multirange contain the element?

'{[2011-01-01,2011-03-01)}'::tsmultirange @> '2011-01-10'::timestampt

anyrange @> anymultirangeboolean

Does the range contain the multirange?

'[2,4)'::int4range @> '{[2,3)}'::int4multiranget

anymultirange <@ anymultirangeboolean

Is the first multirange contained by the second?

'{[2,4)}'::int4multirange <@ '{[1,7)}'::int4multiranget

anymultirange <@ anyrangeboolean

Is the multirange contained by the range?

'{[2,4)}'::int4multirange <@ int4range(1,7)t

anyrange <@ anymultirangeboolean

Is the range contained by the multirange?

int4range(2,4) <@ '{[1,7)}'::int4multiranget

anyelement <@ anymultirangeboolean

Is the element contained by the multirange?

4 <@ '{[1,7)}'::int4multiranget

anymultirange && anymultirangeboolean

Do the multiranges overlap, that is, have any elements in common?

'{[3,7)}'::int8multirange && '{[4,12)}'::int8multiranget

anymultirange && anyrangeboolean

Does the multirange overlap the range?

'{[3,7)}'::int8multirange && int8range(4,12)t

anyrange && anymultirangeboolean

Does the range overlap the multirange?

int8range(3,7) && '{[4,12)}'::int8multiranget

anymultirange << anymultirangeboolean

Is the first multirange strictly left of the second?

'{[1,10)}'::int8multirange << '{[100,110)}'::int8multiranget

anymultirange << anyrangeboolean

Is the multirange strictly left of the range?

'{[1,10)}'::int8multirange << int8range(100,110)t

anyrange << anymultirangeboolean

Is the range strictly left of the multirange?

int8range(1,10) << '{[100,110)}'::int8multiranget

anymultirange >> anymultirangeboolean

Is the first multirange strictly right of the second?

'{[50,60)}'::int8multirange >> '{[20,30)}'::int8multiranget

anymultirange >> anyrangeboolean

Is the multirange strictly right of the range?

'{[50,60)}'::int8multirange >> int8range(20,30)t

anyrange >> anymultirangeboolean

Is the range strictly right of the multirange?

int8range(50,60) >> '{[20,30)}'::int8multiranget

anymultirange &< anymultirangeboolean

Does the first multirange not extend to the right of the second?

'{[1,20)}'::int8multirange &< '{[18,20)}'::int8multiranget

anymultirange &< anyrangeboolean

Does the multirange not extend to the right of the range?

'{[1,20)}'::int8multirange &< int8range(18,20)t

anyrange &< anymultirangeboolean

Does the range not extend to the right of the multirange?

int8range(1,20) &< '{[18,20)}'::int8multiranget

anymultirange &> anymultirangeboolean

Does the first multirange not extend to the left of the second?

'{[7,20)}'::int8multirange &> '{[5,10)}'::int8multiranget

anymultirange &> anyrangeboolean

Does the multirange not extend to the left of the range?

'{[7,20)}'::int8multirange &> int8range(5,10)t

anyrange &> anymultirangeboolean

Does the range not extend to the left of the multirange?

int8range(7,20) &> '{[5,10)}'::int8multiranget

anymultirange _-

-_ anymultirangeboolean

Are the multiranges adjacent?

_'{[1.1,2.2)}'::nummultirange -

- '{[2.2,3.3)}'::nummultirange_ → t

anymultirange _-

-_ anyrangeboolean

Is the multirange adjacent to the range?

_'{[1.1,2.2)}'::nummultirange -

- numrange(2.2,3.3)_ → t

anyrange _-

-_ anymultirangeboolean

Is the range adjacent to the multirange?

_numrange(1.1,2.2) -

- '{[2.2,3.3)}'::nummultirange_ → t

anymultirange + anymultirangeanymultirange

Computes the union of the multiranges. The multiranges need not overlap or be adjacent.

'{[5,10)}'::nummultirange + '{[15,20)}'::nummultirange{[5,10), [15,20)}

anymultirange * anymultirangeanymultirange

Computes the intersection of the multiranges.

'{[5,15)}'::int8multirange * '{[10,20)}'::int8multirange{[10,15)}

anymultirange - anymultirangeanymultirange

Computes the difference of the multiranges.

'{[5,20)}'::int8multirange - '{[10,15)}'::int8multirange{[5,10), [15,20)}

涉及到空区间或多区间时,左右/邻近运算符始终会返回 false;也就是说,空区间不会被视为在任何其他区间之前或之后。

The left-of/right-of/adjacent operators always return false when an empty range or multirange is involved; that is, an empty range is not considered to be either before or after any other range.

在其他地方,空区间和多区间会被视为加法单位:与空值联合的任何东西都是它本身。减去一个空值后的任何东西都是它本身。一个空的多区间与一个空区间具有完全相同的点。每个区间都包含空区间。每个多区间都包含任意数量的空区间。

Elsewhere empty ranges and multiranges are treated as the additive identity: anything unioned with an empty value is itself. Anything minus an empty value is itself. An empty multirange has exactly the same points as an empty range. Every range contains the empty range. Every multirange contains as many empty ranges as you like.

如果结果区间需要包含两个不重叠的子区间时,区间并集和差集运算符将失败,因为无法表示这样的区间。有单独的运算符用于并集和差集,它们采用多区间参数并返回一个多区间,即使其参数不相交,它们也不会失败。因此,如果你需要对可能不相交的区间进行并集或差集操作,你可以通过首先将区间转换为多区间来避免错误。

The range union and difference operators will fail if the resulting range would need to contain two disjoint sub-ranges, as such a range cannot be represented. There are separate operators for union and difference that take multirange parameters and return a multirange, and they do not fail even if their arguments are disjoint. So if you need a union or difference operation for ranges that may be disjoint, you can avoid errors by first casting your ranges to multiranges.

Table 9.57 显示可用于范围类型的函数。 Table 9.58 显示可用于多范围类型的函数。

Table 9.57 shows the functions available for use with range types. Table 9.58 shows the functions available for use with multirange types.

Table 9.57. Range Functions

Function

Description

Example(s)

lower ( anyrange ) → anyelement

Extracts the lower bound of the range (NULL if the range is empty or has no lower bound).

lower(numrange(1.1,2.2))1.1

upper ( anyrange ) → anyelement

Extracts the upper bound of the range (NULL if the range is empty or has no upper bound).

upper(numrange(1.1,2.2))2.2

isempty ( anyrange ) → boolean

Is the range empty?

isempty(numrange(1.1,2.2))f

lower_inc ( anyrange ) → boolean

Is the range’s lower bound inclusive?

lower_inc(numrange(1.1,2.2))t

upper_inc ( anyrange ) → boolean

Is the range’s upper bound inclusive?

upper_inc(numrange(1.1,2.2))f

lower_inf ( anyrange ) → boolean

Does the range have no lower bound? (A lower bound of -Infinity returns false.)

lower_inf('(,)'::daterange)t

upper_inf ( anyrange ) → boolean

Does the range have no upper bound? (An upper bound of Infinity returns false.)

upper_inf('(,)'::daterange)t

range_merge ( anyrange, anyrange ) → anyrange

Computes the smallest range that includes both of the given ranges.

range_merge('[1,2)'::int4range, '[3,4)'::int4range)[1,4)

Table 9.58. Multirange Functions

Function

Description

Example(s)

lower ( anymultirange ) → anyelement

Extracts the lower bound of the multirange (NULL if the multirange is empty has no lower bound).

lower('{[1.1,2.2)}'::nummultirange)1.1

upper ( anymultirange ) → anyelement

Extracts the upper bound of the multirange (NULL if the multirange is empty or has no upper bound).

upper('{[1.1,2.2)}'::nummultirange)2.2

isempty ( anymultirange ) → boolean

Is the multirange empty?

isempty('{[1.1,2.2)}'::nummultirange)f

lower_inc ( anymultirange ) → boolean

Is the multirange’s lower bound inclusive?

lower_inc('{[1.1,2.2)}'::nummultirange)t

upper_inc ( anymultirange ) → boolean

Is the multirange’s upper bound inclusive?

upper_inc('{[1.1,2.2)}'::nummultirange)f

lower_inf ( anymultirange ) → boolean

Does the multirange have no lower bound? (A lower bound of -Infinity returns false.)

lower_inf('{(,)}'::datemultirange)t

upper_inf ( anymultirange ) → boolean

Does the multirange have no upper bound? (An upper bound of Infinity returns false.)

upper_inf('{(,)}'::datemultirange)t

range_merge ( anymultirange ) → anyrange

Computes the smallest range that includes the entire multirange.

range_merge('{[1,2), [3,4)}'::int4multirange)[1,4)

multirange ( anyrange ) → anymultirange

Returns a multirange containing just the given range.

multirange('[1,2)'::int4range){[1,2)}

unnest ( anymultirange ) → setof anyrange

Expands a multirange into a set of ranges. The ranges are read out in storage order (ascending).

unnest('{[1,2), [3,4)}'::int4multirange) → [1,2) [3,4)

对于空范围或多范围,lower_incupper_inclower_infupper_inf 函数都返回 false。

The lower_inc, upper_inc, lower_inf, and upper_inf functions all return false for an empty range or multirange.