diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 6f42df8ceb65fb938390088df0f1dbf5bfebbf17..aa77085c55e49336613044f62d6f5f0dc4d16c93 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.14 1998/01/20 22:11:39 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.15 1998/02/13 03:40:19 vadim Exp $
  *
  * HISTORY
  *	  AUTHOR			DATE			MAJOR EVENT
@@ -22,6 +22,7 @@
 #include "nodes/primnodes.h"
 #include "nodes/relation.h"
 #include "nodes/parsenodes.h"
+#include "nodes/plannodes.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
 
@@ -57,6 +58,7 @@ make_clause(int type, Node *oper, List *args)
 	}
 	else
 	{
+		elog (ERROR, "make_clause: unsupported type %d", type);
 		/* will this ever happen? translated from lispy C code - ay 10/94 */
 		return ((Expr *) args);
 	}
@@ -375,10 +377,10 @@ clause_relids_vars(Node *clause, List **relids, List **vars)
 		foreach(vi, var_list)
 		{
 			Var		   *in_list = (Var *) lfirst(vi);
-
+			
+			Assert (var->varlevelsup == 0);
 			if (in_list->varno == var->varno &&
-				in_list->varattno == var->varattno &&
-				in_list->varlevelsup == var->varlevelsup)
+				in_list->varattno == var->varattno)
 				break;
 		}
 		if (vi == NIL)
@@ -492,6 +494,7 @@ qual_clause_p(Node *clause)
 	if (!is_opclause(clause))
 		return false;
 
+	/* How about Param-s ?	- vadim 02/03/98 */
 	if (IsA(get_leftop((Expr *) clause), Var) &&
 		IsA(get_rightop((Expr *) clause), Const))
 	{
@@ -550,6 +553,17 @@ fix_opid(Node *clause)
 	{
 		fix_opid(((Aggreg *) clause)->target);
 	}
+	else if (is_subplan(clause) && 
+		((SubPlan*) ((Expr*) clause)->oper)->sublink->subLinkType != EXISTS_SUBLINK)
+	{
+		List   *lst;
+		
+		foreach (lst, ((SubPlan*) ((Expr*) clause)->oper)->sublink->oper)
+		{
+			replace_opid((Oper*) ((Expr*) lfirst(lst))->oper);
+			fix_opid((Node*) get_leftop((Expr*) lfirst(lst)));
+		}
+	}
 
 }
 
@@ -627,16 +641,13 @@ get_relattval(Node *clause,
 			*flag = (_SELEC_CONSTANT_RIGHT_ | _SELEC_NOT_CONSTANT_);
 
 		}
-#ifdef INDEXSCAN_PATCH
 	}
-	else if (is_opclause(clause) && IsA(left, Var) &&IsA(right, Param))
+	else if (is_opclause(clause) && IsA(left, Var) && IsA(right, Param))
 	{
-		/* Function parameter used as index scan arg.  DZ - 27-8-1996 */
 		*relid = left->varno;
 		*attno = left->varattno;
 		*constval = 0;
 		*flag = (_SELEC_NOT_CONSTANT_);
-#endif
 	}
 	else if (is_opclause(clause) &&
 			 is_funcclause((Node *) left) &&
@@ -680,16 +691,13 @@ get_relattval(Node *clause,
 			*constval = 0;
 			*flag = (_SELEC_NOT_CONSTANT_);
 		}
-#ifdef INDEXSCAN_PATCH
 	}
