2 9.20. Range/Multirange Functions and Operators #
4 See Section 8.17 for an overview of range types.
6 Table 9.58 shows the specialized operators available for range types.
7 Table 9.59 shows the specialized operators available for multirange
8 types. In addition to those, the usual comparison operators shown in
9 Table 9.1 are available for range and multirange types. The comparison
10 operators order first by the range lower bounds, and only if those are
11 equal do they compare the upper bounds. The multirange operators
12 compare each range until one is unequal. This does not usually result
13 in a useful overall ordering, but the operators are provided to allow
14 unique indexes to be constructed on ranges.
16 Table 9.58. Range Operators
24 anyrange @> anyrange → boolean
26 Does the first range contain the second?
28 int4range(2,4) @> int4range(2,3) → t
30 anyrange @> anyelement → boolean
32 Does the range contain the element?
34 '[2011-01-01,2011-03-01)'::tsrange @> '2011-01-10'::timestamp → t
36 anyrange <@ anyrange → boolean
38 Is the first range contained by the second?
40 int4range(2,4) <@ int4range(1,7) → t
42 anyelement <@ anyrange → boolean
44 Is the element contained in the range?
46 42 <@ int4range(1,7) → f
48 anyrange && anyrange → boolean
50 Do the ranges overlap, that is, have any elements in common?
52 int8range(3,7) && int8range(4,12) → t
54 anyrange << anyrange → boolean
56 Is the first range strictly left of the second?
58 int8range(1,10) << int8range(100,110) → t
60 anyrange >> anyrange → boolean
62 Is the first range strictly right of the second?
64 int8range(50,60) >> int8range(20,30) → t
66 anyrange &< anyrange → boolean
68 Does the first range not extend to the right of the second?
70 int8range(1,20) &< int8range(18,20) → t
72 anyrange &> anyrange → boolean
74 Does the first range not extend to the left of the second?
76 int8range(7,20) &> int8range(5,10) → t
78 anyrange -|- anyrange → boolean
80 Are the ranges adjacent?
82 numrange(1.1,2.2) -|- numrange(2.2,3.3) → t
84 anyrange + anyrange → anyrange
86 Computes the union of the ranges. The ranges must overlap or be
87 adjacent, so that the union is a single range (but see range_merge()).
89 numrange(5,15) + numrange(10,20) → [5,20)
91 anyrange * anyrange → anyrange
93 Computes the intersection of the ranges.
95 int8range(5,15) * int8range(10,20) → [10,15)
97 anyrange - anyrange → anyrange
99 Computes the difference of the ranges. The second range must not be
100 contained in the first in such a way that the difference would not be a
103 int8range(5,15) - int8range(10,20) → [5,10)
105 Table 9.59. Multirange Operators
113 anymultirange @> anymultirange → boolean
115 Does the first multirange contain the second?
117 '{[2,4)}'::int4multirange @> '{[2,3)}'::int4multirange → t
119 anymultirange @> anyrange → boolean
121 Does the multirange contain the range?
123 '{[2,4)}'::int4multirange @> int4range(2,3) → t
125 anymultirange @> anyelement → boolean
127 Does the multirange contain the element?
129 '{[2011-01-01,2011-03-01)}'::tsmultirange @> '2011-01-10'::timestamp →
132 anyrange @> anymultirange → boolean
134 Does the range contain the multirange?
136 '[2,4)'::int4range @> '{[2,3)}'::int4multirange → t
138 anymultirange <@ anymultirange → boolean
140 Is the first multirange contained by the second?
142 '{[2,4)}'::int4multirange <@ '{[1,7)}'::int4multirange → t
144 anymultirange <@ anyrange → boolean
146 Is the multirange contained by the range?
148 '{[2,4)}'::int4multirange <@ int4range(1,7) → t
150 anyrange <@ anymultirange → boolean
152 Is the range contained by the multirange?
154 int4range(2,4) <@ '{[1,7)}'::int4multirange → t
156 anyelement <@ anymultirange → boolean
158 Is the element contained by the multirange?
160 4 <@ '{[1,7)}'::int4multirange → t
162 anymultirange && anymultirange → boolean
164 Do the multiranges overlap, that is, have any elements in common?
166 '{[3,7)}'::int8multirange && '{[4,12)}'::int8multirange → t
168 anymultirange && anyrange → boolean
170 Does the multirange overlap the range?
172 '{[3,7)}'::int8multirange && int8range(4,12) → t
174 anyrange && anymultirange → boolean
176 Does the range overlap the multirange?
178 int8range(3,7) && '{[4,12)}'::int8multirange → t
180 anymultirange << anymultirange → boolean
182 Is the first multirange strictly left of the second?
184 '{[1,10)}'::int8multirange << '{[100,110)}'::int8multirange → t
186 anymultirange << anyrange → boolean
188 Is the multirange strictly left of the range?
190 '{[1,10)}'::int8multirange << int8range(100,110) → t
192 anyrange << anymultirange → boolean
194 Is the range strictly left of the multirange?
196 int8range(1,10) << '{[100,110)}'::int8multirange → t
198 anymultirange >> anymultirange → boolean
200 Is the first multirange strictly right of the second?
202 '{[50,60)}'::int8multirange >> '{[20,30)}'::int8multirange → t
204 anymultirange >> anyrange → boolean
206 Is the multirange strictly right of the range?
208 '{[50,60)}'::int8multirange >> int8range(20,30) → t
210 anyrange >> anymultirange → boolean
212 Is the range strictly right of the multirange?
214 int8range(50,60) >> '{[20,30)}'::int8multirange → t
216 anymultirange &< anymultirange → boolean
218 Does the first multirange not extend to the right of the second?
220 '{[1,20)}'::int8multirange &< '{[18,20)}'::int8multirange → t
222 anymultirange &< anyrange → boolean
224 Does the multirange not extend to the right of the range?
226 '{[1,20)}'::int8multirange &< int8range(18,20) → t
228 anyrange &< anymultirange → boolean
230 Does the range not extend to the right of the multirange?
232 int8range(1,20) &< '{[18,20)}'::int8multirange → t
234 anymultirange &> anymultirange → boolean
236 Does the first multirange not extend to the left of the second?
238 '{[7,20)}'::int8multirange &> '{[5,10)}'::int8multirange → t
240 anymultirange &> anyrange → boolean
242 Does the multirange not extend to the left of the range?
244 '{[7,20)}'::int8multirange &> int8range(5,10) → t
246 anyrange &> anymultirange → boolean
248 Does the range not extend to the left of the multirange?
250 int8range(7,20) &> '{[5,10)}'::int8multirange → t
252 anymultirange -|- anymultirange → boolean
254 Are the multiranges adjacent?
256 '{[1.1,2.2)}'::nummultirange -|- '{[2.2,3.3)}'::nummultirange → t
258 anymultirange -|- anyrange → boolean
260 Is the multirange adjacent to the range?
262 '{[1.1,2.2)}'::nummultirange -|- numrange(2.2,3.3) → t
264 anyrange -|- anymultirange → boolean
266 Is the range adjacent to the multirange?
268 numrange(1.1,2.2) -|- '{[2.2,3.3)}'::nummultirange → t
270 anymultirange + anymultirange → anymultirange
272 Computes the union of the multiranges. The multiranges need not overlap
275 '{[5,10)}'::nummultirange + '{[15,20)}'::nummultirange → {[5,10),
278 anymultirange * anymultirange → anymultirange
280 Computes the intersection of the multiranges.
282 '{[5,15)}'::int8multirange * '{[10,20)}'::int8multirange → {[10,15)}
284 anymultirange - anymultirange → anymultirange
286 Computes the difference of the multiranges.
288 '{[5,20)}'::int8multirange - '{[10,15)}'::int8multirange → {[5,10),
291 The left-of/right-of/adjacent operators always return false when an
292 empty range or multirange is involved; that is, an empty range is not
293 considered to be either before or after any other range.
295 Elsewhere empty ranges and multiranges are treated as the additive
296 identity: anything unioned with an empty value is itself. Anything
297 minus an empty value is itself. An empty multirange has exactly the
298 same points as an empty range. Every range contains the empty range.
299 Every multirange contains as many empty ranges as you like.
301 The range union and difference operators will fail if the resulting
302 range would need to contain two disjoint sub-ranges, as such a range
303 cannot be represented. There are separate operators for union and
304 difference that take multirange parameters and return a multirange, and
305 they do not fail even if their arguments are disjoint. So if you need a
306 union or difference operation for ranges that may be disjoint, you can
307 avoid errors by first casting your ranges to multiranges.
309 Table 9.60 shows the functions available for use with range types.
310 Table 9.61 shows the functions available for use with multirange types.
312 Table 9.60. Range Functions
320 lower ( anyrange ) → anyelement
322 Extracts the lower bound of the range (NULL if the range is empty or
325 lower(numrange(1.1,2.2)) → 1.1
327 upper ( anyrange ) → anyelement
329 Extracts the upper bound of the range (NULL if the range is empty or
332 upper(numrange(1.1,2.2)) → 2.2
334 isempty ( anyrange ) → boolean
338 isempty(numrange(1.1,2.2)) → f
340 lower_inc ( anyrange ) → boolean
342 Is the range's lower bound inclusive?
344 lower_inc(numrange(1.1,2.2)) → t
346 upper_inc ( anyrange ) → boolean
348 Is the range's upper bound inclusive?
350 upper_inc(numrange(1.1,2.2)) → f
352 lower_inf ( anyrange ) → boolean
354 Does the range have no lower bound? (A lower bound of -Infinity returns
357 lower_inf('(,)'::daterange) → t
359 upper_inf ( anyrange ) → boolean
361 Does the range have no upper bound? (An upper bound of Infinity returns
364 upper_inf('(,)'::daterange) → t
366 range_merge ( anyrange, anyrange ) → anyrange
368 Computes the smallest range that includes both of the given ranges.
370 range_merge('[1,2)'::int4range, '[3,4)'::int4range) → [1,4)
372 Table 9.61. Multirange Functions
380 lower ( anymultirange ) → anyelement
382 Extracts the lower bound of the multirange (NULL if the multirange is
383 empty or has no lower bound).
385 lower('{[1.1,2.2)}'::nummultirange) → 1.1
387 upper ( anymultirange ) → anyelement
389 Extracts the upper bound of the multirange (NULL if the multirange is
390 empty or has no upper bound).
392 upper('{[1.1,2.2)}'::nummultirange) → 2.2
394 isempty ( anymultirange ) → boolean
396 Is the multirange empty?
398 isempty('{[1.1,2.2)}'::nummultirange) → f
400 lower_inc ( anymultirange ) → boolean
402 Is the multirange's lower bound inclusive?
404 lower_inc('{[1.1,2.2)}'::nummultirange) → t
406 upper_inc ( anymultirange ) → boolean
408 Is the multirange's upper bound inclusive?
410 upper_inc('{[1.1,2.2)}'::nummultirange) → f
412 lower_inf ( anymultirange ) → boolean
414 Does the multirange have no lower bound? (A lower bound of -Infinity
417 lower_inf('{(,)}'::datemultirange) → t
419 upper_inf ( anymultirange ) → boolean
421 Does the multirange have no upper bound? (An upper bound of Infinity
424 upper_inf('{(,)}'::datemultirange) → t
426 range_merge ( anymultirange ) → anyrange
428 Computes the smallest range that includes the entire multirange.
430 range_merge('{[1,2), [3,4)}'::int4multirange) → [1,4)
432 multirange ( anyrange ) → anymultirange
434 Returns a multirange containing just the given range.
436 multirange('[1,2)'::int4range) → {[1,2)}
438 unnest ( anymultirange ) → setof anyrange
440 Expands a multirange into a set of ranges in ascending order.
442 unnest('{[1,2), [3,4)}'::int4multirange) →
446 The lower_inc, upper_inc, lower_inf, and upper_inf functions all return
447 false for an empty range or multirange.