PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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/rel.h"
#include "utils/sortsupport.h"
#include "utils/timestamp.h"
Include dependency graph for btree_time.c:

Go to the source code of this file.

Data Structures

struct  timeKEY
 

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)
 
 PG_FUNCTION_INFO_V1 (gbt_time_sortsupport)
 
 PG_FUNCTION_INFO_V1 (gbt_timetz_sortsupport)
 
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)
 
static int gbt_timekey_ssup_cmp (Datum x, Datum y, SortSupport ssup)
 
Datum gbt_time_sortsupport (PG_FUNCTION_ARGS)
 

Variables

static const gbtree_ninfo tinfo
 

Function Documentation

◆ gbt_time_compress()

Datum gbt_time_compress ( PG_FUNCTION_ARGS  )

Definition at line 150 of file btree_time.c.

151{
152 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
153
155}
static const gbtree_ninfo tinfo
Definition btree_time.c:118
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363

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 192 of file btree_time.c.

193{
194 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
195 TimeADT query = PG_GETARG_TIMEADT(1);
197#ifdef NOT_USED
198 Oid subtype = PG_GETARG_OID(3);
199#endif
200 bool *recheck = (bool *) PG_GETARG_POINTER(4);
201 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
203
204 /* All cases served by this function are exact */
205 *recheck = false;
206
207 key.lower = (GBT_NUMKEY *) &kkk->lower;
208 key.upper = (GBT_NUMKEY *) &kkk->upper;
209
210 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
211 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
212}
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:96
int64 TimeADT
Definition date.h:23
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIST_LEAF(entry)
Definition gist.h:171
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
unsigned int Oid
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
Datum key
Definition gist.h:161

References DatumGetPointer(), fb(), gbt_num_consistent(), GIST_LEAF, GISTENTRY::key, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TIMEADT, PG_GETARG_UINT16, PG_RETURN_BOOL, and tinfo.

◆ gbt_time_dist()

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

Definition at line 105 of file btree_time.c.

106{
107 const TimeADT *aa = (const TimeADT *) a;
108 const TimeADT *bb = (const TimeADT *) b;
109 Interval *i;
110
114 return fabs(INTERVAL_TO_SEC(i));
115}
#define INTERVAL_TO_SEC(ivp)
Datum time_mi_time(PG_FUNCTION_ARGS)
Definition date.c:2129
static Datum TimeADTGetDatum(TimeADT X)
Definition date.h:84
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
static Interval * DatumGetIntervalP(Datum X)
Definition timestamp.h:40

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

◆ gbt_time_distance()

Datum gbt_time_distance ( PG_FUNCTION_ARGS  )

Definition at line 215 of file btree_time.c.

216{
217 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
218 TimeADT query = PG_GETARG_TIMEADT(1);
219#ifdef NOT_USED
220 Oid subtype = PG_GETARG_OID(3);
221#endif
222 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
224
225 key.lower = (GBT_NUMKEY *) &kkk->lower;
226 key.upper = (GBT_NUMKEY *) &kkk->upper;
227
228 PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
229 &tinfo, fcinfo->flinfo));
230}
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:369

References DatumGetPointer(), fb(), gbt_num_distance(), GIST_LEAF, GISTENTRY::key, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TIMEADT, PG_RETURN_FLOAT8, and tinfo.

◆ gbt_time_fetch()

Datum gbt_time_fetch ( PG_FUNCTION_ARGS  )

Definition at line 184 of file btree_time.c.

185{
186 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
187
189}
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 269 of file btree_time.c.

270{
273 float *result = (float *) PG_GETARG_POINTER(2);
274 Interval *intr;
275 double res;
276 double res2;
277
280 TimeADTGetDatum(origentry->upper)));
281 res = INTERVAL_TO_SEC(intr);
282 res = Max(res, 0);
283
286 TimeADTGetDatum(newentry->lower)));
288 res2 = Max(res2, 0);
289
290 res += res2;
291
292 *result = 0.0;
293
294 if (res > 0)
295 {
298 TimeADTGetDatum(origentry->lower)));
299 *result += FLT_MIN;
300 *result += (float) (res / (res + INTERVAL_TO_SEC(intr)));
301 *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
302 }
303
304 PG_RETURN_POINTER(result);
305}
#define Max(x, y)
Definition c.h:991

References DatumGetIntervalP(), DatumGetPointer(), DirectFunctionCall2, fb(), INTERVAL_TO_SEC, Max, PG_GETARG_POINTER, PG_RETURN_POINTER, time_mi_time(), and TimeADTGetDatum().

◆ gbt_time_picksplit()

Datum gbt_time_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 308 of file btree_time.c.

309{
312 &tinfo, fcinfo->flinfo));
313}
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 316 of file btree_time.c.

317{
320 bool *result = (bool *) PG_GETARG_POINTER(2);
321
322 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
323 PG_RETURN_POINTER(result);
324}
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

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

