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-2021, 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 initialization 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  bool indexUnchanged,
172  IndexInfo *indexInfo)
173 {
174  /* nothing to do */
175  return false;
176 }
177 
178 /*
179  * Bulk deletion of all index entries pointing to a set of table tuples.
180  */
181 static IndexBulkDeleteResult *
183  IndexBulkDeleteCallback callback, void *callback_state)
184 {
185  /*
186  * There is nothing to delete. Return NULL as there is nothing to pass to
187  * amvacuumcleanup.
188  */
189  return NULL;
190 }
191 
192 /*
193  * Post-VACUUM cleanup for index AM.
194  */
195 static IndexBulkDeleteResult *
197 {
198  /* Index has not been modified, so returning NULL is fine */
199  return NULL;
200 }
201 
202 /*
203  * Estimate cost of index AM.
204  */
205 static void
206 dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
207  Cost *indexStartupCost, Cost *indexTotalCost,
208  Selectivity *indexSelectivity, double *indexCorrelation,
209  double *indexPages)
210 {
211  /* Tell planner to never use this index! */
212  *indexStartupCost = 1.0e10;
213  *indexTotalCost = 1.0e10;
214 
215  /* Do not care about the rest */
216  *indexSelectivity = 1;
217  *indexCorrelation = 0;
218  *indexPages = 1;
219 }
220 
221 /*
222  * Parse relation options for index AM, returning a DummyIndexOptions
223  * structure filled with option values.
224  */
225 static bytea *
226 dioptions(Datum reloptions, bool validate)
227 {
228  return (bytea *) build_reloptions(reloptions, validate,
230  sizeof(DummyIndexOptions),
231  di_relopt_tab, lengthof(di_relopt_tab));
232 }
233 
234 /*
235  * Validator for index AM.
236  */
237 static bool
238 divalidate(Oid opclassoid)
239 {
240  /* Index is dummy so we are happy with any opclass */
241  return true;
242 }
243 
244 /*
245  * Begin scan of index AM.
246  */
247 static IndexScanDesc
248 dibeginscan(Relation r, int nkeys, int norderbys)
249 {
250  IndexScanDesc scan;
251 
252  /* Let's pretend we are doing something */
253  scan = RelationGetIndexScan(r, nkeys, norderbys);
254  return scan;
255 }
256 
257 /*
258  * Rescan of index AM.
259  */
260 static void
261 direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
262  ScanKey orderbys, int norderbys)
263 {
264  /* nothing to do */
265 }
266 
267 /*
268  * End scan of index AM.
269  */
270 static void
272 {
273  /* nothing to do */
274 }
275 
276 /*
277  * Index AM handler function: returns IndexAmRoutine with access method
278  * parameters and callbacks.
279  */
280 Datum
282 {
283  IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
284 
285  amroutine->amstrategies = 0;
286  amroutine->amsupport = 1;
287  amroutine->amcanorder = false;
288  amroutine->amcanorderbyop = false;
289  amroutine->amcanbackward = false;
290  amroutine->amcanunique = false;
291  amroutine->amcanmulticol = false;
292  amroutine->amoptionalkey = false;
293  amroutine->amsearcharray = false;
294  amroutine->amsearchnulls = false;
295  amroutine->amstorage = false;
296  amroutine->amclusterable = false;
297  amroutine->ampredlocks = false;
298  amroutine->amcanparallel = false;
299  amroutine->amcaninclude = false;
300  amroutine->amusemaintenanceworkmem = false;
302  amroutine->amkeytype = InvalidOid;
303 
304  amroutine->ambuild = dibuild;
305  amroutine->ambuildempty = dibuildempty;
306  amroutine->aminsert = diinsert;
307  amroutine->ambulkdelete = dibulkdelete;
308  amroutine->amvacuumcleanup = divacuumcleanup;
309  amroutine->amcanreturn = NULL;
310  amroutine->amcostestimate = dicostestimate;
311  amroutine->amoptions = dioptions;
312  amroutine->amproperty = NULL;
313  amroutine->ambuildphasename = NULL;
314  amroutine->amvalidate = divalidate;
315  amroutine->ambeginscan = dibeginscan;
316  amroutine->amrescan = direscan;
317  amroutine->amgettuple = NULL;
318  amroutine->amgetbitmap = NULL;
319  amroutine->amendscan = diendscan;
320  amroutine->ammarkpos = NULL;
321  amroutine->amrestrpos = NULL;
322  amroutine->amestimateparallelscan = NULL;
323  amroutine->aminitparallelscan = NULL;
324  amroutine->amparallelrescan = NULL;
325 
326  PG_RETURN_POINTER(amroutine);
327 }
328 
329 void
330 _PG_init(void)
331 {
333 }
ambeginscan_function ambeginscan
Definition: amapi.h:271
uint8 amparallelvacuumoptions
Definition: amapi.h:248
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
ambulkdelete_function ambulkdelete
Definition: amapi.h:262
relopt_type opttype
Definition: reloptions.h:153
bool amcanmulticol
Definition: amapi.h:228
uint16 amsupport
Definition: amapi.h:216
DummyAmEnum option_enum
static struct @142 value
amgettuple_function amgettuple
Definition: amapi.h:273
bool amcanorderbyop
Definition: amapi.h:222
amproperty_function amproperty
Definition: amapi.h:267
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:282
bool amstorage
Definition: amapi.h:236
static IndexBulkDeleteResult * dibulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
bool ampredlocks
Definition: amapi.h:240
aminsert_function aminsert
Definition: amapi.h:261
double Selectivity
Definition: nodes.h:672
#define lengthof(array)
Definition: c.h:734
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:250
static IndexBulkDeleteResult * divacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
bool amoptionalkey
Definition: amapi.h:230
amvalidate_function amvalidate
Definition: amapi.h:269
signed int int32
Definition: c.h:429
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:114
amgetbitmap_function amgetbitmap
Definition: amapi.h:274
static bool divalidate(Oid opclassoid)
ambuild_function ambuild
Definition: amapi.h:259
amoptions_function amoptions
Definition: amapi.h:266
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
bool amcaninclude
Definition: amapi.h:244
void _PG_init(void)
amcostestimate_function amcostestimate
Definition: amapi.h:265
bool amcanunique
Definition: amapi.h:226
relopt_enum_elt_def dummyAmEnumValues[]
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:263
amendscan_function amendscan
Definition: amapi.h:275
bool amcanbackward
Definition: amapi.h:224
PG_FUNCTION_INFO_V1(dihandler)
static void direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
amrescan_function amrescan
Definition: amapi.h:272
bool amcanparallel
Definition: amapi.h:242
#define VACUUM_OPTION_NO_PARALLEL
Definition: vacuum.h:39
static void create_reloptions_table(void)
bool amsearchnulls
Definition: amapi.h:234
uintptr_t Datum
Definition: postgres.h:411
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:238
struct DummyIndexOptions DummyIndexOptions
const char * optname
Definition: reloptions.h:152
bool amsearcharray
Definition: amapi.h:232
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
bool amusemaintenanceworkmem
Definition: amapi.h:246
#define NOTICE
Definition: elog.h:37
#define makeNode(_type_)
Definition: nodes.h:587
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:276
bool amcanorder
Definition: amapi.h:220
ambuildphasename_function ambuildphasename
Definition: amapi.h:268
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:280
static bool diinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:659
relopt_kind di_relopt_kind
uint16 amstrategies
Definition: amapi.h:214
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define AccessExclusiveLock
Definition: lockdefs.h:45
PG_MODULE_MAGIC
relopt_parse_elt di_relopt_tab[6]
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
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:260
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81
Definition: c.h:621
#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:264
double Cost
Definition: nodes.h:673
#define offsetof(type, field)
Definition: c.h:727
double index_tuples
Definition: genam.h:33
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition: genam.h:86
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:281
double heap_tuples
Definition: genam.h:32
static void dibuildempty(Relation index)
amrestrpos_function amrestrpos
Definition: amapi.h:277