pg_stat_statements: Avoid some locking during PGSS entry scans
[pgsql.git] / contrib / btree_gin / btree_gin.c
blob533c55e9eaf41d60382810a645f5c9fdbe1992bc
1 /*
2 * contrib/btree_gin/btree_gin.c
3 */
4 #include "postgres.h"
6 #include <limits.h>
8 #include "access/stratnum.h"
9 #include "utils/builtins.h"
10 #include "utils/date.h"
11 #include "utils/float.h"
12 #include "utils/inet.h"
13 #include "utils/numeric.h"
14 #include "utils/timestamp.h"
15 #include "utils/uuid.h"
17 PG_MODULE_MAGIC;
19 typedef struct QueryInfo
21 StrategyNumber strategy;
22 Datum datum;
23 bool is_varlena;
24 Datum (*typecmp) (FunctionCallInfo);
25 } QueryInfo;
27 /*** GIN support functions shared by all datatypes ***/
29 static Datum
30 gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
32 Datum datum = PG_GETARG_DATUM(0);
33 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
34 Datum *entries = (Datum *) palloc(sizeof(Datum));
36 if (is_varlena)
37 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
38 entries[0] = datum;
39 *nentries = 1;
41 PG_RETURN_POINTER(entries);
45 * For BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, and
46 * BTEqualStrategyNumber we want to start the index scan at the
47 * supplied query datum, and work forward. For BTLessStrategyNumber
48 * and BTLessEqualStrategyNumber, we need to start at the leftmost
49 * key, and work forward until the supplied query datum (which must be
50 * sent along inside the QueryInfo structure).
52 static Datum
53 gin_btree_extract_query(FunctionCallInfo fcinfo,
54 bool is_varlena,
55 Datum (*leftmostvalue) (void),
56 Datum (*typecmp) (FunctionCallInfo))
58 Datum datum = PG_GETARG_DATUM(0);
59 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
60 StrategyNumber strategy = PG_GETARG_UINT16(2);
61 bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
62 Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
63 Datum *entries = (Datum *) palloc(sizeof(Datum));
64 QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
65 bool *ptr_partialmatch;
67 *nentries = 1;
68 ptr_partialmatch = *partialmatch = (bool *) palloc(sizeof(bool));
69 *ptr_partialmatch = false;
70 if (is_varlena)
71 datum = PointerGetDatum(PG_DETOAST_DATUM(datum));
72 data->strategy = strategy;
73 data->datum = datum;
74 data->is_varlena = is_varlena;
75 data->typecmp = typecmp;
76 *extra_data = (Pointer *) palloc(sizeof(Pointer));
77 **extra_data = (Pointer) data;
79 switch (strategy)
81 case BTLessStrategyNumber:
82 case BTLessEqualStrategyNumber:
83 entries[0] = leftmostvalue();
84 *ptr_partialmatch = true;
85 break;
86 case BTGreaterEqualStrategyNumber:
87 case BTGreaterStrategyNumber:
88 *ptr_partialmatch = true;
89 /* FALLTHROUGH */
90 case BTEqualStrategyNumber:
91 entries[0] = datum;
92 break;
93 default:
94 elog(ERROR, "unrecognized strategy number: %d", strategy);
97 PG_RETURN_POINTER(entries);
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.
105 static Datum
106 gin_btree_compare_prefix(FunctionCallInfo fcinfo)
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;
114 cmp = DatumGetInt32(CallerFInfoFunctionCall2(data->typecmp,
115 fcinfo->flinfo,
116 PG_GET_COLLATION(),
117 (data->strategy == BTLessStrategyNumber ||
118 data->strategy == BTLessEqualStrategyNumber)
119 ? data->datum : a,
120 b));
122 switch (data->strategy)
124 case BTLessStrategyNumber:
125 /* If original datum > indexed one then return match */
126 if (cmp > 0)
127 res = 0;
128 else
129 res = 1;
130 break;
131 case BTLessEqualStrategyNumber:
132 /* The same except equality */
133 if (cmp >= 0)
134 res = 0;
135 else
136 res = 1;
137 break;
138 case BTEqualStrategyNumber:
139 if (cmp != 0)
140 res = 1;
141 else
142 res = 0;
143 break;
144 case BTGreaterEqualStrategyNumber:
145 /* If original datum <= indexed one then return match */
146 if (cmp <= 0)
147 res = 0;
148 else
149 res = 1;
150 break;
151 case BTGreaterStrategyNumber:
152 /* If original datum <= indexed one then return match */
153 /* If original datum == indexed one then continue scan */
154 if (cmp < 0)
155 res = 0;
156 else if (cmp == 0)
157 res = -1;
158 else
159 res = 1;
160 break;
161 default:
162 elog(ERROR, "unrecognized strategy number: %d",
163 data->strategy);
164 res = 0;
167 PG_RETURN_INT32(res);
170 PG_FUNCTION_INFO_V1(gin_btree_consistent);
171 Datum
172 gin_btree_consistent(PG_FUNCTION_ARGS)
174 bool *recheck = (bool *) PG_GETARG_POINTER(5);
176 *recheck = false;
177 PG_RETURN_BOOL(true);
180 /*** GIN_SUPPORT macro defines the datatype specific functions ***/
182 #define GIN_SUPPORT(type, is_varlena, leftmostvalue, typecmp) \
183 PG_FUNCTION_INFO_V1(gin_extract_value_##type); \
184 Datum \
185 gin_extract_value_##type(PG_FUNCTION_ARGS) \
187 return gin_btree_extract_value(fcinfo, is_varlena); \
189 PG_FUNCTION_INFO_V1(gin_extract_query_##type); \
190 Datum \
191 gin_extract_query_##type(PG_FUNCTION_ARGS) \
193 return gin_btree_extract_query(fcinfo, \
194 is_varlena, leftmostvalue, typecmp); \
196 PG_FUNCTION_INFO_V1(gin_compare_prefix_##type); \
197 Datum \
198 gin_compare_prefix_##type(PG_FUNCTION_ARGS) \
200 return gin_btree_compare_prefix(fcinfo); \
204 /*** Datatype specifications ***/
206 static Datum
207 leftmostvalue_int2(void)
209 return Int16GetDatum(SHRT_MIN);
212 GIN_SUPPORT(int2, false, leftmostvalue_int2, btint2cmp)
214 static Datum
215 leftmostvalue_int4(void)
217 return Int32GetDatum(INT_MIN);
220 GIN_SUPPORT(int4, false, leftmostvalue_int4, btint4cmp)
222 static Datum
223 leftmostvalue_int8(void)
225 return Int64GetDatum(PG_INT64_MIN);
228 GIN_SUPPORT(int8, false, leftmostvalue_int8, btint8cmp)
230 static Datum
231 leftmostvalue_float4(void)
233 return Float4GetDatum(-get_float4_infinity());
236 GIN_SUPPORT(float4, false, leftmostvalue_float4, btfloat4cmp)
238 static Datum
239 leftmostvalue_float8(void)
241 return Float8GetDatum(-get_float8_infinity());
244 GIN_SUPPORT(float8, false, leftmostvalue_float8, btfloat8cmp)
246 static Datum
247 leftmostvalue_money(void)
249 return Int64GetDatum(PG_INT64_MIN);
252 GIN_SUPPORT(money, false, leftmostvalue_money, cash_cmp)
254 static Datum
255 leftmostvalue_oid(void)
257 return ObjectIdGetDatum(0);
260 GIN_SUPPORT(oid, false, leftmostvalue_oid, btoidcmp)
262 static Datum
263 leftmostvalue_timestamp(void)
265 return TimestampGetDatum(DT_NOBEGIN);
268 GIN_SUPPORT(timestamp, false, leftmostvalue_timestamp, timestamp_cmp)
270 GIN_SUPPORT(timestamptz, false, leftmostvalue_timestamp, timestamp_cmp)
272 static Datum
273 leftmostvalue_time(void)
275 return TimeADTGetDatum(0);
278 GIN_SUPPORT(time, false, leftmostvalue_time, time_cmp)
280 static Datum
281 leftmostvalue_timetz(void)
283 TimeTzADT *v = palloc(sizeof(TimeTzADT));
285 v->time = 0;
286 v->zone = -24 * 3600; /* XXX is that true? */
288 return TimeTzADTPGetDatum(v);
291 GIN_SUPPORT(timetz, false, leftmostvalue_timetz, timetz_cmp)
293 static Datum
294 leftmostvalue_date(void)
296 return DateADTGetDatum(DATEVAL_NOBEGIN);
299 GIN_SUPPORT(date, false, leftmostvalue_date, date_cmp)
301 static Datum
302 leftmostvalue_interval(void)
304 Interval *v = palloc(sizeof(Interval));
306 INTERVAL_NOBEGIN(v);
308 return IntervalPGetDatum(v);
311 GIN_SUPPORT(interval, false, leftmostvalue_interval, interval_cmp)
313 static Datum
314 leftmostvalue_macaddr(void)
316 macaddr *v = palloc0(sizeof(macaddr));
318 return MacaddrPGetDatum(v);
321 GIN_SUPPORT(macaddr, false, leftmostvalue_macaddr, macaddr_cmp)
323 static Datum
324 leftmostvalue_macaddr8(void)
326 macaddr8 *v = palloc0(sizeof(macaddr8));
328 return Macaddr8PGetDatum(v);
331 GIN_SUPPORT(macaddr8, false, leftmostvalue_macaddr8, macaddr8_cmp)
333 static Datum
334 leftmostvalue_inet(void)
336 return DirectFunctionCall1(inet_in, CStringGetDatum("0.0.0.0/0"));
339 GIN_SUPPORT(inet, true, leftmostvalue_inet, network_cmp)
341 GIN_SUPPORT(cidr, true, leftmostvalue_inet, network_cmp)
343 static Datum
344 leftmostvalue_text(void)
346 return PointerGetDatum(cstring_to_text_with_len("", 0));
349 GIN_SUPPORT(text, true, leftmostvalue_text, bttextcmp)
351 GIN_SUPPORT(bpchar, true, leftmostvalue_text, bpcharcmp)
353 static Datum
354 leftmostvalue_char(void)
356 return CharGetDatum(0);
359 GIN_SUPPORT(char, false, leftmostvalue_char, btcharcmp)
361 GIN_SUPPORT(bytea, true, leftmostvalue_text, byteacmp)
363 static Datum
364 leftmostvalue_bit(void)
366 return DirectFunctionCall3(bit_in,
367 CStringGetDatum(""),
368 ObjectIdGetDatum(0),
369 Int32GetDatum(-1));
372 GIN_SUPPORT(bit, true, leftmostvalue_bit, bitcmp)
374 static Datum
375 leftmostvalue_varbit(void)
377 return DirectFunctionCall3(varbit_in,
378 CStringGetDatum(""),
379 ObjectIdGetDatum(0),
380 Int32GetDatum(-1));
383 GIN_SUPPORT(varbit, true, leftmostvalue_varbit, bitcmp)
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.
393 #define NUMERIC_IS_LEFTMOST(x) ((x) == NULL)
395 PG_FUNCTION_INFO_V1(gin_numeric_cmp);
397 Datum
398 gin_numeric_cmp(PG_FUNCTION_ARGS)
400 Numeric a = (Numeric) PG_GETARG_POINTER(0);
401 Numeric b = (Numeric) PG_GETARG_POINTER(1);
402 int res = 0;
404 if (NUMERIC_IS_LEFTMOST(a))
406 res = (NUMERIC_IS_LEFTMOST(b)) ? 0 : -1;
408 else if (NUMERIC_IS_LEFTMOST(b))
410 res = 1;
412 else
414 res = DatumGetInt32(DirectFunctionCall2(numeric_cmp,
415 NumericGetDatum(a),
416 NumericGetDatum(b)));
419 PG_RETURN_INT32(res);
422 static Datum
423 leftmostvalue_numeric(void)
425 return PointerGetDatum(NULL);
428 GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)
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.
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.
440 #define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
442 PG_FUNCTION_INFO_V1(gin_enum_cmp);
444 Datum
445 gin_enum_cmp(PG_FUNCTION_ARGS)
447 Oid a = PG_GETARG_OID(0);
448 Oid b = PG_GETARG_OID(1);
449 int res = 0;
451 if (ENUM_IS_LEFTMOST(a))
453 res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
455 else if (ENUM_IS_LEFTMOST(b))
457 res = 1;
459 else
461 res = DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
462 fcinfo->flinfo,
463 PG_GET_COLLATION(),
464 ObjectIdGetDatum(a),
465 ObjectIdGetDatum(b)));
468 PG_RETURN_INT32(res);
471 static Datum
472 leftmostvalue_enum(void)
474 return ObjectIdGetDatum(InvalidOid);
477 GIN_SUPPORT(anyenum, false, leftmostvalue_enum, gin_enum_cmp)
479 static Datum
480 leftmostvalue_uuid(void)
483 * palloc0 will create the UUID with all zeroes:
484 * "00000000-0000-0000-0000-000000000000"
486 pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
488 return UUIDPGetDatum(retval);
491 GIN_SUPPORT(uuid, false, leftmostvalue_uuid, uuid_cmp)
493 static Datum
494 leftmostvalue_name(void)
496 NameData *result = (NameData *) palloc0(NAMEDATALEN);
498 return NameGetDatum(result);
501 GIN_SUPPORT(name, false, leftmostvalue_name, btnamecmp)
503 static Datum
504 leftmostvalue_bool(void)
506 return BoolGetDatum(false);
509 GIN_SUPPORT(bool, false, leftmostvalue_bool, btboolcmp)