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, char castcontext, char castmethod, DependencyType behavior)
 

Function Documentation

◆ CastCreate()

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

Definition at line 43 of file pg_cast.c.

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

Referenced by CreateCast().

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 }
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
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