PostgreSQL Source Code  git master
btree_gin.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/stratnum.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/float.h"
#include "utils/inet.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
#include "utils/uuid.h"
Include dependency graph for btree_gin.c:

Go to the source code of this file.

Data Structures

struct  QueryInfo
 

Macros

#define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp)
 
#define NUMERIC_IS_LEFTMOST(x)   ((x) == NULL)
 
#define ENUM_IS_LEFTMOST(x)   ((x) == InvalidOid)
 

Typedefs

typedef struct QueryInfo QueryInfo
 

Functions

static Datum gin_btree_extract_value (FunctionCallInfo fcinfo, bool is_varlena)
 
static Datum gin_btree_extract_query (FunctionCallInfo fcinfo, bool is_varlena, Datum(*leftmostvalue)(void), Datum(*typecmp)(FunctionCallInfo))
 
static Datum gin_btree_compare_prefix (FunctionCallInfo fcinfo)
 
 PG_FUNCTION_INFO_V1 (gin_btree_consistent)
 
Datum gin_btree_consistent (PG_FUNCTION_ARGS)
 
static Datum leftmostvalue_int2 (void)
 
static Datum leftmostvalue_int4 (void)
 
static Datum leftmostvalue_int8 (void)
 
static Datum leftmostvalue_float4 (void)
 
static Datum leftmostvalue_float8 (void)
 
static Datum leftmostvalue_money (void)
 
static Datum leftmostvalue_oid (void)
 
static Datum leftmostvalue_timestamp (void)
 
static Datum leftmostvalue_time (void)
 
static Datum leftmostvalue_timetz (void)
 
static Datum leftmostvalue_date (void)
 
static Datum leftmostvalue_interval (void)
 
static Datum leftmostvalue_macaddr (void)
 
static Datum leftmostvalue_macaddr8 (void)
 
static Datum leftmostvalue_inet (void)
 
static Datum leftmostvalue_text (void)
 
static Datum leftmostvalue_char (void)
 
static Datum leftmostvalue_bit (void)
 
static Datum leftmostvalue_varbit (void)
 
 PG_FUNCTION_INFO_V1 (gin_numeric_cmp)
 
Datum gin_numeric_cmp (PG_FUNCTION_ARGS)
 
static Datum leftmostvalue_numeric (void)
 
 PG_FUNCTION_INFO_V1 (gin_enum_cmp)
 
Datum gin_enum_cmp (PG_FUNCTION_ARGS)
 
static Datum leftmostvalue_enum (void)
 
static Datum leftmostvalue_uuid (void)
 
static Datum leftmostvalue_name (void)
 
static Datum leftmostvalue_bool (void)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ ENUM_IS_LEFTMOST

#define ENUM_IS_LEFTMOST (   x)    ((x) == InvalidOid)

Definition at line 440 of file btree_gin.c.

◆ GIN_SUPPORT

#define GIN_SUPPORT (   type,
  is_varlena,
  leftmostvalue,
  typecmp 
)
Value:
PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
Datum \
gin_extract_value_##type(PG_FUNCTION_ARGS) \
{ \
return gin_btree_extract_value(fcinfo, is_varlena); \
} \
PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
Datum \
gin_extract_query_##type(PG_FUNCTION_ARGS) \
{ \
return gin_btree_extract_query(fcinfo, \
is_varlena, leftmostvalue, typecmp); \
} \
PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
Datum \
gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
{ \
return gin_btree_compare_prefix(fcinfo); \
}
static Datum gin_btree_extract_query(FunctionCallInfo fcinfo, bool is_varlena, Datum(*leftmostvalue)(void), Datum(*typecmp)(FunctionCallInfo))
Definition: btree_gin.c:53
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:106
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:30
PG_FUNCTION_INFO_V1(gin_btree_consistent)
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
const char * type

Definition at line 182 of file btree_gin.c.

◆ NUMERIC_IS_LEFTMOST

#define NUMERIC_IS_LEFTMOST (   x)    ((x) == NULL)

Definition at line 393 of file btree_gin.c.

Typedef Documentation

◆ QueryInfo

typedef struct QueryInfo QueryInfo

Function Documentation

◆ gin_btree_compare_prefix()

static Datum gin_btree_compare_prefix ( FunctionCallInfo  fcinfo)
static

Definition at line 106 of file btree_gin.c.

