PostgreSQL Source Code  git master
pg_cast.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_cast.c
4  * routines to support manipulation of the pg_cast 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_cast.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/table.h"
19 #include "catalog/catalog.h"
20 #include "catalog/dependency.h"
21 #include "catalog/indexing.h"
22 #include "catalog/objectaccess.h"
23 #include "catalog/pg_cast.h"
24 #include "catalog/pg_proc.h"
25 #include "catalog/pg_type.h"
26 #include "utils/builtins.h"
27 #include "utils/rel.h"
28 #include "utils/syscache.h"
29 
30 /*
31  * ----------------------------------------------------------------
32  * CastCreate
33  *
34  * Forms and inserts catalog tuples for a new cast being created.
35  * Caller must have already checked privileges, and done consistency
36  * checks on the given datatypes and cast function (if applicable).
37  *
38  * 'behavior' indicates the types of the dependencies that the new
39  * cast will have on its input and output types and the cast function.
40  * ----------------------------------------------------------------
41  */
43 CastCreate(Oid sourcetypeid, Oid targettypeid, Oid funcid, char castcontext,
44  char castmethod, DependencyType behavior)
45 {
46  Relation relation;
47  HeapTuple tuple;
48  Oid castid;
49  Datum values[Natts_pg_cast];
50  bool nulls[Natts_pg_cast];
51  ObjectAddress myself,
52  referenced;
53 
54  relation = table_open(CastRelationId, RowExclusiveLock);
55 
56  /*
57  * Check for duplicate. This is just to give a friendly error message,
58  * the unique index would catch it anyway (so no need to sweat about race
59  * conditions).
60  */
62  ObjectIdGetDatum(sourcetypeid),
63  ObjectIdGetDatum(targettypeid));
64  if (HeapTupleIsValid(tuple))
65  ereport(ERROR,
67  errmsg("cast from type %s to type %s already exists",
68  format_type_be(sourcetypeid),
69  format_type_be(targettypeid))));
70 
71  /* ready to go */
72  castid = GetNewOidWithIndex(relation, CastOidIndexId, Anum_pg_cast_oid);
73  values[Anum_pg_cast_oid - 1] = ObjectIdGetDatum(castid);
74  values[Anum_pg_cast_castsource - 1] = ObjectIdGetDatum(sourcetypeid);
75  values[Anum_pg_cast_casttarget - 1] = ObjectIdGetDatum(targettypeid);
76  values[Anum_pg_cast_castfunc - 1] = ObjectIdGetDatum(funcid);
77  values[Anum_pg_cast_castcontext - 1] = CharGetDatum(castcontext);
78  values[Anum_pg_cast_castmethod - 1] = CharGetDatum(castmethod);
79 
80  MemSet(nulls, false, sizeof(nulls));
81 
82  tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
83 
84  CatalogTupleInsert(relation, tuple);
85 
86  /* make dependency entries */
87  myself.classId = CastRelationId;
88  myself.objectId = castid;
89  myself.objectSubId = 0;
90 
91  /* dependency on source type */
92  referenced.classId = TypeRelationId;
93  referenced.objectId = sourcetypeid;
94  referenced.objectSubId = 0;
95  recordDependencyOn(&myself, &referenced, behavior);
96 
97  /* dependency on target type */
98  referenced.classId = TypeRelationId;
99  referenced.objectId = targettypeid;
100  referenced.objectSubId = 0;
101  recordDependencyOn(&myself, &referenced, behavior);
102 
103  /* dependency on function */
104  if (OidIsValid(funcid))
105  {
106  referenced.classId = ProcedureRelationId;
107  referenced.objectId = funcid;
108  referenced.objectSubId = 0;
109  recordDependencyOn(&myself, &referenced, behavior);
110  }
111 
112  /* dependency on extension */
113  recordDependencyOnCurrentExtension(&myself, false);
114 
115  /* Post creation hook for new cast */
116  InvokeObjectPostCreateHook(CastRelationId, castid, 0);
117 
118  heap_freetuple(tuple);
119 
120  table_close(relation, RowExclusiveLock);
121 
122  return myself;
123 }
DependencyType
Definition: dependency.h:31
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:317
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:482
ObjectAddress CastCreate(Oid sourcetypeid, Oid targettypeid, Oid funcid, char castcontext, char castmethod, DependencyType behavior)
Definition: pg_cast.c:43
int errcode(int sqlerrcode)
Definition: elog.c:610
#define MemSet(start, val, len)
Definition: c.h:978
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CastOidIndexId
Definition: indexing.h:114
uintptr_t Datum
Definition: postgres.h:367
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:138
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:824
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221