PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
btree_time.c File Reference
#include "postgres.h"
#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/fmgrprotos.h"
#include "utils/date.h"
#include "utils/timestamp.h"
Include dependency graph for btree_time.c:

Go to the source code of this file.

Data Structures

struct  timeKEY
 

Macros

#define TimeADTGetDatumFast(X)   PointerGetDatum(&(X))
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_time_compress)
 
 PG_FUNCTION_INFO_V1 (gbt_timetz_compress)
 
 PG_FUNCTION_INFO_V1 (gbt_time_fetch)
 
 PG_FUNCTION_INFO_V1 (gbt_time_union)
 
 PG_FUNCTION_INFO_V1 (gbt_time_picksplit)
 
 PG_FUNCTION_INFO_V1 (gbt_time_consistent)
 
 PG_FUNCTION_INFO_V1 (gbt_time_distance)
 
 PG_FUNCTION_INFO_V1 (gbt_timetz_consistent)
 
 PG_FUNCTION_INFO_V1 (gbt_time_penalty)
 
 PG_FUNCTION_INFO_V1 (gbt_time_same)
 
static bool gbt_timegt (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_timege (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_timeeq (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_timele (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_timelt (const void *a, const void *b, FmgrInfo *flinfo)
 
static int gbt_timekey_cmp (const void *a, const void *b, FmgrInfo *flinfo)
 
static float8 gbt_time_dist (const void *a, const void *b, FmgrInfo *flinfo)
 
 PG_FUNCTION_INFO_V1 (time_dist)
 
Datum time_dist (PG_FUNCTION_ARGS)
 
Datum gbt_time_compress (PG_FUNCTION_ARGS)
 
Datum gbt_timetz_compress (PG_FUNCTION_ARGS)
 
Datum gbt_time_fetch (PG_FUNCTION_ARGS)
 
Datum gbt_time_consistent (PG_FUNCTION_ARGS)
 
Datum gbt_time_distance (PG_FUNCTION_ARGS)
 
Datum gbt_timetz_consistent (PG_FUNCTION_ARGS)
 
Datum gbt_time_union (PG_FUNCTION_ARGS)
 
Datum gbt_time_penalty (PG_FUNCTION_ARGS)
 
Datum gbt_time_picksplit (PG_FUNCTION_ARGS)
 
Datum gbt_time_same (PG_FUNCTION_ARGS)
 

Variables

static const gbtree_ninfo tinfo
 

Macro Definition Documentation

◆ TimeADTGetDatumFast

#define TimeADTGetDatumFast (   X)    PointerGetDatum(&(X))

Definition at line 36 of file btree_time.c.

Function Documentation

◆ gbt_time_compress()

Datum gbt_time_compress ( PG_FUNCTION_ARGS  )

Definition at line 159 of file btree_time.c.

160{
161 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
162
164}
static const gbtree_ninfo tinfo
Definition: btree_time.c:125
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361

References gbt_num_compress(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_time_consistent()

Datum gbt_time_consistent ( PG_FUNCTION_ARGS  )

Definition at line 202 of file btree_time.c.

203{
204 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
205 TimeADT query = PG_GETARG_TIMEADT(1);
207
208 /* Oid subtype = PG_GETARG_OID(3); */
209 bool *recheck = (bool *) PG_GETARG_POINTER(4);
210 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
212
213 /* All cases served by this function are exact */
214 *recheck = false;
215
216 key.lower = (GBT_NUMKEY *) &kkk->lower;
217 key.upper = (GBT_NUMKEY *) &kkk->upper;
218
219 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
220 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
221}
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
char GBT_NUMKEY
#define PG_GETARG_TIMEADT(n)
Definition: date.h:90
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:171
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
uint16 StrategyNumber
Definition: stratnum.h:22
Datum key
Definition: gist.h:161
TimeADT lower
Definition: btree_time.c:14
TimeADT upper
Definition: btree_time.c:15

References DatumGetPointer(), gbt_num_consistent(), GIST_LEAF, GISTENTRY::key, sort-test::key, timeKEY::lower, PG_GETARG_POINTER, PG_GETARG_TIMEADT, PG_GETARG_UINT16, PG_RETURN_BOOL, tinfo, and timeKEY::upper.

◆ gbt_time_dist()

static float8 gbt_time_dist ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 112 of file btree_time.c.

113{
114 const TimeADT *aa = (const TimeADT *) a;
115 const TimeADT *bb = (const TimeADT *) b;
116 Interval *i;
117
120 TimeADTGetDatumFast(*bb)));
121 return fabs(INTERVAL_TO_SEC(i));
122}
#define TimeADTGetDatumFast(X)
Definition: btree_time.c:36
#define INTERVAL_TO_SEC(ivp)
Datum time_mi_time(PG_FUNCTION_ARGS)
Definition: date.c:2052
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
int b
Definition: isn.c:71
int a
Definition: isn.c:70
int i
Definition: isn.c:74
static Interval * DatumGetIntervalP(Datum X)
Definition: timestamp.h:40

References a, b, DatumGetIntervalP(), DirectFunctionCall2, i, INTERVAL_TO_SEC, time_mi_time(), and TimeADTGetDatumFast.

◆ gbt_time_distance()

Datum gbt_time_distance ( PG_FUNCTION_ARGS  )

Definition at line 224 of file btree_time.c.

225{
226 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
227 TimeADT query = PG_GETARG_TIMEADT(1);
228
229 /* Oid subtype = PG_GETARG_OID(3); */
230 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
232
233 key.lower = (GBT_NUMKEY *) &kkk->lower;
234 key.upper = (GBT_NUMKEY *) &kkk->upper;
235
237 &tinfo, fcinfo->flinfo));
238}
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367

References DatumGetPointer(), gbt_num_distance(), GIST_LEAF, GISTENTRY::key, sort-test::key, timeKEY::lower, PG_GETARG_POINTER, PG_GETARG_TIMEADT, PG_RETURN_FLOAT8, tinfo, and timeKEY::upper.

◆ gbt_time_fetch()

Datum gbt_time_fetch ( PG_FUNCTION_ARGS  )

Definition at line 194 of file btree_time.c.

195{
196 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
197
199}
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)

