diff --git a/src/backend/access/gist/gistget.c b/src/backend/access/gist/gistget.c
index 97ae1cfd5857115bf87bc1363091b67f6d065eb0..574eeff7ff33241ff61aae38bf36629a689291ea 100644
--- a/src/backend/access/gist/gistget.c
+++ b/src/backend/access/gist/gistget.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.75 2008/08/23 10:37:24 teodor Exp $
+ *	  $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.76 2008/10/17 17:02:21 teodor Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -134,6 +134,9 @@ gistnext(IndexScanDesc scan, ScanDirection dir, TIDBitmap *tbm)
 
 	so = (GISTScanOpaque) scan->opaque;
 
+	if ( so->qual_ok == false )
+		return 0;
+
 	if (ItemPointerIsValid(&so->curpos) == false)
 	{
 		/* Being asked to fetch the first entry, so start at the root */
diff --git a/src/backend/access/gist/gistscan.c b/src/backend/access/gist/gistscan.c
index ac3ea446ded5543a33e62a9fd9df6e797a9afdcc..6dba277b18f9b290ef9137609ecc7926c70e90d6 100644
--- a/src/backend/access/gist/gistscan.c
+++ b/src/backend/access/gist/gistscan.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/access/gist/gistscan.c,v 1.71 2008/08/23 10:37:24 teodor Exp $
+ *	  $PostgreSQL: pgsql/src/backend/access/gist/gistscan.c,v 1.72 2008/10/17 17:02:21 teodor Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -98,9 +98,19 @@ gistrescan(PG_FUNCTION_ARGS)
 		 * function in the form of its strategy number, which is available
 		 * from the sk_strategy field, and its subtype from the sk_subtype
 		 * field.
+		 *
+		 * Next, if any of keys is a NULL and that key is not marked with
+		 * SK_SEARCHNULL then nothing can be found.
 		 */
-		for (i = 0; i < scan->numberOfKeys; i++)
+		so->qual_ok = true;
+		for (i = 0; i < scan->numberOfKeys; i++) {
 			scan->keyData[i].sk_func = so->giststate->consistentFn[scan->keyData[i].sk_attno - 1];
+
+			if ( scan->keyData[i].sk_flags & SK_ISNULL ) {
+				if ( (scan->keyData[i].sk_flags & SK_SEARCHNULL) == 0 )
+					so->qual_ok = false;
+			}
+		}
 	}
 
 	PG_RETURN_VOID();
diff --git a/src/include/access/gist_private.h b/src/include/access/gist_private.h
index e366dcc53e7d91d516bf911282c76df85d141b73..5b628c93c63e560a6a6f1f2bcdf2f531f132d8f7 100644
--- a/src/include/access/gist_private.h
+++ b/src/include/access/gist_private.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/gist_private.h,v 1.32 2008/08/23 10:37:24 teodor Exp $
+ * $PostgreSQL: pgsql/src/include/access/gist_private.h,v 1.33 2008/10/17 17:02:21 teodor Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -73,6 +73,7 @@ typedef struct GISTScanOpaqueData
 	GISTSearchStack *stack;
 	GISTSearchStack *markstk;
 	uint16		flags;
+	bool        qual_ok;        /* false if qual can never be satisfied */
 	GISTSTATE  *giststate;
 	MemoryContext tempCxt;
 	Buffer		curbuf;