PostgreSQL Source Code  git master
btree_ts.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_ts.c
3  */
4 #include "postgres.h"
5 
6 #include <limits.h>
7 
8 #include "btree_gist.h"
9 #include "btree_utils_num.h"
10 #include "utils/builtins.h"
11 #include "utils/datetime.h"
12 #include "utils/float.h"
13 
14 typedef struct
15 {
18 } tsKEY;
19 
20 /*
21 ** timestamp ops
22 */
34 
35 
36 #ifdef USE_FLOAT8_BYVAL
37 #define TimestampGetDatumFast(X) TimestampGetDatum(X)
38 #else
39 #define TimestampGetDatumFast(X) PointerGetDatum(&(X))
40 #endif
41 
42 
43 static bool
44 gbt_tsgt(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46  const Timestamp *aa = (const Timestamp *) a;
47  const Timestamp *bb = (const Timestamp *) b;
48 
51  TimestampGetDatumFast(*bb)));
52 }
53 
54 static bool
55 gbt_tsge(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57  const Timestamp *aa = (const Timestamp *) a;
58  const Timestamp *bb = (const Timestamp *) b;
59 
62  TimestampGetDatumFast(*bb)));
63 }
64 
65 static bool
66 gbt_tseq(const void *a, const void *b, FmgrInfo *flinfo)
67 {
68  const Timestamp *aa = (const Timestamp *) a;
69  const Timestamp *bb = (const Timestamp *) b;
70 
73  TimestampGetDatumFast(*bb)));
74 }
75 
76 static bool
77 gbt_tsle(const void *a, const void *b, FmgrInfo *flinfo)
78 {
79  const Timestamp *aa = (const Timestamp *) a;
80  const Timestamp *bb = (const Timestamp *) b;
81 
84  TimestampGetDatumFast(*bb)));
85 }
86 
87 static bool
88 gbt_tslt(const void *a, const void *b, FmgrInfo *flinfo)
89 {
90  const Timestamp *aa = (const Timestamp *) a;
91  const Timestamp *bb = (const Timestamp *) b;
92 
95  TimestampGetDatumFast(*bb)));
96 }
97 
98 
99 static int
100 gbt_tskey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
101 {
102  tsKEY *ia = (tsKEY *) (((const Nsrt *) a)->t);
103  tsKEY *ib = (tsKEY *) (((const Nsrt *) b)->t);
104  int res;
105 
107  if (res == 0)
109 
110  return res;
111 }
112 
113 static float8
114 gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
115 {
116  const Timestamp *aa = (const Timestamp *) a;
117  const Timestamp *bb = (const Timestamp *) b;
118  Interval *i;
119 
121  return get_float8_infinity();
122 
125  TimestampGetDatumFast(*bb)));
126  return (float8) Abs(INTERVAL_TO_SEC(i));
127 }
128 
129 
130 static const gbtree_ninfo tinfo =
131 {
132  gbt_t_ts,
133  sizeof(Timestamp),
134  16, /* sizeof(gbtreekey16) */
135  gbt_tsgt,
136  gbt_tsge,
137  gbt_tseq,
138  gbt_tsle,
139  gbt_tslt,
142 };
143 
144 
146 Datum
148 {
151  Interval *r;
152 
154  {
155  Interval *p = palloc(sizeof(Interval));
156 
157  p->day = INT_MAX;
158  p->month = INT_MAX;
159  p->time = PG_INT64_MAX;
161  }
162  else
164  PG_GETARG_DATUM(0),
165  PG_GETARG_DATUM(1)));
167 }
168 
170 Datum
172 {
175  Interval *r;
176 
178  {
179  Interval *p = palloc(sizeof(Interval));
180 
181  p->day = INT_MAX;
182  p->month = INT_MAX;
183  p->time = PG_INT64_MAX;
185  }
186 
188  PG_GETARG_DATUM(0),
189  PG_GETARG_DATUM(1)));
191 }
192 
193 
194 /**************************************************
195  * timestamp ops
196  **************************************************/
197 
198 
199 static inline Timestamp
201 {
202  /* No timezone correction is needed, since GMT is offset 0 by definition */
203  return (Timestamp) ts;
204 }
205 
206 
207 Datum
209 {
210  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
211 
212  PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
213 }
214 
215 
216 Datum
218 {
219  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
220  GISTENTRY *retval;
221 
222  if (entry->leafkey)
223  {
224  tsKEY *r = (tsKEY *) palloc(sizeof(tsKEY));
225  TimestampTz ts = DatumGetTimestampTz(entry->key);
226  Timestamp gmt;
227 
228  gmt = tstz_to_ts_gmt(ts);
229 
230  retval = palloc(sizeof(GISTENTRY));
231  r->lower = r->upper = gmt;
232  gistentryinit(*retval, PointerGetDatum(r),
233  entry->rel, entry->page,
234  entry->offset, false);
235  }
236  else
237  retval = entry;
238 
239  PG_RETURN_POINTER(retval);
240 }
241 
242 Datum
244 {
245  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
246 
247  PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
248 }
249 
250 Datum
252 {
253  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
254  Timestamp query = PG_GETARG_TIMESTAMP(1);
256 
257  /* Oid subtype = PG_GETARG_OID(3); */
258  bool *recheck = (bool *) PG_GETARG_POINTER(4);
259  tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
261 
262  /* All cases served by this function are exact */
263  *recheck = false;
264 
265  key.lower = (GBT_NUMKEY *) &kkk->lower;
266  key.upper = (GBT_NUMKEY *) &kkk->upper;
267 
268  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
269  GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
270 }
271 
272 Datum
274 {
275  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
276  Timestamp query = PG_GETARG_TIMESTAMP(1);
277 
278  /* Oid subtype = PG_GETARG_OID(3); */
279  tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
281 
282  key.lower = (GBT_NUMKEY *) &kkk->lower;
283  key.upper = (GBT_NUMKEY *) &kkk->upper;
284 
285  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
286  &tinfo, fcinfo->flinfo));
287 }
288 
289 Datum
291 {
292  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
295 
296  /* Oid subtype = PG_GETARG_OID(3); */
297  bool *recheck = (bool *) PG_GETARG_POINTER(4);
298  char *kkk = (char *) DatumGetPointer(entry->key);
300  Timestamp qqq;
301 
302  /* All cases served by this function are exact */
303  *recheck = false;
304 
305  key.lower = (GBT_NUMKEY *) &kkk[0];
306  key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
307  qqq = tstz_to_ts_gmt(query);
308 
309  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &qqq, &strategy,
310  GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
311 }
312 
313 Datum
315 {
316  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
318 
319  /* Oid subtype = PG_GETARG_OID(3); */
320  char *kkk = (char *) DatumGetPointer(entry->key);
322  Timestamp qqq;
323 
324  key.lower = (GBT_NUMKEY *) &kkk[0];
325  key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
326  qqq = tstz_to_ts_gmt(query);
327 
328  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &qqq, GIST_LEAF(entry),
329  &tinfo, fcinfo->flinfo));
330 }
331 
332 
333 Datum
335 {
337  void *out = palloc(sizeof(tsKEY));
338 
339  *(int *) PG_GETARG_POINTER(1) = sizeof(tsKEY);
340  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
341 }
342 
343 
344 #define penalty_check_max_float(val) \
345  do { \
346  if ( val > FLT_MAX ) \
347  val = FLT_MAX; \
348  if ( val < -FLT_MAX ) \
349  val = -FLT_MAX; \
350  } while (0)
351 
352 
353 Datum
355 {
356  tsKEY *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
357  tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
358  float *result = (float *) PG_GETARG_POINTER(2);
359 
360  double orgdbl[2],
361  newdbl[2];
362 
363  /*
364  * We are always using "double" timestamps here. Precision should be good
365  * enough.
366  */
367  orgdbl[0] = ((double) origentry->lower);
368  orgdbl[1] = ((double) origentry->upper);
369  newdbl[0] = ((double) newentry->lower);
370  newdbl[1] = ((double) newentry->upper);
371 
372  penalty_check_max_float(orgdbl[0]);
373  penalty_check_max_float(orgdbl[1]);
374  penalty_check_max_float(newdbl[0]);
375  penalty_check_max_float(newdbl[1]);
376 
377  penalty_num(result, orgdbl[0], orgdbl[1], newdbl[0], newdbl[1]);
378 
379  PG_RETURN_POINTER(result);
380 
381 }
382 
383 
384 Datum
386 {
389  &tinfo, fcinfo->flinfo));
390 }
391 
392 Datum
394 {
395  tsKEY *b1 = (tsKEY *) PG_GETARG_POINTER(0);
396  tsKEY *b2 = (tsKEY *) PG_GETARG_POINTER(1);
397  bool *result = (bool *) PG_GETARG_POINTER(2);
398 
399  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
400  PG_RETURN_POINTER(result);
401 }
#define GIST_LEAF(entry)
Definition: gist.h:162
Relation rel
Definition: gist.h:153
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Definition: fmgr.h:56
#define PG_INT64_MAX
Definition: c.h:460
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
static float8 get_float8_infinity(void)
Definition: float.h:93
Datum gbt_ts_compress(PG_FUNCTION_ARGS)
Definition: btree_ts.c:208
const GBT_NUMKEY * lower
Datum gbt_ts_consistent(PG_FUNCTION_ARGS)
Definition: btree_ts.c:251
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum timestamp_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2112
Datum timestamp_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2076
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
Datum gbt_tstz_compress(PG_FUNCTION_ARGS)
Definition: btree_ts.c:217
int64 TimestampTz
Definition: timestamp.h:39
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum timestamp_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2085
Datum timestamp_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:2632
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:365
uint16 StrategyNumber
Definition: stratnum.h:22
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Timestamp lower
Definition: btree_ts.c:16
Datum gbt_ts_union(PG_FUNCTION_ARGS)
Definition: btree_ts.c:334
Datum gbt_tstz_distance(PG_FUNCTION_ARGS)
Definition: btree_ts.c:314
int32 day
Definition: timestamp.h:47
PG_FUNCTION_INFO_V1(gbt_ts_compress)
Definition: btree_ts.c:14
Page page
Definition: gist.h:154
#define Abs(x)
Definition: c.h:933
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static bool gbt_tslt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:88
double float8
Definition: c.h:498
#define penalty_num(result, olower, oupper, nlower, nupper)
Datum ts_dist(PG_FUNCTION_ARGS)
Definition: btree_ts.c:147
static bool gbt_tseq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:66
Datum key
Definition: gist.h:152
char GBT_NUMKEY
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define DatumGetBool(X)
Definition: postgres.h:393
TimeOffset time
Definition: timestamp.h:45
Datum timestamp_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2058
Datum timestamp_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2103
bool leafkey
Definition: gist.h:156
int32 month
Definition: timestamp.h:48
int64 Timestamp
Definition: timestamp.h:38
static Timestamp tstz_to_ts_gmt(TimestampTz ts)
Definition: btree_ts.c:200
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uintptr_t Datum
Definition: postgres.h:367
Interval * abs_interval(Interval *a)
Datum gbt_tstz_consistent(PG_FUNCTION_ARGS)
Definition: btree_ts.c:290
Datum gbt_ts_fetch(PG_FUNCTION_ARGS)
Definition: btree_ts.c:243
static int gbt_tskey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:100
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:236
Datum gbt_ts_penalty(PG_FUNCTION_ARGS)
Definition: btree_ts.c:354
static bool gbt_tsge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:55
#define TimestampGetDatumFast(X)
Definition: btree_ts.c:39
#define MAXALIGN(LEN)
Definition: c.h:698
static float8 gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:114
Timestamp upper
Definition: btree_ts.c:17
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define DatumGetPointer(X)
Definition: postgres.h:549
static bool gbt_tsgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:44
Datum gbt_ts_distance(PG_FUNCTION_ARGS)
Definition: btree_ts.c:273
void * palloc(Size size)
Definition: mcxt.c:950
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int i
Datum timestamp_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2094
Datum tstz_dist(PG_FUNCTION_ARGS)
Definition: btree_ts.c:171
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Datum gbt_ts_same(PG_FUNCTION_ARGS)
Definition: btree_ts.c:393
static const gbtree_ninfo tinfo
Definition: btree_ts.c:130
#define INTERVAL_TO_SEC(ivp)
OffsetNumber offset
Definition: gist.h:155
static const char gmt[]
Definition: localtime.c:53
Datum gbt_ts_picksplit(PG_FUNCTION_ARGS)
Definition: btree_ts.c:385
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
#define penalty_check_max_float(val)
Definition: btree_ts.c:344
static bool gbt_tsle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_ts.c:77
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)