Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6df7a96

Browse files
committed
Multirange datatypes
Multiranges are basically sorted arrays of non-overlapping ranges with set-theoretic operations defined over them. Since v14, each range type automatically gets a corresponding multirange datatype. There are both manual and automatic mechanisms for naming multirange types. Once can specify multirange type name using multirange_type_name attribute in CREATE TYPE.  Otherwise, a multirange type name is generated automatically. If the range type name contains "range" then we change that to "multirange". Otherwise, we add "_multirange" to the end. Implementation of multiranges comes with a space-efficient internal representation format, which evades extra paddings and duplicated storage of oids.  Altogether this format allows fetching a particular range by its index in O(n). Statistic gathering and selectivity estimation are implemented for multiranges. For this purpose, stored multirange is approximated as union range without gaps. This field will likely need improvements in the future. Catversion is bumped. Discussion: https://postgr.es/m/CALNJ-vSUpQ_Y%3DjXvTxt1VYFztaBSsWVXeF1y6gTYQ4bOiWDLgQ%40mail.gmail.com Discussion: https://postgr.es/m/a0b8026459d1e6167933be2104a6174e7d40d0ab.camel%40j-davis.com#fe7218c83b08068bfffb0c5293eceda0 Author: Paul Jungwirth, revised by me Reviewed-by: David Fetter, Corey Huinker, Jeff Davis, Pavel Stehule Reviewed-by: Alvaro Herrera, Tom Lane, Isaac Morland, David G. Johnston Reviewed-by: Zhihong Yu, Alexander Korotkov
1 parent 08b01d4 commit 6df7a96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+10568
-270
lines changed

doc/src/sgml/catalogs.sgml

+13-2
Original file line numberDiff line numberDiff line change
@@ -6237,6 +6237,16 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
62376237
</para></entry>
62386238
</row>
62396239

6240+
<row>
6241+
<entry role="catalog_table_entry"><para role="column_definition">
6242+
<structfield>rngmultitypid</structfield> <type>oid</type>
6243+
(references <link linkend="catalog-pg-type"><structname>pg_type</structname></link>.<structfield>oid</structfield>)
6244+
</para>
6245+
<para>
6246+
OID of the multirange type for this range type
6247+
</para></entry>
6248+
</row>
6249+
62406250
<row>
62416251
<entry role="catalog_table_entry"><para role="column_definition">
62426252
<structfield>rngcollation</structfield> <type>oid</type>
@@ -8671,8 +8681,9 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
86718681
<literal>c</literal> for a composite type (e.g., a table's row type),
86728682
<literal>d</literal> for a domain,
86738683
<literal>e</literal> for an enum type,
8674-
<literal>p</literal> for a pseudo-type, or
8675-
<literal>r</literal> for a range type.
8684+
<literal>p</literal> for a pseudo-type,
8685+
<literal>r</literal> for a range type, or
8686+
<literal>m</literal> for a multirange type.
86768687
See also <structfield>typrelid</structfield> and
86778688
<structfield>typbasetype</structfield>.
86788689
</para></entry>

doc/src/sgml/datatype.sgml

+23
Original file line numberDiff line numberDiff line change
@@ -4907,6 +4907,10 @@ SELECT * FROM pg_attribute
49074907
<primary>anyrange</primary>
49084908
</indexterm>
49094909

4910+
<indexterm zone="datatype-pseudo">
4911+
<primary>anymultirange</primary>
4912+
</indexterm>
4913+
49104914
<indexterm zone="datatype-pseudo">
49114915
<primary>anycompatible</primary>
49124916
</indexterm>
@@ -4923,6 +4927,10 @@ SELECT * FROM pg_attribute
49234927
<primary>anycompatiblerange</primary>
49244928
</indexterm>
49254929

4930+
<indexterm zone="datatype-pseudo">
4931+
<primary>anycompatiblemultirange</primary>
4932+
</indexterm>
4933+
49264934
<indexterm zone="datatype-pseudo">
49274935
<primary>void</primary>
49284936
</indexterm>
@@ -5034,6 +5042,13 @@ SELECT * FROM pg_attribute
50345042
<xref linkend="rangetypes"/>).</entry>
50355043
</row>
50365044

5045+
<row>
5046+
<entry><type>anymultirange</type></entry>
5047+
<entry>Indicates that a function accepts any multirange data type
5048+
(see <xref linkend="extend-types-polymorphic"/> and
5049+
<xref linkend="rangetypes"/>).</entry>
5050+
</row>
5051+
50375052
<row>
50385053
<entry><type>anycompatible</type></entry>
50395054
<entry>Indicates that a function accepts any data type,
@@ -5063,6 +5078,14 @@ SELECT * FROM pg_attribute
50635078
<xref linkend="rangetypes"/>).</entry>
50645079
</row>
50655080

