1 /*-------------------------------------------------------------------------
2  *
3  * amapi.h
4  *	  API for Postgres index access methods.
5  *
6  * Copyright (c) 2015-2016, PostgreSQL Global Development Group
7  *
8  * src/include/access/amapi.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef AMAPI_H
13 #define AMAPI_H
14 
15 #include "access/genam.h"
16 
17 /*
18  * We don't wish to include planner header files here, since most of an index
19  * AM's implementation isn't concerned with those data structures.  To allow
20  * declaring amcostestimate_function here, use forward struct references.
21  */
22 struct PlannerInfo;
23 struct IndexPath;
24 
25 /* Likewise, this file shouldn't depend on execnodes.h. */
26 struct IndexInfo;
27 
28 
29 /*
30  * Properties for amproperty API.  This list covers properties known to the
31  * core code, but an index AM can define its own properties, by matching the
32  * string property name.
33  */
34 typedef enum IndexAMProperty
35 {
36 	AMPROP_UNKNOWN = 0,			/* anything not known to core code */
37 	AMPROP_ASC,					/* column properties */
38 	AMPROP_DESC,
39 	AMPROP_NULLS_FIRST,
40 	AMPROP_NULLS_LAST,
41 	AMPROP_ORDERABLE,
42 	AMPROP_DISTANCE_ORDERABLE,
43 	AMPROP_RETURNABLE,
44 	AMPROP_SEARCH_ARRAY,
45 	AMPROP_SEARCH_NULLS,
46 	AMPROP_CLUSTERABLE,			/* index properties */
47 	AMPROP_INDEX_SCAN,
48 	AMPROP_BITMAP_SCAN,
49 	AMPROP_BACKWARD_SCAN,
50 	AMPROP_CAN_ORDER,			/* AM properties */
51 	AMPROP_CAN_UNIQUE,
52 	AMPROP_CAN_MULTI_COL,
53 	AMPROP_CAN_EXCLUDE
54 } IndexAMProperty;
55 
56 
57 /*
58  * Callback function signatures --- see indexam.sgml for more info.
59  */
60 
61 /* build new index */
62 typedef IndexBuildResult *(*ambuild_function) (Relation heapRelation,
63 													  Relation indexRelation,
64 												struct IndexInfo *indexInfo);
65 
66 /* build empty index */
67 typedef void (*ambuildempty_function) (Relation indexRelation);
68 
69 /* insert this tuple */
70 typedef bool (*aminsert_function) (Relation indexRelation,
71 											   Datum *values,
72 											   bool *isnull,
73 											   ItemPointer heap_tid,
74 											   Relation heapRelation,
75 											   IndexUniqueCheck checkUnique);
76 
77 /* bulk delete */
78 typedef IndexBulkDeleteResult *(*ambulkdelete_function) (IndexVacuumInfo *info,
79 												IndexBulkDeleteResult *stats,
80 											IndexBulkDeleteCallback callback,
81 													   void *callback_state);
82 
83 /* post-VACUUM cleanup */
84 typedef IndexBulkDeleteResult *(*amvacuumcleanup_function) (IndexVacuumInfo *info,
85 											   IndexBulkDeleteResult *stats);
86 
87 /* can indexscan return IndexTuples? */
88 typedef bool (*amcanreturn_function) (Relation indexRelation, int attno);
89 
90 /* estimate cost of an indexscan */
91 typedef void (*amcostestimate_function) (struct PlannerInfo *root,
92 													 struct IndexPath *path,
93 													 double loop_count,
94 													 Cost *indexStartupCost,
95 													 Cost *indexTotalCost,
96 											   Selectivity *indexSelectivity,
97 												   double *indexCorrelation);
98 
99 /* parse index reloptions */
100 typedef bytea *(*amoptions_function) (Datum reloptions,
101 												  bool validate);
102 
103 /* report AM, index, or index column property */
104 typedef bool (*amproperty_function) (Oid index_oid, int attno,
105 								  IndexAMProperty prop, const char *propname,
106 												 bool *res, bool *isnull);
107 
108 /* validate definition of an opclass for this AM */
109 typedef bool (*amvalidate_function) (Oid opclassoid);
110 
111 /* prepare for index scan */
112 typedef IndexScanDesc (*ambeginscan_function) (Relation indexRelation,
113 														   int nkeys,
114 														   int norderbys);
115 
116 /* (re)start index scan */
117 typedef void (*amrescan_function) (IndexScanDesc scan,
118 											   ScanKey keys,
119 											   int nkeys,
120 											   ScanKey orderbys,
121 											   int norderbys);
122 
123 /* next valid tuple */
124 typedef bool (*amgettuple_function) (IndexScanDesc scan,
125 												 ScanDirection direction);
126 
127 /* fetch all valid tuples */
128 typedef int64 (*amgetbitmap_function) (IndexScanDesc scan,
129 												   TIDBitmap *tbm);
130 
131 /* end index scan */
132 typedef void (*amendscan_function) (IndexScanDesc scan);
133 
134 /* mark current scan position */
135 typedef void (*ammarkpos_function) (IndexScanDesc scan);
136 
137 /* restore marked scan position */
138 typedef void (*amrestrpos_function) (IndexScanDesc scan);
139 
140 
141 /*
142  * API struct for an index AM.  Note this must be stored in a single palloc'd
143  * chunk of memory.
144  */
145 typedef struct IndexAmRoutine
146 {
147 	NodeTag		type;
148 
149 	/*
150 	 * Total number of strategies (operators) by which we can traverse/search
151 	 * this AM.  Zero if AM does not have a fixed set of strategy assignments.
152 	 */
153 	uint16		amstrategies;
154 	/* total number of support functions that this AM uses */
155 	uint16		amsupport;
156 	/* does AM support ORDER BY indexed column's value? */
157 	bool		amcanorder;
158 	/* does AM support ORDER BY result of an operator on indexed column? */
159 	bool		amcanorderbyop;
160 	/* does AM support backward scanning? */
161 	bool		amcanbackward;
162 	/* does AM support UNIQUE indexes? */
163 	bool		amcanunique;
164 	/* does AM support multi-column indexes? */
165 	bool		amcanmulticol;
166 	/* does AM require scans to have a constraint on the first index column? */
167 	bool		amoptionalkey;
168 	/* does AM handle ScalarArrayOpExpr quals? */
169 	bool		amsearcharray;
170 	/* does AM handle IS NULL/IS NOT NULL quals? */
171 	bool		amsearchnulls;
172 	/* can index storage data type differ from column data type? */
173 	bool		amstorage;
174 	/* can an index of this type be clustered on? */
175 	bool		amclusterable;
176 	/* does AM handle predicate locks? */
177 	bool		ampredlocks;
178 	/* type of data stored in index, or InvalidOid if variable */
179 	Oid			amkeytype;
180 
181 	/* interface functions */
182 	ambuild_function ambuild;
183 	ambuildempty_function ambuildempty;
184 	aminsert_function aminsert;
185 	ambulkdelete_function ambulkdelete;
186 	amvacuumcleanup_function amvacuumcleanup;
187 	amcanreturn_function amcanreturn;	/* can be NULL */
188 	amcostestimate_function amcostestimate;
189 	amoptions_function amoptions;
190 	amproperty_function amproperty;		/* can be NULL */
191 	amvalidate_function amvalidate;
192 	ambeginscan_function ambeginscan;
193 	amrescan_function amrescan;
194 	amgettuple_function amgettuple;		/* can be NULL */
195 	amgetbitmap_function amgetbitmap;	/* can be NULL */
196 	amendscan_function amendscan;
197 	ammarkpos_function ammarkpos;		/* can be NULL */
198 	amrestrpos_function amrestrpos;		/* can be NULL */
199 } IndexAmRoutine;
200 
201 
202 /* Functions in access/index/amapi.c */
203 extern IndexAmRoutine *GetIndexAmRoutine(Oid amhandler);
204 extern IndexAmRoutine *GetIndexAmRoutineByAmId(Oid amoid, bool noerror);
205 
206 extern Datum amvalidate(PG_FUNCTION_ARGS);
207 
208 #endif   /* AMAPI_H */
209