1/*-------------------------------------------------------------------------
2 *
3 * pg_range.c
4 * routines to support manipulation of the pg_range relation
5 *
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/catalog/pg_range.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/genam.h"
18#include "access/htup_details.h"
19#include "access/table.h"
20#include "catalog/dependency.h"
21#include "catalog/indexing.h"
22#include "catalog/pg_collation.h"
23#include "catalog/pg_opclass.h"
24#include "catalog/pg_proc.h"
25#include "catalog/pg_range.h"
26#include "catalog/pg_type.h"
27#include "utils/fmgroids.h"
28#include "utils/rel.h"
29
30
31/*
32 * RangeCreate
33 * Create an entry in pg_range.
34 */
35void
36RangeCreate(Oid rangeTypeOid, Oid rangeSubType, Oid rangeCollation,
37 Oid rangeSubOpclass, RegProcedure rangeCanonical,
38 RegProcedure rangeSubDiff)
39{
40 Relation pg_range;
41 Datum values[Natts_pg_range];
42 bool nulls[Natts_pg_range];
43 HeapTuple tup;
44 ObjectAddress myself;
45 ObjectAddress referenced;
46
47 pg_range = table_open(RangeRelationId, RowExclusiveLock);
48
49 memset(nulls, 0, sizeof(nulls));
50
51 values[Anum_pg_range_rngtypid - 1] = ObjectIdGetDatum(rangeTypeOid);
52 values[Anum_pg_range_rngsubtype - 1] = ObjectIdGetDatum(rangeSubType);
53 values[Anum_pg_range_rngcollation - 1] = ObjectIdGetDatum(rangeCollation);
54 values[Anum_pg_range_rngsubopc - 1] = ObjectIdGetDatum(rangeSubOpclass);
55 values[Anum_pg_range_rngcanonical - 1] = ObjectIdGetDatum(rangeCanonical);
56 values[Anum_pg_range_rngsubdiff - 1] = ObjectIdGetDatum(rangeSubDiff);
57
58 tup = heap_form_tuple(RelationGetDescr(pg_range), values, nulls);
59
60 CatalogTupleInsert(pg_range, tup);
61 heap_freetuple(tup);
62
63 /* record type's dependencies on range-related items */
64
65 myself.classId = TypeRelationId;
66 myself.objectId = rangeTypeOid;
67 myself.objectSubId = 0;
68
69 referenced.classId = TypeRelationId;
70 referenced.objectId = rangeSubType;
71 referenced.objectSubId = 0;
72 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
73
74 referenced.classId = OperatorClassRelationId;
75 referenced.objectId = rangeSubOpclass;
76 referenced.objectSubId = 0;
77 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
78
79 if (OidIsValid(rangeCollation))
80 {
81 referenced.classId = CollationRelationId;
82 referenced.objectId = rangeCollation;
83 referenced.objectSubId = 0;
84 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
85 }
86
87 if (OidIsValid(rangeCanonical))
88 {
89 referenced.classId = ProcedureRelationId;
90 referenced.objectId = rangeCanonical;
91 referenced.objectSubId = 0;
92 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
93 }
94
95 if (OidIsValid(rangeSubDiff))
96 {
97 referenced.classId = ProcedureRelationId;
98 referenced.objectId = rangeSubDiff;
99 referenced.objectSubId = 0;
100 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
101 }
102
103 table_close(pg_range, RowExclusiveLock);
104}
105
106
107/*
108 * RangeDelete
109 * Remove the pg_range entry for the specified type.
110 */
111void
112RangeDelete(Oid rangeTypeOid)
113{
114 Relation pg_range;
115 ScanKeyData key[1];
116 SysScanDesc scan;
117 HeapTuple tup;
118
119 pg_range = table_open(RangeRelationId, RowExclusiveLock);
120
121 ScanKeyInit(&key[0],
122 Anum_pg_range_rngtypid,
123 BTEqualStrategyNumber, F_OIDEQ,
124 ObjectIdGetDatum(rangeTypeOid));
125
126 scan = systable_beginscan(pg_range, RangeTypidIndexId, true,
127 NULL, 1, key);
128
129 while (HeapTupleIsValid(tup = systable_getnext(scan)))
130 {
131 CatalogTupleDelete(pg_range, &tup->t_self);
132 }
133
134 systable_endscan(scan);
135
136 table_close(pg_range, RowExclusiveLock);
137}
138