References gbt_num_fetch(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_time_penalty()

Datum gbt_time_penalty ( PG_FUNCTION_ARGS  )

Definition at line 278 of file btree_time.c.

279{
280 timeKEY *origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
282 float *result = (float *) PG_GETARG_POINTER(2);
283 Interval *intr;
284 double res;
285 double res2;
286
288 TimeADTGetDatumFast(newentry->upper),
289 TimeADTGetDatumFast(origentry->upper)));
290 res = INTERVAL_TO_SEC(intr);
291 res = Max(res, 0);
292
294 TimeADTGetDatumFast(origentry->lower),
295 TimeADTGetDatumFast(newentry->lower)));
296 res2 = INTERVAL_TO_SEC(intr);
297 res2 = Max(res2, 0);
298
299 res += res2;
300
301 *result = 0.0;
302
303 if (res > 0)
304 {
306 TimeADTGetDatumFast(origentry->upper),
307 TimeADTGetDatumFast(origentry->lower)));
308 *result += FLT_MIN;
309 *result += (float) (res / (res + INTERVAL_TO_SEC(intr)));
310 *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
311 }
312
313 PG_RETURN_POINTER(result);
314}
#define Max(x, y)
Definition: c.h:969

References DatumGetIntervalP(), DatumGetPointer(), DirectFunctionCall2, INTERVAL_TO_SEC, sort-test::key, timeKEY::lower, Max, PG_GETARG_POINTER, PG_RETURN_POINTER, time_mi_time(), TimeADTGetDatumFast, and timeKEY::upper.

◆ gbt_time_picksplit()

Datum gbt_time_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 318 of file btree_time.c.

319{
322 &tinfo, fcinfo->flinfo));
323}
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

