Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 69c7f25

Browse files
committed
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. Submitted by: Massimo Dal Zotto <dz@cs.unitn.it>
1 parent 6ada9df commit 69c7f25

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.6 1996/10/21 11:49:36 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/heapvalid.c,v 1.7 1996/10/30 06:07:56 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -71,6 +71,10 @@ heap_keytest(HeapTuple t,
7171
/* XXX eventually should check if SK_ISNULL */
7272
return false;
7373

74+
if (keys->sk_flags & SK_ISNULL) {
75+
return (false);
76+
}
77+
7478
if (keys->sk_flags & SK_COMMUTE)
7579
test = (long) FMGR_PTR2(keys->sk_func, keys->sk_procedure,
7680
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.6 1996/10/21 11:49:38 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/common/Attic/indexvalid.c,v 1.7 1996/10/30 06:07:55 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -68,6 +68,10 @@ index_keytest(IndexTuple tuple,
6868
return (false);
6969
}
7070

71+
if (key[0].sk_flags & SK_ISNULL) {
72+
return (false);
73+
}
74+
7175
if (key[0].sk_flags & SK_COMMUTE) {
7276
test = (int) (*(key[0].sk_func))
7377
(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.4 1996/10/23 07:39:10 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.5 1996/10/30 06:08:01 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -269,6 +269,18 @@ _bt_skeycmp(Relation rel,
269269
&isNull);
270270
keyDatum = entry->sk_argument;
271271

272+
/*
273+
* This may happen in a nested loop if an attribute used
274+
* as scan key is null. DZ 29-10-1996
275+
*/
276+
if ((entry->sk_flags & SK_ISNULL) || (isNull)) {
277+
if ((entry->sk_flags & SK_ISNULL) && (isNull)) {
278+
return (true);
279+
} else {
280+
return (false);
281+
}
282+
}
283+
272284
compare = _bt_invokestrat(rel, i, strat, keyDatum, attrDatum);
273285
if (!compare)
274286
return (false);
@@ -501,6 +513,19 @@ _bt_compare(Relation rel,
501513
entry = &scankey[i - 1];
502514
attno = entry->sk_attno;
503515
datum = index_getattr(itup, attno, itupdesc, &null);
516+
517+
/*
518+
* This may happen in a nested loop if an attribute used
519+
* as scan key is null. DZ 29-10-1996
520+
*/
521+
if ((entry->sk_flags & SK_ISNULL) || (null)) {
522+
if ((entry->sk_flags & SK_ISNULL) && (null)) {
523+
return (0);
524+
} else {
525+
return (null ? +1 : -1);
526+
}
527+
}
528+
504529
tmpres = (long) FMGR_PTR2(entry->sk_func, entry->sk_procedure,
505530
entry->sk_argument, datum);
506531
result = tmpres;
@@ -641,7 +666,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
641666
* hardwired attno == 1.
642667
*/
643668
proc = index_getprocid(rel, 1, BTORDER_PROC);
644-
ScanKeyEntryInitialize(&skdata, 0x0, 1, proc,
669+
ScanKeyEntryInitialize(&skdata, so->keyData[0].sk_flags, 1, proc,
645670
so->keyData[0].sk_argument);
646671

647672
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.2 1996/10/30 06:08:10 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)