PostgreSQL Source Code  git master
amutils.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/htup_details.h"
#include "catalog/pg_class.h"
#include "catalog/pg_index.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
Include dependency graph for amutils.c:

Go to the source code of this file.

Data Structures

struct  am_propname
 

Functions

static IndexAMProperty lookup_prop_name (const char *name)
 
static bool test_indoption (HeapTuple tuple, int attno, bool guard, int16 iopt_mask, int16 iopt_expect, bool *res)
 
static Datum indexam_property (FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
 
Datum pg_indexam_has_property (PG_FUNCTION_ARGS)
 
Datum pg_index_has_property (PG_FUNCTION_ARGS)
 
Datum pg_index_column_has_property (PG_FUNCTION_ARGS)
 
Datum pg_indexam_progress_phasename (PG_FUNCTION_ARGS)
 

Variables

static const struct am_propname am_propnames []
 

Function Documentation

◆ indexam_property()

static Datum indexam_property ( FunctionCallInfo  fcinfo,
const char *  propname,
Oid  amoid,
Oid  index_oid,
int  attno 
)
static

Definition at line 151 of file amutils.c.

154 {
155  bool res = false;
156  bool isnull = false;
157  int natts = 0;
158  IndexAMProperty prop;
159  IndexAmRoutine *routine;
160 
161  /* Try to convert property name to enum (no error if not known) */
162  prop = lookup_prop_name(propname);
163 
164  /* If we have an index OID, look up the AM, and get # of columns too */
165  if (OidIsValid(index_oid))
166  {
167  HeapTuple tuple;
168  Form_pg_class rd_rel;
169 
170  Assert(!OidIsValid(amoid));
171  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(index_oid));
172  if (!HeapTupleIsValid(tuple))
173  PG_RETURN_NULL();
174  rd_rel = (Form_pg_class) GETSTRUCT(tuple);
175  if (rd_rel->relkind != RELKIND_INDEX &&
176  rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
177  {
178  ReleaseSysCache(tuple);
179  PG_RETURN_NULL();
180  }
181  amoid = rd_rel->relam;
182  natts = rd_rel->relnatts;
183  ReleaseSysCache(tuple);
184  }
185 
186  /*
187  * At this point, either index_oid == InvalidOid or it's a valid index
188  * OID. Also, after this test and the one below, either attno == 0 for
189  * index-wide or AM-wide tests, or it's a valid column number in a valid
190  * index.
191  */
192  if (attno < 0 || attno > natts)
193  PG_RETURN_NULL();
194 
195  /*
196  * Get AM information. If we don't have a valid AM OID, return NULL.
197  */
198  routine = GetIndexAmRoutineByAmId(amoid, true);
199  if (routine == NULL)
200  PG_RETURN_NULL();
201 
202  /*
203  * If there's an AM property routine, give it a chance to override the
204  * generic logic. Proceed if it returns false.
205  */
206  if (routine->amproperty &&
207  routine->amproperty(index_oid, attno, prop, propname,
208  &res, &isnull))
209  {
210  if (isnull)
211  PG_RETURN_NULL();
213  }
214 
215  if (attno > 0)
216  {
217  HeapTuple tuple;
218  Form_pg_index rd_index;
219  bool iskey = true;
220 
221  /*
222  * Handle column-level properties. Many of these need the pg_index row
223  * (which we also need to use to check for nonkey atts) so we fetch
224  * that first.
225  */
226  tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
227  if (!HeapTupleIsValid(tuple))
228  PG_RETURN_NULL();
229  rd_index = (Form_pg_index) GETSTRUCT(tuple);
230 
231  Assert(index_oid == rd_index->indexrelid);
232  Assert(attno > 0 && attno <= rd_index->indnatts);
233 
234  isnull = true;
235 
236  /*
237  * If amcaninclude, we might be looking at an attno for a nonkey
238  * column, for which we (generically) assume that most properties are
239  * null.
240  */
241  if (routine->amcaninclude
242  && attno > rd_index->indnkeyatts)
243  iskey = false;
244 
245  switch (prop)
246  {
247  case AMPROP_ASC:
248  if (iskey &&
249  test_indoption(tuple, attno, routine->amcanorder,
250  INDOPTION_DESC, 0, &res))
251  isnull = false;
252  break;
253 
254  case AMPROP_DESC:
255  if (iskey &&
256  test_indoption(tuple, attno, routine->amcanorder,
257  INDOPTION_DESC, INDOPTION_DESC, &res))
258  isnull = false;
259  break;
260 
261  case AMPROP_NULLS_FIRST:
262  if (iskey &&
263  test_indoption(tuple, attno, routine->amcanorder,
264  INDOPTION_NULLS_FIRST, INDOPTION_NULLS_FIRST, &res))
265  isnull = false;
266  break;
267 
268  case AMPROP_NULLS_LAST:
269  if (iskey &&
270  test_indoption(tuple, attno, routine->amcanorder,
271  INDOPTION_NULLS_FIRST, 0, &res))
272  isnull = false;
273  break;
274 
275  case AMPROP_ORDERABLE:
276 
277  /*
278  * generic assumption is that nonkey columns are not orderable
279  */
280  res = iskey ? routine->amcanorder : false;
281  isnull = false;
282  break;
283 
285 
286  /*
287  * The conditions for whether a column is distance-orderable
288  * are really up to the AM (at time of writing, only GiST
289  * supports it at all). The planner has its own idea based on
290  * whether it finds an operator with amoppurpose 'o', but
291  * getting there from just the index column type seems like a
292  * lot of work. So instead we expect the AM to handle this in
293  * its amproperty routine. The generic result is to return
294  * false if the AM says it never supports this, or if this is
295  * a nonkey column, and null otherwise (meaning we don't
296  * know).
297  */
298  if (!iskey || !routine->amcanorderbyop)
299  {
300  res = false;
301  isnull = false;
302  }
303  break;
304 
305  case AMPROP_RETURNABLE:
306 
307  /* note that we ignore iskey for this property */
308 
309  isnull = false;
310  res = false;
311 
312  if (routine->amcanreturn)
313  {
314  /*
315  * If possible, the AM should handle this test in its
316  * amproperty function without opening the rel. But this
317  * is the generic fallback if it does not.
318  */
319  Relation indexrel = index_open(index_oid, AccessShareLock);
320 
321  res = index_can_return(indexrel, attno);
322  index_close(indexrel, AccessShareLock);
323  }
324  break;
325 
326  case AMPROP_SEARCH_ARRAY:
327  if (iskey)
328  {
329  res = routine->amsearcharray;
330  isnull = false;
331  }
332  break;
333 
334  case AMPROP_SEARCH_NULLS:
335  if (iskey)
336  {
337  res = routine->amsearchnulls;
338  isnull = false;
339  }
340  break;
341 
342  default:
343  break;
344  }
345 
346  ReleaseSysCache(tuple);
347 
348  if (!isnull)
350  PG_RETURN_NULL();
351  }
352 
353  if (OidIsValid(index_oid))
354  {
355  /*
356  * Handle index-level properties. Currently, these only depend on the
357  * AM, but that might not be true forever, so we make users name an
358  * index not just an AM.
359  */
360  switch (prop)
361  {
362  case AMPROP_CLUSTERABLE:
363  PG_RETURN_BOOL(routine->amclusterable);
364 
365  case AMPROP_INDEX_SCAN:
366  PG_RETURN_BOOL(routine->amgettuple ? true : false);
367 
368  case AMPROP_BITMAP_SCAN:
369  PG_RETURN_BOOL(routine->amgetbitmap ? true : false);
370 
372  PG_RETURN_BOOL(routine->amcanbackward);
373 
374  default:
375  PG_RETURN_NULL();
376  }
377  }
378 
379  /*
380  * Handle AM-level properties (those that control what you can say in
381  * CREATE INDEX).
382  */
383  switch (prop)
384  {
385  case AMPROP_CAN_ORDER:
386  PG_RETURN_BOOL(routine->amcanorder);
387 
388  case AMPROP_CAN_UNIQUE:
389  PG_RETURN_BOOL(routine->amcanunique);
390 
392  PG_RETURN_BOOL(routine->amcanmulticol);
393 
394  case AMPROP_CAN_EXCLUDE:
395  PG_RETURN_BOOL(routine->amgettuple ? true : false);
396 
397  case AMPROP_CAN_INCLUDE:
398  PG_RETURN_BOOL(routine->amcaninclude);
399 
400  default:
401  PG_RETURN_NULL();
402  }
403 }
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
IndexAMProperty
Definition: amapi.h:35
@ AMPROP_BACKWARD_SCAN
Definition: amapi.h:49
@ AMPROP_SEARCH_ARRAY
Definition: amapi.h:44
@ AMPROP_CAN_MULTI_COL
Definition: amapi.h:52
@ AMPROP_CAN_ORDER
Definition: amapi.h:50
@ AMPROP_ORDERABLE
Definition: amapi.h:41
@ AMPROP_ASC
Definition: amapi.h:37
@ AMPROP_DISTANCE_ORDERABLE
Definition: amapi.h:42
@ AMPROP_CAN_INCLUDE
Definition: amapi.h:54
@ AMPROP_SEARCH_NULLS
Definition: amapi.h:45
@ AMPROP_CAN_EXCLUDE
Definition: amapi.h:53
@ AMPROP_CAN_UNIQUE
Definition: amapi.h:51
@ AMPROP_DESC
Definition: amapi.h:38
@ AMPROP_NULLS_LAST
Definition: amapi.h:40
@ AMPROP_INDEX_SCAN
Definition: amapi.h:47
@ AMPROP_NULLS_FIRST
Definition: amapi.h:39
@ AMPROP_CLUSTERABLE
Definition: amapi.h:46
@ AMPROP_RETURNABLE
Definition: amapi.h:43
@ AMPROP_BITMAP_SCAN
Definition: amapi.h:48
static IndexAMProperty lookup_prop_name(const char *name)
Definition: amutils.c:90
static bool test_indoption(HeapTuple tuple, int attno, bool guard, int16 iopt_mask, int16 iopt_expect, bool *res)
Definition: amutils.c:117
#define Assert(condition)
Definition: c.h:858
#define OidIsValid(objectId)
Definition: c.h:775
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
bool index_can_return(Relation indexRelation, int attno)
Definition: indexam.c:789
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
#define AccessShareLock
Definition: lockdefs.h:36
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
bool amclusterable
Definition: amapi.h:241
bool amcanorderbyop
Definition: amapi.h:225
amgettuple_function amgettuple
Definition: amapi.h:281
amcanreturn_function amcanreturn
Definition: amapi.h:272
bool amcanunique
Definition: amapi.h:229
amgetbitmap_function amgetbitmap
Definition: amapi.h:282
amproperty_function amproperty
Definition: amapi.h:275
bool amsearcharray
Definition: amapi.h:235
bool amcanmulticol
Definition: amapi.h:231
bool amcanorder
Definition: amapi.h:223
bool amcanbackward
Definition: amapi.h:227
bool amcaninclude
Definition: amapi.h:249
bool amsearchnulls
Definition: amapi.h:237
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218

References AccessShareLock, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcaninclude, IndexAmRoutine::amcanmulticol, IndexAmRoutine::amcanorder, IndexAmRoutine::amcanorderbyop, IndexAmRoutine::amcanreturn, IndexAmRoutine::amcanunique, IndexAmRoutine::amclusterable, IndexAmRoutine::amgetbitmap, IndexAmRoutine::amgettuple, AMPROP_ASC, AMPROP_BACKWARD_SCAN, AMPROP_BITMAP_SCAN, AMPROP_CAN_EXCLUDE, AMPROP_CAN_INCLUDE, AMPROP_CAN_MULTI_COL, AMPROP_CAN_ORDER, AMPROP_CAN_UNIQUE, AMPROP_CLUSTERABLE, AMPROP_DESC, AMPROP_DISTANCE_ORDERABLE, AMPROP_INDEX_SCAN, AMPROP_NULLS_FIRST, AMPROP_NULLS_LAST, AMPROP_ORDERABLE, AMPROP_RETURNABLE, AMPROP_SEARCH_ARRAY, AMPROP_SEARCH_NULLS, IndexAmRoutine::amproperty, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, Assert, GetIndexAmRoutineByAmId(), GETSTRUCT, HeapTupleIsValid, index_can_return(), index_close(), index_open(), lookup_prop_name(), ObjectIdGetDatum(), OidIsValid, PG_RETURN_BOOL, PG_RETURN_NULL, am_propname::prop, ReleaseSysCache(), res, SearchSysCache1(), and test_indoption().

Referenced by pg_index_column_has_property(), pg_index_has_property(), and pg_indexam_has_property().

◆ lookup_prop_name()

static IndexAMProperty lookup_prop_name ( const char *  name)
static

Definition at line 90 of file amutils.c.

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 }
@ AMPROP_UNKNOWN
Definition: amapi.h:36
static const struct am_propname am_propnames[]
Definition: amutils.c:31
#define lengthof(array)
Definition: c.h:788
int i
Definition: isn.c:73
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name

