More btree fixes from Massimo Dal Zotto <[email protected]>
authorMarc G. Fournier <[email protected]>
Wed, 30 Oct 1996 06:06:50 +0000 (06:06 +0000)
committerMarc G. Fournier <[email protected]>
Wed, 30 Oct 1996 06:06:50 +0000 (06:06 +0000)
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.

src/backend/access/common/heapvalid.c
src/backend/access/common/indexvalid.c
src/backend/access/nbtree/nbtsearch.c
src/backend/executor/nodeIndexscan.c

index b80c5dd9eb0abcfc608d50a26e5bb54872035e4a..535ed67e731a21003fe3115f156b8653c69dae60 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/access/common/Attic/heapvalid.c,v 1.1.1.1 1996/07/09 06:21:09 scrappy Exp $
+ *    $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 $
  *
  *-------------------------------------------------------------------------
  */
@@ -54,6 +54,10 @@ heap_keytest(HeapTuple t,
        /* XXX eventually should check if SK_ISNULL */
        return false;
    
+   if (keys->sk_flags & SK_ISNULL) {
+       return (false);
+   }
+
    if (keys->sk_flags & SK_COMMUTE)
        test = (long) FMGR_PTR2(keys->sk_func, keys->sk_procedure,
                    keys->sk_argument, atp);
index b437718ceccf985859cd8ea7d0b29710bbb3eb84..fc2eab4be43d8a615eb4d09e812d2b6db0467acf 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/access/common/Attic/indexvalid.c,v 1.1.1.1 1996/07/09 06:21:09 scrappy Exp $
+ *    $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 $
  *
  *-------------------------------------------------------------------------
  */
@@ -61,6 +61,10 @@ index_keytest(IndexTuple tuple,
        return (false);
    }
    
+   if (key[0].sk_flags & SK_ISNULL) {
+       return (false);
+   }
+
    if (key[0].sk_flags & SK_COMMUTE) {
        test = (int) (*(key[0].sk_func))
        (DatumGetPointer(key[0].sk_argument),
index 3756c2fc30cad38c4a57eb212f333839dc7a3212..26898a7a0bcbd844bbfac97fb23d1e4859bbac12 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.2 1996/07/30 07:56:02 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.2.2.1 1996/10/30 06:06:40 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -248,6 +248,18 @@ _bt_skeycmp(Relation rel,
                  &isNull);
    keyDatum  = entry->sk_argument;
    
+   /*
+    * This may happen in a nested loop if an attribute used
+    * as scan key is null.         DZ 29-10-1996
+    */
+   if ((entry->sk_flags & SK_ISNULL) || (isNull)) {
+       if ((entry->sk_flags & SK_ISNULL) && (isNull)) {
+       return (true);
+       } else {
+       return (false);
+       }
+   }
+
    compare = _bt_invokestrat(rel, i, strat, keyDatum, attrDatum);
    if (!compare)
        return (false);
@@ -490,6 +502,19 @@ _bt_compare(Relation rel,
    entry = &scankey[i - 1];
    attno = entry->sk_attno;
    datum = index_getattr(itup, attno, itupdesc, &null);
+
+   /*
+    * This may happen in a nested loop if an attribute used
+    * as scan key is null.         DZ 29-10-1996
+    */
+   if ((entry->sk_flags & SK_ISNULL) || (null)) {
+       if ((entry->sk_flags & SK_ISNULL) && (null)) {
+       return (0);
+       } else {
+       return (null ? +1 : -1);
+       }
+   }
+
    tmpres = (long) FMGR_PTR2(entry->sk_func, entry->sk_procedure,
                  entry->sk_argument, datum);
    result = tmpres;
@@ -630,7 +655,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
      *        hardwired attno == 1.
      */
     proc = index_getprocid(rel, 1, BTORDER_PROC);
-    ScanKeyEntryInitialize(&skdata, 0x0, 1, proc,
+    ScanKeyEntryInitialize(&skdata, so->keyData[0].sk_flags, 1, proc,
               so->keyData[0].sk_argument);
     
     stack = _bt_search(rel, 1, &skdata, &buf);
index 758fabdefe5278f6832235b6c828cbe5274e2e5a..80f710613c739c4972f603d48f21c5096e9e1896 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.1.1.1 1996/07/09 06:21:26 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.1.1.1.2.1 1996/10/30 06:06:50 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -273,6 +273,11 @@ ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan* parent)
        scanvalue = (Datum)
            ExecEvalExpr(scanexpr, exprCtxt, &isNull, &isDone);
        scan_keys[j].sk_argument = scanvalue;
+       if (isNull) {
+           scan_keys[j].sk_flags |= SK_ISNULL;
+       } else {
+           scan_keys[j].sk_flags &= ~SK_ISNULL;
+       }
        }
    }
     }