PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 
19 
20 typedef struct QueryInfo
21 {
24  bool is_varlena;
26 } QueryInfo;
27 
28 /*** GIN support functions shared by all datatypes ***/
29 
30 static Datum
31 gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
32 {
33  Datum datum = PG_GETARG_DATUM(0);
34  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
35  Datum *entries = (Datum *) palloc(sizeof(Datum));
36 
37  if (is_varlena)
38  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
39  entries[0] = datum;
40  *nentries = 1;
41 
42  PG_RETURN_POINTER(entries);
43 }
44 
45 /*
46  * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
47  * BTEqualStrategyNumber we want to start the index scan at the
48  * supplied query datum, and work forward. For BTLessStrategyNumber
49  * and BTLessEqualStrategyNumber, we need to start at the leftmost
50  * key, and work forward until the supplied query datum (which must be
51  * sent along inside the QueryInfo structure).
52  */
53 static Datum
55  bool is_varlena,
56  Datum (*leftmostvalue) (void),
57  Datum (*typecmp) (FunctionCallInfo))
58 {
59  Datum datum = PG_GETARG_DATUM(0);
60  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
61  StrategyNumber strategy = PG_GETARG_UINT16(2);
62  bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
63  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
64  Datum *entries = (Datum *) palloc(sizeof(Datum));
65  QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
66  bool *ptr_partialmatch;
67 
68  *nentries = 1;
69  ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
70  *ptr_partialmatch = false;
71  if (is_varlena)
72  datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
73  data->strategy = strategy;
74  data->datum = datum;
75  data->is_varlena = is_varlena;
76  data->typecmp = typecmp;
77  *extra_data = (Pointer *) palloc(sizeof(Pointer));
78  **extra_data = (Pointer) data;
79 
80  switch (strategy)
81  {
84  entries[0] = leftmostvalue();
85  *ptr_partialmatch = true;
86  break;
89  *ptr_partialmatch = true;
91  entries[0] = datum;
92  break;
93  default:
94  elog(ERROR, "unrecognized strategy number: %d", strategy);
95  }
96 
97  PG_RETURN_POINTER(entries);
98 }
99 
100 /*
101  * Datum a is a value from extract_query method and for BTLess*
102  * strategy it is a left-most value. So, use original datum from QueryInfo
103  * to decide to stop scanning or not. Datum b is always from index.
104  */
105 static Datum
107 {
108  Datum a = PG_GETARG_DATUM(0);
109  Datum b = PG_GETARG_DATUM(1);
110  QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
111  int32 res,
112  cmp;
113 
115  data->typecmp,
116  fcinfo->flinfo,
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 }
170 
172 Datum
174 {
175  bool *recheck = (bool *) PG_GETARG_POINTER(5);
176 
177  *recheck = false;
178  PG_RETURN_BOOL(true);
179 }
180 
181 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
182 
183 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
184 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
185 Datum \
186 gin_extract_value_##type(PG_FUNCTION_ARGS) \
187 { \
188  return gin_btree_extract_value(fcinfo, is_varlena); \
189 } \
190 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
191 Datum \
192 gin_extract_query_##type(PG_FUNCTION_ARGS) \
193 { \
194  return gin_btree_extract_query(fcinfo, \
195  is_varlena, leftmostvalue, typecmp); \
196 } \
197 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
198 Datum \
199 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
200 { \
201  return gin_btree_compare_prefix(fcinfo); \
202 }
203 
204 
205 /*** Datatype specifications ***/
206 
207 static Datum
209 {
210  return Int16GetDatum(SHRT_MIN);
211 }
212 
214 
215 static Datum
217 {
218  return Int32GetDatum(INT_MIN);
219 }
220 
222 
223 static Datum
225 {
226  return Int64GetDatum(PG_INT64_MIN);
227 }
228 
230 
231 static Datum
233 {
235 }
236 
238 
239 static Datum
241 {
243 }
244 
246 
247 static Datum
249 {
250  return Int64GetDatum(PG_INT64_MIN);
251 }
252 
254 
255 static Datum
257 {
258  return ObjectIdGetDatum(0);
259 }
260 
262 
263 static Datum
265 {
267 }
268 
270 
272 
273 static Datum
275 {
276  return TimeADTGetDatum(0);
277 }
278 
280 
281 static Datum
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 }
291 
293 
294 static Datum
296 {
298 }
299 
301 
302 static Datum
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 }
312 
314 
315 static Datum
317 {
318  macaddr *v = palloc0(sizeof(macaddr));
319 
320  return MacaddrPGetDatum(v);
321 }
322 
324 
325 static Datum
327 {
328  macaddr8 *v = palloc0(sizeof(macaddr8));
329 
330  return Macaddr8PGetDatum(v);
331 }
332 
334 
335 static Datum
337 {
338  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
339 }
340 
342 
344 
345 static Datum
347 {
349 }
350 
352 
353 static Datum
355 {
356  return CharGetDatum(SCHAR_MIN);
357 }
358 
360 
362 
363 static Datum
365 {
367  CStringGetDatum(""),
368  ObjectIdGetDatum(0),
369  Int32GetDatum(-1));
370 }
371 
373 
374 static Datum
376 {
378  CStringGetDatum(""),
379  ObjectIdGetDatum(0),
380  Int32GetDatum(-1));
381 }
382 
384 
385 /*
386  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
387  * (*not* a SQL NULL) to represent that. We can get away with that because
388  * the value returned by our leftmostvalue function will never be stored in
389  * the index nor passed to anything except our compare and prefix-comparison
390  * functions. The same trick could be used for other pass-by-reference types.
391  */
392 
393 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
394 
396 
397 Datum
399 {
402  int res = 0;
403 
404  if (NUMERIC_IS_LEFTMOST(a))
405  {
406  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
407  }
408  else if (NUMERIC_IS_LEFTMOST(b))
409  {
410  res = 1;
411  }
412  else
413  {
415  NumericGetDatum(a),
416  NumericGetDatum(b)));
417  }
418 
419  PG_RETURN_INT32(res);
420 }
421 
422 static Datum
424 {
425  return PointerGetDatum(NULL);
426 }
427 
429 
430 /*
431  * Use a similar trick to that used for numeric for enums, since we don't
432  * actually know the leftmost value of any enum without knowing the concrete
433  * type, so we use a dummy leftmost value of InvalidOid.
434  *
435  * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
436  * gets a valid fn_extra to work with. Unlike most other type comparison
437  * routines it needs it, so we can't use DirectFunctionCall2.
438  */
439 
440 
441 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
442 
444 
445 Datum
447 {
448  Oid a = PG_GETARG_OID(0);
449  Oid b = PG_GETARG_OID(1);
450  int res = 0;
451 
452  if (ENUM_IS_LEFTMOST(a))
453  {
454  res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
455  }
456  else if (ENUM_IS_LEFTMOST(b))
457  {
458  res = 1;
459  }
460  else
461  {
463  enum_cmp,
464  fcinfo->flinfo,
466  ObjectIdGetDatum(a),
467  ObjectIdGetDatum(b)));
468  }
469 
470  PG_RETURN_INT32(res);
471 }
472 
473 static Datum
475 {
477 }
478 
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:295
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2192
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:398
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Datum macaddr8_cmp(PG_FUNCTION_ARGS)
Definition: mac8.c:333
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:429
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
bool is_varlena
Definition: btree_gin.c:24
TimeADT time
Definition: date.h:28
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:364
#define DatumGetInt32(X)
Definition: postgres.h:478
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2082
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:1136
static Datum leftmostvalue_int8(void)
Definition: btree_gin.c:224
int64 timestamp
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1309
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define MacaddrPGetDatum(X)
Definition: inet.h:132
static Datum leftmostvalue_inet(void)
Definition: btree_gin.c:336
Datum bitcmp(PG_FUNCTION_ARGS)
Definition: varbit.c:928
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2020
#define TimeTzADTPGetDatum(X)
Definition: date.h:58
#define Int16GetDatum(X)
Definition: postgres.h:457
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
uint16 StrategyNumber
Definition: stratnum.h:22
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:1022
#define DateADTGetDatum(X)
Definition: date.h:56
static Datum leftmostvalue_text(void)
Definition: btree_gin.c:346
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
long date
Definition: pgtypes_date.h:8
#define false
Definition: c.h:210
static Datum gin_btree_extract_query(FunctionCallInfo fcinfo, bool is_varlena, Datum(*leftmostvalue)(void), Datum(*typecmp)(FunctionCallInfo))
Definition: btree_gin.c:54
static Datum leftmostvalue_macaddr(void)
Definition: btree_gin.c:316
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
unsigned int Oid
Definition: postgres_ext.h:31
static Datum leftmostvalue_char(void)
Definition: btree_gin.c:354
struct NumericData * Numeric
Definition: numeric.h:43
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2368
#define PG_GET_COLLATION()
Definition: fmgr.h:163
signed int int32
Definition: c.h:256
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:423
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2176
Datum datum
Definition: btree_gin.c:23
static Datum leftmostvalue_time(void)
Definition: btree_gin.c:274
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
int32 day
Definition: timestamp.h:47
StrategyNumber strategy
Definition: btree_gin.c:22
float get_float4_infinity(void)
Definition: float.c:146
Datum(* typecmp)(FunctionCallInfo)
Definition: btree_gin.c:25
Datum gin_enum_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:446
Datum btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:250
FmgrInfo * flinfo
Definition: fmgr.h:79
#define true
Definition: c.h:206
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:303
char * Pointer
Definition: c.h:245
static Datum leftmostvalue_macaddr8(void)
Definition: btree_gin.c:326
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
Datum btcharcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:311
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
Definition: inet.h:107
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1493
static Datum leftmostvalue_timetz(void)
Definition: btree_gin.c:282
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:113
int32 zone
Definition: date.h:29
struct FunctionCallInfoData * FunctionCallInfo
Definition: fmgr.h:38
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:714
Datum Float4GetDatum(float4 X)
Definition: fmgr.c:2164
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_INT64_MIN
Definition: c.h:342
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define DATEVAL_NOBEGIN
Definition: date.h:35
#define CStringGetDatum(X)
Definition: postgres.h:584
Definition: inet.h:52
TimeOffset time
Definition: timestamp.h:45
#define ENUM_IS_LEFTMOST(x)
Definition: btree_gin.c:441
static Datum leftmostvalue_money(void)
Definition: btree_gin.c:248
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
Datum btint4cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:92
#define TimestampGetDatum(X)
Definition: timestamp.h:31
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
double get_float8_infinity(void)
Definition: float.c:121
int32 month
Definition: timestamp.h:48
signed char int8
Definition: c.h:254
float float4
Definition: c.h:380
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:256
void * palloc0(Size size)
Definition: mcxt.c:878
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:393
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
#define Macaddr8PGetDatum(X)
Definition: inet.h:138
#define TimeADTGetDatum(X)
Definition: date.h:57
static Datum leftmostvalue_int4(void)
Definition: btree_gin.c:216
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:375
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3607
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:361
struct QueryInfo QueryInfo
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:264
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:388
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:80
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:232
static Datum leftmostvalue_enum(void)
Definition: btree_gin.c:474
static Datum leftmostvalue_float8(void)
Definition: btree_gin.c:240
Definition: inet.h:94
#define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp)
Definition: btree_gin.c:183
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:443
PG_FUNCTION_INFO_V1(gin_btree_consistent)
#define timestamptz
Definition: pg_authid.h:34
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition: mac.c:197
#define CharGetDatum(X)
Definition: postgres.h:422
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:129
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1760
#define Int32GetDatum(X)
Definition: postgres.h:485
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:31
void * palloc(Size size)
Definition: mcxt.c:849
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:173
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:65
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum network_cmp(PG_FUNCTION_ARGS)
Definition: network.c:384
#define elog
Definition: elog.h:219
#define DT_NOBEGIN
Definition: timestamp.h:109
Datum cash_cmp(PG_FUNCTION_ARGS)
Definition: cash.c:592
PG_MODULE_MAGIC
Definition: btree_gin.c:18
static Datum leftmostvalue_int2(void)
Definition: btree_gin.c:208
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: date.h:26
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:106
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742