-	else if (is_opclause(clause) && IsA(right, Var) &&IsA(left, Param))
+	else if (is_opclause(clause) && IsA(right, Var) && IsA(left, Param))
 	{
-		/* ...And here... - vadim 01/22/97 */
 		*relid = right->varno;
 		*attno = right->varattno;
 		*constval = 0;
 		*flag = (_SELEC_NOT_CONSTANT_);
-#endif
 	}
 	else
 	{
diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/util/tlist.c
index 805ac38b902f3dc962abe390bc017fe646a899f6..a98ee8f22cc36646a678d981958646e5af064aeb 100644
--- a/src/backend/optimizer/util/tlist.c
+++ b/src/backend/optimizer/util/tlist.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.10 1998/02/10 04:01:21 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.11 1998/02/13 03:40:21 vadim Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -273,6 +273,7 @@ match_varid(Var *test_var, List *tlist)
 
 	type_var = (Oid) test_var->vartype;
 
+	Assert (test_var->varlevelsup == 0);
 	foreach(tl, tlist)
 	{
 		TargetEntry *entry;
@@ -288,9 +289,9 @@ match_varid(Var *test_var, List *tlist)
 		 * we test the original varno (instead of varno which might be
 		 * changed to INNER/OUTER.
 		 */
+		Assert (tlvar->varlevelsup == 0);
 		if (tlvar->varnoold == test_var->varnoold &&
-			tlvar->varoattno == test_var->varoattno &&
-			tlvar->varlevelsup == test_var->varlevelsup)
+				tlvar->varoattno == test_var->varoattno)
 		{
 
 			if (tlvar->vartype == type_var)
diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c
index 41624a171077979da1acba2241b547601f208672..4c3ebe7f326ec25004766af8ea44dc5dcd7a0e7b 100644
--- a/src/backend/optimizer/util/var.c
+++ b/src/backend/optimizer/util/var.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.9 1998/02/10 04:01:27 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.10 1998/02/13 03:40:23 vadim Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -18,6 +18,7 @@
 #include <nodes/relation.h>
 
 #include "nodes/primnodes.h"
+#include "nodes/plannodes.h"
 #include "nodes/nodeFuncs.h"
 
 #include "optimizer/internal.h"
@@ -85,7 +86,7 @@ contain_var_clause(Node *clause)
 		return contain_var_clause(((Iter *) clause)->iterexpr);
 	else if (single_node(clause))
 		return FALSE;
-	else if (or_clause(clause) || and_clause(clause))
+	else if (or_clause(clause) || and_clause(clause) || is_funcclause(clause))
 	{
 		List	   *temp;
 
@@ -96,7 +97,7 @@ contain_var_clause(Node *clause)
 		}
 		return FALSE;
 	}
-	else if (is_funcclause(clause))
+	else if (is_subplan(clause))
 	{
 		List	   *temp;
 
@@ -105,6 +106,12 @@ contain_var_clause(Node *clause)
 			if (contain_var_clause(lfirst(temp)))
 				return TRUE;
 		}
+		/* Ok - check left sides of Oper-s */
+		foreach(temp, ((SubPlan*) ((Expr *) clause)->oper)->sublink->oper)
+		{
+			if (contain_var_clause(lfirst(((Expr*) lfirst(temp))->args)))
+				return TRUE;
+		}
 		return FALSE;
 	}
 	else if (IsA(clause, ArrayRef))
@@ -156,19 +163,23 @@ pull_var_clause(Node *clause)
 		retval = pull_var_clause(((Iter *) clause)->iterexpr);
 	else if (single_node(clause))
 		retval = NIL;
-	else if (or_clause(clause) || and_clause(clause))
+	else if (or_clause(clause) || and_clause(clause) || is_funcclause(clause))
 	{
 		List	   *temp;
 
 		foreach(temp, ((Expr *) clause)->args)
 			retval = nconc(retval, pull_var_clause(lfirst(temp)));
 	}
-	else if (is_funcclause(clause))
+	else if (is_subplan(clause))
 	{
 		List	   *temp;
 
-		foreach(temp, ((Expr *) clause)->args)
+		foreach(temp, ((Expr*) clause)->args)
 			retval = nconc(retval, pull_var_clause(lfirst(temp)));
+		/* Ok - get Var-s from left sides of Oper-s */
+		foreach(temp, ((SubPlan*) ((Expr*) clause)->oper)->sublink->oper)
+			retval = nconc(retval, 
+				pull_var_clause(lfirst(((Expr*) lfirst(temp))->args)));
 	}
 	else if (IsA(clause, Aggreg))
 	{
@@ -213,7 +224,7 @@ var_equal(Var *var1, Var *var2)
 		(((Var *) var1)->varlevelsup == ((Var *) var2)->varlevelsup) &&
 		(((Var *) var1)->varattno == ((Var *) var2)->varattno))
 	{
-
+		Assert (((Var *) var1)->varlevelsup == 0);
 		return (true);
 	}
 	else