PostgreSQL Source Code  git master
pg_conversion.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/tableam.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_conversion.c:

Go to the source code of this file.

Functions

ObjectAddress ConversionCreate (const char *conname, Oid connamespace, Oid conowner, int32 conforencoding, int32 contoencoding, Oid conproc, bool def)
 
Oid FindDefaultConversion (Oid name_space, int32 for_encoding, int32 to_encoding)
 

Function Documentation

◆ ConversionCreate()

ObjectAddress ConversionCreate ( const char *  conname,
Oid  connamespace,
Oid  conowner,
int32  conforencoding,
int32  contoencoding,
Oid  conproc,
bool  def 
)

Definition at line 41 of file pg_conversion.c.

45 {
46  int i;
47  Relation rel;
48  TupleDesc tupDesc;
49  HeapTuple tup;
50  Oid oid;
51  bool nulls[Natts_pg_conversion];
52  Datum values[Natts_pg_conversion];
54  ObjectAddress myself,
55  referenced;
56 
57  /* sanity checks */
58  if (!conname)
59  elog(ERROR, "no conversion name supplied");
60 
61  /* make sure there is no existing conversion of same name */
63  PointerGetDatum(conname),
64  ObjectIdGetDatum(connamespace)))
65  ereport(ERROR,
67  errmsg("conversion \"%s\" already exists", conname)));
68 
69  if (def)
70  {
71  /*
72  * make sure there is no existing default <for encoding><to encoding>
73  * pair in this name space
74  */
75  if (FindDefaultConversion(connamespace,
76  conforencoding,
77  contoencoding))
78  ereport(ERROR,
80  errmsg("default conversion for %s to %s already exists",
81  pg_encoding_to_char(conforencoding),
82  pg_encoding_to_char(contoencoding))));
83  }
84 
85  /* open pg_conversion */
86  rel = table_open(ConversionRelationId, RowExclusiveLock);
87  tupDesc = rel->rd_att;
88 
89  /* initialize nulls and values */
90  for (i = 0; i < Natts_pg_conversion; i++)
91  {
92  nulls[i] = false;
93  values[i] = (Datum) NULL;
94  }
95 
96  /* form a tuple */
97  namestrcpy(&cname, conname);
98  oid = GetNewOidWithIndex(rel, ConversionOidIndexId,
99  Anum_pg_conversion_oid);
100  values[Anum_pg_conversion_oid - 1] = ObjectIdGetDatum(oid);
101  values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
102  values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
103  values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
104  values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
105  values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
106  values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
107  values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);
108 
109  tup = heap_form_tuple(tupDesc, values, nulls);
110 
111  /* insert a new tuple */
112  CatalogTupleInsert(rel, tup);
113 
114  myself.classId = ConversionRelationId;
115  myself.objectId = oid;
116  myself.objectSubId = 0;
117 
118  /* create dependency on conversion procedure */
119  referenced.classId = ProcedureRelationId;
120  referenced.objectId = conproc;
121  referenced.objectSubId = 0;
122  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
123 
124  /* create dependency on namespace */
125  referenced.classId = NamespaceRelationId;
126  referenced.objectId = connamespace;
127  referenced.objectSubId = 0;
128  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
129 
130  /* create dependency on owner */
131  recordDependencyOnOwner(ConversionRelationId, oid, conowner);
132 
133  /* dependency on extension */
134  recordDependencyOnCurrentExtension(&myself, false);
135 
136  /* Post creation hook for new conversion */
137  InvokeObjectPostCreateHook(ConversionRelationId, oid, 0);
138 
139  heap_freetuple(tup);
141 
142  return myself;
143 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:393
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
int i
Definition: isn.c:73
#define RowExclusiveLock
Definition: lockdefs.h:38
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
Oid FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:192
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
#define pg_encoding_to_char
Definition: pg_wchar.h:563
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
unsigned int Oid
Definition: postgres_ext.h:31
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
TupleDesc rd_att
Definition: rel.h:112
Definition: c.h:730
@ CONNAMENSP
Definition: syscache.h:52
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:193
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References BoolGetDatum(), CatalogTupleInsert(), ObjectAddress::classId, CONNAMENSP, DEPENDENCY_NORMAL, elog(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, FindDefaultConversion(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), i, Int32GetDatum(), InvokeObjectPostCreateHook, NameGetDatum(), namestrcpy(), ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, pg_encoding_to_char, PointerGetDatum(), RelationData::rd_att, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, SearchSysCacheExists2, table_close(), table_open(), and values.

Referenced by CreateConversionCommand().

◆ FindDefaultConversion()

Oid FindDefaultConversion ( Oid  name_space,
int32  for_encoding,
int32  to_encoding 
)

Definition at line 155 of file pg_conversion.c.

156 {
157  CatCList *catlist;
158  HeapTuple tuple;
159  Form_pg_conversion body;
160  Oid proc = InvalidOid;
161  int i;
162 
164  ObjectIdGetDatum(name_space),
165  Int32GetDatum(for_encoding),
166  Int32GetDatum(to_encoding));
167 
168  for (i = 0; i < catlist->n_members; i++)
169  {
170  tuple = &catlist->members[i]->tuple;
171  body = (Form_pg_conversion) GETSTRUCT(tuple);
172  if (body->condefault)
173  {
174  proc = body->conproc;
175  break;
176  }
177  }
178  ReleaseSysCacheList(catlist);
179  return proc;
180 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:61
#define InvalidOid
Definition: postgres_ext.h:36
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
int n_members
Definition: catcache.h:176
HeapTupleData tuple
Definition: catcache.h:121
#define SearchSysCacheList3(cacheId, key1, key2, key3)
Definition: syscache.h:222
@ CONDEFAULT
Definition: syscache.h:51
#define ReleaseSysCacheList(x)
Definition: syscache.h:225

References CONDEFAULT, GETSTRUCT, i, Int32GetDatum(), InvalidOid, catclist::members, catclist::n_members, ObjectIdGetDatum(), ReleaseSysCacheList, SearchSysCacheList3, and catctup::tuple.

Referenced by ConversionCreate(), and FindDefaultConversionProc().