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  data->typecmp,
119  fcinfo->flinfo,
121  (data->strategy == BTLessStrategyNumber ||
123  ? data->datum : a,
124  b));
125 
126  switch (data->strategy)
127  {
129  /* If original datum > indexed one then return match */
130  if (cmp > 0)
131  res = 0;
132  else
133  res = 1;
134  break;
136  /* The same except equality */
137  if (cmp >= 0)
138  res = 0;
139  else
140  res = 1;
141  break;
143  if (cmp != 0)
144  res = 1;
145  else
146  res = 0;
147  break;
149  /* If original datum <= indexed one then return match */
150  if (cmp <= 0)
151  res = 0;
152  else
153  res = 1;
154  break;
156  /* If original datum <= indexed one then return match */
157  /* If original datum == indexed one then continue scan */
158  if (cmp < 0)
159  res = 0;
160  else if (cmp == 0)
161  res = -1;
162  else
163  res = 1;
164  break;
165  default:
166  elog(ERROR, "unrecognized strategy number: %d",
167  data->strategy);
168  res = 0;
169  }
170 
171  PG_RETURN_INT32(res);
172 }
173 
175 Datum
177 {
178  bool *recheck = (bool *) PG_GETARG_POINTER(5);
179 
180  *recheck = false;
181  PG_RETURN_BOOL(true);
182 }
183 
184 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
185 
186 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
187 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
188 Datum \
189 gin_extract_value_##type(PG_FUNCTION_ARGS) \
190 { \
191  return gin_btree_extract_value(fcinfo, is_varlena); \
192 } \
193 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
194 Datum \
195 gin_extract_query_##type(PG_FUNCTION_ARGS) \
196 { \
197  return gin_btree_extract_query(fcinfo, \
198  is_varlena, leftmostvalue, typecmp); \
199 } \
200 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
201 Datum \
202 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
203 { \
204  return gin_btree_compare_prefix(fcinfo); \
205 }
206 
207 
208 /*** Datatype specifications ***/
209 
210 static Datum
212 {
213  return Int16GetDatum(SHRT_MIN);
214 }
215 
217 
218 static Datum
220 {
221  return Int32GetDatum(INT_MIN);
222 }
223 
225 
226 static Datum
228 {
229  return Int64GetDatum(PG_INT64_MIN);
230 }
231 
233 
234 static Datum
236 {
238 }
239 
241 
242 static Datum
244 {
246 }
247 
249 
250 static Datum
252 {
253  return Int64GetDatum(PG_INT64_MIN);
254 }
255 
257 
258 static Datum
260 {
261  return ObjectIdGetDatum(0);
262 }
263 
265 
266 static Datum
268 {
270 }
271 
273 
275 
276 static Datum
278 {
279  return TimeADTGetDatum(0);
280 }
281 
283 
284 static Datum
286 {
287  TimeTzADT *v = palloc(sizeof(TimeTzADT));
288 
289  v->time = 0;
290  v->zone = -24 * 3600; /* XXX is that true? */
291 
292  return TimeTzADTPGetDatum(v);
293 }
294 
296 
297 static Datum
299 {
301 }
302 
304 
305 static Datum
307 {
308  Interval *v = palloc(sizeof(Interval));
309 
310  v->time = DT_NOBEGIN;
311  v->day = 0;
312  v->month = 0;
313  return IntervalPGetDatum(v);
314 }
315 
317 
318 static Datum
320 {
321  macaddr *v = palloc0(sizeof(macaddr));
322 
323  return MacaddrPGetDatum(v);
324 }
325 
327 
328 static Datum
330 {
331  macaddr8 *v = palloc0(sizeof(macaddr8));
332 
333  return Macaddr8PGetDatum(v);
334 }
335 
337 
338 static Datum
340 {
341  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
342 }
343 
345 
347 
348 static Datum
350 {
352 }
353 
355 
357 
358 static Datum
360 {
361  return CharGetDatum(SCHAR_MIN);
362 }
363 
365 
367 
368 static Datum
370 {
372  CStringGetDatum(""),
373  ObjectIdGetDatum(0),
374  Int32GetDatum(-1));
375 }
376 
378 
379 static Datum
381 {
383  CStringGetDatum(""),
384  ObjectIdGetDatum(0),
385  Int32GetDatum(-1));
386 }
387 
389 
390 /*
391  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
392  * (*not* a SQL NULL) to represent that. We can get away with that because
393  * the value returned by our leftmostvalue function will never be stored in
394  * the index nor passed to anything except our compare and prefix-comparison
395  * functions. The same trick could be used for other pass-by-reference types.
396  */
397 
398 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
399 
401 
402 Datum
404 {
407  int res = 0;
408 
409  if (NUMERIC_IS_LEFTMOST(a))
410  {
411  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
412  }
413  else if (NUMERIC_IS_LEFTMOST(b))
414  {
415  res = 1;
416  }
417  else
418  {
420  NumericGetDatum(a),
421  NumericGetDatum(b)));
422  }
423 
424  PG_RETURN_INT32(res);
425 }
426 
427 static Datum
429 {
430  return PointerGetDatum(NULL);
431 }
432 
434 
435 /*
436  * Use a similar trick to that used for numeric for enums, since we don't
437  * actually know the leftmost value of any enum without knowing the concrete
438  * type, so we use a dummy leftmost value of InvalidOid.
439  *
440  * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
441  * gets a valid fn_extra to work with. Unlike most other type comparison
442  * routines it needs it, so we can't use DirectFunctionCall2.
443  */
444 
445 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
446 
448 
449 Datum
451 {
452  Oid a = PG_GETARG_OID(0);
453  Oid b = PG_GETARG_OID(1);
454  int res = 0;
455 
456  if (ENUM_IS_LEFTMOST(a))
457  {
458  res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
459  }
460  else if (ENUM_IS_LEFTMOST(b))
461  {
462  res = 1;
463  }
464  else
465  {
467  enum_cmp,
468  fcinfo->flinfo,
470  ObjectIdGetDatum(a),
471  ObjectIdGetDatum(b)));
472  }
473 
474  PG_RETURN_INT32(res);
475 }
476 
477 static Datum
479 {
481 }
482 
484 
485 static Datum
487 {
488  /*
489  * palloc0 will create the UUID with all zeroes:
490  * "00000000-0000-0000-0000-000000000000"
491  */
492  pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
493 
494  return UUIDPGetDatum(retval);
495 }
496 
498 
499 static Datum
501 {
502  NameData *result = (NameData *) palloc0(NAMEDATALEN);
503 
504  return NameGetDatum(result);
505 }
506 
508 
509 static Datum
511 {
512  return BoolGetDatum(false);
513 }
514 
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:681
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:298
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2275
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:403
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
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:90
bool is_varlena
Definition: btree_gin.c:26
TimeADT time
Definition: date.h:29
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:369
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2119
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:983
static Datum leftmostvalue_int8(void)
Definition: btree_gin.c:227
int64 timestamp
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1086
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define MacaddrPGetDatum(X)
Definition: inet.h:132
static Datum leftmostvalue_inet(void)
Definition: btree_gin.c:339
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:2046
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
#define Int16GetDatum(X)
Definition: postgres.h:451
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
uint16 StrategyNumber
Definition: stratnum.h:22
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:889
#define DateADTGetDatum(X)
Definition: date.h:57
static Datum leftmostvalue_text(void)
Definition: btree_gin.c:349
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
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:319
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
unsigned int Oid
Definition: postgres_ext.h:31
static Datum leftmostvalue_char(void)
Definition: btree_gin.c:359
struct NumericData * Numeric
Definition: numeric.h:43
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2430
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Datum btboolcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:73
signed int int32
Definition: c.h:347
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:428
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1708
Datum datum
Definition: btree_gin.c:25
static Datum leftmostvalue_time(void)
Definition: btree_gin.c:277
#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:450
Datum btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:267
#define NAMEDATALEN
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:306
char * Pointer
Definition: c.h:336
static Datum leftmostvalue_macaddr8(void)
Definition: btree_gin.c:329
#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:492
Definition: inet.h:107
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1531
Datum btnamecmp(PG_FUNCTION_ARGS)
Definition: name.c:202
static Datum leftmostvalue_timetz(void)
Definition: btree_gin.c:285
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:604
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
#define PG_INT64_MIN
Definition: c.h:444
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#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:445
static Datum leftmostvalue_money(void)
Definition: btree_gin.c:251
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1699
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:486
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:619
int32 month
Definition: timestamp.h:48
signed char int8
Definition: c.h:345
float float4
Definition: c.h:491
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:259
void * palloc0(Size size)
Definition: mcxt.c:980
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:398
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
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:219
#define BoolGetDatum(X)
Definition: postgres.h:402
static Datum leftmostvalue_bool(void)
Definition: btree_gin.c:510
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:380
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:4133
#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:500
struct QueryInfo QueryInfo
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:267
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:373
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:235
static Datum leftmostvalue_enum(void)
Definition: btree_gin.c:478
static Datum leftmostvalue_float8(void)
Definition: btree_gin.c:243
Definition: inet.h:94
#define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp)
Definition: btree_gin.c:186
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:521
#define CharGetDatum(X)
Definition: postgres.h:416
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:267
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:146
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1925
#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:949
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:176
#define elog(elevel,...)
Definition: elog.h:228
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:82
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
Definition: c.h:550
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
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:211
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: date.h:27
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617
#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:70