PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_conversion.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_conversion.c
4 * routines to support manipulation of the pg_conversion relation
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/catalog/pg_conversion.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/htup_details.h"
18#include "access/table.h"
19#include "catalog/catalog.h"
20#include "catalog/dependency.h"
21#include "catalog/indexing.h"
25#include "catalog/pg_proc.h"
26#include "mb/pg_wchar.h"
27#include "utils/builtins.h"
28#include "utils/catcache.h"
29#include "utils/rel.h"
30#include "utils/syscache.h"
31
32/*
33 * ConversionCreate
34 *
35 * Add a new tuple to pg_conversion.
36 */
38ConversionCreate(const char *conname, Oid connamespace,
41 Oid conproc, bool def)
42{
43 int i;
44 Relation rel;
45 TupleDesc tupDesc;
47 Oid oid;
48 bool nulls[Natts_pg_conversion];
53
54 /* sanity checks */
55 if (!conname)
56 elog(ERROR, "no conversion name supplied");
57
58 /* make sure there is no existing conversion of same name */
60 PointerGetDatum(conname),
64 errmsg("conversion \"%s\" already exists", conname)));
65
66 if (def)
67 {
68 /*
69 * make sure there is no existing default <for encoding><to encoding>
70 * pair in this name space
71 */
77 errmsg("default conversion for %s to %s already exists",
80 }
81
82 /* open pg_conversion */
84 tupDesc = rel->rd_att;
85
86 /* initialize nulls and values */
87 for (i = 0; i < Natts_pg_conversion; i++)
88 {
89 nulls[i] = false;
90 values[i] = (Datum) 0;
91 }
92
93 /* form a tuple */
94 namestrcpy(&cname, conname);
105
106 tup = heap_form_tuple(tupDesc, values, nulls);
107
108 /* insert a new tuple */
110
112 myself.objectId = oid;
113 myself.objectSubId = 0;
114
115 /* create dependency on conversion procedure */
117 referenced.objectId = conproc;
118 referenced.objectSubId = 0;
120
121 /* create dependency on namespace */
123 referenced.objectId = connamespace;
124 referenced.objectSubId = 0;
126
127 /* create dependency on owner */
129
130 /* dependency on extension */
132
133 /* Post creation hook for new conversion */
135
138
139 return myself;
140}
141
142/*
143 * FindDefaultConversion
144 *
145 * Find "default" conversion proc by for_encoding and to_encoding in the
146 * given namespace.
147 *
148 * If found, returns the procedure's oid, otherwise InvalidOid. Note that
149 * you get the procedure's OID not the conversion's OID!
150 */
151Oid
153{
155 HeapTuple tuple;
157 Oid proc = InvalidOid;
158 int i;
159
164
165 for (i = 0; i < catlist->n_members; i++)
166 {
167 tuple = &catlist->members[i]->tuple;
168 body = (Form_pg_conversion) GETSTRUCT(tuple);
169 if (body->condefault)
170 {
171 proc = body->conproc;
172 break;
173 }
174 }
176 return proc;
177}
static Datum values[MAXATTR]
Definition bootstrap.c:155
int32_t int32
Definition c.h:542
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition catalog.c:448
@ DEPENDENCY_NORMAL
Definition dependency.h:33
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
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
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
int i
Definition isn.c:77
#define RowExclusiveLock
Definition lockdefs.h:38
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Oid FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
ObjectAddress ConversionCreate(const char *conname, Oid connamespace, Oid conowner, int32 conforencoding, int32 contoencoding, Oid conproc, bool def)
FormData_pg_conversion * Form_pg_conversion
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:45
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition pg_depend.c:193
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
#define pg_encoding_to_char
Definition pg_wchar.h:630
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:403
uint64_t Datum
Definition postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
#define InvalidOid
unsigned int Oid
static int fb(int x)
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
TupleDesc rd_att
Definition rel.h:112
Definition c.h:760
#define SearchSysCacheList3(cacheId, key1, key2, key3)
Definition syscache.h:131
#define ReleaseSysCacheList(x)
Definition syscache.h:134
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition syscache.h:102
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40