107 {
108  Datum a = PG_GETARG_DATUM(0);
109  Datum b = PG_GETARG_DATUM(1);
111  int32 res,
112  cmp;
113 
115  fcinfo->flinfo,
117  (data->strategy == BTLessStrategyNumber ||
118  data->strategy == BTLessEqualStrategyNumber)
119  ? data->datum : a,
120  b));
121 
122  switch (data->strategy)
123  {
125  /* If original datum > indexed one then return match */
126  if (cmp > 0)
127  res = 0;
128  else
129  res = 1;
130  break;
132  /* The same except equality */
133  if (cmp >= 0)
134  res = 0;
135  else
136  res = 1;
137  break;
139  if (cmp != 0)
140  res = 1;
141  else
142  res = 0;
143  break;
145  /* If original datum <= indexed one then return match */
146  if (cmp <= 0)
147  res = 0;
148  else
149  res = 1;
150  break;
152  /* If original datum <= indexed one then return match */
153  /* If original datum == indexed one then continue scan */
154  if (cmp < 0)
155  res = 0;
156  else if (cmp == 0)
157  res = -1;
158  else
159  res = 1;
160  break;
161  default:
162  elog(ERROR, "unrecognized strategy number: %d",
163  data->strategy);
164  res = 0;
165  }
166 
168 }
signed int int32
Definition: c.h:482
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1085
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
int b
Definition: isn.c:69
int a
Definition: isn.c:68
const void * data
uintptr_t Datum
Definition: postgres.h:64
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
FmgrInfo * flinfo
Definition: fmgr.h:87

References a, b, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, CallerFInfoFunctionCall2(), cmp(), data, DatumGetInt32(), elog, ERROR, FunctionCallInfoBaseData::flinfo, PG_GET_COLLATION, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_INT32, and res.

◆ gin_btree_consistent()

Datum gin_btree_consistent ( PG_FUNCTION_ARGS  )

Definition at line 172 of file btree_gin.c.

173 {
174  bool *recheck = (bool *) PG_GETARG_POINTER(5);
175 
176  *recheck = false;
177  PG_RETURN_BOOL(true);
178 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_POINTER, and PG_RETURN_BOOL.

◆ gin_btree_extract_query()

static Datum gin_btree_extract_query ( FunctionCallInfo  fcinfo,
bool  is_varlena,
Datum(*)(void)  leftmostvalue,
Datum(*)(FunctionCallInfo typecmp 
)
static

Definition at line 53 of file btree_gin.c.

57 {
58  Datum datum = PG_GETARG_DATUM(0);
59  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
60  StrategyNumber strategy = PG_GETARG_UINT16(2);
61  bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
62  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
63  Datum *entries = (Datum *) palloc(sizeof(Datum));
64  QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
65  bool *ptr_partialmatch;
66 
67  *nentries = 1;
68  ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
69  *ptr_partialmatch = false;
70  if (is_varlena)
71  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
72  data->strategy = strategy;
73  data->datum = datum;
74  data->is_varlena = is_varlena;
75  data->typecmp = typecmp;
76  *extra_data = (Pointer *) palloc(sizeof(Pointer));
77  **extra_data = (Pointer) data;
78 
79  switch (strategy)
80  {
83  entries[0] = leftmostvalue();
84  *ptr_partialmatch = true;
85  break;
88  *ptr_partialmatch = true;
89  /* FALLTHROUGH */
91  entries[0] = datum;
92  break;
93  default:
94  elog(ERROR, "unrecognized strategy number: %d", strategy);
95  }
96 
97  PG_RETURN_POINTER(entries);
98 }
char * Pointer
Definition: c.h:472
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:1317
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uint16 StrategyNumber
Definition: stratnum.h:22

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, data, elog, ERROR, palloc(), PG_DETOAST_DATUM, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_POINTER, and PointerGetDatum().

◆ gin_btree_extract_value()

static Datum gin_btree_extract_value ( FunctionCallInfo  fcinfo,
bool  is_varlena 
)
static

Definition at line 30 of file btree_gin.c.

31 {
32  Datum datum = PG_GETARG_DATUM(0);
33  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
34  Datum *entries = (Datum *) palloc(sizeof(Datum));
35 
36  if (is_varlena)
37  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
38  entries[0] = datum;
39  *nentries = 1;
40 
41  PG_RETURN_POINTER(entries);
42 }

References palloc(), PG_DETOAST_DATUM, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, and PointerGetDatum().

◆ gin_enum_cmp()

Datum gin_enum_cmp ( PG_FUNCTION_ARGS  )

Definition at line 445 of file btree_gin.c.

446 {
447  Oid a = PG_GETARG_OID(0);
448  Oid b = PG_GETARG_OID(1);
449  int res = 0;
450 
451  if (ENUM_IS_LEFTMOST(a))
452  {
453  res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
454  }
455  else if (ENUM_IS_LEFTMOST(b))
456  {
457  res = 1;
458  }
459  else
460  {
462  fcinfo->flinfo,
465  ObjectIdGetDatum(b)));
466  }
467 
469 }
#define ENUM_IS_LEFTMOST(x)
Definition: btree_gin.c:440
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:378
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31

