PostgreSQL Source Code  git master
pg_range.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_range.c
4  * routines to support manipulation of the pg_range relation
5  *
6  * Portions Copyright (c) 1996-2020, 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  */
35 void
36 RangeCreate(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  ObjectAddresses *addrs;
47 
48  pg_range = table_open(RangeRelationId, RowExclusiveLock);
49 
50  memset(nulls, 0, sizeof(nulls));
51 
52  values[Anum_pg_range_rngtypid - 1] = ObjectIdGetDatum(rangeTypeOid);
53  values[Anum_pg_range_rngsubtype - 1] = ObjectIdGetDatum(rangeSubType);
54  values[Anum_pg_range_rngcollation - 1] = ObjectIdGetDatum(rangeCollation);
55  values[Anum_pg_range_rngsubopc - 1] = ObjectIdGetDatum(rangeSubOpclass);
56  values[Anum_pg_range_rngcanonical - 1] = ObjectIdGetDatum(rangeCanonical);
57  values[Anum_pg_range_rngsubdiff - 1] = ObjectIdGetDatum(rangeSubDiff);
58 
59  tup = heap_form_tuple(RelationGetDescr(pg_range), values, nulls);
60 
61  CatalogTupleInsert(pg_range, tup);
62  heap_freetuple(tup);
63 
64  /* record type's dependencies on range-related items */
65  addrs = new_object_addresses();
66 
67  ObjectAddressSet(myself, TypeRelationId, rangeTypeOid);
68 
69  ObjectAddressSet(referenced, TypeRelationId, rangeSubType);
70  add_exact_object_address(&referenced, addrs);
71 
72  ObjectAddressSet(referenced, OperatorClassRelationId, rangeSubOpclass);
73  add_exact_object_address(&referenced, addrs);
74 
75  if (OidIsValid(rangeCollation))
76  {
77  ObjectAddressSet(referenced, CollationRelationId, rangeCollation);
78  add_exact_object_address(&referenced, addrs);
79  }
80 
81  if (OidIsValid(rangeCanonical))
82  {
83  ObjectAddressSet(referenced, ProcedureRelationId, rangeCanonical);
84  add_exact_object_address(&referenced, addrs);
85  }
86 
87  if (OidIsValid(rangeSubDiff))
88  {
89  ObjectAddressSet(referenced, ProcedureRelationId, rangeSubDiff);
90  add_exact_object_address(&referenced, addrs);
91  }
92 
94  free_object_addresses(addrs);
95 
96  table_close(pg_range, RowExclusiveLock);
97 }
98 
99 
100 /*
101  * RangeDelete
102  * Remove the pg_range entry for the specified type.
103  */
104 void
105 RangeDelete(Oid rangeTypeOid)
106 {
107  Relation pg_range;
108  ScanKeyData key[1];
109  SysScanDesc scan;
110  HeapTuple tup;
111 
112  pg_range = table_open(RangeRelationId, RowExclusiveLock);
113 
114  ScanKeyInit(&key[0],
115  Anum_pg_range_rngtypid,
116  BTEqualStrategyNumber, F_OIDEQ,
117  ObjectIdGetDatum(rangeTypeOid));
118 
119  scan = systable_beginscan(pg_range, RangeTypidIndexId, true,
120  NULL, 1, key);
121 
122  while (HeapTupleIsValid(tup = systable_getnext(scan)))
123  {
124  CatalogTupleDelete(pg_range, &tup->t_self);
125  }
126 
127  systable_endscan(scan);
128 
129  table_close(pg_range, RowExclusiveLock);
130 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define RelationGetDescr(relation)
Definition: rel.h:482
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2676
regproc RegProcedure
Definition: c.h:519
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2467
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
void RangeCreate(Oid rangeTypeOid, Oid rangeSubType, Oid rangeCollation, Oid rangeSubOpclass, RegProcedure rangeCanonical, RegProcedure rangeSubDiff)
Definition: pg_range.c:36
#define RowExclusiveLock
Definition: lockdefs.h:38
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define RangeTypidIndexId
Definition: indexing.h:339
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void RangeDelete(Oid rangeTypeOid)
Definition: pg_range.c:105
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
#define BTEqualStrategyNumber
Definition: stratnum.h:31