1 /*-------------------------------------------------------------------------
4 * Support routines for API for Postgres index access methods.
6 * Copyright (c) 2015-2017, PostgreSQL Global Development Group
10 * src/backend/access/index/amapi.c
12 *-------------------------------------------------------------------------
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/builtins.h"
21 #include "utils/syscache.h"
25 * GetIndexAmRoutine - call the specified access method handler routine to get
26 * its IndexAmRoutine struct, which will be palloc'd in the caller's context.
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.
33 GetIndexAmRoutine(Oid amhandler)
36 IndexAmRoutine *routine;
38 datum = OidFunctionCall0(amhandler);
39 routine = (IndexAmRoutine *) DatumGetPointer(datum);
41 if (routine == NULL || !IsA(routine, IndexAmRoutine))
42 elog(ERROR, "index access method handler function %u did not return an IndexAmRoutine struct",
49 * GetIndexAmRoutineByAmId - look up the handler of the index access method
50 * with the given OID, and get its IndexAmRoutine struct.
52 * If the given OID isn't a valid index access method, returns NULL if
53 * noerror is true, else throws error.
56 GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
62 /* Get handler function OID for the access method */
63 tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(amoid));
64 if (!HeapTupleIsValid(tuple))
68 elog(ERROR, "cache lookup failed for access method %u",
71 amform = (Form_pg_am) GETSTRUCT(tuple);
73 /* Check if it's an index access method as opposed to some other AM */
74 if (amform->amtype != AMTYPE_INDEX)
78 ReleaseSysCache(tuple);
82 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
83 errmsg("access method \"%s\" is not of type %s",
84 NameStr(amform->amname), "INDEX")));
87 amhandler = amform->amhandler;
89 /* Complain if handler OID is invalid */
90 if (!RegProcedureIsValid(amhandler))
94 ReleaseSysCache(tuple);
98 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
99 errmsg("index access method \"%s\" does not have a handler",
100 NameStr(amform->amname))));
103 ReleaseSysCache(tuple);
105 /* And finally, call the handler function to get the API struct. */
106 return GetIndexAmRoutine(amhandler);
111 * Ask appropriate access method to validate the specified opclass.
114 amvalidate(PG_FUNCTION_ARGS)
116 Oid opclassoid = PG_GETARG_OID(0);
119 Form_pg_opclass classform;
121 IndexAmRoutine *amroutine;
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);
128 amoid = classform->opcmethod;
130 ReleaseSysCache(classtup);
132 amroutine = GetIndexAmRoutineByAmId(amoid, false);
134 if (amroutine->amvalidate == NULL)
135 elog(ERROR, "function amvalidate is not defined for index access method %u",
138 result = amroutine->amvalidate(opclassoid);
142 PG_RETURN_BOOL(result);