amapi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*-------------------------------------------------------------------------
  2. *
  3. * amapi.h
  4. * API for Postgres index access methods.
  5. *
  6. * Copyright (c) 2015-2022, PostgreSQL Global Development Group
  7. *
  8. * src/include/access/amapi.h
  9. *
  10. *-------------------------------------------------------------------------
  11. */
  12. #ifndef AMAPI_H
  13. #define AMAPI_H
  14. #include "access/genam.h"
  15. /*
  16. * We don't wish to include planner header files here, since most of an index
  17. * AM's implementation isn't concerned with those data structures. To allow
  18. * declaring amcostestimate_function here, use forward struct references.
  19. */
  20. struct PlannerInfo;
  21. struct IndexPath;
  22. /* Likewise, this file shouldn't depend on execnodes.h. */
  23. struct IndexInfo;
  24. /*
  25. * Properties for amproperty API. This list covers properties known to the
  26. * core code, but an index AM can define its own properties, by matching the
  27. * string property name.
  28. */
  29. typedef enum IndexAMProperty
  30. {
  31. AMPROP_UNKNOWN = 0, /* anything not known to core code */
  32. AMPROP_ASC, /* column properties */
  33. AMPROP_DESC,
  34. AMPROP_NULLS_FIRST,
  35. AMPROP_NULLS_LAST,
  36. AMPROP_ORDERABLE,
  37. AMPROP_DISTANCE_ORDERABLE,
  38. AMPROP_RETURNABLE,
  39. AMPROP_SEARCH_ARRAY,
  40. AMPROP_SEARCH_NULLS,
  41. AMPROP_CLUSTERABLE, /* index properties */
  42. AMPROP_INDEX_SCAN,
  43. AMPROP_BITMAP_SCAN,
  44. AMPROP_BACKWARD_SCAN,
  45. AMPROP_CAN_ORDER, /* AM properties */
  46. AMPROP_CAN_UNIQUE,
  47. AMPROP_CAN_MULTI_COL,
  48. AMPROP_CAN_EXCLUDE,
  49. AMPROP_CAN_INCLUDE
  50. } IndexAMProperty;
  51. /*
  52. * We use lists of this struct type to keep track of both operators and
  53. * support functions while building or adding to an opclass or opfamily.
  54. * amadjustmembers functions receive lists of these structs, and are allowed
  55. * to alter their "ref" fields.
  56. *
  57. * The "ref" fields define how the pg_amop or pg_amproc entry should depend
  58. * on the associated objects (that is, which dependency type to use, and
  59. * which opclass or opfamily it should depend on).
  60. *
  61. * If ref_is_hard is true, the entry will have a NORMAL dependency on the
  62. * operator or support func, and an INTERNAL dependency on the opclass or
  63. * opfamily. This forces the opclass or opfamily to be dropped if the
  64. * operator or support func is dropped, and requires the CASCADE option
  65. * to do so. Nor will ALTER OPERATOR FAMILY DROP be allowed. This is
  66. * the right behavior for objects that are essential to an opclass.
  67. *
  68. * If ref_is_hard is false, the entry will have an AUTO dependency on the
  69. * operator or support func, and also an AUTO dependency on the opclass or
  70. * opfamily. This allows ALTER OPERATOR FAMILY DROP, and causes that to
  71. * happen automatically if the operator or support func is dropped. This
  72. * is the right behavior for inessential ("loose") objects.
  73. */
  74. typedef struct OpFamilyMember
  75. {
  76. bool is_func; /* is this an operator, or support func? */
  77. Oid object; /* operator or support func's OID */
  78. int number; /* strategy or support func number */
  79. Oid lefttype; /* lefttype */
  80. Oid righttype; /* righttype */
  81. Oid sortfamily; /* ordering operator's sort opfamily, or 0 */
  82. bool ref_is_hard; /* hard or soft dependency? */
  83. bool ref_is_family; /* is dependency on opclass or opfamily? */
  84. Oid refobjid; /* OID of opclass or opfamily */
  85. } OpFamilyMember;
  86. /*
  87. * Callback function signatures --- see indexam.sgml for more info.
  88. */
  89. /* build new index */
  90. typedef IndexBuildResult *(*ambuild_function) (Relation heapRelation,
  91. Relation indexRelation,
  92. struct IndexInfo *indexInfo);
  93. /* build empty index */
  94. typedef void (*ambuildempty_function) (Relation indexRelation);
  95. /* insert this tuple */
  96. typedef bool (*aminsert_function) (Relation indexRelation,
  97. Datum *values,
  98. bool *isnull,
  99. ItemPointer heap_tid,
  100. Relation heapRelation,
  101. IndexUniqueCheck checkUnique,
  102. bool indexUnchanged,
  103. struct IndexInfo *indexInfo);
  104. /* bulk delete */
  105. typedef IndexBulkDeleteResult *(*ambulkdelete_function) (IndexVacuumInfo *info,
  106. IndexBulkDeleteResult *stats,
  107. IndexBulkDeleteCallback callback,
  108. void *callback_state);
  109. /* post-VACUUM cleanup */
  110. typedef IndexBulkDeleteResult *(*amvacuumcleanup_function) (IndexVacuumInfo *info,
  111. IndexBulkDeleteResult *stats);
  112. /* can indexscan return IndexTuples? */
  113. typedef bool (*amcanreturn_function) (Relation indexRelation, int attno);
  114. /* estimate cost of an indexscan */
  115. typedef void (*amcostestimate_function) (struct PlannerInfo *root,
  116. struct IndexPath *path,
  117. double loop_count,
  118. Cost *indexStartupCost,
  119. Cost *indexTotalCost,
  120. Selectivity *indexSelectivity,
  121. double *indexCorrelation,
  122. double *indexPages);
  123. /* parse index reloptions */
  124. typedef bytea *(*amoptions_function) (Datum reloptions,
  125. bool validate);
  126. /* report AM, index, or index column property */
  127. typedef bool (*amproperty_function) (Oid index_oid, int attno,
  128. IndexAMProperty prop, const char *propname,
  129. bool *res, bool *isnull);
  130. /* name of phase as used in progress reporting */
  131. typedef char *(*ambuildphasename_function) (int64 phasenum);
  132. /* validate definition of an opclass for this AM */
  133. typedef bool (*amvalidate_function) (Oid opclassoid);
  134. /* validate operators and support functions to be added to an opclass/family */
  135. typedef void (*amadjustmembers_function) (Oid opfamilyoid,
  136. Oid opclassoid,
  137. List *operators,
  138. List *functions);
  139. /* prepare for index scan */
  140. typedef IndexScanDesc (*ambeginscan_function) (Relation indexRelation,
  141. int nkeys,
  142. int norderbys);
  143. /* (re)start index scan */
  144. typedef void (*amrescan_function) (IndexScanDesc scan,
  145. ScanKey keys,
  146. int nkeys,
  147. ScanKey orderbys,
  148. int norderbys);
  149. /* next valid tuple */
  150. typedef bool (*amgettuple_function) (IndexScanDesc scan,
  151. ScanDirection direction);
  152. /* fetch all valid tuples */
  153. typedef int64 (*amgetbitmap_function) (IndexScanDesc scan,
  154. TIDBitmap *tbm);
  155. /* end index scan */
  156. typedef void (*amendscan_function) (IndexScanDesc scan);
  157. /* mark current scan position */
  158. typedef void (*ammarkpos_function) (IndexScanDesc scan);
  159. /* restore marked scan position */
  160. typedef void (*amrestrpos_function) (IndexScanDesc scan);
  161. /*
  162. * Callback function signatures - for parallel index scans.
  163. */
  164. /* estimate size of parallel scan descriptor */
  165. typedef Size (*amestimateparallelscan_function) (void);
  166. /* prepare for parallel index scan */
  167. typedef void (*aminitparallelscan_function) (void *target);
  168. /* (re)start parallel index scan */
  169. typedef void (*amparallelrescan_function) (IndexScanDesc scan);
  170. /*
  171. * API struct for an index AM. Note this must be stored in a single palloc'd
  172. * chunk of memory.
  173. */
  174. typedef struct IndexAmRoutine
  175. {
  176. NodeTag type;
  177. /*
  178. * Total number of strategies (operators) by which we can traverse/search
  179. * this AM. Zero if AM does not have a fixed set of strategy assignments.
  180. */
  181. uint16 amstrategies;
  182. /* total number of support functions that this AM uses */
  183. uint16 amsupport;
  184. /* opclass options support function number or 0 */
  185. uint16 amoptsprocnum;
  186. /* does AM support ORDER BY indexed column's value? */
  187. bool amcanorder;
  188. /* does AM support ORDER BY result of an operator on indexed column? */
  189. bool amcanorderbyop;
  190. /* does AM support backward scanning? */
  191. bool amcanbackward;
  192. /* does AM support UNIQUE indexes? */
  193. bool amcanunique;
  194. /* does AM support multi-column indexes? */
  195. bool amcanmulticol;
  196. /* does AM require scans to have a constraint on the first index column? */
  197. bool amoptionalkey;
  198. /* does AM handle ScalarArrayOpExpr quals? */
  199. bool amsearcharray;
  200. /* does AM handle IS NULL/IS NOT NULL quals? */
  201. bool amsearchnulls;
  202. /* can index storage data type differ from column data type? */
  203. bool amstorage;
  204. /* can an index of this type be clustered on? */
  205. bool amclusterable;
  206. /* does AM handle predicate locks? */
  207. bool ampredlocks;
  208. /* does AM support parallel scan? */
  209. bool amcanparallel;
  210. /* does AM support columns included with clause INCLUDE? */
  211. bool amcaninclude;
  212. /* does AM use maintenance_work_mem? */
  213. bool amusemaintenanceworkmem;
  214. /* OR of parallel vacuum flags. See vacuum.h for flags. */
  215. uint8 amparallelvacuumoptions;
  216. /* type of data stored in index, or InvalidOid if variable */
  217. Oid amkeytype;
  218. /*
  219. * If you add new properties to either the above or the below lists, then
  220. * they should also (usually) be exposed via the property API (see
  221. * IndexAMProperty at the top of the file, and utils/adt/amutils.c).
  222. */
  223. /* interface functions */
  224. ambuild_function ambuild;
  225. ambuildempty_function ambuildempty;
  226. aminsert_function aminsert;
  227. ambulkdelete_function ambulkdelete;
  228. amvacuumcleanup_function amvacuumcleanup;
  229. amcanreturn_function amcanreturn; /* can be NULL */
  230. amcostestimate_function amcostestimate;
  231. amoptions_function amoptions;
  232. amproperty_function amproperty; /* can be NULL */
  233. ambuildphasename_function ambuildphasename; /* can be NULL */
  234. amvalidate_function amvalidate;
  235. amadjustmembers_function amadjustmembers; /* can be NULL */
  236. ambeginscan_function ambeginscan;
  237. amrescan_function amrescan;
  238. amgettuple_function amgettuple; /* can be NULL */
  239. amgetbitmap_function amgetbitmap; /* can be NULL */
  240. amendscan_function amendscan;
  241. ammarkpos_function ammarkpos; /* can be NULL */
  242. amrestrpos_function amrestrpos; /* can be NULL */
  243. /* interface functions to support parallel index scans */
  244. amestimateparallelscan_function amestimateparallelscan; /* can be NULL */
  245. aminitparallelscan_function aminitparallelscan; /* can be NULL */
  246. amparallelrescan_function amparallelrescan; /* can be NULL */
  247. } IndexAmRoutine;
  248. /* Functions in access/index/amapi.c */
  249. extern IndexAmRoutine *GetIndexAmRoutine(Oid amhandler);
  250. extern IndexAmRoutine *GetIndexAmRoutineByAmId(Oid amoid, bool noerror);
  251. #endif /* AMAPI_H */