PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
amapi.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/htup_details.h"
#include "catalog/pg_am.h"
#include "catalog/pg_opclass.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
Include dependency graph for amapi.c:

Go to the source code of this file.

Functions

IndexAmRoutineGetIndexAmRoutine (Oid amhandler)
 
IndexAmRoutineGetIndexAmRoutineByAmId (Oid amoid, bool noerror)
 
Datum amvalidate (PG_FUNCTION_ARGS)
 

Function Documentation

Datum amvalidate ( PG_FUNCTION_ARGS  )

Definition at line 114 of file amapi.c.

References IndexAmRoutine::amvalidate, CLAOID, elog, ERROR, GetIndexAmRoutineByAmId(), GETSTRUCT, HeapTupleIsValid, NULL, ObjectIdGetDatum, pfree(), PG_GETARG_OID, PG_RETURN_BOOL, ReleaseSysCache(), result, and SearchSysCache1.

115 {
116  Oid opclassoid = PG_GETARG_OID(0);
117  bool result;
118  HeapTuple classtup;
119  Form_pg_opclass classform;
120  Oid amoid;
121  IndexAmRoutine *amroutine;
122 
123  classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
124  if (!HeapTupleIsValid(classtup))
125  elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
126  classform = (Form_pg_opclass) GETSTRUCT(classtup);
127 
128  amoid = classform->opcmethod;
129 
130  ReleaseSysCache(classtup);
131 
132  amroutine = GetIndexAmRoutineByAmId(amoid, false);
133 
134  if (amroutine->amvalidate == NULL)
135  elog(ERROR, "function amvalidate is not defined for index access method %u",
136  amoid);
137 
138  result = amroutine->amvalidate(opclassoid);
139 
140  pfree(amroutine);
141 
142  PG_RETURN_BOOL(result);
143 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
return result
Definition: formatting.c:1618
unsigned int Oid
Definition: postgres_ext.h:31
amvalidate_function amvalidate
Definition: amapi.h:207
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void pfree(void *pointer)
Definition: mcxt.c:950
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:68
IndexAmRoutine* GetIndexAmRoutine ( Oid  amhandler)

Definition at line 33 of file amapi.c.

References DatumGetPointer, elog, ERROR, IsA, NULL, and OidFunctionCall0.

Referenced by CheckIndexCompatible(), DefineIndex(), GetIndexAmRoutineByAmId(), InitIndexAmRoutine(), and pg_get_indexdef_worker().

34 {
35  Datum datum;
36  IndexAmRoutine *routine;
37 
38  datum = OidFunctionCall0(amhandler);
39  routine = (IndexAmRoutine *) DatumGetPointer(datum);
40 
41  if (routine == NULL || !IsA(routine, IndexAmRoutine))
42  elog(ERROR, "index access method handler function %u did not return an IndexAmRoutine struct",
43  amhandler);
44 
45  return routine;
46 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define ERROR
Definition: elog.h:43
uintptr_t Datum
Definition: postgres.h:372
#define OidFunctionCall0(functionId)
Definition: fmgr.h:620
#define NULL
Definition: c.h:229
#define DatumGetPointer(X)
Definition: postgres.h:555
#define elog
Definition: elog.h:219
IndexAmRoutine* GetIndexAmRoutineByAmId ( Oid  amoid,
bool  noerror 
)

Definition at line 56 of file amapi.c.

References AMOID, AMTYPE_INDEX, elog, ereport, errcode(), errmsg(), ERROR, GetIndexAmRoutine(), GETSTRUCT, HeapTupleIsValid, NameStr, NULL, ObjectIdGetDatum, RegProcedureIsValid, ReleaseSysCache(), and SearchSysCache1.

Referenced by AlterOpFamily(), amvalidate(), assignOperTypes(), ConstructTupleDescriptor(), DefineOpClass(), indexam_property(), and IndexSupportsBackwardScan().

57 {
58  HeapTuple tuple;
59  Form_pg_am amform;
60  regproc amhandler;
61 
62  /* Get handler function OID for the access method */
63  tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(amoid));
64  if (!HeapTupleIsValid(tuple))
65  {
66  if (noerror)
67  return NULL;
68  elog(ERROR, "cache lookup failed for access method %u",
69  amoid);
70  }
71  amform = (Form_pg_am) GETSTRUCT(tuple);
72 
73  /* Check if it's an index access method as opposed to some other AM */
74  if (amform->amtype != AMTYPE_INDEX)
75  {
76  if (noerror)
77  {
78  ReleaseSysCache(tuple);
79  return NULL;
80  }
81  ereport(ERROR,
82  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
83  errmsg("access method \"%s\" is not of type %s",
84  NameStr(amform->amname), "INDEX")));
85  }
86 
87  amhandler = amform->amhandler;
88 
89  /* Complain if handler OID is invalid */
90  if (!RegProcedureIsValid(amhandler))
91  {
92  if (noerror)
93  {
94  ReleaseSysCache(tuple);
95  return NULL;
96  }
97  ereport(ERROR,
98  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
99  errmsg("index access method \"%s\" does not have a handler",
100  NameStr(amform->amname))));
101  }
102 
103  ReleaseSysCache(tuple);
104 
105  /* And finally, call the handler function to get the API struct. */
106  return GetIndexAmRoutine(amhandler);
107 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Definition: syscache.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition: amapi.c:33
#define RegProcedureIsValid(p)
Definition: c.h:540
#define ereport(elevel, rest)
Definition: elog.h:122
#define AMTYPE_INDEX
Definition: pg_am.h:61
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
Oid regproc
Definition: c.h:394
FormData_pg_am * Form_pg_am
Definition: pg_am.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219