◆ gbt_time_sortsupport()

Datum gbt_time_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 339 of file btree_time.c.

340{
342
344 ssup->ssup_extra = NULL;
345
347}
static int gbt_timekey_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition btree_time.c:327
#define PG_RETURN_VOID()
Definition fmgr.h:350
struct SortSupportData * SortSupport
Definition sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)

References SortSupportData::comparator, fb(), gbt_timekey_ssup_cmp(), PG_GETARG_POINTER, PG_RETURN_VOID, and SortSupportData::ssup_extra.

◆ gbt_time_union()

Datum gbt_time_union ( PG_FUNCTION_ARGS  )

Definition at line 259 of file btree_time.c.

260{
262 void *out = palloc(sizeof(timeKEY));
263
264 *(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
265 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
266}
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * palloc(Size size)
Definition mcxt.c:1387

References fb(), 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 58 of file btree_time.c.

59{
60 const TimeADT *aa = (const TimeADT *) a;
61 const TimeADT *bb = (const TimeADT *) b;
62
66}
Datum time_eq(PG_FUNCTION_ARGS)
Definition date.c:1776
static bool DatumGetBool(Datum X)
Definition postgres.h:100

References a, b, DatumGetBool(), DirectFunctionCall2, fb(), time_eq(), and TimeADTGetDatum().

◆ gbt_timege()

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

Definition at line 47 of file btree_time.c.

48{
49 const TimeADT *aa = (const TimeADT *) a;
50 const TimeADT *bb = (const TimeADT *) b;
51
55}
Datum time_ge(PG_FUNCTION_ARGS)
Definition date.c:1821

References a, b, DatumGetBool(), DirectFunctionCall2, fb(), time_ge(), and TimeADTGetDatum().

◆ gbt_timegt()

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

Definition at line 36 of file btree_time.c.

37{
38 const TimeADT *aa = (const TimeADT *) a;
39 const TimeADT *bb = (const TimeADT *) b;
40
44}
Datum time_gt(PG_FUNCTION_ARGS)
Definition date.c:1812

References a, b, DatumGetBool(), DirectFunctionCall2, fb(), time_gt(), and TimeADTGetDatum().

◆ gbt_timekey_cmp()

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

Definition at line 91 of file btree_time.c.

92{
93 timeKEY *ia = (timeKEY *) (((const Nsrt *) a)->t);
94 timeKEY *ib = (timeKEY *) (((const Nsrt *) b)->t);
95 int res;
96
98 if (res == 0)
100
101 return res;
102}
Datum time_cmp(PG_FUNCTION_ARGS)
Definition date.c:1830
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212

References a, b, DatumGetInt32(), DirectFunctionCall2, fb(), time_cmp(), and TimeADTGetDatum().

◆ gbt_timekey_ssup_cmp()

static int gbt_timekey_ssup_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 327 of file btree_time.c.

328{
331
332 /* for leaf items we expect lower == upper, so only compare lower */
334 TimeADTGetDatum(arg1->lower),
335 TimeADTGetDatum(arg2->lower)));
336}
int y
Definition isn.c:76
int x
Definition isn.c:75

References DatumGetInt32(), DatumGetPointer(), DirectFunctionCall2, fb(), time_cmp(), TimeADTGetDatum(), x, and y.

Referenced by gbt_time_sortsupport().

◆ gbt_timele()

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

Definition at line 69 of file btree_time.c.

70{
71 const TimeADT *aa = (const TimeADT *) a;
72 const TimeADT *bb = (const TimeADT *) b;
73
77}
Datum time_le(PG_FUNCTION_ARGS)
Definition date.c:1803

References a, b, DatumGetBool(), DirectFunctionCall2, fb(), time_le(), and TimeADTGetDatum().

◆ gbt_timelt()

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

Definition at line 80 of file btree_time.c.

81{
82 const TimeADT *aa = (const TimeADT *) a;
83 const TimeADT *bb = (const TimeADT *) b;
84
88}
Datum time_lt(PG_FUNCTION_ARGS)
Definition date.c:1794

References a, b, DatumGetBool(), DirectFunctionCall2, fb(), time_lt(), and TimeADTGetDatum().

◆ gbt_timetz_compress()

Datum gbt_timetz_compress ( PG_FUNCTION_ARGS  )

Definition at line 158 of file btree_time.c.