References a, b, CallerFInfoFunctionCall2(), DatumGetInt32(), enum_cmp(), ENUM_IS_LEFTMOST, FunctionCallInfoBaseData::flinfo, ObjectIdGetDatum(), PG_GET_COLLATION, PG_GETARG_OID, PG_RETURN_INT32, and res.

◆ gin_numeric_cmp()

Datum gin_numeric_cmp ( PG_FUNCTION_ARGS  )

Definition at line 398 of file btree_gin.c.

399 {
402  int res = 0;
403 
404  if (NUMERIC_IS_LEFTMOST(a))
405  {
406  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
407  }
408  else if (NUMERIC_IS_LEFTMOST(b))
409  {
410  res = 1;
411  }
412  else
413  {
416  NumericGetDatum(b)));
417  }
418 
420 }
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2517
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:393
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
struct NumericData * Numeric
Definition: numeric.h:54
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73

References a, b, DatumGetInt32(), DirectFunctionCall2, numeric_cmp(), NUMERIC_IS_LEFTMOST, NumericGetDatum(), PG_GETARG_POINTER, PG_RETURN_INT32, and res.

◆ leftmostvalue_bit()

static Datum leftmostvalue_bit ( void  )
static

Definition at line 364 of file btree_gin.c.

365 {
367  CStringGetDatum(""),
368  ObjectIdGetDatum(0),
369  Int32GetDatum(-1));
370 }
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:147

References bit_in(), CStringGetDatum(), DirectFunctionCall3, Int32GetDatum(), and ObjectIdGetDatum().

◆ leftmostvalue_bool()

static Datum leftmostvalue_bool ( void  )
static

Definition at line 504 of file btree_gin.c.

505 {
506  return BoolGetDatum(false);
507 }
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102

References BoolGetDatum().

◆ leftmostvalue_char()

static Datum leftmostvalue_char ( void  )
static

Definition at line 354 of file btree_gin.c.

355 {
356  return CharGetDatum(0);
357 }
static Datum CharGetDatum(char X)
Definition: postgres.h:122

References CharGetDatum().

◆ leftmostvalue_date()

static Datum leftmostvalue_date ( void  )
static

Definition at line 294 of file btree_gin.c.

295 {
297 }
static Datum DateADTGetDatum(DateADT X)
Definition: date.h:72
#define DATEVAL_NOBEGIN
Definition: date.h:36

References DateADTGetDatum(), and DATEVAL_NOBEGIN.

◆ leftmostvalue_enum()

static Datum leftmostvalue_enum ( void  )
static

Definition at line 472 of file btree_gin.c.

473 {
475 }
#define InvalidOid
Definition: postgres_ext.h:36

References InvalidOid, and ObjectIdGetDatum().

◆ leftmostvalue_float4()

static Datum leftmostvalue_float4 ( void  )
static

Definition at line 231 of file btree_gin.c.

232 {
234 }
static float4 get_float4_infinity(void)
Definition: float.h:74
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:475

References Float4GetDatum(), and get_float4_infinity().

◆ leftmostvalue_float8()

static Datum leftmostvalue_float8 ( void  )
static

Definition at line 239 of file btree_gin.c.

240 {
242 }
static float8 get_float8_infinity(void)
Definition: float.h:94
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816

References Float8GetDatum(), and get_float8_infinity().

◆ leftmostvalue_inet()

static Datum leftmostvalue_inet ( void  )
static

Definition at line 334 of file btree_gin.c.

