PostgreSQL Source Code  git master
btree_gin.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gin/btree_gin.c
3  */
4 #include "postgres.h"
5 
6 #include <limits.h>
7 
8 #include "access/stratnum.h"
9 #include "utils/builtins.h"
10 #include "utils/bytea.h"
11 #include "utils/cash.h"
12 #include "utils/date.h"
13 #include "utils/float.h"
14 #include "utils/inet.h"
15 #include "utils/numeric.h"
16 #include "utils/timestamp.h"
17 #include "utils/uuid.h"
18 #include "utils/varbit.h"
19 
21 
22 typedef struct QueryInfo
23 {
26  bool is_varlena;
29 
30 /*** GIN support functions shared by all datatypes ***/
31 
32 static Datum
33 gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
34 {
35  Datum datum = PG_GETARG_DATUM(0);
36  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
37  Datum *entries = (Datum *) palloc(sizeof(Datum));
38 
39  if (is_varlena)
40  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
41  entries[0] = datum;
42  *nentries = 1;
43 
44  PG_RETURN_POINTER(entries);
45 }
46 
47 /*
48  * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
49  * BTEqualStrategyNumber we want to start the index scan at the
50  * supplied query datum, and work forward. For BTLessStrategyNumber
51  * and BTLessEqualStrategyNumber, we need to start at the leftmost
52  * key, and work forward until the supplied query datum (which must be
53  * sent along inside the QueryInfo structure).
54  */
55 static Datum
57  bool is_varlena,
58  Datum (*leftmostvalue) (void),
59  Datum (*typecmp) (FunctionCallInfo))
60 {
61  Datum datum = PG_GETARG_DATUM(0);
62  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
63  StrategyNumber strategy = PG_GETARG_UINT16(2);
64  bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
65  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
66  Datum *entries = (Datum *) palloc(sizeof(Datum));
67  QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
68  bool *ptr_partialmatch;
69 
70  *nentries = 1;
71  ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
72  *ptr_partialmatch = false;
73  if (is_varlena)
74  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
75  data->strategy = strategy;
76  data->datum = datum;
77  data->is_varlena = is_varlena;
78  data->typecmp = typecmp;
79  *extra_data = (Pointer *) palloc(sizeof(Pointer));
80  **extra_data = (Pointer) data;
81 
82  switch (strategy)
83  {
86  entries[0] = leftmostvalue();
87  *ptr_partialmatch = true;
88  break;
91  *ptr_partialmatch = true;
92  /* FALLTHROUGH */
94  entries[0] = datum;
95  break;
96  default:
97  elog(ERROR, "unrecognized strategy number: %d", strategy);
98  }
99 
100  PG_RETURN_POINTER(entries);
101 }
102 
103 /*
104  * Datum a is a value from extract_query method and for BTLess*
105  * strategy it is a left-most value. So, use original datum from QueryInfo
106  * to decide to stop scanning or not. Datum b is always from index.
107  */
108 static Datum
110 {
111  Datum a = PG_GETARG_DATUM(0);
112  Datum b = PG_GETARG_DATUM(1);
114  int32 res,
115  cmp;
116 
118  fcinfo->flinfo,
120  (data->strategy == BTLessStrategyNumber ||
121  data->strategy == BTLessEqualStrategyNumber)
122  ? data->datum : a,
123  b));
124 
125  switch (data->strategy)
126  {
128  /* If original datum > indexed one then return match */
129  if (cmp > 0)
130  res = 0;
131  else
132  res = 1;
133  break;
135  /* The same except equality */
136  if (cmp >= 0)
137  res = 0;
138  else
139  res = 1;
140  break;
142  if (cmp != 0)
143  res = 1;
144  else
145  res = 0;
146  break;
148  /* If original datum <= indexed one then return match */
149  if (cmp <= 0)
150  res = 0;
151  else
152  res = 1;
153  break;
155  /* If original datum <= indexed one then return match */
156  /* If original datum == indexed one then continue scan */
157  if (cmp < 0)
158  res = 0;
159  else if (cmp == 0)
160  res = -1;
161  else
162  res = 1;
163  break;
164  default:
165  elog(ERROR, "unrecognized strategy number: %d",
166  data->strategy);
167  res = 0;
168  }
169 
171 }
172 
174 Datum
176 {
177  bool *recheck = (bool *) PG_GETARG_POINTER(5);
178 
179  *recheck = false;
180  PG_RETURN_BOOL(true);
181 }
182 
183 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
184 
185 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
186 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
187 Datum \
188 gin_extract_value_##type(PG_FUNCTION_ARGS) \
189 { \
190  return gin_btree_extract_value(fcinfo, is_varlena); \
191 } \
192 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
193 Datum \
194 gin_extract_query_##type(PG_FUNCTION_ARGS) \
195 { \
196  return gin_btree_extract_query(fcinfo, \
197  is_varlena, leftmostvalue, typecmp); \
198 } \
199 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
200 Datum \
201 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
202 { \
203  return gin_btree_compare_prefix(fcinfo); \
204 }
205 
206 
207 /*** Datatype specifications ***/
208 
209 static Datum
211 {
212  return Int16GetDatum(SHRT_MIN);
213 }
214 
216 
217 static Datum
219 {
220  return Int32GetDatum(INT_MIN);
221 }
222 
224 
225 static Datum
227 {
228  return Int64GetDatum(PG_INT64_MIN);
229 }
230 
232 
233 static Datum
235 {
237 }
238 
240 
241 static Datum
243 {
245 }
246 
248 
249 static Datum
251 {
252  return Int64GetDatum(PG_INT64_MIN);
253 }
254 
256 
257 static Datum
259 {
260  return ObjectIdGetDatum(0);
261 }
262 
264 
265 static Datum
267 {
269 }
270 
272 
274 
275 static Datum
277 {
278  return TimeADTGetDatum(0);
279 }
280 
282 
283 static Datum
285 {
286  TimeTzADT *v = palloc(sizeof(TimeTzADT));
287 
288  v->time = 0;
289  v->zone = -24 * 3600; /* XXX is that true? */
290 
291  return TimeTzADTPGetDatum(v);
292 }
293 
295 
296 static Datum
298 {
300 }
301 
303 
304 static Datum
306 {
307  Interval *v = palloc(sizeof(Interval));
308 
309  INTERVAL_NOBEGIN(v);
310 
311  return IntervalPGetDatum(v);
312 }
313 
315 
316 static Datum
318 {
319  macaddr *v = palloc0(sizeof(macaddr));
320 
321  return MacaddrPGetDatum(v);
322 }
323 
325 
326 static Datum
328 {
329  macaddr8 *v = palloc0(sizeof(macaddr8));
330 
331  return Macaddr8PGetDatum(v);
332 }
333 
335 
336 static Datum
338 {
339  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
340 }
341 
343 
345 
346 static Datum
348 {
350 }
351 
353 
355 
356 static Datum
358 {
359  return CharGetDatum(0);
360 }
361 
363 
365 
366 static Datum
368 {
370  CStringGetDatum(""),
371  ObjectIdGetDatum(0),
372  Int32GetDatum(-1));
373 }
374 
376 
377 static Datum
379 {
381  CStringGetDatum(""),
382  ObjectIdGetDatum(0),
383  Int32GetDatum(-1));
384 }
385 
387 
388 /*
389  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
390  * (*not* a SQL NULL) to represent that. We can get away with that because
391  * the value returned by our leftmostvalue function will never be stored in
392  * the index nor passed to anything except our compare and prefix-comparison
393  * functions. The same trick could be used for other pass-by-reference types.
394  */
395 
396 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
397 
399 
400 Datum
402 {
405  int res = 0;
406 
407  if (NUMERIC_IS_LEFTMOST(a))
408  {
409  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
410  }
411  else if (NUMERIC_IS_LEFTMOST(b))
412  {
413  res = 1;
414  }
415  else
416  {
419  NumericGetDatum(b)));
420  }
421 
423 }
424 
425 static Datum
427 {
428  return PointerGetDatum(NULL);
429 }
430 
432 
433 /*
434  * Use a similar trick to that used for numeric for enums, since we don't
435  * actually know the leftmost value of any enum without knowing the concrete
436  * type, so we use a dummy leftmost value of InvalidOid.
437  *
438  * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
439  * gets a valid fn_extra to work with. Unlike most other type comparison
440  * routines it needs it, so we can't use DirectFunctionCall2.
441  */
442 
443 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
444 
446 
447 Datum
449 {
450  Oid a = PG_GETARG_OID(0);
451  Oid b = PG_GETARG_OID(1);
452  int res = 0;
453 
454  if (ENUM_IS_LEFTMOST(a))
455  {
456  res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
457  }
458  else if (ENUM_IS_LEFTMOST(b))
459  {
460  res = 1;
461  }
462  else
463  {
465  fcinfo->flinfo,
468  ObjectIdGetDatum(b)));
469  }
470 
472 }
473 
474 static Datum
476 {
478 }
479 
481 
482 static Datum
484 {
485  /*
486  * palloc0 will create the UUID with all zeroes:
487  * "00000000-0000-0000-0000-000000000000"
488  */
489  pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
490 
491  return UUIDPGetDatum(retval);
492 }
493 
495 
496 static Datum
498 {
499  NameData *result = (NameData *) palloc0(NAMEDATALEN);
500 
501  return NameGetDatum(result);
502 }
503 
505 
506 static Datum
508 {
509  return BoolGetDatum(false);
510 }
511 
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2398
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2265
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2572
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:258
static Datum gin_btree_extract_query(FunctionCallInfo fcinfo, bool is_varlena, Datum(*leftmostvalue)(void), Datum(*typecmp)(FunctionCallInfo))
Definition: btree_gin.c:56
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:401
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:266
#define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp)
Definition: btree_gin.c:185
static Datum leftmostvalue_int4(void)
Definition: btree_gin.c:218
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:378
static Datum leftmostvalue_float8(void)
Definition: btree_gin.c:242
static Datum leftmostvalue_time(void)
Definition: btree_gin.c:276
static Datum leftmostvalue_int2(void)
Definition: btree_gin.c:210
Datum gin_enum_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:448
PG_MODULE_MAGIC
Definition: btree_gin.c:20
static Datum leftmostvalue_macaddr8(void)
Definition: btree_gin.c:327
static Datum leftmostvalue_text(void)
Definition: btree_gin.c:347
struct QueryInfo QueryInfo
static Datum leftmostvalue_enum(void)
Definition: btree_gin.c:475
static Datum leftmostvalue_bool(void)
Definition: btree_gin.c:507
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:396
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:305
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:234
static Datum leftmostvalue_name(void)
Definition: btree_gin.c:497
static Datum leftmostvalue_inet(void)
Definition: btree_gin.c:337
static Datum leftmostvalue_char(void)
Definition: btree_gin.c:357
static Datum leftmostvalue_uuid(void)
Definition: btree_gin.c:483
static Datum leftmostvalue_macaddr(void)
Definition: btree_gin.c:317
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:175
static Datum leftmostvalue_timetz(void)
Definition: btree_gin.c:284
#define ENUM_IS_LEFTMOST(x)
Definition: btree_gin.c:443
static Datum leftmostvalue_money(void)
Definition: btree_gin.c:250
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:426
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:109
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:33
static Datum leftmostvalue_int8(void)
Definition: btree_gin.c:226
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:297
PG_FUNCTION_INFO_V1(gin_btree_consistent)
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:367
signed char int8
Definition: c.h:481
signed int int32
Definition: c.h:483
char * Pointer
Definition: c.h:472
double float8
Definition: c.h:619
#define PG_INT64_MIN
Definition: c.h:580
float float4
Definition: c.h:618
Datum cash_cmp(PG_FUNCTION_ARGS)
Definition: cash.c:593
#define DT_NOBEGIN
Definition: timestamp.h:156
#define INTERVAL_NOBEGIN(i)
Definition: timestamp.h:175
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:437
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1734
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2524
static Datum DateADTGetDatum(DateADT X)
Definition: date.h:72
static Datum TimeTzADTPGetDatum(const TimeTzADT *X)
Definition: date.h:84
#define DATEVAL_NOBEGIN
Definition: date.h:36
static Datum TimeADTGetDatum(TimeADT X)
Definition: date.h:78
#define ERROR
Definition: elog.h:39
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:378
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:879
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:973
static float4 get_float4_infinity(void)
Definition: float.h:74
static float8 get_float8_infinity(void)
Definition: float.h:94
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1085
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
struct FunctionCallInfoBaseData * FunctionCallInfo
Definition: fmgr.h:38
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int b
Definition: isn.c:70
int a
Definition: isn.c:69
Datum macaddr8_cmp(PG_FUNCTION_ARGS)
Definition: mac8.c:324
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition: mac.c:197
void * palloc0(Size size)
Definition: mcxt.c:1257
void * palloc(Size size)
Definition: mcxt.c:1226
Datum btnamecmp(PG_FUNCTION_ARGS)
Definition: name.c:202
Datum btint4cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:109
Datum btboolcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:73
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:82
Datum btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:259
Datum btcharcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:320
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:132
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:121
Datum network_cmp(PG_FUNCTION_ARGS)
Definition: network.c:425
struct NumericData * Numeric
Definition: numeric.h:53
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:72
#define NAMEDATALEN
const void * data
long date
Definition: pgtypes_date.h:9
int64 timestamp
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:475
uintptr_t Datum
Definition: postgres.h:64
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
static Datum CharGetDatum(char X)
Definition: postgres.h:122
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
uint16 StrategyNumber
Definition: stratnum.h:22
#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
bool is_varlena
Definition: btree_gin.c:26
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:27
Datum datum
Definition: btree_gin.c:25
StrategyNumber strategy
Definition: btree_gin.c:24
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30
Definition: inet.h:53
Definition: inet.h:108
Definition: inet.h:95
Definition: c.h:730
Definition: uuid.h:21
Definition: c.h:676
static Datum MacaddrPGetDatum(const macaddr *X)
Definition: inet.h:153
static Datum Macaddr8PGetDatum(const macaddr8 *X)
Definition: inet.h:169
static Datum TimestampGetDatum(Timestamp X)
Definition: timestamp.h:46
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58
Datum uuid_cmp(PG_FUNCTION_ARGS)
Definition: uuid.c:224
static Datum UUIDPGetDatum(const pg_uuid_t *X)
Definition: uuid.h:27
Datum bitcmp(PG_FUNCTION_ARGS)
Definition: varbit.c:949
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:391
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:742
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:147
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:452
Datum bpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:272
Datum bpcharcmp(PG_FUNCTION_ARGS)
Definition: varchar.c:918
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3937
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1830
const char * name