PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 (Oid relid, 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)
 

Variables

static const struct am_propname am_propnames []
 

Function Documentation

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

Definition at line 162 of file amutils.c.

References AccessShareLock, IndexAmRoutine::amcanbackward, 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_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(), INDOPTION_DESC, INDOPTION_NULLS_FIRST, lookup_prop_name(), NULL, ObjectIdGetDatum, OidIsValid, PG_RETURN_BOOL, PG_RETURN_NULL, am_propname::prop, ReleaseSysCache(), RELKIND_INDEX, RELOID, SearchSysCache1, and test_indoption().

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

165 {
166  bool res = false;
167  bool isnull = false;
168  int natts = 0;
169  IndexAMProperty prop;
170  IndexAmRoutine *routine;
171 
172  /* Try to convert property name to enum (no error if not known) */
173  prop = lookup_prop_name(propname);
174 
175  /* If we have an index OID, look up the AM, and get # of columns too */
176  if (OidIsValid(index_oid))
177  {
178  HeapTuple tuple;
179  Form_pg_class rd_rel;
180 
181  Assert(!OidIsValid(amoid));
182  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(index_oid));
183  if (!HeapTupleIsValid(tuple))
184  PG_RETURN_NULL();
185  rd_rel = (Form_pg_class) GETSTRUCT(tuple);
186  if (rd_rel->relkind != RELKIND_INDEX)
187  {
188  ReleaseSysCache(tuple);
189  PG_RETURN_NULL();
190  }
191  amoid = rd_rel->relam;
192  natts = rd_rel->relnatts;
193  ReleaseSysCache(tuple);
194  }
195 
196  /*
197  * At this point, either index_oid == InvalidOid or it's a valid index
198  * OID. Also, after this test, either attno == 0 for index-wide or
199  * AM-wide tests, or it's a valid column number in a valid index.
200  */
201  if (attno < 0 || attno > natts)
202  PG_RETURN_NULL();
203 
204  /*
205  * Get AM information. If we don't have a valid AM OID, return NULL.
206  */
207  routine = GetIndexAmRoutineByAmId(amoid, true);
208  if (routine == NULL)
209  PG_RETURN_NULL();
210 
211  /*
212  * If there's an AM property routine, give it a chance to override the
213  * generic logic. Proceed if it returns false.
214  */
215  if (routine->amproperty &&
216  routine->amproperty(index_oid, attno, prop, propname,
217  &res, &isnull))
218  {
219  if (isnull)
220  PG_RETURN_NULL();
221  PG_RETURN_BOOL(res);
222  }
223 
224  if (attno > 0)
225  {
226  /* Handle column-level properties */
227  switch (prop)
228  {
229  case AMPROP_ASC:
230  if (test_indoption(index_oid, attno, routine->amcanorder,
231  INDOPTION_DESC, 0, &res))
232  PG_RETURN_BOOL(res);
233  PG_RETURN_NULL();
234 
235  case AMPROP_DESC:
236  if (test_indoption(index_oid, attno, routine->amcanorder,
238  PG_RETURN_BOOL(res);
239  PG_RETURN_NULL();
240 
241  case AMPROP_NULLS_FIRST:
242  if (test_indoption(index_oid, attno, routine->amcanorder,
244  PG_RETURN_BOOL(res);
245  PG_RETURN_NULL();
246 
247  case AMPROP_NULLS_LAST:
248  if (test_indoption(index_oid, attno, routine->amcanorder,
249  INDOPTION_NULLS_FIRST, 0, &res))
250  PG_RETURN_BOOL(res);
251  PG_RETURN_NULL();
252 
253  case AMPROP_ORDERABLE:
254  PG_RETURN_BOOL(routine->amcanorder);
255 
257 
258  /*
259  * The conditions for whether a column is distance-orderable
260  * are really up to the AM (at time of writing, only GiST
261  * supports it at all). The planner has its own idea based on
262  * whether it finds an operator with amoppurpose 'o', but
263  * getting there from just the index column type seems like a
264  * lot of work. So instead we expect the AM to handle this in
265  * its amproperty routine. The generic result is to return
266  * false if the AM says it never supports this, and null
267  * otherwise (meaning we don't know).
268  */
269  if (!routine->amcanorderbyop)
270  PG_RETURN_BOOL(false);
271  PG_RETURN_NULL();
272 
273  case AMPROP_RETURNABLE:
274  if (!routine->amcanreturn)
275  PG_RETURN_BOOL(false);
276 
277  /*
278  * If possible, the AM should handle this test in its
279  * amproperty function without opening the rel. But this is
280  * the generic fallback if it does not.
281  */
282  {
283  Relation indexrel = index_open(index_oid, AccessShareLock);
284 
285  res = index_can_return(indexrel, attno);
286  index_close(indexrel, AccessShareLock);
287  }
288 
289  PG_RETURN_BOOL(res);
290 
291  case AMPROP_SEARCH_ARRAY:
292  PG_RETURN_BOOL(routine->amsearcharray);
293 
294  case AMPROP_SEARCH_NULLS:
295  PG_RETURN_BOOL(routine->amsearchnulls);
296 
297  default:
298  PG_RETURN_NULL();
299  }
300  }
301 
302  if (OidIsValid(index_oid))
303  {
304  /*
305  * Handle index-level properties. Currently, these only depend on the
306  * AM, but that might not be true forever, so we make users name an
307  * index not just an AM.
308  */
309  switch (prop)
310  {
311  case AMPROP_CLUSTERABLE:
312  PG_RETURN_BOOL(routine->amclusterable);
313 
314  case AMPROP_INDEX_SCAN:
315  PG_RETURN_BOOL(routine->amgettuple ? true : false);
316 
317  case AMPROP_BITMAP_SCAN:
318  PG_RETURN_BOOL(routine->amgetbitmap ? true : false);
319 
321  PG_RETURN_BOOL(routine->amcanbackward);
322 
323  default:
324  PG_RETURN_NULL();
325  }
326  }
327 
328  /*
329  * Handle AM-level properties (those that control what you can say in
330  * CREATE INDEX).
331  */
332  switch (prop)
333  {
334  case AMPROP_CAN_ORDER:
335  PG_RETURN_BOOL(routine->amcanorder);
336 
337  case AMPROP_CAN_UNIQUE:
338  PG_RETURN_BOOL(routine->amcanunique);
339 
341  PG_RETURN_BOOL(routine->amcanmulticol);
342 
343  case AMPROP_CAN_EXCLUDE:
344  PG_RETURN_BOOL(routine->amgettuple ? true : false);
345 
346  default:
347  PG_RETURN_NULL();
348  }
349 }
static IndexAMProperty lookup_prop_name(const char *name)
Definition: amutils.c:87
#define INDOPTION_NULLS_FIRST
Definition: pg_index.h:100
bool amcanmulticol
Definition: amapi.h:179
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
IndexAMProperty
Definition: amapi.h:34
amgettuple_function amgettuple
Definition: amapi.h:210
bool amcanorderbyop
Definition: amapi.h:173
amproperty_function amproperty
Definition: amapi.h:206
#define AccessShareLock
Definition: lockdefs.h:36
#define OidIsValid(objectId)
Definition: c.h:534
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
bool index_can_return(Relation indexRelation, int attno)
Definition: indexam.c:783
amgetbitmap_function amgetbitmap
Definition: amapi.h:211
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
bool amcanunique
Definition: amapi.h:177
bool amcanbackward
Definition: amapi.h:175
bool amsearchnulls
Definition: amapi.h:185
static bool test_indoption(Oid relid, int attno, bool guard, int16 iopt_mask, int16 iopt_expect, bool *res)
Definition: amutils.c:113
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
bool amclusterable
Definition: amapi.h:189
bool amsearcharray
Definition: amapi.h:183
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define INDOPTION_DESC
Definition: pg_index.h:99
bool amcanorder
Definition: amapi.h:171
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define RELKIND_INDEX
Definition: pg_class.h:161
amcanreturn_function amcanreturn
Definition: amapi.h:203
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
#define PG_RETURN_NULL()
Definition: fmgr.h:289
static IndexAMProperty lookup_prop_name ( const char *  name)
static

Definition at line 87 of file amutils.c.

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

Referenced by indexam_property().

88 {
89  int i;
90 
91  for (i = 0; i < lengthof(am_propnames); i++)
92  {
93  if (pg_strcasecmp(am_propnames[i].name, name) == 0)
94  return am_propnames[i].prop;
95  }
96 
97  /* We do not throw an error, so that AMs can define their own properties */
98  return AMPROP_UNKNOWN;
99 }
static const struct am_propname am_propnames[]
Definition: amutils.c:31
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define lengthof(array)
Definition: c.h:558
const char * name
Definition: encode.c:521
int i
Datum pg_index_column_has_property ( PG_FUNCTION_ARGS  )

Definition at line 379 of file amutils.c.

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

380 {
381  Oid relid = PG_GETARG_OID(0);
382  int32 attno = PG_GETARG_INT32(1);
383  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(2));
384 
385  /* Reject attno 0 immediately, so that attno > 0 identifies this case */
386  if (attno <= 0)
387  PG_RETURN_NULL();
388 
389  return indexam_property(fcinfo, propname, InvalidOid, relid, attno);
390 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:253
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
static Datum indexam_property(FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
Definition: amutils.c:162
#define InvalidOid
Definition: postgres_ext.h:36
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum pg_index_has_property ( PG_FUNCTION_ARGS  )

Definition at line 367 of file amutils.c.

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

368 {
369  Oid relid = PG_GETARG_OID(0);
370  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
371 
372  return indexam_property(fcinfo, propname, InvalidOid, relid, 0);
373 }
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
static Datum indexam_property(FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
Definition: amutils.c:162
#define InvalidOid
Definition: postgres_ext.h:36
char * text_to_cstring(const text *t)
Definition: varlena.c:184
Datum pg_indexam_has_property ( PG_FUNCTION_ARGS  )

Definition at line 355 of file amutils.c.

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

356 {
357  Oid amoid = PG_GETARG_OID(0);
358  char *propname = text_to_cstring(PG_GETARG_TEXT_PP(1));
359 
360  return indexam_property(fcinfo, propname, amoid, InvalidOid, 0);
361 }
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
static Datum indexam_property(FunctionCallInfo fcinfo, const char *propname, Oid amoid, Oid index_oid, int attno)
Definition: amutils.c:162
#define InvalidOid
Definition: postgres_ext.h:36
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static bool test_indoption ( Oid  relid,
int  attno,
bool  guard,
int16  iopt_mask,
int16  iopt_expect,
bool res 
)
static

Definition at line 113 of file amutils.c.

References Anum_pg_index_indoption, Assert, DatumGetPointer, GETSTRUCT, HeapTupleIsValid, INDEXRELID, ObjectIdGetDatum, PG_USED_FOR_ASSERTS_ONLY, ReleaseSysCache(), SearchSysCache1, SysCacheGetAttr(), and int2vector::values.

Referenced by indexam_property().

116 {
117  HeapTuple tuple;
119  Datum datum;
120  bool isnull;
121  int2vector *indoption;
122  int16 indoption_val;
123 
124  if (!guard)
125  {
126  *res = false;
127  return true;
128  }
129 
131  if (!HeapTupleIsValid(tuple))
132  return false;
133  rd_index = (Form_pg_index) GETSTRUCT(tuple);
134 
135  Assert(relid == rd_index->indexrelid);
136  Assert(attno > 0 && attno <= rd_index->indnatts);
137 
138  datum = SysCacheGetAttr(INDEXRELID, tuple,
139  Anum_pg_index_indoption, &isnull);
140  Assert(!isnull);
141 
142  indoption = ((int2vector *) DatumGetPointer(datum));
143  indoption_val = indoption->values[attno - 1];
144 
145  *res = (indoption_val & iopt_mask) == iopt_expect;
146 
147  ReleaseSysCache(tuple);
148 
149  return true;
150 }
signed short int16
Definition: c.h:252
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
FormData_pg_index * Form_pg_index
Definition: pg_index.h:67
Definition: c.h:463
uintptr_t Datum
Definition: postgres.h:374
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:671
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:471
#define DatumGetPointer(X)
Definition: postgres.h:557
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:986
#define Anum_pg_index_indoption
Definition: pg_index.h:90

Variable Documentation

const struct am_propname am_propnames[]
static

Definition at line 31 of file amutils.c.