Fix minor nbtree page deletion buffer lock issue.
[pgsql.git] / contrib / btree_gist / btree_utils_var.c
blob2886c08b85e405629524cd9a46a51bf7c42a4f57
1 /*
2 * contrib/btree_gist/btree_utils_var.c
3 */
4 #include "postgres.h"
6 #include <math.h>
7 #include <limits.h>
8 #include <float.h>
10 #include "btree_gist.h"
11 #include "btree_utils_var.h"
12 #include "utils/builtins.h"
13 #include "utils/pg_locale.h"
14 #include "utils/rel.h"
16 /* used for key sorting */
17 typedef struct
19 int i;
20 GBT_VARKEY *t;
21 } Vsrt;
23 typedef struct
25 const gbtree_vinfo *tinfo;
26 Oid collation;
27 FmgrInfo *flinfo;
28 } gbt_vsrt_arg;
31 PG_FUNCTION_INFO_V1(gbt_var_decompress);
32 PG_FUNCTION_INFO_V1(gbt_var_fetch);
35 Datum
36 gbt_var_decompress(PG_FUNCTION_ARGS)
38 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
39 GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
41 if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
43 GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
45 gistentryinit(*retval, PointerGetDatum(key),
46 entry->rel, entry->page,
47 entry->offset, false);
49 PG_RETURN_POINTER(retval);
52 PG_RETURN_POINTER(entry);
55 /* Returns a better readable representation of variable key ( sets pointer ) */
56 GBT_VARKEY_R
57 gbt_var_key_readable(const GBT_VARKEY *k)
59 GBT_VARKEY_R r;
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;
71 * Create a leaf-entry to store in the index, from a single Datum.
73 static GBT_VARKEY *
74 gbt_var_key_from_datum(const struct varlena *u)
76 int32 lowersize = VARSIZE(u);
77 GBT_VARKEY *r;
79 r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
80 memcpy(VARDATA(r), u, lowersize);
81 SET_VARSIZE(r, lowersize + VARHDRSZ);
83 return r;
87 * Create an entry to store in the index, from lower and upper bound.
89 GBT_VARKEY *
90 gbt_var_key_copy(const GBT_VARKEY_R *u)
92 int32 lowersize = VARSIZE(u->lower);
93 int32 uppersize = VARSIZE(u->upper);
94 GBT_VARKEY *r;
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);
101 return r;
105 static GBT_VARKEY *
106 gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
108 GBT_VARKEY *out = leaf;
110 if (tinfo->f_l2n)
111 out = tinfo->f_l2n(leaf, flinfo);
113 return out;
118 * returns the common prefix length of a node key
120 static int32
121 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
123 GBT_VARKEY_R r = gbt_var_key_readable(node);
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);
132 if (ml == 0)
133 return 0;
135 while (i < ml)
137 if (tinfo->eml > 1 && l == 0)
139 if ((l = pg_mblen(p1)) != pg_mblen(p2))
141 return i;
144 if (*p1 != *p2)
146 if (tinfo->eml > 1)
148 return (i - l + 1);
150 else
152 return i;
156 p1++;
157 p2++;
158 l--;
159 i++;
161 return ml; /* lower == upper */
166 * returns true, if query matches prefix ( common prefix )
168 static bool
169 gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
171 bool out = false;
172 int32 qlen = VARSIZE(query) - VARHDRSZ;
173 int32 nlen = VARSIZE(pf) - VARHDRSZ;
175 if (nlen <= qlen)
177 char *q = VARDATA(query);
178 char *n = VARDATA(pf);
180 out = (memcmp(q, n, nlen) == 0);
183 return out;
188 * returns true, if query matches node using common prefix
190 static bool
191 gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
193 return (tinfo->trnc &&
194 (gbt_bytea_pf_match(node->lower, query, tinfo) ||
195 gbt_bytea_pf_match(node->upper, query, tinfo)));
200 * truncates / compresses the node key
201 * cpf_length .. common prefix length
203 static GBT_VARKEY *
204 gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
206 GBT_VARKEY *out = NULL;
207 GBT_VARKEY_R r = gbt_var_key_readable(node);
208 int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
209 int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
210 int32 si;
211 char *out2;
213 len1 = Min(len1, (cpf_length + 1));
214 len2 = Min(len2, (cpf_length + 1));
216 si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
217 out = (GBT_VARKEY *) palloc0(si);
218 SET_VARSIZE(out, si);
220 memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
221 SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
223 out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
224 memcpy(out2, r.upper, len2 + VARHDRSZ);
225 SET_VARSIZE(out2, len2 + VARHDRSZ);
227 return out;
232 void
233 gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation,
234 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
236 GBT_VARKEY_R eo = gbt_var_key_readable(e);
237 GBT_VARKEY_R nr;
239 if (eo.lower == eo.upper) /* leaf */
241 GBT_VARKEY *tmp;
243 tmp = gbt_var_leaf2node(e, tinfo, flinfo);
244 if (tmp != e)
245 eo = gbt_var_key_readable(tmp);
248 if (DatumGetPointer(*u))
250 GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
251 bool update = false;
253 nr.lower = ro.lower;
254 nr.upper = ro.upper;
256 if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
258 nr.lower = eo.lower;
259 update = true;
262 if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
264 nr.upper = eo.upper;
265 update = true;
268 if (update)
269 *u = PointerGetDatum(gbt_var_key_copy(&nr));
271 else
273 nr.lower = eo.lower;
274 nr.upper = eo.upper;
275 *u = PointerGetDatum(gbt_var_key_copy(&nr));
280 GISTENTRY *
281 gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
283 GISTENTRY *retval;
285 if (entry->leafkey)
287 struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
288 GBT_VARKEY *r;
290 r = gbt_var_key_from_datum(leaf);
292 retval = palloc(sizeof(GISTENTRY));
293 gistentryinit(*retval, PointerGetDatum(r),
294 entry->rel, entry->page,
295 entry->offset, true);
297 else
298 retval = entry;
300 return retval;
304 Datum
305 gbt_var_fetch(PG_FUNCTION_ARGS)
307 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
308 GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
309 GBT_VARKEY_R r = gbt_var_key_readable(key);
310 GISTENTRY *retval;
312 retval = palloc(sizeof(GISTENTRY));
313 gistentryinit(*retval, PointerGetDatum(r.lower),
314 entry->rel, entry->page,
315 entry->offset, true);
317 PG_RETURN_POINTER(retval);
321 GBT_VARKEY *
322 gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation,
323 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
325 int i = 0,
326 numranges = entryvec->n;
327 GBT_VARKEY *cur;
328 Datum out;
329 GBT_VARKEY_R rk;
331 *size = sizeof(GBT_VARKEY);
333 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
334 rk = gbt_var_key_readable(cur);
335 out = PointerGetDatum(gbt_var_key_copy(&rk));
337 for (i = 1; i < numranges; i++)
339 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
340 gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
344 /* Truncate (=compress) key */
345 if (tinfo->trnc)
347 int32 plen;
348 GBT_VARKEY *trc = NULL;
350 plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
351 trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
353 out = PointerGetDatum(trc);
356 return ((GBT_VARKEY *) DatumGetPointer(out));
360 bool
361 gbt_var_same(Datum d1, Datum d2, Oid collation,
362 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
364 GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
365 GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
366 GBT_VARKEY_R r1,
369 r1 = gbt_var_key_readable(t1);
370 r2 = gbt_var_key_readable(t2);
372 return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
373 tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
377 float *
378 gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
379 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
381 GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
382 GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
383 GBT_VARKEY_R ok,
386 *res = 0.0;
388 nk = gbt_var_key_readable(newe);
389 if (nk.lower == nk.upper) /* leaf */
391 GBT_VARKEY *tmp;
393 tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
394 if (tmp != newe)
395 nk = gbt_var_key_readable(tmp);
397 ok = gbt_var_key_readable(orge);
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 ||
402 gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
403 (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
404 gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
406 Datum d = PointerGetDatum(0);
407 double dres;
408 int32 ol,
411 gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
412 ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
413 gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
414 ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
416 if (ul < ol)
418 dres = (ol - ul); /* reduction of common prefix len */
420 else
422 GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d));
423 unsigned char tmp[4];
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;
433 *res += FLT_MIN;
434 *res += (float) (dres / ((double) (ol + 1)));
435 *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
438 return res;
442 static int
443 gbt_vsrt_cmp(const void *a, const void *b, void *arg)
445 GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
446 GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
447 const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
448 int res;
450 res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
451 if (res == 0)
452 return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
454 return res;
457 GIST_SPLITVEC *
458 gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
459 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
461 OffsetNumber i,
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;
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);
474 v->spl_ldatum = PointerGetDatum(0);
475 v->spl_rdatum = PointerGetDatum(0);
476 v->spl_nleft = 0;
477 v->spl_nright = 0;
479 sv = palloc(sizeof(bytea *) * (maxoff + 1));
481 /* Sort entries */
483 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
485 GBT_VARKEY_R ro;
487 cur = (char *) DatumGetPointer(entryvec->vector[i].key);
488 ro = gbt_var_key_readable((GBT_VARKEY *) cur);
489 if (ro.lower == ro.upper) /* leaf */
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++;
496 else
497 arr[i].t = (GBT_VARKEY *) cur;
498 arr[i].i = i;
501 /* sort */
502 varg.tinfo = tinfo;
503 varg.collation = collation;
504 varg.flinfo = flinfo;
505 qsort_arg((void *) &arr[FirstOffsetNumber],
506 maxoff - FirstOffsetNumber + 1,
507 sizeof(Vsrt),
508 gbt_vsrt_cmp,
509 (void *) &varg);
511 /* We do simply create two parts */
513 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
515 if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
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++;
521 else
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++;
529 /* Truncate (=compress) key */
530 if (tinfo->trnc)
532 int32 ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
533 int32 lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
534 GBT_VARKEY *dl;
535 GBT_VARKEY *dr;
537 ll = Max(ll, lr);
538 ll++;
540 dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
541 dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
542 v->spl_ldatum = PointerGetDatum(dl);
543 v->spl_rdatum = PointerGetDatum(dr);
546 return v;
551 * The GiST consistent method
553 bool
554 gbt_var_consistent(GBT_VARKEY_R *key,
555 const void *query,
556 StrategyNumber strategy,
557 Oid collation,
558 bool is_leaf,
559 const gbtree_vinfo *tinfo,
560 FmgrInfo *flinfo)
562 bool retval = false;
564 switch (strategy)
566 case BTLessEqualStrategyNumber:
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;
573 case BTLessStrategyNumber:
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;
580 case BTEqualStrategyNumber:
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) ||
587 gbt_var_node_pf_match(key, query, tinfo);
588 break;
589 case BTGreaterStrategyNumber:
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;
596 case BTGreaterEqualStrategyNumber:
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;
603 case BtreeGistNotEqualStrategyNumber:
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;
611 return retval;