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 "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_conversion_fn.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 "utils/tqual.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)
 
void RemoveConversionById (Oid conversionOid)
 
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.

References Anum_pg_conversion_condefault, Anum_pg_conversion_conforencoding, Anum_pg_conversion_conname, Anum_pg_conversion_connamespace, Anum_pg_conversion_conowner, Anum_pg_conversion_conproc, Anum_pg_conversion_contoencoding, BoolGetDatum, CatalogTupleInsert(), ObjectAddress::classId, CONNAMENSP, ConversionRelationId, DEPENDENCY_NORMAL, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, FindDefaultConversion(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleGetOid, i, Int32GetDatum, InvokeObjectPostCreateHook, NameGetDatum, NamespaceRelationId, namestrcpy(), Natts_pg_conversion, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, pg_encoding_to_char(), PointerGetDatum, ProcedureRelationId, RelationData::rd_att, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, SearchSysCacheExists2, and values.

Referenced by CreateConversionCommand().

45 {
46  int i;
47  Relation rel;
48  TupleDesc tupDesc;
49  HeapTuple tup;
50  bool nulls[Natts_pg_conversion];
53  ObjectAddress myself,
54  referenced;
55 
56  /* sanity checks */
57  if (!conname)
58  elog(ERROR, "no conversion name supplied");
59 
60  /* make sure there is no existing conversion of same name */
62  PointerGetDatum(conname),
63  ObjectIdGetDatum(connamespace)))
64  ereport(ERROR,
66  errmsg("conversion \"%s\" already exists", conname)));
67 
68  if (def)
69  {
70  /*
71  * make sure there is no existing default <for encoding><to encoding>
72  * pair in this name space
73  */
74  if (FindDefaultConversion(connamespace,
75  conforencoding,
76  contoencoding))
77  ereport(ERROR,
79  errmsg("default conversion for %s to %s already exists",
80  pg_encoding_to_char(conforencoding),
81  pg_encoding_to_char(contoencoding))));
82  }
83 
84  /* open pg_conversion */
86  tupDesc = rel->rd_att;
87 
88  /* initialize nulls and values */
89  for (i = 0; i < Natts_pg_conversion; i++)
90  {
91  nulls[i] = false;
92  values[i] = (Datum) NULL;
93  }
94 
95  /* form a tuple */
96  namestrcpy(&cname, conname);
97  values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
98  values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
99  values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
100  values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
101  values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
102  values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
103  values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);
104 
105  tup = heap_form_tuple(tupDesc, values, nulls);
106 
107  /* insert a new tuple */
108  CatalogTupleInsert(rel, tup);
109 
110  myself.classId = ConversionRelationId;
111  myself.objectId = HeapTupleGetOid(tup);
112  myself.objectSubId = 0;
113 
114  /* create dependency on conversion procedure */
115  referenced.classId = ProcedureRelationId;
116  referenced.objectId = conproc;
117  referenced.objectSubId = 0;
118  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
119 
120  /* create dependency on namespace */
121  referenced.classId = NamespaceRelationId;
122  referenced.objectId = connamespace;
123  referenced.objectSubId = 0;
124  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
125 
126  /* create dependency on owner */
128  conowner);
129 
130  /* dependency on extension */
131  recordDependencyOnCurrentExtension(&myself, false);
132 
133  /* Post creation hook for new conversion */
135 
136  heap_freetuple(tup);
138 
139  return myself;
140 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:601
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define Anum_pg_conversion_condefault
Definition: pg_conversion.h:70
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ProcedureRelationId
Definition: pg_proc.h:33
#define Anum_pg_conversion_contoencoding
Definition: pg_conversion.h:68
int errcode(int sqlerrcode)
Definition: elog.c:575
#define Natts_pg_conversion
Definition: pg_conversion.h:63
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373
int namestrcpy(Name name, const char *str)
Definition: name.c:216
#define Anum_pg_conversion_conforencoding
Definition: pg_conversion.h:67
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
Definition: c.h:541
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
#define BoolGetDatum(X)
Definition: postgres.h:408
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
#define Anum_pg_conversion_conowner
Definition: pg_conversion.h:66
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:184
#define Anum_pg_conversion_conname
Definition: pg_conversion.h:64
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define Anum_pg_conversion_conproc
Definition: pg_conversion.h:69
#define elog
Definition: elog.h:219
Oid FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700
#define Anum_pg_conversion_connamespace
Definition: pg_conversion.h:65
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
#define ConversionRelationId
Definition: pg_conversion.h:38

◆ FindDefaultConversion()

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

Definition at line 185 of file pg_conversion.c.

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

Referenced by ConversionCreate(), and FindDefaultConversionProc().

186 {
187  CatCList *catlist;
188  HeapTuple tuple;
189  Form_pg_conversion body;
190  Oid proc = InvalidOid;
191  int i;
192 
194  ObjectIdGetDatum(name_space),
195  Int32GetDatum(for_encoding),
196  Int32GetDatum(to_encoding));
197 
198  for (i = 0; i < catlist->n_members; i++)
199  {
200  tuple = &catlist->members[i]->tuple;
201  body = (Form_pg_conversion) GETSTRUCT(tuple);
202  if (body->condefault)
203  {
204  proc = body->conproc;
205  break;
206  }
207  }
208  ReleaseSysCacheList(catlist);
209  return proc;
210 }
int n_members
Definition: catcache.h:176
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
unsigned int Oid
Definition: postgres_ext.h:31
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ReleaseSysCacheList(x)
Definition: syscache.h:218
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:56
#define InvalidOid
Definition: postgres_ext.h:36
#define Int32GetDatum(X)
Definition: postgres.h:485
#define SearchSysCacheList3(cacheId, key1, key2, key3)
Definition: syscache.h:213
int i
HeapTupleData tuple
Definition: catcache.h:121

◆ RemoveConversionById()

void RemoveConversionById ( Oid  conversionOid)

Definition at line 149 of file pg_conversion.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), ConversionRelationId, elog, ERROR, ForwardScanDirection, heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleIsValid, ObjectIdAttributeNumber, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), and HeapTupleData::t_self.

Referenced by doDeletion().

150 {
151  Relation rel;
152  HeapTuple tuple;
153  HeapScanDesc scan;
154  ScanKeyData scanKeyData;
155 
156  ScanKeyInit(&scanKeyData,
158  BTEqualStrategyNumber, F_OIDEQ,
159  ObjectIdGetDatum(conversionOid));
160 
161  /* open pg_conversion */
163 
164  scan = heap_beginscan_catalog(rel, 1, &scanKeyData);
165 
166  /* search for the target tuple */
168  CatalogTupleDelete(rel, &tuple->t_self);
169  else
170  elog(ERROR, "could not find tuple for conversion %u", conversionOid);
171  heap_endscan(scan);
173 }
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1565
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1405
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1808
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define ConversionRelationId
Definition: pg_conversion.h:38
#define BTEqualStrategyNumber
Definition: stratnum.h:31