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