Skip to content

Commit 45b315e

Browse files
committed
Fix erroneous range-union logic for varlena types in contrib/btree_gist.
gbt_var_bin_union() failed to do the right thing when the existing range needed to be widened at both ends rather than just one end. This could result in an invalid index in which keys that are present would not be found by searches, because the searches would not think they need to descend to the relevant leaf pages. This error affected all the varlena datatypes supported by btree_gist (text, bytea, bit, numeric). Per investigation of a trouble report from Tomas Vondra. (There is also an issue in gbt_var_penalty(), but that should only result in inefficiency not wrong answers. I'm committing this separately so that we have a git state in which it can be tested that bad penalty results don't produce invalid indexes.) Back-patch to all supported branches.
1 parent 932ab74 commit 45b315e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

contrib/btree_gist/btree_utils_var.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,40 +233,40 @@ gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vin
233233
void
234234
gbt_var_bin_union(Datum *u, GBT_VARKEY *e, const gbtree_vinfo *tinfo)
235235
{
236-
237-
GBT_VARKEY *nk = NULL;
238-
GBT_VARKEY *tmp = NULL;
239-
GBT_VARKEY_R nr;
240236
GBT_VARKEY_R eo = gbt_var_key_readable(e);
237+
GBT_VARKEY_R nr;
241238

242239
if (eo.lower == eo.upper) /* leaf */
243240
{
241+
GBT_VARKEY *tmp;
242+
244243
tmp = gbt_var_leaf2node(e, tinfo);
245244
if (tmp != e)
246245
eo = gbt_var_key_readable(tmp);
247246
}
248247

249248
if (DatumGetPointer(*u))
250249
{
251-
252250
GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
251+
bool update = false;
252+
253+
nr.lower = ro.lower;
254+
nr.upper = ro.upper;
253255

254256
if ((*tinfo->f_cmp) ((bytea *) ro.lower, (bytea *) eo.lower) > 0)
255257
{
256258
nr.lower = eo.lower;
257-
nr.upper = ro.upper;
258-
nk = gbt_var_key_copy(&nr, TRUE);
259+
update = true;
259260
}
260261

261262
if ((*tinfo->f_cmp) ((bytea *) ro.upper, (bytea *) eo.upper) < 0)
262263
{
263264
nr.upper = eo.upper;
264-
nr.lower = ro.lower;
265-
nk = gbt_var_key_copy(&nr, TRUE);
265+
update = true;
266266
}
267267

268-
if (nk)
269-
*u = PointerGetDatum(nk);
268+
if (update)
269+
*u = PointerGetDatum(gbt_var_key_copy(&nr, TRUE));
270270
}
271271
else
272272
{

0 commit comments

Comments
 (0)