PostgreSQL Source Code  git master
amutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * amutils.c
4  * SQL-level APIs related to index access methods.
5  *
6  * Copyright (c) 2016-2018, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/utils/adt/amutils.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/amapi.h"
17 #include "access/htup_details.h"
18 #include "catalog/pg_class.h"
19 #include "catalog/pg_index.h"
20 #include "utils/builtins.h"
21 #include "utils/syscache.h"
22 
23 
24 /* Convert string property name to enum, for efficiency */
26 {
27  const char *name;
29 };
30 
31 static const struct am_propname am_propnames[] =
32 {
33  {
34  "asc", AMPROP_ASC
35  },
36  {
37  "desc", AMPROP_DESC
38  },
39  {
40  "nulls_first", AMPROP_NULLS_FIRST
41  },
42  {
43  "nulls_last", AMPROP_NULLS_LAST
44  },
45  {
46  "orderable", AMPROP_ORDERABLE
47  },
48  {
49  "distance_orderable", AMPROP_DISTANCE_ORDERABLE
50  },
51  {
52  "returnable", AMPROP_RETURNABLE
53  },
54  {
55  "search_array", AMPROP_SEARCH_ARRAY
56  },
57  {
58  "search_nulls", AMPROP_SEARCH_NULLS
59  },
60  {
61  "clusterable", AMPROP_CLUSTERABLE
62  },
63  {
64  "index_scan", AMPROP_INDEX_SCAN
65  },
66  {
67  "bitmap_scan", AMPROP_BITMAP_SCAN
68  },
69  {
70  "backward_scan", AMPROP_BACKWARD_SCAN
71  },
72  {
73  "can_order", AMPROP_CAN_ORDER
74  },
75  {
76  "can_unique", AMPROP_CAN_UNIQUE
77  },
78  {
79  "can_multi_col", AMPROP_CAN_MULTI_COL
80  },
81  {
82  "can_exclude", AMPROP_CAN_EXCLUDE
83  },
84  {
85  "can_include", AMPROP_CAN_INCLUDE
86  },
87 };
88 
89 static IndexAMProperty
90 lookup_prop_name(const char *name)
91 {
92  int i;
93 
94  for (i = 0; i < lengthof(am_propnames); i++)
95  {
96  if (pg_strcasecmp(am_propnames[i].name, name) == 0)
97  return am_propnames[i].prop;
98  }
99 
100  /* We do not throw an error, so that AMs can define their own properties */
101  return AMPROP_UNKNOWN;
102 }
103 
104 /*
105  * Common code for properties that are just bit tests of indoptions.
106  *
107  * tuple: the pg_index heaptuple
108  * attno: identify the index column to test the indoptions of.
109  * guard: if false, a boolean false result is forced (saves code in caller).
110  * iopt_mask: mask for interesting indoption bit.
111  * iopt_expect: value for a "true" result (should be 0 or iopt_mask).
112  *
113  * Returns false to indicate a NULL result (for "unknown/inapplicable"),
114  * otherwise sets *res to the boolean value to return.
115  */
116 static bool
117 test_indoption(HeapTuple tuple, int attno, bool guard,
118  int16 iopt_mask, int16 iopt_expect,
119  bool *res)
120 {
121  Datum datum;
122  bool isnull;
123  int2vector *indoption;
124  int16 indoption_val;
125 
126  if (!guard)
127  {
128  *res = false;
129  return true;
130  }
131 
132  datum = SysCacheGetAttr(INDEXRELID, tuple,
133  Anum_pg_index_indoption, &isnull);
134  Assert(!isnull);
135 
136  indoption = ((int2vector *) DatumGetPointer(datum));
137  indoption_val = indoption->values[attno - 1];
138 
139  *res = (indoption_val & iopt_mask) == iopt_expect;
140 
141  return true;
142 }
143 
144 
145 /*
146  * Test property of an index AM, index, or index column.
147  *
148  * This is common code for different SQL-level funcs, so the amoid and
149  * index_oid parameters are mutually exclusive; we look up the amoid from the
150  * index_oid if needed, or if no index oid is given, we're looking at AM-wide
151  * properties.
152  */
153 static Datum
155  const char *propname,
156  Oid amoid, Oid index_oid, int attno)
157 {
158  bool res = false;
159  bool isnull = false;
160  int natts = 0;
162  IndexAmRoutine *routine;
163 
164  /* Try to convert property name to enum (no error if not known) */
165  prop = lookup_prop_name(propname);
166 
167  /* If we have an index OID, look up the AM, and get # of columns too */
168  if (OidIsValid(index_oid))
169  {
170  HeapTuple tuple;
171  Form_pg_class rd_rel;
172 
173  Assert(!OidIsValid(amoid));
174  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(index_oid));
175  if (!HeapTupleIsValid(tuple))
176  PG_RETURN_NULL();
177  rd_rel = (Form_pg_class) GETSTRUCT(tuple);
178  if (rd_rel->relkind != RELKIND_INDEX &&
179  rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
180  {
181  ReleaseSysCache(tuple);
182  PG_RETURN_NULL();
183  }
184  amoid = rd_rel->relam;
185  natts = rd_rel->relnatts;
186  ReleaseSysCache(tuple);
187  }
188 
189  /*
190  * At this point, either index_oid == InvalidOid or it's a valid index OID.
191  * Also, after this test and the one below, either attno == 0 for
192  * index-wide or AM-wide tests, or it's a valid column number in a valid
193  * index.
194  */
195  if (attno < 0 || attno > natts)
196  PG_RETURN_NULL();
197 
198  /*
199  * Get AM information. If we don't have a valid AM OID, return NULL.
200  */
201  routine = GetIndexAmRoutineByAmId(amoid, true);
202  if (routine == NULL)
203  PG_RETURN_NULL();
204 
205  /*
206  * If there's an AM property routine, give it a chance to override the
207  * generic logic. Proceed if it returns false.
208  */
209  if (routine->amproperty &&
210  routine->amproperty(index_oid, attno, prop, propname,
211  &res, &isnull))
212  {
213  if (isnull)
214  PG_RETURN_NULL();
215  PG_RETURN_BOOL(res);
216  }
217 
218  if (attno > 0)
219  {
220  HeapTuple tuple;
221  Form_pg_index rd_index;
222  bool iskey = true;
223 
224  /*
225  * Handle column-level properties. Many of these need the pg_index row
226  * (which we also need to use to check for nonkey atts) so we fetch
227  * that first.
228  */
229  tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
230  if (!HeapTupleIsValid(tuple))
231  PG_RETURN_NULL();
232  rd_index = (Form_pg_index) GETSTRUCT(tuple);
233 
234  Assert(index_oid == rd_index->indexrelid);
235  Assert(attno > 0 && attno <= rd_index->indnatts);
236 
237  isnull = true;
238 
239  /*
240  * If amcaninclude, we might be looking at an attno for a nonkey
241  * column, for which we (generically) assume that most properties are
242  * null.
243  */
244  if (routine->amcaninclude
245  && attno > rd_index->indnkeyatts)
246  iskey = false;
247 
248  switch (prop)
249  {
250  case AMPROP_ASC:
251  if (iskey &&
252  test_indoption(tuple, attno, routine->amcanorder,
253  INDOPTION_DESC, 0, &res))
254  isnull = false;
255  break;
256 
257  case AMPROP_DESC:
258  if (iskey &&
259  test_indoption(tuple, attno, routine->amcanorder,
260  INDOPTION_DESC, INDOPTION_DESC, &res))
261  isnull = false;
262  break;
263 
264  case AMPROP_NULLS_FIRST:
265  if (iskey &&
266  test_indoption(tuple, attno, routine->amcanorder,
267  INDOPTION_NULLS_FIRST, INDOPTION_NULLS_FIRST, &res))
268  isnull = false;
269  break;
270 
271  case AMPROP_NULLS_LAST:
272  if (iskey &&
273  test_indoption(tuple, attno, routine->amcanorder,
274  INDOPTION_NULLS_FIRST, 0, &res))
275  isnull = false;
276  break;
277 
278  case AMPROP_ORDERABLE:
279  /*
280  * generic assumption is that nonkey columns are not orderable
281  */
282  res = iskey ? routine->amcanorder : false;
283  isnull = false;
284  break;
285 
287 
288  /*
289  * The conditions for whether a column is distance-orderable
290  * are really up to the AM (at time of writing, only GiST
291  * supports it at all). The planner has its own idea based on
292  * whether it finds an operator with amoppurpose 'o', but
293  * getting there from just the index column type seems like a
294  * lot of work. So instead we expect the AM to handle this in
295  * its amproperty routine. The generic result is to return
296  * false if the AM says it never supports this, or if this is a
297  * nonkey column, and null otherwise (meaning we don't know).
298  */
299  if (!iskey || !routine->amcanorderbyop)
300  {
301  res = false;
302  isnull = false;
303  }
304  break;
305 
306  case AMPROP_RETURNABLE:
307 
308  /* note that we ignore iskey for this property */
309 
310  isnull = false;
311  res = false;
312 
313  if (routine->amcanreturn)
314  {
315  /*
316  * If possible, the AM should handle this test in its
317  * amproperty function without opening the rel. But this is the
318  * generic fallback if it does not.
319  */
320  Relation indexrel = index_open(index_oid, AccessShareLock);
321 
322  res = index_can_return(indexrel, attno);
323  index_close(indexrel, AccessShareLock);
324  }
325  break;
326 
327  case AMPROP_SEARCH_ARRAY:
328  if (iskey)
329  {
330  res = routine->amsearcharray;
331  isnull = false;
332  }
333  break;
334 
335  case AMPROP_SEARCH_NULLS:
336  if (iskey)
337  {
338  res = routine->amsearchnulls;
339  isnull = false;
340  }
341  break;
342 
343  default:
344  break;
345  }
346 
347  ReleaseSysCache(tuple);
348 
349  if (!isnull)
350  PG_RETURN_BOOL(res);
351  PG_RETURN_NULL();
352  }
353 
354  if (OidIsValid(index_oid))
355  {
356  /*
357  * Handle index-level properties. Currently, these only depend on the
358  * AM, but that might not be true forever, so we make users name an
359  * index not just an AM.
360  */
361  switch (prop)
362  {
363  case AMPROP_CLUSTERABLE:
364  PG_RETURN_BOOL(routine->amclusterable);
365 
366  case AMPROP_INDEX_SCAN:
367  PG_RETURN_BOOL(routine->amgettuple ? true : false);
368 
369  case AMPROP_BITMAP_SCAN:
370  PG_RETURN_BOOL(routine->amgetbitmap ? true : false);
371 
373  PG_RETURN_BOOL(routine->amcanbackward);
374 
375  default:
376  PG_RETURN_NULL();
377  }
378  }
379 
380  /*
381  * Handle AM-level properties (those that control what you can say in
382  * CREATE INDEX).
383  */
384  switch (prop)
385  {
386  case AMPROP_CAN_ORDER:
387  PG_RETURN_BOOL(routine->amcanorder);
388 
389  case AMPROP_CAN_UNIQUE:
390  PG_RETURN_BOOL(routine->amcanunique);
391 
393  PG_RETURN_BOOL(routine->amcanmulticol);
394 
395  case AMPROP_CAN_EXCLUDE:
396  PG_RETURN_BOOL(routine->amgettuple ? true : false);
397 
398  case AMPROP_CAN_INCLUDE:
399  PG_RETURN_BOOL(routine->amcaninclude);
400 
401  default:
402  PG_RETURN_NULL();
403  }
404 }
405 
406 /*
407  * Test property of an AM specified by AM OID
408  */
409 Datum
411 {
412  Oid amoid = PG_GETARG_OID(0);
413  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
414 
415  return indexam_property(fcinfo, propname, amoid, InvalidOid, 0);
416 }
417 
418 /*
419  * Test property of an index specified by index OID
420  */
421 Datum
423 {
424  Oid relid = PG_GETARG_OID(0);
425  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
426 
427  return indexam_property(fcinfo, propname, InvalidOid, relid, 0);
428 }
429 
430 /*
431  * Test property of an index column specified by index OID and column number
432  */
433 Datum
435 {
436  Oid relid = PG_GETARG_OID(0);
437  int32 attno = PG_GETARG_INT32(1);
438  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(2));
439 
440  /* Reject attno 0 immediately, so that attno > 0 identifies this case */
441  if (attno <= 0)
442  PG_RETURN_NULL();
443 
444  return indexam_property(fcinfo, propname, InvalidOid, relid, attno);
445 }
static IndexAMProperty lookup_prop_name(const char *name)
Definition: amutils.c:90
signed short int16
Definition: c.h:312
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
bool amcanmulticol
Definition: amapi.h:180
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
IndexAMProperty
Definition: amapi.h:34
amgettuple_function amgettuple
Definition: amapi.h:219
bool amcanorderbyop
Definition: amapi.h:174
amproperty_function amproperty
Definition: amapi.h:215
static const struct am_propname am_propnames[]
Definition: amutils.c:31
#define AccessShareLock
Definition: lockdefs.h:36
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define lengthof(array)
Definition: c.h:629
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
Datum pg_index_has_property(PG_FUNCTION_ARGS)
Definition: amutils.c:422
signed int int32
Definition: c.h:313
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
bool index_can_return(Relation indexRelation, int attno)
Definition: indexam.c:783
amgetbitmap_function amgetbitmap
Definition: amapi.h:220
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
bool amcaninclude
Definition: amapi.h:196
bool amcanunique
Definition: amapi.h:178
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
bool amcanbackward
Definition: amapi.h:176
static Datum indexam_property(FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
Definition: amutils.c:154
static bool test_indoption(HeapTuple tuple, int attno, bool guard, int16 iopt_mask, int16 iopt_expect, bool *res)
Definition: amutils.c:117
FormData_pg_index * Form_pg_index
Definition: pg_index.h:66
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
Datum pg_indexam_has_property(PG_FUNCTION_ARGS)
Definition: amutils.c:410
bool amsearchnulls
Definition: amapi.h:186
Definition: c.h:544
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:365
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
bool amclusterable
Definition: amapi.h:190
bool amsearcharray
Definition: amapi.h:184
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
IndexAMProperty prop
Definition: amutils.c:28
bool amcanorder
Definition: amapi.h:172
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:552
Datum pg_index_column_has_property(PG_FUNCTION_ARGS)
Definition: amutils.c:434
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
#define DatumGetPointer(X)
Definition: postgres.h:532
const char * name
Definition: amutils.c:27
char * text_to_cstring(const text *t)
Definition: varlena.c:182
FormData_pg_class * Form_pg_class
Definition: pg_class.h:92
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
amcanreturn_function amcanreturn
Definition: amapi.h:212
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:150
#define PG_RETURN_NULL()
Definition: fmgr.h:310