PostgreSQL Source Code git master
Loading...
Searching...
No Matches
amapi.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * amapi.c
4 * Support routines for API for Postgres index access methods.
5 *
6 * Copyright (c) 2015-2026, PostgreSQL Global Development Group
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/access/index/amapi.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres.h"
15
16#include "access/amapi.h"
17#include "access/htup_details.h"
18#include "catalog/pg_am.h"
19#include "catalog/pg_opclass.h"
20#include "utils/fmgrprotos.h"
21#include "utils/syscache.h"
22
23
24/*
25 * GetIndexAmRoutine - call the specified access method handler routine to get
26 * its IndexAmRoutine struct, which we expect to be statically allocated.
27 *
28 * Note that if the amhandler function is built-in, this will not involve
29 * any catalog access. It's therefore safe to use this while bootstrapping
30 * indexes for the system catalogs. relcache.c relies on that.
31 */
32const IndexAmRoutine *
34{
35 Datum datum;
36 const IndexAmRoutine *routine;
37
38 datum = OidFunctionCall0(amhandler);
39 routine = (const 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}
60
61/*
62 * GetIndexAmRoutineByAmId - look up the handler of the index access method
63 * with the given OID, and get its IndexAmRoutine struct.
64 *
65 * If the given OID isn't a valid index access method, returns NULL if
66 * noerror is true, else throws error.
67 */
68const IndexAmRoutine *
70{
71 HeapTuple tuple;
73 regproc amhandler;
74
75 /* Get handler function OID for the access method */
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 }
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 }
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}
121
122
123/*
124 * IndexAmTranslateStrategy - given an access method and strategy, get the
125 * corresponding compare type.
126 *
127 * If missing_ok is false, throw an error if no compare type is found. If
128 * true, just return COMPARE_INVALID.
129 */
131IndexAmTranslateStrategy(StrategyNumber strategy, Oid amoid, Oid opfamily, bool missing_ok)
132{
133 CompareType result;
135
136 /* shortcut for common case */
137 if (amoid == BTREE_AM_OID &&
138 (strategy > InvalidStrategy && strategy <= BTMaxStrategyNumber))
139 return (CompareType) strategy;
140
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}
152
153/*
154 * IndexAmTranslateCompareType - given an access method and compare type, get
155 * the corresponding strategy number.
156 *
157 * If missing_ok is false, throw an error if no strategy is found correlating
158 * to the given cmptype. If true, just return InvalidStrategy.
159 */
161IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
162{
163 StrategyNumber result;
165
166 /* shortcut for common case */
167 if (amoid == BTREE_AM_OID &&
168 (cmptype > COMPARE_INVALID && cmptype <= COMPARE_GT))
169 return (StrategyNumber) cmptype;
170
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}
182
183/*
184 * Ask appropriate access method to validate the specified opclass.
185 */
186Datum
188{
189 Oid opclassoid = PG_GETARG_OID(0);
190 bool result;
193 Oid amoid;
195
198 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
200
201 amoid = classform->opcmethod;
202
204
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 PG_RETURN_BOOL(result);
214}
const IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition amapi.c:69
const IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
Definition amapi.c:33
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:161
Datum amvalidate(PG_FUNCTION_ARGS)
Definition amapi.c:187
CompareType IndexAmTranslateStrategy(StrategyNumber strategy, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:131
#define NameStr(name)
Definition c.h:765
#define RegProcedureIsValid(p)
Definition c.h:792
#define Assert(condition)
Definition c.h:873
Oid regproc
Definition c.h:663
CompareType
Definition cmptype.h:32
@ COMPARE_INVALID
Definition cmptype.h:33
@ COMPARE_GT
Definition cmptype.h:38
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
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define OidFunctionCall0(functionId)
Definition fmgr.h:720
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define IsA(nodeptr, _type_)
Definition nodes.h:164
FormData_pg_am * Form_pg_am
Definition pg_am.h:48
FormData_pg_opclass * Form_pg_opclass
Definition pg_opclass.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
unsigned int Oid
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
#define InvalidStrategy
Definition stratnum.h:24
#define BTMaxStrategyNumber
Definition stratnum.h:35
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
amvalidate_function amvalidate
Definition amapi.h:307
ambeginscan_function ambeginscan
Definition amapi.h:309
amrescan_function amrescan
Definition amapi.h:310
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition syscache.c:220