PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btree_gin.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/stratnum.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/cash.h"
#include "utils/date.h"
#include "utils/inet.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
#include "utils/varbit.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)
 

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_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)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

#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:55
PG_FUNCTION_INFO_V1(gin_btree_consistent)
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:32
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:107

Definition at line 183 of file btree_gin.c.

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

Definition at line 383 of file btree_gin.c.

Referenced by gin_numeric_cmp().

Typedef Documentation

Function Documentation

static Datum gin_btree_compare_prefix ( FunctionCallInfo  fcinfo)
static

Definition at line 107 of file btree_gin.c.

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, cmp(), QueryInfo::datum, DatumGetInt32, DirectFunctionCall2Coll(), elog, ERROR, PG_GET_COLLATION, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_INT32, QueryInfo::strategy, and QueryInfo::typecmp.

108 {
109  Datum a = PG_GETARG_DATUM(0);
110  Datum b = PG_GETARG_DATUM(1);
111  QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
112  int32 res,
113  cmp;
114 
116  data->typecmp,
118  (data->strategy == BTLessStrategyNumber ||
120  ? data->datum : a,
121  b));
122 
123  switch (data->strategy)
124  {
126  /* If original datum > indexed one then return match */
127  if (cmp > 0)
128  res = 0;
129  else
130  res = 1;
131  break;
133  /* The same except equality */
134  if (cmp >= 0)
135  res = 0;
136  else
137  res = 1;
138  break;
140  if (cmp != 0)
141  res = 1;
142  else
143  res = 0;
144  break;
146  /* If original datum <= indexed one then return match */
147  if (cmp <= 0)
148  res = 0;
149  else
150  res = 1;
151  break;
153  /* If original datum <= indexed one then return match */
154  /* If original datum == indexed one then continue scan */
155  if (cmp < 0)
156  res = 0;
157  else if (cmp == 0)
158  res = -1;
159  else
160  res = 1;
161  break;
162  default:
163  elog(ERROR, "unrecognized strategy number: %d",
164  data->strategy);
165  res = 0;
166  }
167 
168  PG_RETURN_INT32(res);
169 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_GET_COLLATION()
Definition: fmgr.h:155
signed int int32
Definition: c.h:253
Datum datum
Definition: btree_gin.c:23
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
StrategyNumber strategy
Definition: btree_gin.c:22
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:25
#define ERROR
Definition: elog.h:43
uintptr_t Datum
Definition: postgres.h:374
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1036
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum gin_btree_consistent ( PG_FUNCTION_ARGS  )

Definition at line 173 of file btree_gin.c.

References PG_GETARG_POINTER, and PG_RETURN_BOOL.

