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/inet.h"
14 #include "utils/numeric.h"
15 #include "utils/timestamp.h"
16 #include "utils/varbit.h"
17 #include "utils/uuid.h"
18 
20 
21 typedef struct QueryInfo
22 {
25  bool is_varlena;
27 } QueryInfo;
28 
29 /*** GIN support functions shared by all datatypes ***/
30 
31 static Datum
33 {
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 }
45 
46 /*
47  * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
48  * BTEqualStrategyNumber we want to start the index scan at the
49  * supplied query datum, and work forward. For BTLessStrategyNumber
50  * and BTLessEqualStrategyNumber, we need to start at the leftmost
51  * key, and work forward until the supplied query datum (which must be
52  * sent along inside the QueryInfo structure).
53  */
54 static Datum
56  bool is_varlena,
57  Datum (*leftmostvalue) (void),
59 {
61  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
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;
91  /* FALLTHROUGH */
93  entries[0] = datum;
94  break;
95  default:
96  elog(ERROR, "unrecognized strategy number: %d", strategy);
97  }
98 
99  PG_RETURN_POINTER(entries);
100 }
101 
102 /*
103  * Datum a is a value from extract_query method and for BTLess*
104  * strategy it is a left-most value. So, use original datum from QueryInfo
105  * to decide to stop scanning or not. Datum b is always from index.
106  */
107 static Datum
109 {
110  Datum a = PG_GETARG_DATUM(0);
111  Datum b = PG_GETARG_DATUM(1);
112  QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
113  int32 res,
114  cmp;
115 
117  data->typecmp,
118  fcinfo->flinfo,
120  (data->strategy == BTLessStrategyNumber ||
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 
170  PG_RETURN_INT32(res);
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  v->time = DT_NOBEGIN;
310  v->day = 0;
311  v->month = 0;
312  return IntervalPGetDatum(v);
313 }
314 
316 
317 static Datum
319 {
320  macaddr *v = palloc0(sizeof(macaddr));
321 
322  return MacaddrPGetDatum(v);
323 }
324 
326 
327 static Datum
329 {
330  macaddr8 *v = palloc0(sizeof(macaddr8));
331 
332  return Macaddr8PGetDatum(v);
333 }
334 
336 
337 static Datum
339 {
340  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
341 }
342 
344 
346 
347 static Datum
349 {
351 }
352 
354 
356 
357 static Datum
359 {
360  return CharGetDatum(SCHAR_MIN);
361 }
362 
364 
366 
367 static Datum
369 {
371  CStringGetDatum(""),
372  ObjectIdGetDatum(0),
373  Int32GetDatum(-1));
374 }
375 
377 
378 static Datum
380 {
382  CStringGetDatum(""),
383  ObjectIdGetDatum(0),
384  Int32GetDatum(-1));
385 }
386 
388 
389 /*
390  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
391  * (*not* a SQL NULL) to represent that. We can get away with that because
392  * the value returned by our leftmostvalue function will never be stored in
393  * the index nor passed to anything except our compare and prefix-comparison
394  * functions. The same trick could be used for other pass-by-reference types.
395  */
396 
397 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
398 
400 
401 Datum
403 {
406  int res = 0;
407 
408  if (NUMERIC_IS_LEFTMOST(a))
409  {
410  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
411  }
412  else if (NUMERIC_IS_LEFTMOST(b))
413  {
414  res = 1;
415  }
416  else
417  {
419  NumericGetDatum(a),
420  NumericGetDatum(b)));
421  }
422 
423  PG_RETURN_INT32(res);
424 }
425 
426 static Datum
428 {
429  return PointerGetDatum(NULL);
430 }
431 
433 
434 /*
435  * Use a similar trick to that used for numeric for enums, since we don't
436  * actually know the leftmost value of any enum without knowing the concrete
437  * type, so we use a dummy leftmost value of InvalidOid.
438  *
439  * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
440  * gets a valid fn_extra to work with. Unlike most other type comparison
441  * routines it needs it, so we can't use DirectFunctionCall2.
442  */
443 
444 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
445 
447 
448 Datum
450 {
451  Oid a = PG_GETARG_OID(0);
452  Oid b = PG_GETARG_OID(1);
453  int res = 0;
454 
455  if (ENUM_IS_LEFTMOST(a))
456  {
457  res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
458  }
459  else if (ENUM_IS_LEFTMOST(b))
460  {
461  res = 1;
462  }
463  else
464  {
466  enum_cmp,
467  fcinfo->flinfo,
469  ObjectIdGetDatum(a),
470  ObjectIdGetDatum(b)));
471  }
472 
473  PG_RETURN_INT32(res);
474 }
475 
476 static Datum
478 {
480 }
481 
483 
484 static Datum
486 {
487  /*
488  * palloc0 will create the UUID with all zeroes:
489  * "00000000-0000-0000-0000-000000000000"
490  */
491  pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
492 
493  return UUIDPGetDatum(retval);
494 }
495 
497 
498 static Datum
500 {
501  NameData *result = (NameData *) palloc0(NAMEDATALEN);
502 
503  return NameGetDatum(result);
504 }
505 
507 
508 static Datum
510 {
511  return BoolGetDatum(false);
512 }
513 
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:297
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2263
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:402
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
Datum macaddr8_cmp(PG_FUNCTION_ARGS)
Definition: mac8.c:333
#define NameGetDatum(X)
Definition: postgres.h:580
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:430
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
bool is_varlena
Definition: btree_gin.c:25
TimeADT time
Definition: date.h:29
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:368
#define DatumGetInt32(X)
Definition: postgres.h:457
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2083
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:1133
static Datum leftmostvalue_int8(void)
Definition: btree_gin.c:226
int64 timestamp
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1086
#define PointerGetDatum(X)
Definition: postgres.h:541
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define MacaddrPGetDatum(X)
Definition: inet.h:132
static Datum leftmostvalue_inet(void)
Definition: btree_gin.c:338
Datum bitcmp(PG_FUNCTION_ARGS)
Definition: varbit.c:929
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2032
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
#define Int16GetDatum(X)
Definition: postgres.h:436
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
uint16 StrategyNumber
Definition: stratnum.h:22
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:1019
#define DateADTGetDatum(X)
Definition: date.h:57
static Datum leftmostvalue_text(void)
Definition: btree_gin.c:348
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
long date
Definition: pgtypes_date.h:9
static Datum gin_btree_extract_query(FunctionCallInfo fcinfo, bool is_varlena, Datum(*leftmostvalue)(void), Datum(*typecmp)(FunctionCallInfo))
Definition: btree_gin.c:55
static Datum leftmostvalue_macaddr(void)
Definition: btree_gin.c:318
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
unsigned int Oid
Definition: postgres_ext.h:31
static Datum leftmostvalue_char(void)
Definition: btree_gin.c:358
struct NumericData * Numeric
Definition: numeric.h:43
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2394
#define PG_GET_COLLATION()
Definition: fmgr.h:168
Datum btboolcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:56
signed int int32
Definition: c.h:313
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:427
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1901
Datum datum
Definition: btree_gin.c:24
static Datum leftmostvalue_time(void)
Definition: btree_gin.c:276
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
int32 day
Definition: timestamp.h:47
StrategyNumber strategy
Definition: btree_gin.c:23
float get_float4_infinity(void)
Definition: float.c:143
Datum gin_enum_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:449
Datum btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:250
#define NAMEDATALEN
FmgrInfo * flinfo
Definition: fmgr.h:79
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:305
char * Pointer
Definition: c.h:302
static Datum leftmostvalue_macaddr8(void)
Definition: btree_gin.c:328
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
Datum btcharcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:311
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
Definition: inet.h:107
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1519
static Datum leftmostvalue_timetz(void)
Definition: btree_gin.c:284
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:114
#define UUIDPGetDatum(X)
Definition: uuid.h:26
int32 zone
Definition: date.h:30
struct FunctionCallInfoData * FunctionCallInfo
Definition: fmgr.h:38
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:715
Datum Float4GetDatum(float4 X)
Definition: fmgr.c:1889
Definition: c.h:570
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_INT64_MIN
Definition: c.h:410
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
#define DATEVAL_NOBEGIN
Definition: date.h:36
#define CStringGetDatum(X)
Definition: postgres.h:563
Definition: inet.h:52
TimeOffset time
Definition: timestamp.h:45
#define ENUM_IS_LEFTMOST(x)
Definition: btree_gin.c:444
static Datum leftmostvalue_money(void)
Definition: btree_gin.c:250
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1877
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:26
Datum btint4cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:92
#define TimestampGetDatum(X)
Definition: timestamp.h:31
static Datum leftmostvalue_uuid(void)
Definition: btree_gin.c:485
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:594
double get_float8_infinity(void)
Definition: float.c:118
int32 month
Definition: timestamp.h:48
signed char int8
Definition: c.h:311
float float4
Definition: c.h:457
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:258
void * palloc0(Size size)
Definition: mcxt.c:955
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:397
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:367
#define Macaddr8PGetDatum(X)
Definition: inet.h:138
#define TimeADTGetDatum(X)
Definition: date.h:58
static Datum leftmostvalue_int4(void)
Definition: btree_gin.c:218
#define BoolGetDatum(X)
Definition: postgres.h:387
static Datum leftmostvalue_bool(void)
Definition: btree_gin.c:509
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:379
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3659
#define InvalidOid
Definition: postgres_ext.h:36
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:362
static Datum leftmostvalue_name(void)
Definition: btree_gin.c:499
struct QueryInfo QueryInfo
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:266
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:294
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:80
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:234
static Datum leftmostvalue_enum(void)
Definition: btree_gin.c:477
static Datum leftmostvalue_float8(void)
Definition: btree_gin.c:242
Definition: inet.h:94
#define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp)
Definition: btree_gin.c:185
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:441
PG_FUNCTION_INFO_V1(gin_btree_consistent)
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition: mac.c:197
const char * name
Definition: encode.c:521
#define CharGetDatum(X)
Definition: postgres.h:401
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:242
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:129
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1793
#define Int32GetDatum(X)
Definition: postgres.h:464
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:32
Definition: uuid.h:20
void * palloc(Size size)
Definition: mcxt.c:924
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:175
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:65
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:210
Definition: c.h:516
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
Datum bpcharcmp(PG_FUNCTION_ARGS)
Definition: varchar.c:849
Datum network_cmp(PG_FUNCTION_ARGS)
Definition: network.c:384
Datum uuid_cmp(PG_FUNCTION_ARGS)
Definition: uuid.c:225
#define elog
Definition: elog.h:219
#define DT_NOBEGIN
Definition: timestamp.h:109
Datum cash_cmp(PG_FUNCTION_ARGS)
Definition: cash.c:593
Datum bpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:267
PG_MODULE_MAGIC
Definition: btree_gin.c:19
static Datum leftmostvalue_int2(void)
Definition: btree_gin.c:210
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: date.h:27
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:108
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum btnamecmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:321