PostgreSQL Source Code  git master
pg_cast.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_cast.c:

Go to the source code of this file.

Functions

ObjectAddress CastCreate (Oid sourcetypeid, Oid targettypeid, Oid funcid, Oid incastid, Oid outcastid, char castcontext, char castmethod, DependencyType behavior)
 

Function Documentation

◆ CastCreate()

ObjectAddress CastCreate ( Oid  sourcetypeid,
Oid  targettypeid,
Oid  funcid,
Oid  incastid,
Oid  outcastid,
char  castcontext,
char  castmethod,
DependencyType  behavior 
)

Definition at line 49 of file pg_cast.c.

52 {
53  Relation relation;
54  HeapTuple tuple;
55  Oid castid;
56  Datum values[Natts_pg_cast];
57  bool nulls[Natts_pg_cast] = {0};
58  ObjectAddress myself,
59  referenced;
60  ObjectAddresses *addrs;
61 
62  relation = table_open(CastRelationId, RowExclusiveLock);
63 
64  /*
65  * Check for duplicate. This is just to give a friendly error message,
66  * the unique index would catch it anyway (so no need to sweat about race
67  * conditions).
68  */
70  ObjectIdGetDatum(sourcetypeid),
71  ObjectIdGetDatum(targettypeid));
72  if (HeapTupleIsValid(tuple))
73  ereport(ERROR,
75  errmsg("cast from type %s to type %s already exists",
76  format_type_be(sourcetypeid),
77  format_type_be(targettypeid))));
78 
79  /* ready to go */
80  castid = GetNewOidWithIndex(relation, CastOidIndexId, Anum_pg_cast_oid);
81  values[Anum_pg_cast_oid - 1] = ObjectIdGetDatum(castid);
82  values[Anum_pg_cast_castsource - 1] = ObjectIdGetDatum(sourcetypeid);
83  values[Anum_pg_cast_casttarget - 1] = ObjectIdGetDatum(targettypeid);
84  values[Anum_pg_cast_castfunc - 1] = ObjectIdGetDatum(funcid);
85  values[Anum_pg_cast_castcontext - 1] = CharGetDatum(castcontext);
86  values[Anum_pg_cast_castmethod - 1] = CharGetDatum(castmethod);
87 
88  tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
89 
90  CatalogTupleInsert(relation, tuple);
91 
92  addrs = new_object_addresses();
93 
94  /* make dependency entries */
95  ObjectAddressSet(myself, CastRelationId, castid);
96 
97  /* dependency on source type */
98  ObjectAddressSet(referenced, TypeRelationId, sourcetypeid);
99  add_exact_object_address(&referenced, addrs);
100 
101  /* dependency on target type */
102  ObjectAddressSet(referenced, TypeRelationId, targettypeid);
103  add_exact_object_address(&referenced, addrs);
104 
105  /* dependency on function */
106  if (OidIsValid(funcid))
107  {
108  ObjectAddressSet(referenced, ProcedureRelationId, funcid);
109  add_exact_object_address(&referenced, addrs);
110  }
111 
112  /* dependencies on casts required for function */
113  if (OidIsValid(incastid))
114  {
115  ObjectAddressSet(referenced, CastRelationId, incastid);
116  add_exact_object_address(&referenced, addrs);
117  }
118  if (OidIsValid(outcastid))
119  {
120  ObjectAddressSet(referenced, CastRelationId, outcastid);
121  add_exact_object_address(&referenced, addrs);
122  }
123 
124  record_object_address_dependencies(&myself, addrs, behavior);
125  free_object_addresses(addrs);
126 
127  /* dependency on extension */
128  recordDependencyOnCurrentExtension(&myself, false);
129 
130  /* Post creation hook for new cast */
131  InvokeObjectPostCreateHook(CastRelationId, castid, 0);
132 
133  heap_freetuple(tuple);
134 
135  table_close(relation, RowExclusiveLock);
136 
137  return myself;
138 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define OidIsValid(objectId)
Definition: c.h:711
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:393
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2790
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2526
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2581
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2821
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:192
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum CharGetDatum(char X)
Definition: postgres.h:470
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:527
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
@ CASTSOURCETARGET
Definition: syscache.h:46
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References add_exact_object_address(), CASTSOURCETARGET, CatalogTupleInsert(), CharGetDatum(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, format_type_be(), free_object_addresses(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), HeapTupleIsValid, InvokeObjectPostCreateHook, new_object_addresses(), ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, record_object_address_dependencies(), recordDependencyOnCurrentExtension(), RelationGetDescr, RowExclusiveLock, SearchSysCache2(), table_close(), table_open(), and values.

Referenced by CreateCast(), and DefineRange().