PostgreSQL Source Code  git master
btree_utils_var.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <float.h>
#include "btree_gist.h"
#include "btree_utils_var.h"
#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "utils/rel.h"
Include dependency graph for btree_utils_var.c:

Go to the source code of this file.

Data Structures

struct  Vsrt
 
struct  gbt_vsrt_arg
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_var_decompress)
 
 PG_FUNCTION_INFO_V1 (gbt_var_fetch)
 
Datum gbt_var_decompress (PG_FUNCTION_ARGS)
 
GBT_VARKEY_R gbt_var_key_readable (const GBT_VARKEY *k)
 
static GBT_VARKEYgbt_var_key_from_datum (const struct varlena *u)
 
GBT_VARKEYgbt_var_key_copy (const GBT_VARKEY_R *u)
 
static GBT_VARKEYgbt_var_leaf2node (GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
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)
 
static bool gbt_var_node_pf_match (const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
 
static GBT_VARKEYgbt_var_node_truncate (const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
 
void gbt_var_bin_union (Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
GISTENTRYgbt_var_compress (GISTENTRY *entry, const gbtree_vinfo *tinfo)
 
Datum gbt_var_fetch (PG_FUNCTION_ARGS)
 
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)
 
static int gbt_vsrt_cmp (const void *a, const void *b, void *arg)
 
GIST_SPLITVECgbt_var_picksplit (const GistEntryVector *entryvec, GIST_SPLITVEC *v, 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)
 

Function Documentation

◆ gbt_bytea_pf_match()

static bool gbt_bytea_pf_match ( const bytea pf,
const bytea query,
const gbtree_vinfo tinfo 
)
static

Definition at line 169 of file btree_utils_var.c.

References VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_node_pf_match(), and gbt_var_penalty().

170 {
171  bool out = false;
172  int32 qlen = VARSIZE(query) - VARHDRSZ;
173  int32 nlen = VARSIZE(pf) - VARHDRSZ;
174 
175  if (nlen <= qlen)
176  {
177  char *q = VARDATA(query);
178  char *n = VARDATA(pf);
179 
180  out = (memcmp(q, n, nlen) == 0);
181  }
182 
183  return out;
184 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
signed int int32
Definition: c.h:347

◆ gbt_var_bin_union()

void gbt_var_bin_union ( Datum u,
GBT_VARKEY e,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 234 of file btree_utils_var.c.

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

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

236 {
238  GBT_VARKEY_R nr;
239 
240  if (eo.lower == eo.upper) /* leaf */
241  {
242  GBT_VARKEY *tmp;
243 
244  tmp = gbt_var_leaf2node(e, tinfo, flinfo);
245  if (tmp != e)
246  eo = gbt_var_key_readable(tmp);
247  }
248 
249  if (DatumGetPointer(*u))
250  {
252  bool update = false;
253 
254  nr.lower = ro.lower;
255  nr.upper = ro.upper;
256 
257  if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
258  {
259  nr.lower = eo.lower;
260  update = true;
261  }
262 
263  if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
264  {
265  nr.upper = eo.upper;
266  update = true;
267  }
268 
269  if (update)
271  }
272  else
273  {
274  nr.lower = eo.lower;
275  nr.upper = eo.upper;
277  }
278 }
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define PointerGetDatum(X)
Definition: postgres.h:556
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
#define DatumGetPointer(X)
Definition: postgres.h:549
Definition: c.h:556

◆ gbt_var_compress()

GISTENTRY* gbt_var_compress ( GISTENTRY entry,
const gbtree_vinfo tinfo 
)

Definition at line 282 of file btree_utils_var.c.

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_bpchar_compress(), gbt_bytea_compress(), gbt_numeric_compress(), and gbt_text_compress().

283 {
284  GISTENTRY *retval;
285 
286  if (entry->leafkey)
287  {
288  struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
289  GBT_VARKEY *r;
290 
291  r = gbt_var_key_from_datum(leaf);
292 
293  retval = palloc(sizeof(GISTENTRY));
294  gistentryinit(*retval, PointerGetDatum(r),
295  entry->rel, entry->page,
296  entry->offset, true);
297  }
298  else
299  retval = entry;
300 
301  return retval;
302 }
Relation rel
Definition: gist.h:132
#define PointerGetDatum(X)
Definition: postgres.h:556
static GBT_VARKEY * gbt_var_key_from_datum(const struct varlena *u)
Page page
Definition: gist.h:133
Datum key
Definition: gist.h:131
bool leafkey
Definition: gist.h:135
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:215
void * palloc(Size size)
Definition: mcxt.c:949
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
Definition: c.h:556
OffsetNumber offset
Definition: gist.h:134

◆ gbt_var_consistent()

bool gbt_var_consistent ( GBT_VARKEY_R key,
const void *  query,
StrategyNumber  strategy,
Oid  collation,
bool  is_leaf,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 555 of file btree_utils_var.c.

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(), GBT_VARKEY_R::lower, and GBT_VARKEY_R::upper.

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

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

◆ gbt_var_decompress()

Datum gbt_var_decompress ( PG_FUNCTION_ARGS  )

Definition at line 36 of file btree_utils_var.c.

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

37 {
38  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
40 
41  if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
42  {
43  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
44 
45  gistentryinit(*retval, PointerGetDatum(key),
46  entry->rel, entry->page,
47  entry->offset, false);
48 
49  PG_RETURN_POINTER(retval);
50  }
51 
52  PG_RETURN_POINTER(entry);
53 }
Relation rel
Definition: gist.h:132
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Page page
Definition: gist.h:133
Datum key
Definition: gist.h:131
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:215
#define DatumGetPointer(X)
Definition: postgres.h:549
void * palloc(Size size)
Definition: mcxt.c:949
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
Definition: c.h:556
OffsetNumber offset
Definition: gist.h:134

◆ gbt_var_fetch()

Datum gbt_var_fetch ( PG_FUNCTION_ARGS  )

Definition at line 306 of file btree_utils_var.c.

References gbt_var_key_readable(), gistentryinit, sort-test::key, GISTENTRY::key, GBT_VARKEY_R::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, and GISTENTRY::rel.

307 {
308  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
311  GISTENTRY *retval;
312 
313  retval = palloc(sizeof(GISTENTRY));
314  gistentryinit(*retval, PointerGetDatum(r.lower),
315  entry->rel, entry->page,
316  entry->offset, true);
317 
318  PG_RETURN_POINTER(retval);
319 }
Relation rel
Definition: gist.h:132
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Page page
Definition: gist.h:133
Datum key
Definition: gist.h:131
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:215
void * palloc(Size size)
Definition: mcxt.c:949
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
Definition: c.h:556
OffsetNumber offset
Definition: gist.h:134

◆ gbt_var_key_copy()

GBT_VARKEY* gbt_var_key_copy ( const GBT_VARKEY_R u)

Definition at line 90 of file btree_utils_var.c.

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

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 VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
#define INTALIGN(LEN)
Definition: c.h:689
signed int int32
Definition: c.h:347
void * palloc0(Size size)
Definition: mcxt.c:980
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ gbt_var_key_from_datum()

static GBT_VARKEY* gbt_var_key_from_datum ( const struct varlena u)
static

Definition at line 74 of file btree_utils_var.c.

References palloc(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_compress().

75 {
76  int32 lowersize = VARSIZE(u);
77  GBT_VARKEY *r;
78 
79  r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
80  memcpy(VARDATA(r), u, lowersize);
81  SET_VARSIZE(r, lowersize + VARHDRSZ);
82 
83  return r;
84 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
signed int int32
Definition: c.h:347
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ gbt_var_key_readable()

GBT_VARKEY_R gbt_var_key_readable ( const GBT_VARKEY k)

Definition at line 57 of file btree_utils_var.c.

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

Referenced by gbt_bit_consistent(), gbt_bit_l2n(), gbt_bpchar_consistent(), gbt_bytea_consistent(), gbt_numeric_consistent(), gbt_numeric_penalty(), gbt_text_consistent(), 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().

58 {
59  GBT_VARKEY_R r;
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 }
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
#define INTALIGN(LEN)
Definition: c.h:689
Definition: c.h:556

◆ gbt_var_leaf2node()

static GBT_VARKEY* gbt_var_leaf2node ( GBT_VARKEY leaf,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)
static

Definition at line 106 of file btree_utils_var.c.

References gbtree_vinfo::f_l2n.

Referenced by gbt_var_bin_union(), gbt_var_penalty(), and gbt_var_picksplit().

107 {
108  GBT_VARKEY *out = leaf;
109 
110  if (tinfo->f_l2n)
111  out = tinfo->f_l2n(leaf, flinfo);
112 
113  return out;
114 }
GBT_VARKEY *(* f_l2n)(GBT_VARKEY *, FmgrInfo *flinfo)
Definition: c.h:556

◆ gbt_var_node_cp_len()

static int32 gbt_var_node_cp_len ( const GBT_VARKEY node,
const gbtree_vinfo tinfo 
)
static

Definition at line 121 of file btree_utils_var.c.

References gbtree_vinfo::eml, gbt_var_key_readable(), i, GBT_VARKEY_R::lower, Min, pg_mblen(), GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

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

122 {
124  int32 i = 0;
125  int32 l = 0;
126  int32 t1len = VARSIZE(r.lower) - VARHDRSZ;
127  int32 t2len = VARSIZE(r.upper) - VARHDRSZ;
128  int32 ml = Min(t1len, t2len);
129  char *p1 = VARDATA(r.lower);
130  char *p2 = VARDATA(r.upper);
131 
132  if (ml == 0)
133  return 0;
134 
135  while (i < ml)
136  {
137  if (tinfo->eml > 1 && l == 0)
138  {
139  if ((l = pg_mblen(p1)) != pg_mblen(p2))
140  {
141  return i;
142  }
143  }
144  if (*p1 != *p2)
145  {
146  if (tinfo->eml > 1)
147  {
148  return (i - l + 1);
149  }
150  else
151  {
152  return i;
153  }
154  }
155 
156  p1++;
157  p2++;
158  l--;
159  i++;
160  }
161  return ml; /* lower == upper */
162 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
#define Min(x, y)
Definition: c.h:911
signed int int32
Definition: c.h:347
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
int i

◆ gbt_var_node_pf_match()

static bool gbt_var_node_pf_match ( const GBT_VARKEY_R node,
const bytea query,
const gbtree_vinfo tinfo 
)
static

Definition at line 191 of file btree_utils_var.c.

References gbt_bytea_pf_match(), GBT_VARKEY_R::lower, gbtree_vinfo::trnc, and GBT_VARKEY_R::upper.

Referenced by gbt_var_consistent().

192 {
193  return (tinfo->trnc && (
194  gbt_bytea_pf_match(node->lower, query, tinfo) ||
195  gbt_bytea_pf_match(node->upper, query, tinfo)
196  ));
197 }
static bool gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)

◆ gbt_var_node_truncate()

static GBT_VARKEY* gbt_var_node_truncate ( const GBT_VARKEY node,
int32  cpf_length,
const gbtree_vinfo tinfo 
)
static

Definition at line 205 of file btree_utils_var.c.

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

Referenced by gbt_var_picksplit(), and gbt_var_union().

206 {
207  GBT_VARKEY *out = NULL;
209  int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
210  int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
211  int32 si;
212  char *out2;
213 
214  len1 = Min(len1, (cpf_length + 1));
215  len2 = Min(len2, (cpf_length + 1));
216 
217  si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
218  out = (GBT_VARKEY *) palloc0(si);
219  SET_VARSIZE(out, si);
220 
221  memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
222  SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
223 
224  out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
225  memcpy(out2, r.upper, len2 + VARHDRSZ);
226  SET_VARSIZE(out2, len2 + VARHDRSZ);
227 
228  return out;
229 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
#define Min(x, y)
Definition: c.h:911
#define INTALIGN(LEN)
Definition: c.h:689
signed int int32
Definition: c.h:347
void * palloc0(Size size)
Definition: mcxt.c:980
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ gbt_var_penalty()

float* gbt_var_penalty ( float *  res,
const GISTENTRY o,
const GISTENTRY n,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 379 of file btree_utils_var.c.

References Abs, 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, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

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

381 {
382  GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
383  GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
384  GBT_VARKEY_R ok,
385  nk;
386 
387  *res = 0.0;
388 
389  nk = gbt_var_key_readable(newe);
390  if (nk.lower == nk.upper) /* leaf */
391  {
392  GBT_VARKEY *tmp;
393 
394  tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
395  if (tmp != newe)
396  nk = gbt_var_key_readable(tmp);
397  }
398  ok = gbt_var_key_readable(orge);
399 
400  if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
401  *res = 0.0;
402  else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
403  gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
404  (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
405  gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
406  {
407  Datum d = PointerGetDatum(0);
408  double dres;
409  int32 ol,
410  ul;
411 
412  gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
413  ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
414  gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
415  ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
416 
417  if (ul < ol)
418  {
419  dres = (ol - ul); /* reduction of common prefix len */
420  }
421  else
422  {
424  unsigned char tmp[4];
425 
426  tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
427  tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
428  tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
429  tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
430  dres = Abs(tmp[0] - tmp[1]) + Abs(tmp[3] - tmp[2]);
431  dres /= 256.0;
432  }
433 
434  *res += FLT_MIN;
435  *res += (float) (dres / ((double) (ol + 1)));
436  *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
437  }
438 
439  return res;
440 }
Relation rel
Definition: gist.h:132
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PointerGetDatum(X)
Definition: postgres.h:556
#define VARHDRSZ
Definition: c.h:562
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
signed int int32
Definition: c.h:347
#define Abs(x)
Definition: c.h:917
Datum key
Definition: gist.h:131
static bool gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
uintptr_t Datum
Definition: postgres.h:367
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
TupleDesc rd_att
Definition: rel.h:84
#define DatumGetPointer(X)
Definition: postgres.h:549
Definition: c.h:556

◆ gbt_var_picksplit()

GIST_SPLITVEC* gbt_var_picksplit ( const GistEntryVector entryvec,
GIST_SPLITVEC v,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 459 of file btree_utils_var.c.

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, gbt_vsrt_arg::tinfo, 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().

461 {
462  OffsetNumber i,
463  maxoff = entryvec->n - 1;
464  Vsrt *arr;
465  int svcntr = 0,
466  nbytes;
467  char *cur;
468  GBT_VARKEY **sv = NULL;
469  gbt_vsrt_arg varg;
470 
471  arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
472  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
473  v->spl_left = (OffsetNumber *) palloc(nbytes);
474  v->spl_right = (OffsetNumber *) palloc(nbytes);
475  v->spl_ldatum = PointerGetDatum(0);
476  v->spl_rdatum = PointerGetDatum(0);
477  v->spl_nleft = 0;
478  v->spl_nright = 0;
479 
480  sv = palloc(sizeof(bytea *) * (maxoff + 1));
481 
482  /* Sort entries */
483 
484  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
485  {
486  GBT_VARKEY_R ro;
487 
488  cur = (char *) DatumGetPointer(entryvec->vector[i].key);
489  ro = gbt_var_key_readable((GBT_VARKEY *) cur);
490  if (ro.lower == ro.upper) /* leaf */
491  {
492  sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
493  arr[i].t = sv[svcntr];
494  if (sv[svcntr] != (GBT_VARKEY *) cur)
495  svcntr++;
496  }
497  else
498  arr[i].t = (GBT_VARKEY *) cur;
499  arr[i].i = i;
500  }
501 
502  /* sort */
503  varg.tinfo = tinfo;
504  varg.collation = collation;
505  varg.flinfo = flinfo;
506  qsort_arg((void *) &arr[FirstOffsetNumber],
507  maxoff - FirstOffsetNumber + 1,
508  sizeof(Vsrt),
509  gbt_vsrt_cmp,
510  (void *) &varg);
511 
512  /* We do simply create two parts */
513 
514  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
515  {
516  if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
517  {
518  gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
519  v->spl_left[v->spl_nleft] = arr[i].i;
520  v->spl_nleft++;
521  }
522  else
523  {
524  gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
525  v->spl_right[v->spl_nright] = arr[i].i;
526  v->spl_nright++;
527  }
528  }
529 
530  /* Truncate (=compress) key */
531  if (tinfo->trnc)
532  {
535  GBT_VARKEY *dl;
536  GBT_VARKEY *dr;
537 
538  ll = Max(ll, lr);
539  ll++;
540 
543  v->spl_ldatum = PointerGetDatum(dl);
544  v->spl_rdatum = PointerGetDatum(dr);
545  }
546 
547  return v;
548 }
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define PointerGetDatum(X)
Definition: postgres.h:556
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
OffsetNumber * spl_left
Definition: gist.h:113
Datum spl_rdatum
Definition: gist.h:120
int32 n
Definition: gist.h:206
struct cursor * cur
Definition: ecpg.c:28
static int gbt_vsrt_cmp(const void *a, const void *b, void *arg)
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
int spl_nleft
Definition: gist.h:114
signed int int32
Definition: c.h:347
uint16 OffsetNumber
Definition: off.h:24
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:207
int spl_nright
Definition: gist.h:119
Datum key
Definition: gist.h:131
#define FirstOffsetNumber
Definition: off.h:27
GBT_VARKEY * t
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
#define Max(x, y)
Definition: c.h:905
Datum spl_ldatum
Definition: gist.h:115
static const gbtree_vinfo tinfo
Definition: btree_bit.c:110
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
OffsetNumber * spl_right
Definition: gist.h:118
#define DatumGetPointer(X)
Definition: postgres.h:549
FmgrInfo * flinfo
void * palloc(Size size)
Definition: mcxt.c:949
int i
const gbtree_vinfo * tinfo
Definition: c.h:556

◆ gbt_var_same()

bool gbt_var_same ( Datum  d1,
Datum  d2,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 362 of file btree_utils_var.c.

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

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

364 {
365  GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
366  GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
367  GBT_VARKEY_R r1,
368  r2;
369 
370  r1 = gbt_var_key_readable(t1);
371  r2 = gbt_var_key_readable(t2);
372 
373  return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
374  tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
375 }
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
#define DatumGetPointer(X)
Definition: postgres.h:549
Definition: c.h:556

◆ gbt_var_union()

GBT_VARKEY* gbt_var_union ( const GistEntryVector entryvec,
int32 size,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 323 of file btree_utils_var.c.

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, gbtree_vinfo::trnc, and GistEntryVector::vector.

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

325 {
326  int i = 0,
327  numranges = entryvec->n;
328  GBT_VARKEY *cur;
329  Datum out;
330  GBT_VARKEY_R rk;
331 
332  *size = sizeof(GBT_VARKEY);
333 
334  cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
335  rk = gbt_var_key_readable(cur);
336  out = PointerGetDatum(gbt_var_key_copy(&rk));
337 
338  for (i = 1; i < numranges; i++)
339  {
340  cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
341  gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
342  }
343 
344 
345  /* Truncate (=compress) key */
346  if (tinfo->trnc)
347  {
348  int32 plen;
349  GBT_VARKEY *trc = NULL;
350 
351  plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
352  trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
353 
354  out = PointerGetDatum(trc);
355  }
356 
357  return ((GBT_VARKEY *) DatumGetPointer(out));
358 }
#define PointerGetDatum(X)
Definition: postgres.h:556
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
int32 n
Definition: gist.h:206
struct cursor * cur
Definition: ecpg.c:28
bytea GBT_VARKEY
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
signed int int32
Definition: c.h:347
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:207
Datum key
Definition: gist.h:131
uintptr_t Datum
Definition: postgres.h:367
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
#define DatumGetPointer(X)
Definition: postgres.h:549
int i
Definition: c.h:556

◆ gbt_vsrt_cmp()

static int gbt_vsrt_cmp ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 444 of file btree_utils_var.c.

References gbt_vsrt_arg::collation, gbtree_vinfo::f_cmp, gbt_vsrt_arg::flinfo, gbt_var_key_readable(), GBT_VARKEY_R::lower, gbt_vsrt_arg::tinfo, and GBT_VARKEY_R::upper.

Referenced by gbt_var_picksplit().

445 {
446  GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
447  GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
448  const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
449  int res;
450 
451  res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
452  if (res == 0)
453  return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
454 
455  return res;
456 }
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
FmgrInfo * flinfo
const gbtree_vinfo * tinfo
void * arg

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( gbt_var_decompress  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( gbt_var_fetch  )