Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_range.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pg_range.c
00004  *    routines to support manipulation of the pg_range relation
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/catalog/pg_range.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #include "postgres.h"
00016 
00017 #include "access/genam.h"
00018 #include "access/heapam.h"
00019 #include "access/htup_details.h"
00020 #include "catalog/dependency.h"
00021 #include "catalog/indexing.h"
00022 #include "catalog/pg_collation.h"
00023 #include "catalog/pg_opclass.h"
00024 #include "catalog/pg_proc.h"
00025 #include "catalog/pg_range.h"
00026 #include "catalog/pg_type.h"
00027 #include "utils/fmgroids.h"
00028 #include "utils/rel.h"
00029 #include "utils/tqual.h"
00030 
00031 
00032 /*
00033  * RangeCreate
00034  *      Create an entry in pg_range.
00035  */
00036 void
00037 RangeCreate(Oid rangeTypeOid, Oid rangeSubType, Oid rangeCollation,
00038             Oid rangeSubOpclass, RegProcedure rangeCanonical,
00039             RegProcedure rangeSubDiff)
00040 {
00041     Relation    pg_range;
00042     Datum       values[Natts_pg_range];
00043     bool        nulls[Natts_pg_range];
00044     HeapTuple   tup;
00045     ObjectAddress myself;
00046     ObjectAddress referenced;
00047 
00048     pg_range = heap_open(RangeRelationId, RowExclusiveLock);
00049 
00050     memset(nulls, 0, sizeof(nulls));
00051 
00052     values[Anum_pg_range_rngtypid - 1] = ObjectIdGetDatum(rangeTypeOid);
00053     values[Anum_pg_range_rngsubtype - 1] = ObjectIdGetDatum(rangeSubType);
00054     values[Anum_pg_range_rngcollation - 1] = ObjectIdGetDatum(rangeCollation);
00055     values[Anum_pg_range_rngsubopc - 1] = ObjectIdGetDatum(rangeSubOpclass);
00056     values[Anum_pg_range_rngcanonical - 1] = ObjectIdGetDatum(rangeCanonical);
00057     values[Anum_pg_range_rngsubdiff - 1] = ObjectIdGetDatum(rangeSubDiff);
00058 
00059     tup = heap_form_tuple(RelationGetDescr(pg_range), values, nulls);
00060 
00061     simple_heap_insert(pg_range, tup);
00062     CatalogUpdateIndexes(pg_range, tup);
00063     heap_freetuple(tup);
00064 
00065     /* record type's dependencies on range-related items */
00066 
00067     myself.classId = TypeRelationId;
00068     myself.objectId = rangeTypeOid;
00069     myself.objectSubId = 0;
00070 
00071     referenced.classId = TypeRelationId;
00072     referenced.objectId = rangeSubType;
00073     referenced.objectSubId = 0;
00074     recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
00075 
00076     referenced.classId = OperatorClassRelationId;
00077     referenced.objectId = rangeSubOpclass;
00078     referenced.objectSubId = 0;
00079     recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
00080 
00081     if (OidIsValid(rangeCollation))
00082     {
00083         referenced.classId = CollationRelationId;
00084         referenced.objectId = rangeCollation;
00085         referenced.objectSubId = 0;
00086         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
00087     }
00088 
00089     if (OidIsValid(rangeCanonical))
00090     {
00091         referenced.classId = ProcedureRelationId;
00092         referenced.objectId = rangeCanonical;
00093         referenced.objectSubId = 0;
00094         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
00095     }
00096 
00097     if (OidIsValid(rangeSubDiff))
00098     {
00099         referenced.classId = ProcedureRelationId;
00100         referenced.objectId = rangeSubDiff;
00101         referenced.objectSubId = 0;
00102         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
00103     }
00104 
00105     heap_close(pg_range, RowExclusiveLock);
00106 }
00107 
00108 
00109 /*
00110  * RangeDelete
00111  *      Remove the pg_range entry for the specified type.
00112  */
00113 void
00114 RangeDelete(Oid rangeTypeOid)
00115 {
00116     Relation    pg_range;
00117     ScanKeyData key[1];
00118     SysScanDesc scan;
00119     HeapTuple   tup;
00120 
00121     pg_range = heap_open(RangeRelationId, RowExclusiveLock);
00122 
00123     ScanKeyInit(&key[0],
00124                 Anum_pg_range_rngtypid,
00125                 BTEqualStrategyNumber, F_OIDEQ,
00126                 ObjectIdGetDatum(rangeTypeOid));
00127 
00128     scan = systable_beginscan(pg_range, RangeTypidIndexId, true,
00129                               SnapshotNow, 1, key);
00130 
00131     while (HeapTupleIsValid(tup = systable_getnext(scan)))
00132     {
00133         simple_heap_delete(pg_range, &tup->t_self);
00134     }
00135 
00136     systable_endscan(scan);
00137 
00138     heap_close(pg_range, RowExclusiveLock);
00139 }