References am_propnames, AMPROP_UNKNOWN, i, lengthof, name, pg_strcasecmp(), and am_propname::prop.

Referenced by indexam_property().

◆ pg_index_column_has_property()

Datum pg_index_column_has_property ( PG_FUNCTION_ARGS  )

Definition at line 433 of file amutils.c.

434 {
435  Oid relid = PG_GETARG_OID(0);
436  int32 attno = PG_GETARG_INT32(1);
437  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(2));
438 
439  /* Reject attno 0 immediately, so that attno > 0 identifies this case */
440  if (attno <= 0)
441  PG_RETURN_NULL();
442 
443  return indexam_property(fcinfo, propname, InvalidOid, relid, attno);
444 }
static Datum indexam_property(FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
Definition: amutils.c:151
signed int int32
Definition: c.h:494
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References indexam_property(), InvalidOid, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_NULL, and text_to_cstring().

◆ pg_index_has_property()

Datum pg_index_has_property ( PG_FUNCTION_ARGS  )

Definition at line 421 of file amutils.c.

422 {
423  Oid relid = PG_GETARG_OID(0);
424  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
425 
426  return indexam_property(fcinfo, propname, InvalidOid, relid, 0);
427 }

References indexam_property(), InvalidOid, PG_GETARG_OID, PG_GETARG_TEXT_PP, and text_to_cstring().

◆ pg_indexam_has_property()

Datum pg_indexam_has_property ( PG_FUNCTION_ARGS  )

Definition at line 409 of file amutils.c.

410 {
411  Oid amoid = PG_GETARG_OID(0);
412  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
413 
414  return indexam_property(fcinfo, propname, amoid, InvalidOid, 0);
415 }

References indexam_property(), InvalidOid, PG_GETARG_OID, PG_GETARG_TEXT_PP, and text_to_cstring().

◆ pg_indexam_progress_phasename()

Datum pg_indexam_progress_phasename ( PG_FUNCTION_ARGS  )

Definition at line 451 of file amutils.c.

452 {
453  Oid amoid = PG_GETARG_OID(0);
454  int32 phasenum = PG_GETARG_INT32(1);
455  IndexAmRoutine *routine;
456  char *name;
457 
458  routine = GetIndexAmRoutineByAmId(amoid, true);
459  if (routine == NULL || !routine->ambuildphasename)
460  PG_RETURN_NULL();
461 
462  name = routine->ambuildphasename(phasenum);
463  if (!name)
464  PG_RETURN_NULL();
465 
467 }
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
ambuildphasename_function ambuildphasename
Definition: amapi.h:276

References IndexAmRoutine::ambuildphasename, CStringGetTextDatum, GetIndexAmRoutineByAmId(), name, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_DATUM, and PG_RETURN_NULL.

◆ test_indoption()

static bool test_indoption ( HeapTuple  tuple,
int  attno,
bool  guard,
int16  iopt_mask,
int16  iopt_expect,
bool res 
)
static

Definition at line 117 of file amutils.c.

120 {
121  Datum datum;
122  int2vector *indoption;
123  int16 indoption_val;
124 
125  if (!guard)
126  {
127  *res = false;
128  return true;
129  }
130 
131  datum = SysCacheGetAttrNotNull(INDEXRELID, tuple, Anum_pg_index_indoption);
132 
133  indoption = ((int2vector *) DatumGetPointer(datum));
134  indoption_val = indoption->values[attno - 1];
135 
136  *res = (indoption_val & iopt_mask) == iopt_expect;
137 
138  return true;
139 }
signed short int16
Definition: c.h:493
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
Definition: c.h:715
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:722
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:510

References DatumGetPointer(), res, SysCacheGetAttrNotNull(), and int2vector::values.

Referenced by indexam_property().

Variable Documentation

◆ am_propnames

const struct am_propname am_propnames[]
static

Definition at line 1 of file amutils.c.

Referenced by lookup_prop_name().