References gbt_num_picksplit(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_time_same()

Datum gbt_time_same ( PG_FUNCTION_ARGS  )

Definition at line 326 of file btree_time.c.

327{
328 timeKEY *b1 = (timeKEY *) PG_GETARG_POINTER(0);
329 timeKEY *b2 = (timeKEY *) PG_GETARG_POINTER(1);
330 bool *result = (bool *) PG_GETARG_POINTER(2);
331
332 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
333 PG_RETURN_POINTER(result);
334}
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

References gbt_num_same(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_time_union()

Datum gbt_time_union ( PG_FUNCTION_ARGS  )

Definition at line 267 of file btree_time.c.

268{
270 void *out = palloc(sizeof(timeKEY));
271
272 *(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
273 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
274}
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * palloc(Size size)
Definition: mcxt.c:1317

References gbt_num_union(), palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_timeeq()

static bool gbt_timeeq ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 63 of file btree_time.c.

64{
65 const TimeADT *aa = (const TimeADT *) a;
66 const TimeADT *bb = (const TimeADT *) b;
67
71}
Datum time_eq(PG_FUNCTION_ARGS)
Definition: date.c:1699
static bool DatumGetBool(Datum X)
Definition: postgres.h:95

References a, b, DatumGetBool(), DirectFunctionCall2, time_eq(), and TimeADTGetDatumFast.

◆ gbt_timege()

static bool gbt_timege ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 52 of file btree_time.c.

53{
54 const TimeADT *aa = (const TimeADT *) a;
55 const TimeADT *bb = (const TimeADT *) b;
56
60}
Datum time_ge(PG_FUNCTION_ARGS)
Definition: date.c:1744

References a, b, DatumGetBool(), DirectFunctionCall2, time_ge(), and TimeADTGetDatumFast.

◆ gbt_timegt()

static bool gbt_timegt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 41 of file btree_time.c.

42{
43 const TimeADT *aa = (const TimeADT *) a;
44 const TimeADT *bb = (const TimeADT *) b;
45
49}
Datum time_gt(PG_FUNCTION_ARGS)
Definition: date.c:1735

References a, b, DatumGetBool(), DirectFunctionCall2, time_gt(), and TimeADTGetDatumFast.

◆ gbt_timekey_cmp()

static int gbt_timekey_cmp ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 98 of file btree_time.c.

99{
100 timeKEY *ia = (timeKEY *) (((const Nsrt *) a)->t);
101 timeKEY *ib = (timeKEY *) (((const Nsrt *) b)->t);
102 int res;
103
105 if (res == 0)
107
108 return res;
109}
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1753
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207

References a, b, DatumGetInt32(), DirectFunctionCall2, timeKEY::lower, time_cmp(), TimeADTGetDatumFast, and timeKEY::upper.

◆ gbt_timele()

static bool gbt_timele ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 74 of file btree_time.c.

75{
76 const TimeADT *aa = (const TimeADT *) a;
77 const TimeADT *bb = (const TimeADT *) b;
78
82}
Datum time_le(PG_FUNCTION_ARGS)
Definition: date.c:1726

References a, b, DatumGetBool(), DirectFunctionCall2, time_le(), and TimeADTGetDatumFast.

◆ gbt_timelt()

static bool gbt_timelt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 85 of file btree_time.c.

86{
87 const TimeADT *aa = (const TimeADT *) a;
88 const TimeADT *bb = (const TimeADT *) b;
89
93}
Datum time_lt(PG_FUNCTION_ARGS)
Definition: date.c:1717

References a, b, DatumGetBool(), DirectFunctionCall2, time_lt(), and TimeADTGetDatumFast.

◆ gbt_timetz_compress()

Datum gbt_timetz_compress ( PG_FUNCTION_ARGS  )

Definition at line 168 of file btree_time.c.

169{
170 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
171 GISTENTRY *retval;
172
173 if (entry->leafkey)
174 {
175 timeKEY *r = (timeKEY *) palloc(sizeof(timeKEY));
176 TimeTzADT *tz = DatumGetTimeTzADTP(entry->key);
177 TimeADT tmp;
178
179 retval = palloc(sizeof(GISTENTRY));
180
181 /* We are using the time + zone only to compress */
182 tmp = tz->time + (tz->zone * INT64CONST(1000000));
183 r->lower = r->upper = tmp;
184 gistentryinit(*retval, PointerGetDatum(r),
185 entry->rel, entry->page,
186 entry->offset, false);
187 }
188 else
189 retval = entry;
190 PG_RETURN_POINTER(retval);
191}
#define INT64CONST(x)
Definition: c.h:516
static TimeTzADT * DatumGetTimeTzADTP(Datum X)
Definition: date.h:66
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:245
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
OffsetNumber offset
Definition: gist.h:164
Page page
Definition: gist.h:163
Relation rel
Definition: gist.h:162
bool leafkey
Definition: gist.h:165
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

References DatumGetTimeTzADTP(), gistentryinit, INT64CONST, GISTENTRY::key, GISTENTRY::leafkey, timeKEY::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), GISTENTRY::rel, TimeTzADT::time, timeKEY::upper, and TimeTzADT::zone.

◆ gbt_timetz_consistent()

Datum gbt_timetz_consistent ( PG_FUNCTION_ARGS  )

Definition at line 241 of file btree_time.c.

242{
243 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
246
247 /* Oid subtype = PG_GETARG_OID(3); */
248 bool *recheck = (bool *) PG_GETARG_POINTER(4);
249 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
250 TimeADT qqq;
252
253 /* All cases served by this function are inexact */
254 *recheck = true;
255
256 qqq = query->time + (query->zone * INT64CONST(1000000));
257
258 key.lower = (GBT_NUMKEY *) &kkk->lower;
259 key.upper = (GBT_NUMKEY *) &kkk->upper;
260
261 PG_RETURN_BOOL(gbt_num_consistent(&key, &qqq, &strategy,
262 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
263}
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:91

References DatumGetPointer(), gbt_num_consistent(), GIST_LEAF, INT64CONST, GISTENTRY::key, sort-test::key, timeKEY::lower, PG_GETARG_POINTER, PG_GETARG_TIMETZADT_P, PG_GETARG_UINT16, PG_RETURN_BOOL, TimeTzADT::time, tinfo, timeKEY::upper, and TimeTzADT::zone.

◆ PG_FUNCTION_INFO_V1() [1/11]

PG_FUNCTION_INFO_V1 ( gbt_time_compress  )

◆ PG_FUNCTION_INFO_V1() [2/11]

PG_FUNCTION_INFO_V1 ( gbt_time_consistent  )

◆ PG_FUNCTION_INFO_V1() [3/11]

PG_FUNCTION_INFO_V1 ( gbt_time_distance  )

◆ PG_FUNCTION_INFO_V1() [4/11]

PG_FUNCTION_INFO_V1 ( gbt_time_fetch  )

◆ PG_FUNCTION_INFO_V1() [5/11]

PG_FUNCTION_INFO_V1 ( gbt_time_penalty  )

◆ PG_FUNCTION_INFO_V1() [6/11]

PG_FUNCTION_INFO_V1 ( gbt_time_picksplit  )

◆ PG_FUNCTION_INFO_V1() [7/11]

PG_FUNCTION_INFO_V1 ( gbt_time_same  )

◆ PG_FUNCTION_INFO_V1() [8/11]

PG_FUNCTION_INFO_V1 ( gbt_time_union  )

◆ PG_FUNCTION_INFO_V1() [9/11]

PG_FUNCTION_INFO_V1 ( gbt_timetz_compress  )

◆ PG_FUNCTION_INFO_V1() [10/11]

PG_FUNCTION_INFO_V1 ( gbt_timetz_consistent  )

◆ PG_FUNCTION_INFO_V1() [11/11]

PG_FUNCTION_INFO_V1 ( time_dist  )

◆ time_dist()

Datum time_dist ( PG_FUNCTION_ARGS  )

Definition at line 142 of file btree_time.c.

143{
146 PG_GETARG_DATUM(1));
147
149}
Interval * abs_interval(Interval *a)
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uintptr_t Datum
Definition: postgres.h:69
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:69

References abs_interval(), DatumGetIntervalP(), DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_INTERVAL_P, and time_mi_time().

Variable Documentation

◆ tinfo

const gbtree_ninfo tinfo
static
Initial value:
=
{
sizeof(TimeADT),
16,
}
@ gbt_t_time
Definition: btree_gist.h:26
static bool gbt_timeeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:63
static bool gbt_timele(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:74
static bool gbt_timelt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:85
static bool gbt_timegt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:41
static bool gbt_timege(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:52
static int gbt_timekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:98
static float8 gbt_time_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_time.c:112

Definition at line 125 of file btree_time.c.

Referenced by gbt_time_compress(), gbt_time_consistent(), gbt_time_distance(), gbt_time_fetch(), gbt_time_picksplit(), gbt_time_same(), gbt_time_union(), and gbt_timetz_consistent().