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 CastOidIndexId, CASTSOURCETARGET, CatalogTupleInsert(), CharGetDatum, ObjectAddress::classId, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, format_type_be(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), HeapTupleIsValid, InvokeObjectPostCreateHook, MemSet, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, recordDependencyOn(), 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 
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 }
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
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