PostgreSQL Source Code git master
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/fmgrprotos.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)
 
CompareType IndexAmTranslateStrategy (StrategyNumber strategy, Oid amoid, Oid opfamily, bool missing_ok)
 
StrategyNumber IndexAmTranslateCompareType (CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
 
Datum amvalidate (PG_FUNCTION_ARGS)
 

Function Documentation

◆ amvalidate()

Datum amvalidate ( PG_FUNCTION_ARGS  )

Definition at line 164 of file amapi.c.

165{
166 Oid opclassoid = PG_GETARG_OID(0);
167 bool result;
168 HeapTuple classtup;
169 Form_pg_opclass classform;
170 Oid amoid;
171 IndexAmRoutine *amroutine;
172
173 classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
174 if (!HeapTupleIsValid(classtup))
175 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
176 classform = (Form_pg_opclass) GETSTRUCT(classtup);
177
178 amoid = classform->opcmethod;
179
180 ReleaseSysCache(classtup);
181
182 amroutine = GetIndexAmRoutineByAmId(amoid, false);
183
184 if (amroutine->amvalidate == NULL)
185 elog(ERROR, "function amvalidate is not defined for index access method %u",
186 amoid);
187
188 result = amroutine->amvalidate(opclassoid);
189
190 pfree(amroutine);
191
192 PG_RETURN_BOOL(result);
193}
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void pfree(void *pointer)
Definition: mcxt.c:1524
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:32
amvalidate_function amvalidate
Definition: amapi.h:304
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

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

◆ GetIndexAmRoutine()

IndexAmRoutine * GetIndexAmRoutine ( Oid  amhandler)

Definition at line 33 of file amapi.c.

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 OidFunctionCall0(functionId)
Definition: fmgr.h:677
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
uintptr_t Datum
Definition: postgres.h:69
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317

References DatumGetPointer(), elog, ERROR, IsA, and OidFunctionCall0.

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

◆ GetIndexAmRoutineByAmId()

IndexAmRoutine * GetIndexAmRoutineByAmId ( Oid  amoid,
bool  noerror 
)

Definition at line 56 of file amapi.c.

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 }
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 }
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}
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition: amapi.c:33
#define NameStr(name)
Definition: c.h:717
#define RegProcedureIsValid(p)
Definition: c.h:748
Oid regproc
Definition: c.h:620
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
FormData_pg_am * Form_pg_am
Definition: pg_am.h:48

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

Referenced by AlterOpFamily(), AlterOpFamilyAdd(), amvalidate(), assignOperTypes(), assignProcTypes(), comparison_ops_are_compatible(), ConstructTupleDescriptor(), DefineOpClass(), equality_ops_are_compatible(), indexam_property(), IndexAmTranslateCompareType(), IndexAmTranslateStrategy(), IndexSupportsBackwardScan(), IsIndexUsableForReplicaIdentityFull(), and pg_indexam_progress_phasename().

◆ IndexAmTranslateCompareType()

StrategyNumber IndexAmTranslateCompareType ( CompareType  cmptype,
Oid  amoid,
Oid  opfamily,
bool  missing_ok 
)

Definition at line 143 of file amapi.c.

144{
145 StrategyNumber result;
146 IndexAmRoutine *amroutine;
147
148 amroutine = GetIndexAmRoutineByAmId(amoid, false);
149 if (amroutine->amtranslatecmptype)
150 result = amroutine->amtranslatecmptype(cmptype, opfamily);
151 else
152 result = InvalidStrategy;
153
154 if (!missing_ok && result == InvalidStrategy)
155 elog(ERROR, "could not translate compare type %u for index AM %u", cmptype, amoid);
156
157 return result;
158}
uint16 StrategyNumber
Definition: stratnum.h:22
#define InvalidStrategy
Definition: stratnum.h:24
amtranslate_cmptype_function amtranslatecmptype
Definition: amapi.h:321

References IndexAmRoutine::amtranslatecmptype, elog, ERROR, GetIndexAmRoutineByAmId(), and InvalidStrategy.

Referenced by ATAddForeignKeyConstraint(), build_replindex_scan_key(), BuildSpeculativeIndexInfo(), GetOperatorFromCompareType(), and IsIndexUsableForReplicaIdentityFull().

◆ IndexAmTranslateStrategy()

CompareType IndexAmTranslateStrategy ( StrategyNumber  strategy,
Oid  amoid,
Oid  opfamily,
bool  missing_ok 
)

Definition at line 118 of file amapi.c.

119{
120 CompareType result;
121 IndexAmRoutine *amroutine;
122
123 amroutine = GetIndexAmRoutineByAmId(amoid, false);
124 if (amroutine->amtranslatestrategy)
125 result = amroutine->amtranslatestrategy(strategy, opfamily);
126 else
127 result = COMPARE_INVALID;
128
129 if (!missing_ok && result == COMPARE_INVALID)
130 elog(ERROR, "could not translate strategy number %d for index AM %u", strategy, amoid);
131
132 return result;
133}
CompareType
Definition: cmptype.h:32
@ COMPARE_INVALID
Definition: cmptype.h:33
amtranslate_strategy_function amtranslatestrategy
Definition: amapi.h:320

References IndexAmRoutine::amtranslatestrategy, COMPARE_INVALID, elog, ERROR, and GetIndexAmRoutineByAmId().