PostgreSQL Source Code git master
btree_interval.c File Reference
#include "postgres.h"
#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/fmgrprotos.h"
#include "utils/sortsupport.h"
#include "utils/timestamp.h"
Include dependency graph for btree_interval.c:

Go to the source code of this file.

Data Structures

struct  intvKEY
 

Macros

#define INTERVALSIZE   16
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_intv_compress)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_fetch)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_decompress)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_union)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_picksplit)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_consistent)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_distance)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_penalty)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_same)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_sortsupport)
 
static bool gbt_intvgt (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intvge (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intveq (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intvle (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intvlt (const void *a, const void *b, FmgrInfo *flinfo)
 
static int gbt_intvkey_cmp (const void *a, const void *b, FmgrInfo *flinfo)
 
static double intr2num (const Interval *i)
 
static float8 gbt_intv_dist (const void *a, const void *b, FmgrInfo *flinfo)
 
Intervalabs_interval (Interval *a)
 
 PG_FUNCTION_INFO_V1 (interval_dist)
 
Datum interval_dist (PG_FUNCTION_ARGS)
 
Datum gbt_intv_compress (PG_FUNCTION_ARGS)
 
Datum gbt_intv_fetch (PG_FUNCTION_ARGS)
 
Datum gbt_intv_decompress (PG_FUNCTION_ARGS)
 
Datum gbt_intv_consistent (PG_FUNCTION_ARGS)
 
Datum gbt_intv_distance (PG_FUNCTION_ARGS)
 
Datum gbt_intv_union (PG_FUNCTION_ARGS)
 
Datum gbt_intv_penalty (PG_FUNCTION_ARGS)
 
Datum gbt_intv_picksplit (PG_FUNCTION_ARGS)
 
Datum gbt_intv_same (PG_FUNCTION_ARGS)
 
static int gbt_intv_ssup_cmp (Datum x, Datum y, SortSupport ssup)
 
Datum gbt_intv_sortsupport (PG_FUNCTION_ARGS)
 

Variables

static const gbtree_ninfo tinfo
 

Macro Definition Documentation

◆ INTERVALSIZE

#define INTERVALSIZE   16

Definition at line 95 of file btree_interval.c.

Function Documentation

◆ abs_interval()

Interval * abs_interval ( Interval a)

Definition at line 113 of file btree_interval.c.

114{
115 static const Interval zero = {0, 0, 0};
116
119 IntervalPGetDatum(&zero))))
122
123 return a;
124}
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2600
Datum interval_um(PG_FUNCTION_ARGS)
Definition: timestamp.c:3464
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
int a
Definition: isn.c:73
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58
static Interval * DatumGetIntervalP(Datum X)
Definition: timestamp.h:40

References a, DatumGetBool(), DatumGetIntervalP(), DirectFunctionCall1, DirectFunctionCall2, interval_lt(), interval_um(), and IntervalPGetDatum().

Referenced by interval_dist(), time_dist(), ts_dist(), and tstz_dist().

◆ gbt_intv_compress()

Datum gbt_intv_compress ( PG_FUNCTION_ARGS  )

Definition at line 143 of file btree_interval.c.

144{
145 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
146 GISTENTRY *retval = entry;
147
148 if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
149 {
150 char *r = (char *) palloc(2 * INTERVALSIZE);
151
152 retval = palloc(sizeof(GISTENTRY));
153
154 if (entry->leafkey)
155 {
157
158 memcpy(r, key, INTERVALSIZE);
159 memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
160 }
161 else
162 {
163 intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
164
165 memcpy(r, &key->lower, INTERVALSIZE);
166 memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
167 }
168 gistentryinit(*retval, PointerGetDatum(r),
169 entry->rel, entry->page,
170 entry->offset, false);
171 }
172
173 PG_RETURN_POINTER(retval);
174}
#define INTERVALSIZE
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:245
void * palloc(Size size)
Definition: mcxt.c:1943
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
OffsetNumber offset
Definition: gist.h:164
Datum key
Definition: gist.h:161
Page page
Definition: gist.h:163
Relation rel
Definition: gist.h:162
bool leafkey
Definition: gist.h:165

References DatumGetIntervalP(), DatumGetPointer(), gistentryinit, INTERVALSIZE, GISTENTRY::key, sort-test::key, GISTENTRY::leafkey, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), and GISTENTRY::rel.

◆ gbt_intv_consistent()

Datum gbt_intv_consistent ( PG_FUNCTION_ARGS  )

Definition at line 208 of file btree_interval.c.

