PostgreSQL Source Code  git master
amcmds.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * amcmds.c
4  * Routines for SQL commands that manipulate access methods.
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/commands/amcmds.c
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/htup_details.h"
17 #include "access/table.h"
18 #include "catalog/catalog.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/pg_am.h"
22 #include "catalog/pg_proc.h"
23 #include "catalog/pg_type.h"
24 #include "commands/defrem.h"
25 #include "miscadmin.h"
26 #include "parser/parse_func.h"
27 #include "utils/builtins.h"
28 #include "utils/lsyscache.h"
29 #include "utils/rel.h"
30 #include "utils/syscache.h"
31 
32 
33 static Oid lookup_am_handler_func(List *handler_name, char amtype);
34 static const char *get_am_type_string(char amtype);
35 
36 
37 /*
38  * CreateAccessMethod
39  * Registers a new access method.
40  */
43 {
44  Relation rel;
45  ObjectAddress myself;
46  ObjectAddress referenced;
47  Oid amoid;
48  Oid amhandler;
49  bool nulls[Natts_pg_am];
50  Datum values[Natts_pg_am];
51  HeapTuple tup;
52 
53  rel = table_open(AccessMethodRelationId, RowExclusiveLock);
54 
55  /* Must be super user */
56  if (!superuser())
57  ereport(ERROR,
58  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
59  errmsg("permission denied to create access method \"%s\"",
60  stmt->amname),
61  errhint("Must be superuser to create an access method.")));
62 
63  /* Check if name is used */
64  amoid = GetSysCacheOid1(AMNAME, Anum_pg_am_oid,
65  CStringGetDatum(stmt->amname));
66  if (OidIsValid(amoid))
67  {
68  ereport(ERROR,
70  errmsg("access method \"%s\" already exists",
71  stmt->amname)));
72  }
73 
74  /*
75  * Get the handler function oid, verifying the AM type while at it.
76  */
77  amhandler = lookup_am_handler_func(stmt->handler_name, stmt->amtype);
78 
79  /*
80  * Insert tuple into pg_am.
81  */
82  memset(values, 0, sizeof(values));
83  memset(nulls, false, sizeof(nulls));
84 
85  amoid = GetNewOidWithIndex(rel, AmOidIndexId, Anum_pg_am_oid);
86  values[Anum_pg_am_oid - 1] = ObjectIdGetDatum(amoid);
87  values[Anum_pg_am_amname - 1] =
89  values[Anum_pg_am_amhandler - 1] = ObjectIdGetDatum(amhandler);
90  values[Anum_pg_am_amtype - 1] = CharGetDatum(stmt->amtype);
91 
92  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
93 
94  CatalogTupleInsert(rel, tup);
95  heap_freetuple(tup);
96 
97  myself.classId = AccessMethodRelationId;
98  myself.objectId = amoid;
99  myself.objectSubId = 0;
100 
101  /* Record dependency on handler function */
102  referenced.classId = ProcedureRelationId;
103  referenced.objectId = amhandler;
104  referenced.objectSubId = 0;
105 
106  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
107 
108  recordDependencyOnCurrentExtension(&myself, false);
109 
111 
112  return myself;
113 }
114 
115 /*
116  * Guts of access method deletion.
117  */
118 void
120 {
121  Relation relation;
122  HeapTuple tup;
123 
124  if (!superuser())
125  ereport(ERROR,
126  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
127  errmsg("must be superuser to drop access methods")));
128 
129  relation = table_open(AccessMethodRelationId, RowExclusiveLock);
130 
131  tup = SearchSysCache1(AMOID, ObjectIdGetDatum(amOid));
132  if (!HeapTupleIsValid(tup))
133  elog(ERROR, "cache lookup failed for access method %u", amOid);
134 
135  CatalogTupleDelete(relation, &tup->t_self);
136 
137  ReleaseSysCache(tup);
138 
139  table_close(relation, RowExclusiveLock);
140 }
141 
142 /*
143  * get_am_type_oid
144  * Worker for various get_am_*_oid variants
145  *
146  * If missing_ok is false, throw an error if access method not found. If
147  * true, just return InvalidOid.
148  *
149  * If amtype is not '\0', an error is raised if the AM found is not of the
150  * given type.
151  */
152 static Oid
153 get_am_type_oid(const char *amname, char amtype, bool missing_ok)
154 {
155  HeapTuple tup;
156  Oid oid = InvalidOid;
157 
158  tup = SearchSysCache1(AMNAME, CStringGetDatum(amname));
159  if (HeapTupleIsValid(tup))
160  {
161  Form_pg_am amform = (Form_pg_am) GETSTRUCT(tup);
162 
163  if (amtype != '\0' &&
164  amform->amtype != amtype)
165  ereport(ERROR,
166  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
167  errmsg("access method \"%s\" is not of type %s",
168  NameStr(amform->amname),
169  get_am_type_string(amtype))));
170 
171  oid = amform->oid;
172  ReleaseSysCache(tup);
173  }
174 
175  if (!OidIsValid(oid) && !missing_ok)
176  ereport(ERROR,
177  (errcode(ERRCODE_UNDEFINED_OBJECT),
178  errmsg("access method \"%s\" does not exist", amname)));
179  return oid;
180 }
181 
182 /*
183  * get_index_am_oid - given an access method name, look up its OID
184  * and verify it corresponds to an index AM.
185  */
186 Oid
187 get_index_am_oid(const char *amname, bool missing_ok)
188 {
189  return get_am_type_oid(amname, AMTYPE_INDEX, missing_ok);
190 }
191 
192 /*
193  * get_table_am_oid - given an access method name, look up its OID
194  * and verify it corresponds to an table AM.
195  */
196 Oid
197 get_table_am_oid(const char *amname, bool missing_ok)
198 {
199  return get_am_type_oid(amname, AMTYPE_TABLE, missing_ok);
200 }
201 
202 /*
203  * get_am_oid - given an access method name, look up its OID.
204  * The type is not checked.
205  */
206 Oid
207 get_am_oid(const char *amname, bool missing_ok)
208 {
209  return get_am_type_oid(amname, '\0', missing_ok);
210 }
211 
212 /*
213  * get_am_name - given an access method OID name and type, look up its name.
214  */
215 char *
217 {
218  HeapTuple tup;
219  char *result = NULL;
220 
221  tup = SearchSysCache1(AMOID, ObjectIdGetDatum(amOid));
222  if (HeapTupleIsValid(tup))
223  {
224  Form_pg_am amform = (Form_pg_am) GETSTRUCT(tup);
225 
226  result = pstrdup(NameStr(amform->amname));
227  ReleaseSysCache(tup);
228  }
229  return result;
230 }
231 
232 /*
233  * Convert single-character access method type into string for error reporting.
234  */
235 static const char *
236 get_am_type_string(char amtype)
237 {
238  switch (amtype)
239  {
240  case AMTYPE_INDEX:
241  return "INDEX";
242  case AMTYPE_TABLE:
243  return "TABLE";
244  default:
245  /* shouldn't happen */
246  elog(ERROR, "invalid access method type '%c'", amtype);
247  return NULL; /* keep compiler quiet */
248  }
249 }
250 
251 /*
252  * Convert a handler function name to an Oid. If the return type of the
253  * function doesn't match the given AM type, an error is raised.
254  *
255  * This function either return valid function Oid or throw an error.
256  */
257 static Oid
258 lookup_am_handler_func(List *handler_name, char amtype)
259 {
260  Oid handlerOid;
261  Oid funcargtypes[1] = {INTERNALOID};
262  Oid expectedType = InvalidOid;
263 
264  if (handler_name == NIL)
265  ereport(ERROR,
266  (errcode(ERRCODE_UNDEFINED_FUNCTION),
267  errmsg("handler function is not specified")));
268 
269  /* handlers have one argument of type internal */
270  handlerOid = LookupFuncName(handler_name, 1, funcargtypes, false);
271 
272  /* check that handler has the correct return type */
273  switch (amtype)
274  {
275  case AMTYPE_INDEX:
276  expectedType = INDEX_AM_HANDLEROID;
277  break;
278  case AMTYPE_TABLE:
279  expectedType = TABLE_AM_HANDLEROID;
280  break;
281  default:
282  elog(ERROR, "unrecognized access method type \"%c\"", amtype);
283  }
284 
285  if (get_func_rettype(handlerOid) != expectedType)
286  ereport(ERROR,
287  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
288  errmsg("function %s must return type %s",
289  get_func_name(handlerOid),
290  format_type_extended(expectedType, -1, 0))));
291 
292  return handlerOid;
293 }
#define NIL
Definition: pg_list.h:65
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static const char * get_am_type_string(char amtype)
Definition: amcmds.c:236
static Oid lookup_am_handler_func(List *handler_name, char amtype)
Definition: amcmds.c:258
Oid get_am_oid(const char *amname, bool missing_ok)
Definition: amcmds.c:207
Definition: syscache.h:36
#define RelationGetDescr(relation)
Definition: rel.h:442
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static Oid get_am_type_oid(const char *amname, char amtype, bool missing_ok)
Definition: amcmds.c:153
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
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
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
void RemoveAccessMethodById(Oid amOid)
Definition: amcmds.c:119
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1457
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1410
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
Definition: parse_func.c:2100
char * amname
Definition: parsenodes.h:2395
ItemPointerData t_self
Definition: htup.h:65
Oid get_index_am_oid(const char *amname, bool missing_ok)
Definition: amcmds.c:187
char * get_am_name(Oid amOid)
Definition: amcmds.c:216
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
List * handler_name
Definition: parsenodes.h:2396
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid get_table_am_oid(const char *amname, bool missing_ok)
Definition: amcmds.c:197
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:138
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:108
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
FormData_pg_am * Form_pg_am
Definition: pg_am.h:48
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
#define AmOidIndexId
Definition: indexing.h:74
ObjectAddress CreateAccessMethod(CreateAmStmt *stmt)
Definition: amcmds.c:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:33
Definition: pg_list.h:50
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183