159{
160 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
161 GISTENTRY *retval;
162
163 if (entry->leafkey)
164 {
166 TimeTzADT *tz = DatumGetTimeTzADTP(entry->key);
167 TimeADT tmp;
168
169 retval = palloc_object(GISTENTRY);
170
171 /* We are using the time + zone only to compress */
172 tmp = tz->time + (tz->zone * INT64CONST(1000000));
173 r->lower = r->upper = tmp;
174 gistentryinit(*retval, PointerGetDatum(r),
175 entry->rel, entry->page,
176 entry->offset, false);
177 }
178 else
179 retval = entry;
180 PG_RETURN_POINTER(retval);
181}
#define INT64CONST(x)
Definition c.h:560
static TimeTzADT * DatumGetTimeTzADTP(Datum X)
Definition date.h:72
#define palloc_object(type)
Definition fe_memutils.h:74
#define gistentryinit(e, k, r, pg, o, l)
Definition gist.h:245
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
OffsetNumber offset
Definition gist.h:164
Page page
Definition gist.h:163
Relation rel
Definition gist.h:162
bool leafkey
Definition gist.h:165
TimeADT time
Definition date.h:27
int32 zone
Definition date.h:28
TimeADT lower
Definition btree_time.c:16
TimeADT upper
Definition btree_time.c:17

References DatumGetTimeTzADTP(), gistentryinit, INT64CONST, GISTENTRY::key, GISTENTRY::leafkey, timeKEY::lower, GISTENTRY::offset, GISTENTRY::page, palloc_object, 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 233 of file btree_time.c.

234{
235 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
238#ifdef NOT_USED
239 Oid subtype = PG_GETARG_OID(3);
240#endif
241 bool *recheck = (bool *) PG_GETARG_POINTER(4);
242 timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
243 TimeADT qqq;
245
246 /* All cases served by this function are inexact */
247 *recheck = true;
248
249 qqq = query->time + (query->zone * INT64CONST(1000000));
250
251 key.lower = (GBT_NUMKEY *) &kkk->lower;
252 key.upper = (GBT_NUMKEY *) &kkk->upper;
253
254 PG_RETURN_BOOL(gbt_num_consistent(&key, &qqq, &strategy,
255 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
256}
#define PG_GETARG_TIMETZADT_P(n)
Definition date.h:97

References DatumGetPointer(), fb(), gbt_num_consistent(), GIST_LEAF, INT64CONST, GISTENTRY::key, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_TIMETZADT_P, PG_GETARG_UINT16, PG_RETURN_BOOL, TimeTzADT::time, tinfo, and TimeTzADT::zone.

◆ PG_FUNCTION_INFO_V1() [1/13]

PG_FUNCTION_INFO_V1 ( gbt_time_compress  )

◆ PG_FUNCTION_INFO_V1() [2/13]

PG_FUNCTION_INFO_V1 ( gbt_time_consistent  )

◆ PG_FUNCTION_INFO_V1() [3/13]

PG_FUNCTION_INFO_V1 ( gbt_time_distance  )

◆ PG_FUNCTION_INFO_V1() [4/13]

PG_FUNCTION_INFO_V1 ( gbt_time_fetch  )

◆ PG_FUNCTION_INFO_V1() [5/13]

PG_FUNCTION_INFO_V1 ( gbt_time_penalty  )

◆ PG_FUNCTION_INFO_V1() [6/13]

PG_FUNCTION_INFO_V1 ( gbt_time_picksplit  )

◆ PG_FUNCTION_INFO_V1() [7/13]

PG_FUNCTION_INFO_V1 ( gbt_time_same  )

◆ PG_FUNCTION_INFO_V1() [8/13]

PG_FUNCTION_INFO_V1 ( gbt_time_sortsupport  )

◆ PG_FUNCTION_INFO_V1() [9/13]

PG_FUNCTION_INFO_V1 ( gbt_time_union  )

◆ PG_FUNCTION_INFO_V1() [10/13]

PG_FUNCTION_INFO_V1 ( gbt_timetz_compress  )

◆ PG_FUNCTION_INFO_V1() [11/13]

PG_FUNCTION_INFO_V1 ( gbt_timetz_consistent  )

◆ PG_FUNCTION_INFO_V1() [12/13]

PG_FUNCTION_INFO_V1 ( gbt_timetz_sortsupport  )

◆ PG_FUNCTION_INFO_V1() [13/13]

PG_FUNCTION_INFO_V1 ( time_dist  )

◆ time_dist()

Datum time_dist ( PG_FUNCTION_ARGS  )

Definition at line 135 of file btree_time.c.

136{
139 PG_GETARG_DATUM(1));
140
142}
Interval * abs_interval(Interval *a)
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
uint64_t Datum
Definition postgres.h:70
#define PG_RETURN_INTERVAL_P(x)
Definition timestamp.h:69

References abs_interval(), DatumGetIntervalP(), DirectFunctionCall2, fb(), 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:58
static bool gbt_timele(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:69
static bool gbt_timelt(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:80
static bool gbt_timegt(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:36
static bool gbt_timege(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:47
static int gbt_timekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:91
static float8 gbt_time_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition btree_time.c:105

Definition at line 118 of file btree_time.c.

119{
121 sizeof(TimeADT),
122 16, /* sizeof(gbtreekey16) */
130};

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().