2011-11-03 12:16:28 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_range.c
|
|
|
|
* routines to support manipulation of the pg_range relation
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
2011-11-14 19:59:34 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
2011-11-03 12:16:28 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/catalog/pg_range.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2019-12-27 00:09:00 +01:00
|
|
|
#include "access/genam.h"
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2019-01-21 19:18:20 +01:00
|
|
|
#include "access/table.h"
|
2011-11-03 12:16:28 +01:00
|
|
|
#include "catalog/dependency.h"
|
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_collation.h"
|
|
|
|
#include "catalog/pg_opclass.h"
|
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "catalog/pg_range.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/rel.h"
|
2011-11-14 19:59:34 +01:00
|
|
|
|
2011-11-03 12:16:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* RangeCreate
|
|
|
|
* Create an entry in pg_range.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RangeCreate(Oid rangeTypeOid, Oid rangeSubType, Oid rangeCollation,
|
|
|
|
Oid rangeSubOpclass, RegProcedure rangeCanonical,
|
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
2020-12-20 05:20:33 +01:00
|
|
|
RegProcedure rangeSubDiff, Oid multirangeTypeOid)
|
2011-11-03 12:16:28 +01:00
|
|
|
{
|
2011-11-14 18:08:48 +01:00
|
|
|
Relation pg_range;
|
|
|
|
Datum values[Natts_pg_range];
|
|
|
|
bool nulls[Natts_pg_range];
|
|
|
|
HeapTuple tup;
|
|
|
|
ObjectAddress myself;
|
|
|
|
ObjectAddress referenced;
|
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
2020-12-20 05:20:33 +01:00
|
|
|
ObjectAddress referencing;
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddresses *addrs;
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
pg_range = table_open(RangeRelationId, RowExclusiveLock);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2011-11-14 19:59:34 +01:00
|
|
|
memset(nulls, 0, sizeof(nulls));
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2011-11-14 18:08:48 +01:00
|
|
|
values[Anum_pg_range_rngtypid - 1] = ObjectIdGetDatum(rangeTypeOid);
|
|
|
|
values[Anum_pg_range_rngsubtype - 1] = ObjectIdGetDatum(rangeSubType);
|
2011-11-03 12:16:28 +01:00
|
|
|
values[Anum_pg_range_rngcollation - 1] = ObjectIdGetDatum(rangeCollation);
|
2011-11-14 18:08:48 +01:00
|
|
|
values[Anum_pg_range_rngsubopc - 1] = ObjectIdGetDatum(rangeSubOpclass);
|
2011-11-03 12:16:28 +01:00
|
|
|
values[Anum_pg_range_rngcanonical - 1] = ObjectIdGetDatum(rangeCanonical);
|
2011-11-14 18:08:48 +01:00
|
|
|
values[Anum_pg_range_rngsubdiff - 1] = ObjectIdGetDatum(rangeSubDiff);
|
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
2020-12-20 05:20:33 +01:00
|
|
|
values[Anum_pg_range_rngmultitypid - 1] = ObjectIdGetDatum(multirangeTypeOid);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
|
|
|
tup = heap_form_tuple(RelationGetDescr(pg_range), values, nulls);
|
2011-11-14 19:59:34 +01:00
|
|
|
|
2017-01-31 22:42:24 +01:00
|
|
|
CatalogTupleInsert(pg_range, tup);
|
2011-11-03 12:16:28 +01:00
|
|
|
heap_freetuple(tup);
|
|
|
|
|
2011-11-14 19:59:34 +01:00
|
|
|
/* record type's dependencies on range-related items */
|
2020-09-05 14:33:53 +02:00
|
|
|
addrs = new_object_addresses();
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(myself, TypeRelationId, rangeTypeOid);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(referenced, TypeRelationId, rangeSubType);
|
|
|
|
add_exact_object_address(&referenced, addrs);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(referenced, OperatorClassRelationId, rangeSubOpclass);
|
|
|
|
add_exact_object_address(&referenced, addrs);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
|
|
|
if (OidIsValid(rangeCollation))
|
|
|
|
{
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(referenced, CollationRelationId, rangeCollation);
|
|
|
|
add_exact_object_address(&referenced, addrs);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OidIsValid(rangeCanonical))
|
|
|
|
{
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(referenced, ProcedureRelationId, rangeCanonical);
|
|
|
|
add_exact_object_address(&referenced, addrs);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OidIsValid(rangeSubDiff))
|
|
|
|
{
|
2020-09-05 14:33:53 +02:00
|
|
|
ObjectAddressSet(referenced, ProcedureRelationId, rangeSubDiff);
|
|
|
|
add_exact_object_address(&referenced, addrs);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:33:53 +02:00
|
|
|
record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL);
|
|
|
|
free_object_addresses(addrs);
|
|
|
|
|
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
2020-12-20 05:20:33 +01:00
|
|
|
/* record multirange type's dependency on the range type */
|
|
|
|
referencing.classId = TypeRelationId;
|
|
|
|
referencing.objectId = multirangeTypeOid;
|
|
|
|
referencing.objectSubId = 0;
|
|
|
|
recordDependencyOn(&referencing, &myself, DEPENDENCY_INTERNAL);
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(pg_range, RowExclusiveLock);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RangeDelete
|
2011-11-14 19:59:34 +01:00
|
|
|
* Remove the pg_range entry for the specified type.
|
2011-11-03 12:16:28 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
RangeDelete(Oid rangeTypeOid)
|
|
|
|
{
|
|
|
|
Relation pg_range;
|
|
|
|
ScanKeyData key[1];
|
|
|
|
SysScanDesc scan;
|
|
|
|
HeapTuple tup;
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
pg_range = table_open(RangeRelationId, RowExclusiveLock);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_range_rngtypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(rangeTypeOid));
|
|
|
|
|
|
|
|
scan = systable_beginscan(pg_range, RangeTypidIndexId, true,
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
NULL, 1, key);
|
2011-11-03 12:16:28 +01:00
|
|
|
|
|
|
|
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
|
|
{
|
2017-02-01 22:13:30 +01:00
|
|
|
CatalogTupleDelete(pg_range, &tup->t_self);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(pg_range, RowExclusiveLock);
|
2011-11-03 12:16:28 +01:00
|
|
|
}
|