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-2024, 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 /* parse table for fillRelOptions */
28 
29 /* Kind of relation options for dummy index */
31 
32 typedef enum DummyAmEnum
33 {
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";
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";
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";
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",
105  "Valid values are \"one\" and \"two\".",
107  di_relopt_tab[3].optname = "option_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";
117  di_relopt_tab[4].offset = offsetof(DummyIndexOptions,
118  option_string_val_offset);
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";
130  di_relopt_tab[5].offset = offsetof(DummyIndexOptions,
131  option_string_null_offset);
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 initialization 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  bool indexUnchanged,
170  IndexInfo *indexInfo)
171 {
172  /* nothing to do */
173  return false;
174 }
175 
176 /*
177  * Bulk deletion of all index entries pointing to a set of table tuples.
178  */
179 static IndexBulkDeleteResult *
181  IndexBulkDeleteCallback callback, void *callback_state)
182 {
183  /*
184  * There is nothing to delete. Return NULL as there is nothing to pass to
185  * amvacuumcleanup.
186  */
187  return NULL;
188 }
189 
190 /*
191  * Post-VACUUM cleanup for index AM.
192  */
193 static IndexBulkDeleteResult *
195 {
196  /* Index has not been modified, so returning NULL is fine */
197  return NULL;
198 }
199 
200 /*
201  * Estimate cost of index AM.
202  */
203 static void
204 dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
205  Cost *indexStartupCost, Cost *indexTotalCost,
206  Selectivity *indexSelectivity, double *indexCorrelation,
207  double *indexPages)
208 {
209  /* Tell planner to never use this index! */
210  *indexStartupCost = 1.0e10;
211  *indexTotalCost = 1.0e10;
212 
213  /* Do not care about the rest */
214  *indexSelectivity = 1;
215  *indexCorrelation = 0;
216  *indexPages = 1;
217 }
218 
219 /*
220  * Parse relation options for index AM, returning a DummyIndexOptions
221  * structure filled with option values.
222  */
223 static bytea *
224 dioptions(Datum reloptions, bool validate)
225 {
226  return (bytea *) build_reloptions(reloptions, validate,
228  sizeof(DummyIndexOptions),
230 }
231 
232 /*
233  * Validator for index AM.
234  */
235 static bool
236 divalidate(Oid opclassoid)
237 {
238  /* Index is dummy so we are happy with any opclass */
239  return true;
240 }
241 
242 /*
243  * Begin scan of index AM.
244  */
245 static IndexScanDesc
246 dibeginscan(Relation r, int nkeys, int norderbys)
247 {
248  IndexScanDesc scan;
249 
250  /* Let's pretend we are doing something */
251  scan = RelationGetIndexScan(r, nkeys, norderbys);
252  return scan;
253 }
254 
255 /*
256  * Rescan of index AM.
257  */
258 static void
259 direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
260  ScanKey orderbys, int norderbys)
261 {
262  /* nothing to do */
263 }
264 
265 /*
266  * End scan of index AM.
267  */
268 static void
270 {
271  /* nothing to do */
272 }
273 
274 /*
275  * Index AM handler function: returns IndexAmRoutine with access method
276  * parameters and callbacks.
277  */
278 Datum
280 {
281  IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
282 
283  amroutine->amstrategies = 0;
284  amroutine->amsupport = 1;
285  amroutine->amcanorder = false;
286  amroutine->amcanorderbyop = false;
287  amroutine->amcanbackward = false;
288  amroutine->amcanunique = false;
289  amroutine->amcanmulticol = false;
290  amroutine->amoptionalkey = false;
291  amroutine->amsearcharray = false;
292  amroutine->amsearchnulls = false;
293  amroutine->amstorage = false;
294  amroutine->amclusterable = false;
295  amroutine->ampredlocks = false;
296  amroutine->amcanparallel = false;
297  amroutine->amcanbuildparallel = false;
298  amroutine->amcaninclude = false;
299  amroutine->amusemaintenanceworkmem = false;
300  amroutine->amsummarizing = 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 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
signed int int32
Definition: c.h:494
#define lengthof(array)
Definition: c.h:788
static bool divalidate(Oid opclassoid)
static void validate_string_option(const char *value)
static bytea * dioptions(Datum reloptions, bool validate)
static void diendscan(IndexScanDesc scan)
static IndexBuildResult * dibuild(Relation heap, Relation index, IndexInfo *indexInfo)
void _PG_init(void)
static void dibuildempty(Relation index)
relopt_enum_elt_def dummyAmEnumValues[]
PG_FUNCTION_INFO_V1(dihandler)
PG_MODULE_MAGIC
static void create_reloptions_table(void)
static IndexScanDesc dibeginscan(Relation r, int nkeys, int norderbys)
static bool diinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
static void direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
static IndexBulkDeleteResult * dibulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
DummyAmEnum
@ DUMMY_AM_ENUM_ONE
@ DUMMY_AM_ENUM_TWO
struct DummyIndexOptions DummyIndexOptions
static IndexBulkDeleteResult * divacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
static void dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
relopt_parse_elt di_relopt_tab[6]
Datum dihandler(PG_FUNCTION_ARGS)
relopt_kind di_relopt_kind
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition: genam.h:87
IndexUniqueCheck
Definition: genam.h:116
static struct @155 value
#define AccessExclusiveLock
Definition: lockdefs.h:43
void * palloc(Size size)
Definition: mcxt.c:1316
double Cost
Definition: nodes.h:251
double Selectivity
Definition: nodes.h:250
#define makeNode(_type_)
Definition: nodes.h:155
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
tree ctl root
Definition: radixtree.h:1884
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:901
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:1908
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:1098
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:1018
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:954
void add_bool_reloption(bits32 kinds, const char *name, const char *desc, bool default_val, LOCKMODE lockmode)
Definition: reloptions.c:849
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:683
relopt_kind
Definition: reloptions.h:40
@ RELOPT_TYPE_ENUM
Definition: reloptions.h:34
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
@ RELOPT_TYPE_REAL
Definition: reloptions.h:33
@ RELOPT_TYPE_STRING
Definition: reloptions.h:35
DummyAmEnum option_enum
ambuildphasename_function ambuildphasename
Definition: amapi.h:277
ambuildempty_function ambuildempty
Definition: amapi.h:268
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:272
bool amclusterable
Definition: amapi.h:242
amoptions_function amoptions
Definition: amapi.h:275
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:289
amrestrpos_function amrestrpos
Definition: amapi.h:286
aminsert_function aminsert
Definition: amapi.h:269
amendscan_function amendscan
Definition: amapi.h:284
amparallelrescan_function amparallelrescan
Definition: amapi.h:291
Oid amkeytype
Definition: amapi.h:258
bool ampredlocks
Definition: amapi.h:244
uint16 amsupport
Definition: amapi.h:220
amcostestimate_function amcostestimate
Definition: amapi.h:274
bool amcanorderbyop
Definition: amapi.h:226
ambuild_function ambuild
Definition: amapi.h:267
bool amstorage
Definition: amapi.h:240
uint16 amstrategies
Definition: amapi.h:218
bool amoptionalkey
Definition: amapi.h:234
amgettuple_function amgettuple
Definition: amapi.h:282
amcanreturn_function amcanreturn
Definition: amapi.h:273
bool amcanunique
Definition: amapi.h:230
amgetbitmap_function amgetbitmap
Definition: amapi.h:283
amproperty_function amproperty
Definition: amapi.h:276
ambulkdelete_function ambulkdelete
Definition: amapi.h:271
bool amsearcharray
Definition: amapi.h:236
bool amsummarizing
Definition: amapi.h:254
amvalidate_function amvalidate
Definition: amapi.h:278
ammarkpos_function ammarkpos
Definition: amapi.h:285
bool amcanmulticol
Definition: amapi.h:232
bool amusemaintenanceworkmem
Definition: amapi.h:252
ambeginscan_function ambeginscan
Definition: amapi.h:280
bool amcanparallel
Definition: amapi.h:246
amrescan_function amrescan
Definition: amapi.h:281
bool amcanorder
Definition: amapi.h:224
bool amcanbuildparallel
Definition: amapi.h:248
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:290
uint8 amparallelvacuumoptions
Definition: amapi.h:256
bool amcanbackward
Definition: amapi.h:228
bool amcaninclude
Definition: amapi.h:250
bool amsearchnulls
Definition: amapi.h:238
double heap_tuples
Definition: genam.h:32
double index_tuples
Definition: genam.h:33
Definition: type.h:95
const char * optname
Definition: reloptions.h:152
relopt_type opttype
Definition: reloptions.h:153
Definition: c.h:687
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46
#define VACUUM_OPTION_NO_PARALLEL
Definition: vacuum.h:42