335 {
336  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
337 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:121

References CStringGetDatum(), DirectFunctionCall1, and inet_in().

◆ leftmostvalue_int2()

static Datum leftmostvalue_int2 ( void  )
static

Definition at line 207 of file btree_gin.c.

208 {
209  return Int16GetDatum(SHRT_MIN);
210 }
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172

References Int16GetDatum().

◆ leftmostvalue_int4()

static Datum leftmostvalue_int4 ( void  )
static

Definition at line 215 of file btree_gin.c.

216 {
217  return Int32GetDatum(INT_MIN);
218 }

References Int32GetDatum().

◆ leftmostvalue_int8()

static Datum leftmostvalue_int8 ( void  )
static

Definition at line 223 of file btree_gin.c.

224 {
225  return Int64GetDatum(PG_INT64_MIN);
226 }
#define PG_INT64_MIN
Definition: c.h:570
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807

References Int64GetDatum(), and PG_INT64_MIN.

◆ leftmostvalue_interval()

static Datum leftmostvalue_interval ( void  )
static

Definition at line 302 of file btree_gin.c.

303 {
304  Interval *v = palloc(sizeof(Interval));
305 
306  INTERVAL_NOBEGIN(v);
307 
308  return IntervalPGetDatum(v);
309 }
#define INTERVAL_NOBEGIN(i)
Definition: timestamp.h:175
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58

References INTERVAL_NOBEGIN, IntervalPGetDatum(), and palloc().

◆ leftmostvalue_macaddr()

static Datum leftmostvalue_macaddr ( void  )
static

Definition at line 314 of file btree_gin.c.

315 {
316  macaddr *v = palloc0(sizeof(macaddr));
317 
318  return MacaddrPGetDatum(v);
319 }
void * palloc0(Size size)
Definition: mcxt.c:1347
Definition: inet.h:95
static Datum MacaddrPGetDatum(const macaddr *X)
Definition: inet.h:153

References MacaddrPGetDatum(), and palloc0().

◆ leftmostvalue_macaddr8()

static Datum leftmostvalue_macaddr8 ( void  )
static

Definition at line 324 of file btree_gin.c.

325 {
326  macaddr8 *v = palloc0(sizeof(macaddr8));
327 
328  return Macaddr8PGetDatum(v);
329 }
Definition: inet.h:108
static Datum Macaddr8PGetDatum(const macaddr8 *X)
Definition: inet.h:169

References Macaddr8PGetDatum(), and palloc0().

◆ leftmostvalue_money()

static Datum leftmostvalue_money ( void  )
static

Definition at line 247 of file btree_gin.c.

248 {
249  return Int64GetDatum(PG_INT64_MIN);
250 }

References Int64GetDatum(), and PG_INT64_MIN.

◆ leftmostvalue_name()

static Datum leftmostvalue_name ( void  )
static

Definition at line 494 of file btree_gin.c.

495 {
496  NameData *result = (NameData *) palloc0(NAMEDATALEN);
497 
498  return NameGetDatum(result);
499 }
#define NAMEDATALEN
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
Definition: c.h:720

References NAMEDATALEN, NameGetDatum(), and palloc0().

◆ leftmostvalue_numeric()

static Datum leftmostvalue_numeric ( void  )
static

Definition at line 423 of file btree_gin.c.

424 {
425  return PointerGetDatum(NULL);
426 }

References PointerGetDatum().

◆ leftmostvalue_oid()

static Datum leftmostvalue_oid ( void  )
static

Definition at line 255 of file btree_gin.c.

256 {
257  return ObjectIdGetDatum(0);
258 }

References ObjectIdGetDatum().

◆ leftmostvalue_text()

static Datum leftmostvalue_text ( void  )
static

Definition at line 344 of file btree_gin.c.

345 {
347 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196

References cstring_to_text_with_len(), and PointerGetDatum().

◆ leftmostvalue_time()

static Datum leftmostvalue_time ( void  )
static

Definition at line 273 of file btree_gin.c.

274 {
275  return TimeADTGetDatum(0);
276 }
static Datum TimeADTGetDatum(TimeADT X)
Definition: date.h:78

References TimeADTGetDatum().

◆ leftmostvalue_timestamp()

static Datum leftmostvalue_timestamp ( void  )
static

Definition at line 263 of file btree_gin.c.

264 {
266 }
#define DT_NOBEGIN
Definition: timestamp.h:156
static Datum TimestampGetDatum(Timestamp X)
Definition: timestamp.h:46

References DT_NOBEGIN, and TimestampGetDatum().

◆ leftmostvalue_timetz()

static Datum leftmostvalue_timetz ( void  )
static

Definition at line 281 of file btree_gin.c.

282 {
283  TimeTzADT *v = palloc(sizeof(TimeTzADT));
284 
285  v->time = 0;
286  v->zone = -24 * 3600; /* XXX is that true? */
287 
288  return TimeTzADTPGetDatum(v);
289 }
static Datum TimeTzADTPGetDatum(const TimeTzADT *X)
Definition: date.h:84
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

References palloc(), TimeTzADT::time, TimeTzADTPGetDatum(), and TimeTzADT::zone.

◆ leftmostvalue_uuid()

static Datum leftmostvalue_uuid ( void  )
static

Definition at line 480 of file btree_gin.c.

481 {
482  /*
483  * palloc0 will create the UUID with all zeroes:
484  * "00000000-0000-0000-0000-000000000000"
485  */
486  pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
487 
488  return UUIDPGetDatum(retval);
489 }
Definition: uuid.h:21
static Datum UUIDPGetDatum(const pg_uuid_t *X)
Definition: uuid.h:27

References palloc0(), and UUIDPGetDatum().

◆ leftmostvalue_varbit()

static Datum leftmostvalue_varbit ( void  )
static

Definition at line 375 of file btree_gin.c.

376 {
378  CStringGetDatum(""),
379  ObjectIdGetDatum(0),
380  Int32GetDatum(-1));
381 }
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:452

References CStringGetDatum(), DirectFunctionCall3, Int32GetDatum(), ObjectIdGetDatum(), and varbit_in().

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( gin_btree_consistent  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( gin_enum_cmp  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( gin_numeric_cmp  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 17 of file btree_gin.c.