PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 187 of file amapi.c.

188{
189 Oid opclassoid = PG_GETARG_OID(0);
190 bool result;
191 HeapTuple classtup;
192 Form_pg_opclass classform;
193 Oid amoid;
194 IndexAmRoutine *amroutine;
195
196 classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
197 if (!HeapTupleIsValid(classtup))
198 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
199 classform = (Form_pg_opclass) GETSTRUCT(classtup);
200
201 amoid = classform->opcmethod;
202
203 ReleaseSysCache(classtup);
204
205 amroutine = GetIndexAmRoutineByAmId(amoid, false);
206
207 if (amroutine->amvalidate == NULL)
208 elog(ERROR, "function amvalidate is not defined for index access method %u",
209 amoid);
210
211 result = amroutine->amvalidate(opclassoid);
212
213 pfree(amroutine);
214
215 PG_RETURN_BOOL(result);
216}
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:69
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#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:1594
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
unsigned int Oid
Definition: postgres_ext.h:32
amvalidate_function amvalidate
Definition: amapi.h:307
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

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 /* Assert that all required callbacks are present. */
46 Assert(routine->ambuild != NULL);
47 Assert(routine->ambuildempty != NULL);
48 Assert(routine->aminsert != NULL);
49 Assert(routine->ambulkdelete != NULL);
50 Assert(routine->amvacuumcleanup != NULL);
51 Assert(routine->amcostestimate != NULL);
52 Assert(routine->amoptions != NULL);
53 Assert(routine->amvalidate != NULL);
54 Assert(routine->ambeginscan != NULL);
55 Assert(routine->amrescan != NULL);
56 Assert(routine->amendscan != NULL);
57
58 return routine;
59}
#define OidFunctionCall0(functionId)
Definition: fmgr.h:718
Assert(PointerIsAligned(start, uint64))
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
uint64_t Datum
Definition: postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
ambuildempty_function ambuildempty
Definition: amapi.h:296
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:300
amoptions_function amoptions
Definition: amapi.h:304
aminsert_function aminsert
Definition: amapi.h:297
amendscan_function amendscan
Definition: amapi.h:313
amcostestimate_function amcostestimate
Definition: amapi.h:302
ambuild_function ambuild
Definition: amapi.h:295
ambulkdelete_function ambulkdelete
Definition: amapi.h:299
ambeginscan_function ambeginscan
Definition: amapi.h:309
amrescan_function amrescan
Definition: amapi.h:310

References IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcostestimate, IndexAmRoutine::amendscan, IndexAmRoutine::aminsert, IndexAmRoutine::amoptions, IndexAmRoutine::amrescan, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, Assert(), 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 69 of file amapi.c.

70{
71 HeapTuple tuple;
72 Form_pg_am amform;
73 regproc amhandler;
74
75 /* Get handler function OID for the access method */
76 tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(amoid));
77 if (!HeapTupleIsValid(tuple))
78 {
79 if (noerror)
80 return NULL;
81 elog(ERROR, "cache lookup failed for access method %u",
82 amoid);
83 }
84 amform = (Form_pg_am) GETSTRUCT(tuple);
85
86 /* Check if it's an index access method as opposed to some other AM */
87 if (amform->amtype != AMTYPE_INDEX)
88 {
89 if (noerror)
90 {
91 ReleaseSysCache(tuple);
92 return NULL;
93 }
95 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
96 errmsg("access method \"%s\" is not of type %s",
97 NameStr(amform->amname), "INDEX")));
98 }
99
100 amhandler = amform->amhandler;
101
102 /* Complain if handler OID is invalid */
103 if (!RegProcedureIsValid(amhandler))
104 {
105 if (noerror)
106 {
107 ReleaseSysCache(tuple);
108 return NULL;
109 }
111 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
112 errmsg("index access method \"%s\" does not have a handler",
113 NameStr(amform->amname))));
114 }
115
116 ReleaseSysCache(tuple);
117
118 /* And finally, call the handler function to get the API struct. */
119 return GetIndexAmRoutine(amhandler);
120}
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition: amapi.c:33
#define NameStr(name)
Definition: c.h:755
#define RegProcedureIsValid(p)
Definition: c.h:780
Oid regproc
Definition: c.h:658
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
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(), get_op_index_interpretation(), get_opmethod_canorder(), 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 161 of file amapi.c.

162{
163 StrategyNumber result;
164 IndexAmRoutine *amroutine;
165
166 /* shortcut for common case */
167 if (amoid == BTREE_AM_OID &&
168 (cmptype > COMPARE_INVALID && cmptype <= COMPARE_GT))
169 return (StrategyNumber) cmptype;
170
171 amroutine = GetIndexAmRoutineByAmId(amoid, false);
172 if (amroutine->amtranslatecmptype)
173 result = amroutine->amtranslatecmptype(cmptype, opfamily);
174 else
175 result = InvalidStrategy;
176
177 if (!missing_ok && result == InvalidStrategy)
178 elog(ERROR, "could not translate compare type %u for index AM %u", cmptype, amoid);
179
180 return result;
181}
@ COMPARE_INVALID
Definition: cmptype.h:33
@ COMPARE_GT
Definition: cmptype.h:38
uint16 StrategyNumber
Definition: stratnum.h:22
#define InvalidStrategy
Definition: stratnum.h:24
amtranslate_cmptype_function amtranslatecmptype
Definition: amapi.h:324

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

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

◆ IndexAmTranslateStrategy()

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

Definition at line 131 of file amapi.c.

132{
133 CompareType result;
134 IndexAmRoutine *amroutine;
135
136 /* shortcut for common case */
137 if (amoid == BTREE_AM_OID &&
138 (strategy > InvalidStrategy && strategy <= BTMaxStrategyNumber))
139 return (CompareType) strategy;
140
141 amroutine = GetIndexAmRoutineByAmId(amoid, false);
142 if (amroutine->amtranslatestrategy)
143 result = amroutine->amtranslatestrategy(strategy, opfamily);
144 else
145 result = COMPARE_INVALID;
146
147 if (!missing_ok && result == COMPARE_INVALID)
148 elog(ERROR, "could not translate strategy number %d for index AM %u", strategy, amoid);
149
150 return result;
151}
CompareType
Definition: cmptype.h:32
#define BTMaxStrategyNumber
Definition: stratnum.h:35
amtranslate_strategy_function amtranslatestrategy
Definition: amapi.h:323

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

Referenced by get_actual_variable_range(), get_mergejoin_opfamilies(), get_op_index_interpretation(), get_ordering_op_for_equality_op(), get_ordering_op_properties(), mergejoinscansel(), and MJExamineQuals().