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 
29 /*** GIN support functions shared by all datatypes ***/
30 
31 static Datum
32 gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
33 {
34  Datum datum = PG_GETARG_DATUM(0);
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),
58  Datum (*typecmp) (FunctionCallInfo))
59 {
60  Datum datum = PG_GETARG_DATUM(0);
61  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
62  StrategyNumber strategy = PG_GETARG_UINT16(2);
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;
92  entries[0] = datum;
93  break;
94  default:
95  elog(ERROR, "unrecognized strategy number: %d", strategy);
96  }
97 
98  PG_RETURN_POINTER(entries);
99 }
100 
101 /*
102  * Datum a is a value from extract_query method and for BTLess*
103  * strategy it is a left-most value. So, use original datum from QueryInfo
104  * to decide to stop scanning or not. Datum b is always from index.
105  */
106 static Datum
108 {
109  Datum a = PG_GETARG_DATUM(0);
110  Datum b = PG_GETARG_DATUM(1);
111  QueryInfo *data = (QueryInfo *) PG_GETARG_POINTER(3);
112  int32 res,
113  cmp;
114 
116  data->typecmp,
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  return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
329 }
330 
332 
334 
335 static Datum
337 {
339 }
340 
342 
343 static Datum
345 {
346  return CharGetDatum(SCHAR_MIN);
347 }
348 
350 
352 
353 static Datum
355 {
357  CStringGetDatum(""),
358  ObjectIdGetDatum(0),
359  Int32GetDatum(-1));
360 }
361 
363 
364 static Datum
366 {
368  CStringGetDatum(""),
369  ObjectIdGetDatum(0),
370  Int32GetDatum(-1));
371 }
372 
374 
375 /*
376  * Numeric type hasn't a real left-most value, so we use PointerGetDatum(NULL)
377  * (*not* a SQL NULL) to represent that. We can get away with that because
378  * the value returned by our leftmostvalue function will never be stored in
379  * the index nor passed to anything except our compare and prefix-comparison
380  * functions. The same trick could be used for other pass-by-reference types.
381  */
382 
383 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
384 
386 
387 Datum
389 {
392  int res = 0;
393 
394  if (NUMERIC_IS_LEFTMOST(a))
395  {
396  res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
397  }
398  else if (NUMERIC_IS_LEFTMOST(b))
399  {
400  res = 1;
401  }
402  else
403  {
405  NumericGetDatum(a),
406  NumericGetDatum(b)));
407  }
408 
409  PG_RETURN_INT32(res);
410 }
411 
412 static Datum
414 {
415  return PointerGetDatum(NULL);
416 }
417 
static Datum leftmostvalue_date(void)
Definition: btree_gin.c:295
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2369
Datum gin_numeric_cmp(PG_FUNCTION_ARGS)
Definition: btree_gin.c:388
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
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:32
static Datum leftmostvalue_bit(void)
Definition: btree_gin.c:354
#define DatumGetInt32(X)
Definition: postgres.h:480
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2386
Datum btfloat8cmp(PG_FUNCTION_ARGS)
Definition: float.c:1136
static Datum leftmostvalue_int8(void)
Definition: btree_gin.c:224
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define MacaddrPGetDatum(X)
Definition: inet.h:115
static Datum leftmostvalue_inet(void)
Definition: btree_gin.c:326
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:79
#define Int16GetDatum(X)
Definition: postgres.h:459
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
uint16 StrategyNumber
Definition: stratnum.h:22
Datum btfloat4cmp(PG_FUNCTION_ARGS)
Definition: float.c:1022
#define DateADTGetDatum(X)
Definition: date.h:77
static Datum leftmostvalue_text(void)
Definition: btree_gin.c:336
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
long date
Definition: pgtypes_date.h:8
#define false
Definition: c.h:207
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:316
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
static Datum leftmostvalue_char(void)
Definition: btree_gin.c:344
struct NumericData * Numeric
Definition: numeric.h:43
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2685
#define PG_GET_COLLATION()
Definition: fmgr.h:155
signed int int32
Definition: c.h:253
static Datum leftmostvalue_numeric(void)
Definition: btree_gin.c:413
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2126
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:60
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 btoidcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:250
#define true
Definition: c.h:203
static Datum leftmostvalue_interval(void)
Definition: btree_gin.c:303
char * Pointer
Definition: c.h:242
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
Datum btcharcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:311
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1577
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:33
struct FunctionCallInfoData * FunctionCallInfo
Definition: fmgr.h:38
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:714
Datum Float4GetDatum(float4 X)
Definition: fmgr.c:2114
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_INT64_MIN
Definition: c.h:339
#define DATEVAL_NOBEGIN
Definition: date.h:39
#define CStringGetDatum(X)
Definition: postgres.h:586
Definition: inet.h:52
TimeOffset time
Definition: timestamp.h:58
static Datum leftmostvalue_money(void)
Definition: btree_gin.c:248
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2102
Datum btint4cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:92
#define TimestampGetDatum(X)
Definition: timestamp.h:51
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
double get_float8_infinity(void)
Definition: float.c:121
int32 month
Definition: timestamp.h:61
signed char int8
Definition: c.h:251
float float4
Definition: c.h:376
static Datum leftmostvalue_oid(void)
Definition: btree_gin.c:256
void * palloc0(Size size)
Definition: mcxt.c:920
#define NUMERIC_IS_LEFTMOST(x)
Definition: btree_gin.c:383
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define TimeADTGetDatum(X)
Definition: date.h:78
static Datum leftmostvalue_int4(void)
Definition: btree_gin.c:216
static Datum leftmostvalue_varbit(void)
Definition: btree_gin.c:365
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3496
#define NULL
Definition: c.h:226
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:361
struct QueryInfo QueryInfo
static Datum leftmostvalue_timestamp(void)
Definition: btree_gin.c:264
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:80
static Datum leftmostvalue_float4(void)
Definition: btree_gin.c:232
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:171
double timestamp
#define CharGetDatum(X)
Definition: postgres.h:424
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
Datum btint8cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:129
Datum bttextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:1724
#define Int32GetDatum(X)
Definition: postgres.h:487
static Datum gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
Definition: btree_gin.c:32
void * palloc(Size size)
Definition: mcxt.c:891
Datum gin_btree_consistent(PG_FUNCTION_ARGS)
Definition: btree_gin.c:173
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1036
Datum btint2cmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:65
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:196
Definition: c.h:434
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
Datum network_cmp(PG_FUNCTION_ARGS)
Definition: network.c:384
#define elog
Definition: elog.h:219
#define DT_NOBEGIN
Definition: timestamp.h:129
Datum cash_cmp(PG_FUNCTION_ARGS)
Definition: cash.c:593
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:30
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
static Datum gin_btree_compare_prefix(FunctionCallInfo fcinfo)
Definition: btree_gin.c:107
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742