PostgreSQL Source Code git master
Data Structures | Macros | Typedefs | Functions
btree_utils_var.h File Reference
#include "access/gist.h"
#include "btree_gist.h"
Include dependency graph for btree_utils_var.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct   GBT_VARKEY_R
 
struct   gbtree_vinfo
 

Macros

#define  GBT_FREE_IF_COPY(ptr1, ptr2)
 

Typedefs

typedef bytea  GBT_VARKEY
 

Functions

 
 
 
GBT_VARKEYgbt_var_union (const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
bool  gbt_var_same (Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
float *  gbt_var_penalty (float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
bool  gbt_var_consistent (GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
GIST_SPLITVECgbt_var_picksplit (const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
void  gbt_var_bin_union (Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 

Macro Definition Documentation

GBT_FREE_IF_COPY

#define GBT_FREE_IF_COPY (   ptr1,
  ptr2 
)
Value:
do { \
if ((Pointer) (ptr1) != DatumGetPointer(ptr2)) \
pfree(ptr1); \
} while (0)
char * Pointer
Definition: c.h:529
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322

Definition at line 50 of file btree_utils_var.h.

Typedef Documentation

GBT_VARKEY

typedef bytea GBT_VARKEY

Definition at line 11 of file btree_utils_var.h.

Function Documentation

gbt_var_bin_union()

void gbt_var_bin_union ( Datumu,
GBT_VARKEYe,
Oid  collation,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 233 of file btree_utils_var.c.

235{
237 GBT_VARKEY_R nr;
238
239 if (eo.lower == eo.upper) /* leaf */
240 {
241 GBT_VARKEY *tmp;
242
243 tmp = gbt_var_leaf2node(e, tinfo, flinfo);
244 if (tmp != e)
245 eo = gbt_var_key_readable(tmp);
246 }
247
248 if (DatumGetPointer(*u))
249 {
251 bool update = false;
252
253 nr.lower = ro.lower;
254 nr.upper = ro.upper;
255
256 if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
257 {
258 nr.lower = eo.lower;
259 update = true;
260 }
261
262 if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
263 {
264 nr.upper = eo.upper;
265 update = true;
266 }
267
268 if (update)
270 }
271 else
272 {
273 nr.lower = eo.lower;
274 nr.upper = eo.upper;
276 }
277}
static const gbtree_vinfo tinfo
Definition: btree_bit.c:109
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
e
e
Definition: preproc-init.c:82
bytea * upper
bytea * lower
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
Definition: c.h:692

References DatumGetPointer(), gbtree_vinfo::f_cmp, gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_leaf2node(), GBT_VARKEY_R::lower, PointerGetDatum(), tinfo, and GBT_VARKEY_R::upper.

Referenced by gbt_numeric_penalty(), gbt_var_penalty(), gbt_var_picksplit(), and gbt_var_union().

gbt_var_compress()

GISTENTRY * gbt_var_compress ( GISTENTRYentry,
const gbtree_vinfotinfo 
)

Definition at line 281 of file btree_utils_var.c.

282{
283 GISTENTRY *retval;
284
285 if (entry->leafkey)
286 {
287 struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
288 GBT_VARKEY *r;
289
290 r = gbt_var_key_from_datum(leaf);
291
292 retval = palloc(sizeof(GISTENTRY));
293 gistentryinit(*retval, PointerGetDatum(r),
294 entry->rel, entry->page,
295 entry->offset, true);
296 }
297 else
298 retval = entry;
299
300 return retval;
301}
static GBT_VARKEY * gbt_var_key_from_datum(const struct varlena *u)
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:245
void * palloc(Size size)
Definition: mcxt.c:1365
Definition: gist.h:160
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 gbt_var_key_from_datum(), gistentryinit, GISTENTRY::key, GISTENTRY::leafkey, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PointerGetDatum(), and GISTENTRY::rel.

Referenced by gbt_bit_compress(), gbt_bytea_compress(), gbt_numeric_compress(), and gbt_text_compress().

gbt_var_consistent()

bool gbt_var_consistent ( GBT_VARKEY_Rkey,
const void *  query,
StrategyNumber  strategy,
Oid  collation,
bool  is_leaf,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 554 of file btree_utils_var.c.

561{
562 bool retval = false;
563
564 switch (strategy)
565 {
567 if (is_leaf)
568 retval = tinfo->f_ge(query, key->lower, collation, flinfo);
569 else
570 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
571 || gbt_var_node_pf_match(key, query, tinfo);
572 break;
574 if (is_leaf)
575 retval = tinfo->f_gt(query, key->lower, collation, flinfo);
576 else
577 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
578 || gbt_var_node_pf_match(key, query, tinfo);
579 break;
581 if (is_leaf)
582 retval = tinfo->f_eq(query, key->lower, collation, flinfo);
583 else
584 retval =
585 (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
586 tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
588 break;
590 if (is_leaf)
591 retval = tinfo->f_lt(query, key->upper, collation, flinfo);
592 else
593 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
594 || gbt_var_node_pf_match(key, query, tinfo);
595 break;
597 if (is_leaf)
598 retval = tinfo->f_le(query, key->upper, collation, flinfo);
599 else
600 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
601 || gbt_var_node_pf_match(key, query, tinfo);
602 break;
604 retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
605 tinfo->f_eq(query, key->upper, collation, flinfo));
606 break;
607 default:
608 retval = false;
609 }
610
611 return retval;
612}
#define BtreeGistNotEqualStrategyNumber
Definition: btree_gist.h:10
static bool gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
bool(* f_lt)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_eq)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_gt)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_ge)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_le)(const void *, const void *, Oid, FmgrInfo *)

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BtreeGistNotEqualStrategyNumber, gbtree_vinfo::f_cmp, gbtree_vinfo::f_eq, gbtree_vinfo::f_ge, gbtree_vinfo::f_gt, gbtree_vinfo::f_le, gbtree_vinfo::f_lt, gbt_var_node_pf_match(), sort-test::key, and tinfo.

Referenced by gbt_bit_consistent(), gbt_bpchar_consistent(), gbt_bytea_consistent(), gbt_numeric_consistent(), and gbt_text_consistent().

gbt_var_key_copy()

GBT_VARKEY * gbt_var_key_copy ( const GBT_VARKEY_Ru )

Definition at line 90 of file btree_utils_var.c.

91{
92 int32 lowersize = VARSIZE(u->lower);
93 int32 uppersize = VARSIZE(u->upper);
94 GBT_VARKEY *r;
95
96 r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
97 memcpy(VARDATA(r), u->lower, lowersize);
98 memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
99 SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
100
101 return r;
102}
#define INTALIGN(LEN)
Definition: c.h:807
#define VARHDRSZ
Definition: c.h:697
int32_t int32
Definition: c.h:534
void * palloc0(Size size)
Definition: mcxt.c:1395
static Size VARSIZE(const void *PTR)
Definition: varatt.h:298
static char * VARDATA(const void *PTR)
Definition: varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition: varatt.h:432

References INTALIGN, GBT_VARKEY_R::lower, palloc0(), SET_VARSIZE(), GBT_VARKEY_R::upper, VARDATA(), VARHDRSZ, and VARSIZE().

Referenced by gbt_bit_l2n(), gbt_numeric_penalty(), gbt_var_bin_union(), and gbt_var_union().

gbt_var_key_readable()

GBT_VARKEY_R gbt_var_key_readable ( const GBT_VARKEYk )

Definition at line 57 of file btree_utils_var.c.

58{
60
61 r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
62 if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
63 r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
64 else
65 r.upper = r.lower;
66 return r;
67}

References INTALIGN, GBT_VARKEY_R::lower, GBT_VARKEY_R::upper, VARHDRSZ, and VARSIZE().

Referenced by gbt_bit_consistent(), gbt_bit_l2n(), gbt_bit_ssup_cmp(), gbt_bpchar_consistent(), gbt_bpchar_ssup_cmp(), gbt_bytea_consistent(), gbt_bytea_ssup_cmp(), gbt_numeric_consistent(), gbt_numeric_penalty(), gbt_numeric_ssup_cmp(), gbt_text_consistent(), gbt_text_ssup_cmp(), gbt_var_bin_union(), gbt_var_fetch(), gbt_var_node_cp_len(), gbt_var_node_truncate(), gbt_var_penalty(), gbt_var_picksplit(), gbt_var_same(), gbt_var_union(), and gbt_vsrt_cmp().

gbt_var_penalty()

float * gbt_var_penalty ( float *  res,
const GISTENTRYo,
const GISTENTRYn,
Oid  collation,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 378 of file btree_utils_var.c.

380{
383 GBT_VARKEY_R ok,
384 nk;
385
386 *res = 0.0;
387
388 nk = gbt_var_key_readable(newe);
389 if (nk.lower == nk.upper) /* leaf */
390 {
391 GBT_VARKEY *tmp;
392
393 tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
394 if (tmp != newe)
395 nk = gbt_var_key_readable(tmp);
396 }
397 ok = gbt_var_key_readable(orge);
398
399 if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
400 *res = 0.0;
401 else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
403 (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
405 {
406 Datum d = PointerGetDatum(0);
407 double dres;
408 int32 ol,
409 ul;
410
411 gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
413 gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
415
416 if (ul < ol)
417 {
418 dres = (ol - ul); /* reduction of common prefix len */
419 }
420 else
421 {
423 unsigned char tmp[4];
424
425 tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
426 tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
427 tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
428 tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
429 dres = abs(tmp[0] - tmp[1]) + abs(tmp[3] - tmp[2]);
430 dres /= 256.0;
431 }
432
433 *res += FLT_MIN;
434 *res += (float) (dres / ((double) (ol + 1)));
435 *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
436 }
437
438 return res;
439}
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
static bool gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
uint64_t Datum
Definition: postgres.h:70
TupleDesc rd_att
Definition: rel.h:112
int natts
Definition: tupdesc.h:137

References DatumGetPointer(), gbtree_vinfo::f_cmp, gbt_bytea_pf_match(), gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), GISTENTRY::key, GBT_VARKEY_R::lower, TupleDescData::natts, PointerGetDatum(), RelationData::rd_att, GISTENTRY::rel, tinfo, GBT_VARKEY_R::upper, VARDATA(), VARHDRSZ, and VARSIZE().

Referenced by gbt_bit_penalty(), gbt_bytea_penalty(), and gbt_text_penalty().

gbt_var_picksplit()

GIST_SPLITVEC * gbt_var_picksplit ( const GistEntryVectorentryvec,
GIST_SPLITVECv,
Oid  collation,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 458 of file btree_utils_var.c.

460{
462 maxoff = entryvec->n - 1;
463 Vsrt *arr;
464 int svcntr = 0,
465 nbytes;
466 char *cur;
467 GBT_VARKEY **sv = NULL;
468 gbt_vsrt_arg varg;
469
470 arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
471 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
472 v->spl_left = (OffsetNumber *) palloc(nbytes);
473 v->spl_right = (OffsetNumber *) palloc(nbytes);
476 v->spl_nleft = 0;
477 v->spl_nright = 0;
478
479 sv = palloc(sizeof(bytea *) * (maxoff + 1));
480
481 /* Sort entries */
482
483 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
484 {
485 GBT_VARKEY_R ro;
486
487 cur = (char *) DatumGetPointer(entryvec->vector[i].key);
489 if (ro.lower == ro.upper) /* leaf */
490 {
491 sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
492 arr[i].t = sv[svcntr];
493 if (sv[svcntr] != (GBT_VARKEY *) cur)
494 svcntr++;
495 }
496 else
497 arr[i].t = (GBT_VARKEY *) cur;
498 arr[i].i = i;
499 }
500
501 /* sort */
502 varg.tinfo = tinfo;
503 varg.collation = collation;
504 varg.flinfo = flinfo;
506 maxoff - FirstOffsetNumber + 1,
507 sizeof(Vsrt),
509 &varg);
510
511 /* We do simply create two parts */
512
513 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
514 {
515 if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
516 {
517 gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
518 v->spl_left[v->spl_nleft] = arr[i].i;
519 v->spl_nleft++;
520 }
521 else
522 {
523 gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
524 v->spl_right[v->spl_nright] = arr[i].i;
525 v->spl_nright++;
526 }
527 }
528
529 /* Truncate (=compress) key */
530 if (tinfo->trnc)
531 {
534 GBT_VARKEY *dl;
535 GBT_VARKEY *dr;
536
537 ll = Max(ll, lr);
538 ll++;
539
544 }
545
546 return v;
547}
static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
static int gbt_vsrt_cmp(const void *a, const void *b, void *arg)
#define Max(x, y)
Definition: c.h:997
struct cursor * cur
Definition: ecpg.c:29
i
int i
Definition: isn.c:77
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
int spl_nleft
Definition: gist.h:144
OffsetNumber * spl_right
Definition: gist.h:148
Datum spl_ldatum
Definition: gist.h:145
Datum spl_rdatum
Definition: gist.h:150
int spl_nright
Definition: gist.h:149
OffsetNumber * spl_left
Definition: gist.h:143
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:237
int32 n
Definition: gist.h:236
GBT_VARKEY * t
int i
FmgrInfo * flinfo
const gbtree_vinfo * tinfo

References gbt_vsrt_arg::collation, cur, DatumGetPointer(), FirstOffsetNumber, gbt_vsrt_arg::flinfo, gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), gbt_var_node_truncate(), gbt_vsrt_cmp(), Vsrt::i, i, GISTENTRY::key, GBT_VARKEY_R::lower, Max, GistEntryVector::n, OffsetNumberNext, palloc(), PointerGetDatum(), qsort_arg(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, Vsrt::t, tinfo, gbt_vsrt_arg::tinfo, gbtree_vinfo::trnc, GBT_VARKEY_R::upper, and GistEntryVector::vector.

Referenced by gbt_bit_picksplit(), gbt_bytea_picksplit(), gbt_numeric_picksplit(), and gbt_text_picksplit().

gbt_var_same()

bool gbt_var_same ( Datum  d1,
Datum  d2,
Oid  collation,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 361 of file btree_utils_var.c.

363{
366 GBT_VARKEY_R r1,
367 r2;
368
369 r1 = gbt_var_key_readable(t1);
370 r2 = gbt_var_key_readable(t2);
371
372 return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
373 tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
374}

References DatumGetPointer(), gbtree_vinfo::f_cmp, gbt_var_key_readable(), GBT_VARKEY_R::lower, tinfo, and GBT_VARKEY_R::upper.

Referenced by gbt_bit_same(), gbt_bytea_same(), gbt_numeric_same(), and gbt_text_same().

gbt_var_union()

GBT_VARKEY * gbt_var_union ( const GistEntryVectorentryvec,
int32size,
Oid  collation,
const gbtree_vinfotinfo,
FmgrInfoflinfo 
)

Definition at line 322 of file btree_utils_var.c.

324{
325 int i = 0,
326 numranges = entryvec->n;
328 Datum out;
329 GBT_VARKEY_R rk;
330
331 *size = sizeof(GBT_VARKEY);
332
333 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
336
337 for (i = 1; i < numranges; i++)
338 {
339 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
340 gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
341 }
342
343
344 /* Truncate (=compress) key */
345 if (tinfo->trnc)
346 {
347 int32 plen;
348 GBT_VARKEY *trc = NULL;
349
351 trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
352
353 out = PointerGetDatum(trc);
354 }
355
356 return ((GBT_VARKEY *) DatumGetPointer(out));
357}
bytea GBT_VARKEY

References cur, DatumGetPointer(), gbt_var_bin_union(), gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_node_cp_len(), gbt_var_node_truncate(), i, GISTENTRY::key, GistEntryVector::n, PointerGetDatum(), tinfo, gbtree_vinfo::trnc, and GistEntryVector::vector.

Referenced by gbt_bit_union(), gbt_bytea_union(), gbt_numeric_union(), and gbt_text_union().

AltStyle によって変換されたページ (->オリジナル) /