209{
210 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
211 Interval *query = PG_GETARG_INTERVAL_P(1);
213
214 /* Oid subtype = PG_GETARG_OID(3); */
215 bool *recheck = (bool *) PG_GETARG_POINTER(4);
216 intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
218
219 /* All cases served by this function are exact */
220 *recheck = false;
221
222 key.lower = (GBT_NUMKEY *) &kkk->lower;
223 key.upper = (GBT_NUMKEY *) &kkk->upper;
224
225 PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
226 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
227}
static const gbtree_ninfo tinfo
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_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:171
uint16 StrategyNumber
Definition: stratnum.h:22
Interval lower
Interval upper
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:65

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

◆ gbt_intv_decompress()

Datum gbt_intv_decompress ( PG_FUNCTION_ARGS  )

Definition at line 185 of file btree_interval.c.

186{
187 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
188 GISTENTRY *retval = entry;
189
190 if (INTERVALSIZE != sizeof(Interval))
191 {
192 intvKEY *r = palloc(sizeof(intvKEY));
193 char *key = DatumGetPointer(entry->key);
194
195 retval = palloc(sizeof(GISTENTRY));
196 memcpy(&r->lower, key, INTERVALSIZE);
197 memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
198
199 gistentryinit(*retval, PointerGetDatum(r),
200 entry->rel, entry->page,
201 entry->offset, false);
202 }
203 PG_RETURN_POINTER(retval);
204}

References DatumGetPointer(), gistentryinit, INTERVALSIZE, GISTENTRY::key, sort-test::key, intvKEY::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), GISTENTRY::rel, and intvKEY::upper.

◆ gbt_intv_dist()

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

Definition at line 83 of file btree_interval.c.

84{
85 return fabs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
86}
static double intr2num(const Interval *i)
int b
Definition: isn.c:74

References a, b, and intr2num().

◆ gbt_intv_distance()

Datum gbt_intv_distance ( PG_FUNCTION_ARGS  )

Definition at line 231 of file btree_interval.c.

232{
233 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
234 Interval *query = PG_GETARG_INTERVAL_P(1);
235
236 /* Oid subtype = PG_GETARG_OID(3); */
237 intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
239
240 key.lower = (GBT_NUMKEY *) &kkk->lower;
241 key.upper = (GBT_NUMKEY *) &kkk->upper;
242
244 &tinfo, fcinfo->flinfo));
245}
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, intvKEY::lower, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, PG_RETURN_FLOAT8, tinfo, and intvKEY::upper.

◆ gbt_intv_fetch()

Datum gbt_intv_fetch ( PG_FUNCTION_ARGS  )

Definition at line 177 of file btree_interval.c.

178{
179 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
180
182}
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)

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

◆ gbt_intv_penalty()

Datum gbt_intv_penalty ( PG_FUNCTION_ARGS  )

Definition at line 260 of file btree_interval.c.

261{
262 intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
264 float *result = (float *) PG_GETARG_POINTER(2);
265 double iorg[2],
266 inew[2];
267
268 iorg[0] = intr2num(&origentry->lower);
269 iorg[1] = intr2num(&origentry->upper);
270 inew[0] = intr2num(&newentry->lower);
271 inew[1] = intr2num(&newentry->upper);
272
273 penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
274
275 PG_RETURN_POINTER(result);
276}
#define penalty_num(result, olower, oupper, nlower, nupper)

References DatumGetPointer(), intr2num(), sort-test::key, intvKEY::lower, penalty_num, PG_GETARG_POINTER, PG_RETURN_POINTER, and intvKEY::upper.

◆ gbt_intv_picksplit()

Datum gbt_intv_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 279 of file btree_interval.c.

280{
283 &tinfo, fcinfo->flinfo));
284}
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_intv_same()

Datum gbt_intv_same ( PG_FUNCTION_ARGS  )

Definition at line 287 of file btree_interval.c.

288{
289 intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
290 intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
291 bool *result = (bool *) PG_GETARG_POINTER(2);
292
293 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
294 PG_RETURN_POINTER(result);
295}
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_intv_sortsupport()

Datum gbt_intv_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 310 of file btree_interval.c.

311{
313
315 ssup->ssup_extra = NULL;
316
318}
static int gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
#define PG_RETURN_VOID()
Definition: fmgr.h:349
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
void * ssup_extra
Definition: sortsupport.h:87

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

◆ gbt_intv_ssup_cmp()

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

Definition at line 298 of file btree_interval.c.

