Skip to content

Commit 0257096

Browse files
committed
More btree fixes from Massimo Dal Zotto <dz@cs.unitn.it>
Fixes: I found another bug in btree index. Looking at the code it seems that NULL keys are never used to build or scan a btree index (see the explain commands in the example). However this is not the case when a null key is retrieved in an outer loop of a join select and used in an index scan of an inner loop. This bug causes at least three kinds of problems: 1) the backend crashes when it tries to compare a text string with a null. 2) it is not possible to find tuples with null keys in a join. 3) null is considered equal to 0 when the datum is passed by value, see the last query.
1 parent 65e5e59 commit 0257096

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/backend/access/common/heapvalid.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/heapvalid.c,v 1.1.1.1 1996/07/09 06:21:09 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/heapvalid.c,v 1.1.1.1.2.1 1996/10/30 06:06:36 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -54,6 +54,10 @@ heap_keytest(HeapTuple t,
5454
/* XXX eventually should check if SK_ISNULL */
5555
return false;
5656

57+
if (keys->sk_flags & SK_ISNULL) {
58+
return (false);
59+
}
60+
5761
if (keys->sk_flags & SK_COMMUTE)
5862
test = (long) FMGR_PTR2(keys->sk_func, keys->sk_procedure,
5963
keys->sk_argument, atp);

src/backend/access/common/indexvalid.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/indexvalid.c,v 1.1.1.1 1996/07/09 06:21:09 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/indexvalid.c,v 1.1.1.1.2.1 1996/10/30 06:06:34 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -61,6 +61,10 @@ index_keytest(IndexTuple tuple,
6161
return (false);
6262
}
6363

64+
if (key[0].sk_flags & SK_ISNULL) {
65+
return (false);
66+
}
67+
6468
if (key[0].sk_flags & SK_COMMUTE) {
6569
test = (int) (*(key[0].sk_func))
6670
(DatumGetPointer(key[0].sk_argument),

src/backend/access/nbtree/nbtsearch.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.2 1996/07/30 07:56:02 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.2.2.1 1996/10/30 06:06:40 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -248,6 +248,18 @@ _bt_skeycmp(Relation rel,
248248
&isNull);
249249
keyDatum = entry->sk_argument;
250250

251+
/*
252+
* This may happen in a nested loop if an attribute used
253+
* as scan key is null. DZ 29-10-1996
254+
*/
255+
if ((entry->sk_flags & SK_ISNULL) || (isNull)) {
256+
if ((entry->sk_flags & SK_ISNULL) && (isNull)) {
257+
return (true);
258+
} else {
259+
return (false);
260+
}
261+
}
262+
251263
compare = _bt_invokestrat(rel, i, strat, keyDatum, attrDatum);
252264
if (!compare)
253265
return (false);
@@ -490,6 +502,19 @@ _bt_compare(Relation rel,
490502
entry = &scankey[i - 1];
491503
attno = entry->sk_attno;
492504
datum = index_getattr(itup, attno, itupdesc, &null);
505+
506+
/*
507+
* This may happen in a nested loop if an attribute used
508+
* as scan key is null. DZ 29-10-1996
509+
*/
510+
if ((entry->sk_flags & SK_ISNULL) || (null)) {
511+
if ((entry->sk_flags & SK_ISNULL) && (null)) {
512+
return (0);
513+
} else {
514+
return (null ? +1 : -1);
515+
}
516+
}
517+
493518
tmpres = (long) FMGR_PTR2(entry->sk_func, entry->sk_procedure,
494519
entry->sk_argument, datum);
495520
result = tmpres;
@@ -630,7 +655,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
630655
* hardwired attno == 1.
631656
*/
632657
proc = index_getprocid(rel, 1, BTORDER_PROC);
633-
ScanKeyEntryInitialize(&skdata, 0x0, 1, proc,
658+
ScanKeyEntryInitialize(&skdata, so->keyData[0].sk_flags, 1, proc,
634659
so->keyData[0].sk_argument);
635660

636661
stack = _bt_search(rel, 1, &skdata, &buf);

src/backend/executor/nodeIndexscan.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.1.1.1 1996/07/09 06:21:26 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.1.1.1.2.1 1996/10/30 06:06:50 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -273,6 +273,11 @@ ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan* parent)
273273
scanvalue = (Datum)
274274
ExecEvalExpr(scanexpr, exprCtxt, &isNull, &isDone);
275275
scan_keys[j].sk_argument = scanvalue;
276+
if (isNull) {
277+
scan_keys[j].sk_flags |= SK_ISNULL;
278+
} else {
279+
scan_keys[j].sk_flags &= ~SK_ISNULL;
280+
}
276281
}
277282
}
278283
}

0 commit comments

Comments
 (0)