174 {
175  bool *recheck = (bool *) PG_GETARG_POINTER(5);
176 
177  *recheck = false;
178  PG_RETURN_BOOL(true);
179 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static Datum gin_btree_extract_query ( FunctionCallInfo  fcinfo,
bool  is_varlena,
Datum(*)(void)  leftmostvalue,
Datum(*)(FunctionCallInfo typecmp 
)
static

Definition at line 55 of file btree_gin.c.

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, QueryInfo::datum, elog, ERROR, QueryInfo::is_varlena, palloc(), PG_DETOAST_DATUM, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_POINTER, PointerGetDatum, QueryInfo::strategy, and QueryInfo::typecmp.

59 {
60  Datum datum = PG_GETARG_DATUM(0);
61  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
62  StrategyNumber strategy = PG_GETARG_UINT16(2);
63  bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
64  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
65  Datum *entries = (Datum *) palloc(sizeof(Datum));
66  QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
67  bool *ptr_partialmatch;
68 
69  *nentries = 1;
70  ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
71  *ptr_partialmatch = false;
72  if (is_varlena)
73  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
74  data->strategy = strategy;
75  data->datum = datum;
76  data->is_varlena = is_varlena;
77  data->typecmp = typecmp;
78  *extra_data = (Pointer *) palloc(sizeof(Pointer));
79  **extra_data = (Pointer) data;
80 
81  switch (strategy)
82  {
85  entries[0] = leftmostvalue();
86  *ptr_partialmatch = true;
87  break;
90  *ptr_partialmatch = true;
92  entries[0] = datum;
93  break;
94  default:
95  elog(ERROR, "unrecognized strategy number: %d", strategy);
96  }
97 
98  PG_RETURN_POINTER(entries);
99 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
bool is_varlena
Definition: btree_gin.c:24
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
uint16 StrategyNumber
Definition: stratnum.h:22
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
signed int int32
Definition: c.h:253
Datum datum
Definition: btree_gin.c:23
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
StrategyNumber strategy
Definition: btree_gin.c:22
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:25
char * Pointer
Definition: c.h:242
#define ERROR
Definition: elog.h:43
uintptr_t Datum
Definition: postgres.h:374
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
void * palloc(Size size)
Definition: mcxt.c:891
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:196
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static Datum gin_btree_extract_value ( FunctionCallInfo  fcinfo,
bool  is_varlena 
)
static

Definition at line 32 of file btree_gin.c.

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

33 {
34  Datum datum = PG_GETARG_DATUM(0);
35  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
36  Datum *entries = (Datum *) palloc(sizeof(Datum));
37 
38  if (is_varlena)
39  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
40  entries[0] = datum;
41  *nentries = 1;
42 
43  PG_RETURN_POINTER(entries);
44 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
signed int int32
Definition: c.h:253
uintptr_t Datum
Definition: postgres.h:374
void * palloc(Size size)
Definition: mcxt.c:891
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:196
Datum gin_numeric_cmp ( PG_FUNCTION_ARGS  )

Definition at line 388 of file btree_gin.c.

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

389 {
392  int res = 0;
393 
394  if (NUMERIC_IS_LEFTMOST(a))
395  {
396  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
397  }
398  else if (NUMERIC_IS_LEFTMOST(b))
399  {
400  res = 1;
401  }
402  else
403  {
405  NumericGetDatum(a),
406  NumericGetDatum(b)));
407  }
408 
409  PG_RETURN_INT32(res);
410 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2020
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
struct NumericData * Numeric
Definition: numeric.h:43
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:383
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
static Datum leftmostvalue_bit ( void  )
static

Definition at line 354 of file btree_gin.c.

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

355 {
357  CStringGetDatum(""),
358  ObjectIdGetDatum(0),
359  Int32GetDatum(-1));
360 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:113
#define CStringGetDatum(X)
Definition: postgres.h:586
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
#define Int32GetDatum(X)
Definition: postgres.h:487
static Datum leftmostvalue_char ( void  )
static

Definition at line 344 of file btree_gin.c.

References CharGetDatum.

345 {
346  return CharGetDatum(SCHAR_MIN);
347 }
#define CharGetDatum(X)
Definition: postgres.h:424
static Datum leftmostvalue_date ( void  )
static

Definition at line 295 of file btree_gin.c.

References DateADTGetDatum, and DATEVAL_NOBEGIN.

296 {
298 }
#define DateADTGetDatum(X)
Definition: date.h:56
#define DATEVAL_NOBEGIN
Definition: date.h:35
static Datum leftmostvalue_float4 ( void  )
static

Definition at line 232 of file btree_gin.c.

References Float4GetDatum(), and get_float4_infinity().

233 {
235 }
float get_float4_infinity(void)
Definition: float.c:146
Datum Float4GetDatum(float4 X)
Definition: fmgr.c:2114
static Datum leftmostvalue_float8 ( void  )
static

Definition at line 240 of file btree_gin.c.

References Float8GetDatum(), and get_float8_infinity().

241 {
243 }
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2126
double get_float8_infinity(void)
Definition: float.c:121
static Datum leftmostvalue_inet ( void  )
static

Definition at line 326 of file btree_gin.c.

References CStringGetDatum, DirectFunctionCall1, and inet_in().

327 {
328  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
329 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
#define CStringGetDatum(X)
Definition: postgres.h:586
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:80
static Datum leftmostvalue_int2 ( void  )
static

Definition at line 208 of file btree_gin.c.

References Int16GetDatum.

209 {
210  return Int16GetDatum(SHRT_MIN);
211 }
#define Int16GetDatum(X)
Definition: postgres.h:459
static Datum leftmostvalue_int4 ( void  )
static

Definition at line 216 of file btree_gin.c.

References Int32GetDatum.

217 {
218  return Int32GetDatum(INT_MIN);
219 }
#define Int32GetDatum(X)
Definition: postgres.h:487
static Datum leftmostvalue_int8 ( void  )
static

Definition at line 224 of file btree_gin.c.

References Int64GetDatum(), and PG_INT64_MIN.

225 {
226  return Int64GetDatum(PG_INT64_MIN);
227 }
#define PG_INT64_MIN
Definition: c.h:339
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2102
static Datum leftmostvalue_interval ( void  )
static

Definition at line 303 of file btree_gin.c.

References Interval::day, DT_NOBEGIN, IntervalPGetDatum, Interval::month, palloc(), and Interval::time.

304 {
305  Interval *v = palloc(sizeof(Interval));
306 
307  v->time = DT_NOBEGIN;
308  v->day = 0;
309  v->month = 0;
310  return IntervalPGetDatum(v);
311 }
int32 day
Definition: timestamp.h:47
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:891
#define DT_NOBEGIN
Definition: timestamp.h:109
static Datum leftmostvalue_macaddr ( void  )
static

Definition at line 316 of file btree_gin.c.

References MacaddrPGetDatum, and palloc0().

317 {
318  macaddr *v = palloc0(sizeof(macaddr));
319 
320  return MacaddrPGetDatum(v);
321 }
#define MacaddrPGetDatum(X)
Definition: inet.h:115
void * palloc0(Size size)
Definition: mcxt.c:920
Definition: inet.h:94
static Datum leftmostvalue_money ( void  )
static

Definition at line 248 of file btree_gin.c.

References Int64GetDatum(), and PG_INT64_MIN.

249 {
250  return Int64GetDatum(PG_INT64_MIN);
251 }
#define PG_INT64_MIN
Definition: c.h:339
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2102
static Datum leftmostvalue_numeric ( void  )
static

Definition at line 413 of file btree_gin.c.

References NULL, and PointerGetDatum.

414 {
415  return PointerGetDatum(NULL);
416 }
#define PointerGetDatum(X)
Definition: postgres.h:564
#define NULL
Definition: c.h:226
static Datum leftmostvalue_oid ( void  )
static

Definition at line 256 of file btree_gin.c.

References ObjectIdGetDatum.

257 {
258  return ObjectIdGetDatum(0);
259 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
static Datum leftmostvalue_text ( void  )
static

Definition at line 336 of file btree_gin.c.

References cstring_to_text_with_len(), and PointerGetDatum.

337 {
339 }
#define PointerGetDatum(X)
Definition: postgres.h:564
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
static Datum leftmostvalue_time ( void  )
static

Definition at line 274 of file btree_gin.c.

References TimeADTGetDatum.

275 {
276  return TimeADTGetDatum(0);
277 }
#define TimeADTGetDatum(X)
Definition: date.h:57
static Datum leftmostvalue_timestamp ( void  )
static

Definition at line 264 of file btree_gin.c.

References DT_NOBEGIN, and TimestampGetDatum.

265 {
267 }
#define TimestampGetDatum(X)
Definition: timestamp.h:31
#define DT_NOBEGIN
Definition: timestamp.h:109
static Datum leftmostvalue_timetz ( void  )
static

Definition at line 282 of file btree_gin.c.

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

283 {
284  TimeTzADT *v = palloc(sizeof(TimeTzADT));
285 
286  v->time = 0;
287  v->zone = -24 * 3600; /* XXX is that true? */
288 
289  return TimeTzADTPGetDatum(v);
290 }
TimeADT time
Definition: date.h:28
#define TimeTzADTPGetDatum(X)
Definition: date.h:58
int32 zone
Definition: date.h:29
void * palloc(Size size)
Definition: mcxt.c:891
Definition: date.h:26
static Datum leftmostvalue_varbit ( void  )
static

Definition at line 365 of file btree_gin.c.

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

366 {
368  CStringGetDatum(""),
369  ObjectIdGetDatum(0),
370  Int32GetDatum(-1));
371 }
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:429
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define CStringGetDatum(X)
Definition: postgres.h:586
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
#define Int32GetDatum(X)
Definition: postgres.h:487
PG_FUNCTION_INFO_V1 ( gin_btree_consistent  )
PG_FUNCTION_INFO_V1 ( gin_numeric_cmp  )

Variable Documentation

PG_MODULE_MAGIC

Definition at line 18 of file btree_gin.c.