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-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_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  ObjectAddresses *addrs;
54 
55  relation = table_open(CastRelationId, RowExclusiveLock);
56 
57  /*
58  * Check for duplicate. This is just to give a friendly error message,
59  * the unique index would catch it anyway (so no need to sweat about race
60  * conditions).
61  */
63  ObjectIdGetDatum(sourcetypeid),
64  ObjectIdGetDatum(targettypeid));
65  if (HeapTupleIsValid(tuple))
66  ereport(ERROR,
68  errmsg("cast from type %s to type %s already exists",
69  format_type_be(sourcetypeid),
70  format_type_be(targettypeid))));
71 
72  /* ready to go */
73  castid = GetNewOidWithIndex(relation, CastOidIndexId, Anum_pg_cast_oid);
74  values[Anum_pg_cast_oid - 1] = ObjectIdGetDatum(castid);
75  values[Anum_pg_cast_castsource - 1] = ObjectIdGetDatum(sourcetypeid);
76  values[Anum_pg_cast_casttarget - 1] = ObjectIdGetDatum(targettypeid);
77  values[Anum_pg_cast_castfunc - 1] = ObjectIdGetDatum(funcid);
78  values[Anum_pg_cast_castcontext - 1] = CharGetDatum(castcontext);
79  values[Anum_pg_cast_castmethod - 1] = CharGetDatum(castmethod);
80 
81  MemSet(nulls, false, sizeof(nulls));
82 
83  tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
84 
85  CatalogTupleInsert(relation, tuple);
86 
87  addrs = new_object_addresses();
88 
89  /* make dependency entries */
90  ObjectAddressSet(myself, CastRelationId, castid);
91 
92  /* dependency on source type */
93  ObjectAddressSet(referenced, TypeRelationId, sourcetypeid);
94  add_exact_object_address(&referenced, addrs);
95 
96  /* dependency on target type */
97  ObjectAddressSet(referenced, TypeRelationId, targettypeid);
98  add_exact_object_address(&referenced, addrs);
99 
100  /* dependency on function */
101  if (OidIsValid(funcid))
102  {
103  ObjectAddressSet(referenced, ProcedureRelationId, funcid);
104  add_exact_object_address(&referenced, addrs);
105  }
106 
107  record_object_address_dependencies(&myself, addrs, behavior);
108  free_object_addresses(addrs);
109 
110  /* dependency on extension */
111  recordDependencyOnCurrentExtension(&myself, false);
112 
113  /* Post creation hook for new cast */
114  InvokeObjectPostCreateHook(CastRelationId, castid, 0);
115 
116  heap_freetuple(tuple);
117 
118  table_close(relation, RowExclusiveLock);
119 
120  return myself;
121 }
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:167
#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
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2676
int errcode(int sqlerrcode)
Definition: elog.c:610
#define MemSet(start, val, len)
Definition: c.h:950
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
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
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CastOidIndexId
Definition: indexing.h:120
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:180
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:821
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221