5081+
<row>
5082+
<entry><type>anycompatiblemultirange</type></entry>
5083+
<entry>Indicates that a function accepts any multirange data type,
5084+
with automatic promotion of multiple arguments to a common data type
5085+
(see <xref linkend="extend-types-polymorphic"/> and
5086+
<xref linkend="rangetypes"/>).</entry>
5087+
</row>
5088+
50665089
<row>
50675090
<entry><type>cstring</type></entry>
50685091
<entry>Indicates that a function accepts or returns a null-terminated C string.</entry>

doc/src/sgml/extend.sgml

+45-14
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@
288288
</entry>
289289
</row>
290290

291+
<row>
292+
<entry><type>anymultirange</type></entry>
293+
<entry>Simple</entry>
294+
<entry>Indicates that a function accepts any multirange data type
295+
(see <xref linkend="rangetypes"/>)
296+
</entry>
297+
</row>
298+
291299
<row>
292300
<entry><type>anycompatible</type></entry>
293301
<entry>Common</entry>
@@ -319,6 +327,14 @@
319327
with automatic promotion of multiple arguments to a common data type
320328
</entry>
321329
</row>
330+
331+
<row>
332+
<entry><type>anycompatiblemultirange</type></entry>
333+
<entry>Common</entry>
334+
<entry>Indicates that a function accepts any multirange data type,
335+
with automatic promotion of multiple arguments to a common data type
336+
</entry>
337+
</row>
322338
</tbody>
323339
</tgroup>
324340
</table>
@@ -346,17 +362,15 @@
346362
position declared as <type>anyarray</type> can have any array data type,
347363
but similarly they must all be the same type. And similarly,
348364
positions declared as <type>anyrange</type> must all be the same range
349-
type. Furthermore, if there are
365+
type. Likewise for <type>anymultirange</type>.
366+
</para>
367+
368+
<para>
369+
Furthermore, if there are
350370
positions declared <type>anyarray</type> and others declared
351371
<type>anyelement</type>, the actual array type in the
352372
<type>anyarray</type> positions must be an array whose elements are
353373
the same type appearing in the <type>anyelement</type> positions.
354-
Similarly, if there are positions declared <type>anyrange</type>
355-
and others declared <type>anyelement</type> or <type>anyarray</type>,
356-
the actual range type in the <type>anyrange</type> positions must be a
357-
range whose subtype is the same type appearing in
358-
the <type>anyelement</type> positions and the same as the element type
359-
of the <type>anyarray</type> positions.
360374
<type>anynonarray</type> is treated exactly the same as <type>anyelement</type>,
361375
but adds the additional constraint that the actual type must not be
362376
an array type.
@@ -365,6 +379,19 @@
365379
be an enum type.
366380
</para>
367381

382+
<para>
383+
Similarly, if there are positions declared <type>anyrange</type>
384+
and others declared <type>anyelement</type> or <type>anyarray</type>,
385+
the actual range type in the <type>anyrange</type> positions must be a
386+
range whose subtype is the same type appearing in
387+
the <type>anyelement</type> positions and the same as the element type
388+
of the <type>anyarray</type> positions.
389+
If there are positions declared <type>anymultirange</type>,
390+
their actual multirange type must contain ranges matching parameters declared
391+
<type>anyrange</type> and base elements matching parameters declared
392+
<type>anyelement</type> and <type>anyarray</type>.
393+
</para>
394+
368395
<para>
369396
Thus, when more than one argument position is declared with a polymorphic
370397
type, the net effect is that only certain combinations of actual argument
@@ -420,7 +447,8 @@
420447
Selection of the common type considers the actual types
421448
of <type>anycompatible</type> and <type>anycompatiblenonarray</type>
422449
inputs, the array element types of <type>anycompatiblearray</type>
423-
inputs, and the range subtypes of <type>anycompatiblerange</type>
450+
inputs, the range subtypes of <type>anycompatiblerange</type> inputs,
451+
and the multirange subtypes of <type>anycompatiablemultirange</type>
424452
inputs. If <type>anycompatiblenonarray</type> is present then the
425453
common type is required to be a non-array type. Once a common type is
426454
identified, arguments in <type>anycompatible</type>
@@ -431,12 +459,15 @@
431459

432460
<para>
433461
Since there is no way to select a range type knowing only its subtype,
434-
use of <type>anycompatiblerange</type> requires that all arguments
435-
declared with that type have the same actual range type, and that that
436-
type's subtype agree with the selected common type, so that no casting
437-
of the range values is required. As with <type>anyrange</type>, use
438-
of <type>anycompatiblerange</type> as a function result type requires
439-
that there be an <type>anycompatiblerange</type> argument.
462+
use of <type>anycompatiblerange</type> and/or
463+
<type>anycompatiblemultirange</type> requires that all arguments declared
464+
with that type have the same actual range and/or multirange type, and that
465+
that type's subtype agree with the selected common type, so that no casting
466+
of the range values is required. As with <type>anyrange</type> and
467+
<type>anymultirange</type>, use of <type>anycompatiblerange</type> and
468+
<type>anymultirange</type> as a function result type requires that there be
469+
an <type>anycompatiblerange</type> or <type>anycompatiblemultirange</type>
470+
argument.
440471
</para>
441472

442473
<para>

0 commit comments

Comments
 (0)