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;
28 } QueryInfo;
29 
30 /*** GIN support functions shared by all datatypes ***/
31 
32 static Datum
34 {
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),
60 {
62  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
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);
113  QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
114  int32 res,
115  cmp;
116 
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  fcinfo->flinfo,
468  ObjectIdGetDatum(a),
469  ObjectIdGetDatum(b)));
470  }
471 
472  PG_RETURN_INT32(res);
473 }
474 
475 static Datum
477 {
479 }
480 
482 
483 static Datum
485 {
486  /*
487  * palloc0 will create the UUID with all zeroes:
488  * "00000000-0000-0000-0000-000000000000"
489  */
490  pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
491 
492  return UUIDPGetDatum(retval);
493 }
494 
496 
497 static Datum
499 {
500  NameData *result = (NameData *) palloc0(NAMEDATALEN);
501 
502  return NameGetDatum(result);
503 }
504 
506 
507 static Datum
509 {
510  return BoolGetDatum(false);
511 }
512 
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:681
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:297
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2302
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:402
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
Datum macaddr8_cmp(PG_FUNCTION_ARGS)
Definition: mac8.c:333
#define NameGetDatum(X)
Definition: postgres.h:595
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:451
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
static float8 get_float8_infinity(void)
Definition: float.h:93
bool is_varlena
Definition: btree_gin.c:26
TimeADT time
Definition: date.h:29
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:368
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2148
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:978
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:1088
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#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:948
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2262
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
#define Int16GetDatum(X)
Definition: postgres.h:451
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
uint16 StrategyNumber
Definition: stratnum.h:22
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:884
#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:276
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:56
static Datum leftmostvalue_macaddr(void)
Definition: btree_gin.c:318
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
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:2438
#define PG_GET_COLLATION()
Definition: fmgr.h:198
Datum btboolcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:73
signed int int32
Definition: c.h:417
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:427
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1710
Datum datum
Definition: btree_gin.c:25
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:24
Datum gin_enum_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:449
Datum btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:267
#define NAMEDATALEN
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:305
char * Pointer
Definition: c.h:406
static Datum leftmostvalue_macaddr8(void)
Definition: btree_gin.c:328
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
Datum btcharcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:328
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:553
Definition: inet.h:107
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1558
Datum btnamecmp(PG_FUNCTION_ARGS)
Definition: name.c:202
static Datum leftmostvalue_timetz(void)
Definition: btree_gin.c:284
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:146
#define UUIDPGetDatum(X)
Definition: uuid.h:26
int32 zone
Definition: date.h:30
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:741
Definition: c.h:671
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
#define PG_INT64_MIN
Definition: c.h:514
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define DATEVAL_NOBEGIN
Definition: date.h:36
#define CStringGetDatum(X)
Definition: postgres.h:578
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:1701
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:27
Datum btint4cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:109
#define TimestampGetDatum(X)
Definition: timestamp.h:31
static Datum leftmostvalue_uuid(void)
Definition: btree_gin.c:484
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
int32 month
Definition: timestamp.h:48
signed char int8
Definition: c.h:415
float float4
Definition: c.h:552
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:258
void * palloc0(Size size)
Definition: mcxt.c:981
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:397
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
struct FunctionCallInfoBaseData * FunctionCallInfo
Definition: fmgr.h:38
#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:402
static Datum leftmostvalue_bool(void)
Definition: btree_gin.c:508
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:379
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:4197
#define InvalidOid
Definition: postgres_ext.h:36
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:390
static Datum leftmostvalue_name(void)
Definition: btree_gin.c:498
struct QueryInfo QueryInfo
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:266
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:372
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:234
static Datum leftmostvalue_enum(void)
Definition: btree_gin.c:476
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:428
PG_FUNCTION_INFO_V1(gin_btree_consistent)
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition: mac.c:197
const char * name
Definition: encode.c:561
#define CharGetDatum(X)
Definition: postgres.h:416
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:146
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1967
#define Int32GetDatum(X)
Definition: postgres.h:479
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:33
Definition: uuid.h:20
void * palloc(Size size)
Definition: mcxt.c:950
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:175
#define elog(elevel,...)
Definition: elog.h:228
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:82
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
Definition: c.h:617
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Datum bpcharcmp(PG_FUNCTION_ARGS)
Definition: varchar.c:902
Datum network_cmp(PG_FUNCTION_ARGS)
Definition: network.c:426
Datum uuid_cmp(PG_FUNCTION_ARGS)
Definition: uuid.c:225
#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:269
PG_MODULE_MAGIC
Definition: btree_gin.c:20
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:628
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:109
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
static float4 get_float4_infinity(void)
Definition: float.h:73