299{
300 intvKEY *arg1 = (intvKEY *) DatumGetPointer(x);
301 intvKEY *arg2 = (intvKEY *) DatumGetPointer(y);
302
303 /* for leaf items we expect lower == upper, so only compare lower */
305 IntervalPGetDatum(&arg1->lower),
306 IntervalPGetDatum(&arg2->lower)));
307}
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2636
int y
Definition: isn.c:76
int x
Definition: isn.c:75
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207

References DatumGetInt32(), DatumGetPointer(), DirectFunctionCall2, interval_cmp(), IntervalPGetDatum(), intvKEY::lower, x, and y.

Referenced by gbt_intv_sortsupport().

◆ gbt_intv_union()

Datum gbt_intv_union ( PG_FUNCTION_ARGS  )

Definition at line 249 of file btree_interval.c.

250{
252 void *out = palloc(sizeof(intvKEY));
253
254 *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
255 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
256}
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

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

◆ gbt_intveq()

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

Definition at line 44 of file btree_interval.c.

45{
47}
Datum interval_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2582

References a, b, DatumGetBool(), DirectFunctionCall2, interval_eq(), and IntervalPGetDatum().

◆ gbt_intvge()

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

Definition at line 38 of file btree_interval.c.

39{
41}
Datum interval_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2627

References a, b, DatumGetBool(), DirectFunctionCall2, interval_ge(), and IntervalPGetDatum().

◆ gbt_intvgt()

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

Definition at line 32 of file btree_interval.c.

33{
35}
Datum interval_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2609

References a, b, DatumGetBool(), DirectFunctionCall2, interval_gt(), and IntervalPGetDatum().

◆ gbt_intvkey_cmp()

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

Definition at line 62 of file btree_interval.c.

63{
64 intvKEY *ia = (intvKEY *) (((const Nsrt *) a)->t);
65 intvKEY *ib = (intvKEY *) (((const Nsrt *) b)->t);
66 int res;
67
69 if (res == 0)
71
72 return res;
73}

References a, b, DatumGetInt32(), DirectFunctionCall2, interval_cmp(), IntervalPGetDatum(), intvKEY::lower, and intvKEY::upper.

◆ gbt_intvle()

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

Definition at line 50 of file btree_interval.c.

51{
53}
Datum interval_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2618

References a, b, DatumGetBool(), DirectFunctionCall2, interval_le(), and IntervalPGetDatum().

◆ gbt_intvlt()

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

◆ interval_dist()

Datum interval_dist ( PG_FUNCTION_ARGS  )

Definition at line 128 of file btree_interval.c.

129{
132 PG_GETARG_DATUM(1));
133
135}
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3577
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, interval_mi(), PG_GETARG_DATUM, and PG_RETURN_INTERVAL_P.

◆ intr2num()

static double intr2num ( const Interval i)
static

Definition at line 77 of file btree_interval.c.

78{
79 return INTERVAL_TO_SEC(i);
80}
#define INTERVAL_TO_SEC(ivp)
int i
Definition: isn.c:77

References i, and INTERVAL_TO_SEC.

Referenced by gbt_intv_dist(), and gbt_intv_penalty().

◆ PG_FUNCTION_INFO_V1() [1/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_compress  )

◆ PG_FUNCTION_INFO_V1() [2/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_consistent  )

◆ PG_FUNCTION_INFO_V1() [3/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_decompress  )

◆ PG_FUNCTION_INFO_V1() [4/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_distance  )

◆ PG_FUNCTION_INFO_V1() [5/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_fetch  )

◆ PG_FUNCTION_INFO_V1() [6/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_penalty  )

◆ PG_FUNCTION_INFO_V1() [7/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_picksplit  )

◆ PG_FUNCTION_INFO_V1() [8/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_same  )

◆ PG_FUNCTION_INFO_V1() [9/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_sortsupport  )

◆ PG_FUNCTION_INFO_V1() [10/11]

PG_FUNCTION_INFO_V1 ( gbt_intv_union  )

◆ PG_FUNCTION_INFO_V1() [11/11]

PG_FUNCTION_INFO_V1 ( interval_dist  )

Variable Documentation

◆ tinfo

const gbtree_ninfo tinfo
static
Initial value:
=
{
sizeof(Interval),
32,
}
@ gbt_t_intv
Definition: btree_gist.h:28
static bool gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
static float8 gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
static int gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)

Definition at line 97 of file btree_interval.c.

Referenced by gbt_intv_consistent(), gbt_intv_distance(), gbt_intv_fetch(), gbt_intv_picksplit(), gbt_intv_same(), and gbt_intv_union().