PostgreSQL Source Code  git master
dummy_index_am.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dummy_index_am.c
4  * Index AM template main file.
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/test/modules/dummy_index_am/dummy_index_am.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/amapi.h"
17 #include "access/reloptions.h"
18 #include "catalog/index.h"
19 #include "nodes/pathnodes.h"
20 #include "utils/guc.h"
21 #include "utils/rel.h"
22 
24 
25 void _PG_init(void);
26 
27 /* parse table for fillRelOptions */
29 
30 /* Kind of relation options for dummy index */
32 
33 typedef enum DummyAmEnum {
36 } DummyAmEnum;
37 
38 /* Dummy index options */
39 typedef struct DummyIndexOptions
40 {
41  int32 vl_len_; /* varlena header (do not touch directly!) */
43  double option_real;
49 
51 {
52  {"one", DUMMY_AM_ENUM_ONE},
53  {"two", DUMMY_AM_ENUM_TWO},
54  {(const char *)NULL} /* list terminator */
55 };
56 
57 /* Handler for index AM */
59 
60 /*
61  * Validation function for string relation options.
62  */
63 static void
65 {
67  (errmsg("new option value for string parameter %s",
68  value ? value : "NULL")));
69 }
70 
71 /*
72  * This function creates a full set of relation option types,
73  * with various patterns.
74  */
75 static void
77 {
79 
80  add_int_reloption(di_relopt_kind, "option_int",
81  "Integer option for dummy_index_am",
82  10, -10, 100, AccessExclusiveLock);
83  di_relopt_tab[0].optname = "option_int";
84  di_relopt_tab[0].opttype = RELOPT_TYPE_INT;
85  di_relopt_tab[0].offset = offsetof(DummyIndexOptions, option_int);
86 
87  add_real_reloption(di_relopt_kind, "option_real",
88  "Real option for dummy_index_am",
89  3.1415, -10, 100, AccessExclusiveLock);
90  di_relopt_tab[1].optname = "option_real";
91  di_relopt_tab[1].opttype = RELOPT_TYPE_REAL;
92  di_relopt_tab[1].offset = offsetof(DummyIndexOptions, option_real);
93 
94  add_bool_reloption(di_relopt_kind, "option_bool",
95  "Boolean option for dummy_index_am",
96  true, AccessExclusiveLock);
97  di_relopt_tab[2].optname = "option_bool";
98  di_relopt_tab[2].opttype = RELOPT_TYPE_BOOL;
99  di_relopt_tab[2].offset = offsetof(DummyIndexOptions, option_bool);
100 
101  add_enum_reloption(di_relopt_kind, "option_enum",
102  "Enum option for dummy_index_am",
103  dummyAmEnumValues,
105  "Valid values are \"one\" and \"two\".",
107  di_relopt_tab[3].optname = "option_enum";
108  di_relopt_tab[3].opttype = RELOPT_TYPE_ENUM;
109  di_relopt_tab[3].offset = offsetof(DummyIndexOptions, option_enum);
110 
111  add_string_reloption(di_relopt_kind, "option_string_val",
112  "String option for dummy_index_am with non-NULL default",
113  "DefaultValue", &validate_string_option,
115  di_relopt_tab[4].optname = "option_string_val";
116  di_relopt_tab[4].opttype = RELOPT_TYPE_STRING;
117  di_relopt_tab[4].offset = offsetof(DummyIndexOptions,
119 
120  /*
121  * String option for dummy_index_am with NULL default, and without
122  * description.
123  */
124  add_string_reloption(di_relopt_kind, "option_string_null",
125  NULL, /* description */
126  NULL, &validate_string_option,
128  di_relopt_tab[5].optname = "option_string_null";
129  di_relopt_tab[5].opttype = RELOPT_TYPE_STRING;
130  di_relopt_tab[5].offset = offsetof(DummyIndexOptions,
132 }
133 
134 
135 /*
136  * Build a new index.
137  */
138 static IndexBuildResult *
140 {
141  IndexBuildResult *result;
142 
143  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
144 
145  /* let's pretend that no tuples were scanned */
146  result->heap_tuples = 0;
147  /* and no index tuples were created (that is true) */
148  result->index_tuples = 0;
149 
150  return result;
151 }
152 
153 /*
154  * Build an empty index for the initialiation fork.
155  */
156 static void
158 {
159  /* No need to build an init fork for a dummy index */
160 }
161 
162 /*
163  * Insert new tuple to index AM.
164  */
165 static bool
167  ItemPointer ht_ctid, Relation heapRel,
168  IndexUniqueCheck checkUnique,
169  IndexInfo *indexInfo)
170 {
171  /* nothing to do */
172  return false;
173 }
174 
175 /*
176  * Bulk deletion of all index entries pointing to a set of table tuples.
177  */
178 static IndexBulkDeleteResult *
180  IndexBulkDeleteCallback callback, void *callback_state)
181 {
182  /*
183  * There is nothing to delete. Return NULL as there is nothing to pass to
184  * amvacuumcleanup.
185  */
186  return NULL;
187 }
188 
189 /*
190  * Post-VACUUM cleanup for index AM.
191  */
192 static IndexBulkDeleteResult *
194 {
195  /* Index has not been modified, so returning NULL is fine */
196  return NULL;
197 }
198 
199 /*
200  * Estimate cost of index AM.
201  */
202 static void
203 dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
204  Cost *indexStartupCost, Cost *indexTotalCost,
205  Selectivity *indexSelectivity, double *indexCorrelation,
206  double *indexPages)
207 {
208  /* Tell planner to never use this index! */
209  *indexStartupCost = 1.0e10;
210  *indexTotalCost = 1.0e10;
211 
212  /* Do not care about the rest */
213  *indexSelectivity = 1;
214  *indexCorrelation = 0;
215  *indexPages = 1;
216 }
217 
218 /*
219  * Parse relation options for index AM, returning a DummyIndexOptions
220  * structure filled with option values.
221  */
222 static bytea *
223 dioptions(Datum reloptions, bool validate)
224 {
225  return (bytea *) build_reloptions(reloptions, validate,
227  sizeof(DummyIndexOptions),
228  di_relopt_tab, lengthof(di_relopt_tab));
229 }
230 
231 /*
232  * Validator for index AM.
233  */
234 static bool
235 divalidate(Oid opclassoid)
236 {
237  /* Index is dummy so we are happy with any opclass */
238  return true;
239 }
240 
241 /*
242  * Begin scan of index AM.
243  */
244 static IndexScanDesc
245 dibeginscan(Relation r, int nkeys, int norderbys)
246 {
247  IndexScanDesc scan;
248 
249  /* Let's pretend we are doing something */
250  scan = RelationGetIndexScan(r, nkeys, norderbys);
251  return scan;
252 }
253 
254 /*
255  * Rescan of index AM.
256  */
257 static void
258 direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
259  ScanKey orderbys, int norderbys)
260 {
261  /* nothing to do */
262 }
263 
264 /*
265  * End scan of index AM.
266  */
267 static void
269 {
270  /* nothing to do */
271 }
272 
273 /*
274  * Index AM handler function: returns IndexAmRoutine with access method
275  * parameters and callbacks.
276  */
277 Datum
279 {
280  IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
281 
282  amroutine->amstrategies = 0;
283  amroutine->amsupport = 1;
284  amroutine->amcanorder = false;
285  amroutine->amcanorderbyop = false;
286  amroutine->amcanbackward = false;
287  amroutine->amcanunique = false;
288  amroutine->amcanmulticol = false;
289  amroutine->amoptionalkey = false;
290  amroutine->amsearcharray = false;
291  amroutine->amsearchnulls = false;
292  amroutine->amstorage = false;
293  amroutine->amclusterable = false;
294  amroutine->ampredlocks = false;
295  amroutine->amcanparallel = false;
296  amroutine->amcaninclude = false;
297  amroutine->amkeytype = InvalidOid;
298 
299  amroutine->ambuild = dibuild;
300  amroutine->ambuildempty = dibuildempty;
301  amroutine->aminsert = diinsert;
302  amroutine->ambulkdelete = dibulkdelete;
303  amroutine->amvacuumcleanup = divacuumcleanup;
304  amroutine->amcanreturn = NULL;
305  amroutine->amcostestimate = dicostestimate;
306  amroutine->amoptions = dioptions;
307  amroutine->amproperty = NULL;
308  amroutine->ambuildphasename = NULL;
309  amroutine->amvalidate = divalidate;
310  amroutine->ambeginscan = dibeginscan;
311  amroutine->amrescan = direscan;
312  amroutine->amgettuple = NULL;
313  amroutine->amgetbitmap = NULL;
314  amroutine->amendscan = diendscan;
315  amroutine->ammarkpos = NULL;
316  amroutine->amrestrpos = NULL;
317  amroutine->amestimateparallelscan = NULL;
318  amroutine->aminitparallelscan = NULL;
319  amroutine->amparallelrescan = NULL;
320 
321  PG_RETURN_POINTER(amroutine);
322 }
323 
324 void
325 _PG_init(void)
326 {
328 }
ambeginscan_function ambeginscan
Definition: amapi.h:221
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
ambulkdelete_function ambulkdelete
Definition: amapi.h:213
relopt_type opttype
Definition: reloptions.h:147
bool amcanmulticol
Definition: amapi.h:183
uint16 amsupport
Definition: amapi.h:173
DummyAmEnum option_enum
amgettuple_function amgettuple
Definition: amapi.h:223
bool amcanorderbyop
Definition: amapi.h:177
amproperty_function amproperty
Definition: amapi.h:218
DummyAmEnum
static IndexBuildResult * dibuild(Relation heap, Relation index, IndexInfo *indexInfo)
static IndexScanDesc dibeginscan(Relation r, int nkeys, int norderbys)
amparallelrescan_function amparallelrescan
Definition: amapi.h:232
bool amstorage
Definition: amapi.h:191
static IndexBulkDeleteResult * dibulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
static struct @145 value
bool ampredlocks
Definition: amapi.h:195
aminsert_function aminsert
Definition: amapi.h:212
double Selectivity
Definition: nodes.h:658
#define lengthof(array)
Definition: c.h:663
unsigned int Oid
Definition: postgres_ext.h:31
void add_int_reloption(bits32 kinds, const char *name, const char *desc, int default_val, int min_val, int max_val, LOCKMODE lockmode)
Definition: reloptions.c:739
Oid amkeytype
Definition: amapi.h:201
static IndexBulkDeleteResult * divacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
bool amoptionalkey
Definition: amapi.h:185
amvalidate_function amvalidate
Definition: amapi.h:220
signed int int32
Definition: c.h:347
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1554
static void diendscan(IndexScanDesc scan)
Definition: type.h:89
IndexUniqueCheck
Definition: genam.h:112
amgetbitmap_function amgetbitmap
Definition: amapi.h:224
static bool divalidate(Oid opclassoid)
ambuild_function ambuild
Definition: amapi.h:210
amoptions_function amoptions
Definition: amapi.h:217
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
bool amcaninclude
Definition: amapi.h:199
struct DummyIndexOptions DummyIndexOptions
void _PG_init(void)
amcostestimate_function amcostestimate
Definition: amapi.h:216
bool amcanunique
Definition: amapi.h:181
relopt_enum_elt_def dummyAmEnumValues[]
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:214
amendscan_function amendscan
Definition: amapi.h:225
bool amcanbackward
Definition: amapi.h:179
PG_FUNCTION_INFO_V1(dihandler)
static void direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
#define ereport(elevel, rest)
Definition: elog.h:141
amrescan_function amrescan
Definition: amapi.h:222
bool amcanparallel
Definition: amapi.h:197
static void create_reloptions_table(void)
bool amsearchnulls
Definition: amapi.h:189
uintptr_t Datum
Definition: postgres.h:367
static bool diinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
static void dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
bool amclusterable
Definition: amapi.h:193
const char * optname
Definition: reloptions.h:146
bool amsearcharray
Definition: amapi.h:187
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
#define makeNode(_type_)
Definition: nodes.h:573
static void validate_string_option(const char *value)
void add_real_reloption(bits32 kinds, const char *name, const char *desc, double default_val, double min_val, double max_val, LOCKMODE lockmode)
Definition: reloptions.c:758
ammarkpos_function ammarkpos
Definition: amapi.h:226
bool amcanorder
Definition: amapi.h:175
ambuildphasename_function ambuildphasename
Definition: amapi.h:219
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:230
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:618
relopt_kind di_relopt_kind
uint16 amstrategies
Definition: amapi.h:171
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define AccessExclusiveLock
Definition: lockdefs.h:45
PG_MODULE_MAGIC
relopt_parse_elt di_relopt_tab[6]
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
void add_string_reloption(bits32 kinds, const char *name, const char *desc, const char *default_val, validate_string_relopt validator, LOCKMODE lockmode)
Definition: reloptions.c:810
relopt_kind
Definition: reloptions.h:39
ambuildempty_function ambuildempty
Definition: amapi.h:211
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:80
Definition: c.h:550
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
static bytea * dioptions(Datum reloptions, bool validate)
Datum dihandler(PG_FUNCTION_ARGS)
void add_enum_reloption(bits32 kinds, const char *name, const char *desc, relopt_enum_elt_def *members, int default_val, const char *detailmsg, LOCKMODE lockmode)
Definition: reloptions.c:785
void add_bool_reloption(bits32 kinds, const char *name, const char *desc, bool default_val, LOCKMODE lockmode)
Definition: reloptions.c:722
amcanreturn_function amcanreturn
Definition: amapi.h:215
double Cost
Definition: nodes.h:659
#define offsetof(type, field)
Definition: c.h:656
double index_tuples
Definition: genam.h:33
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition: genam.h:84
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:231
double heap_tuples
Definition: genam.h:32
static void dibuildempty(Relation index)
amrestrpos_function amrestrpos
Definition: amapi.h:227