diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index dab4cf3233afe8ebd4fb5cf91f47cfc44d8b52fa..cf0d8f8f6dd2dc939a0298b7e32ba470f52ccb2a 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.84 2003/11/25 21:00:54 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.85 2003/11/25 23:59:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -118,6 +118,11 @@ replace_outer_var(Var *var)
 	 * well, I believe that this sort of aliasing will cause no trouble.
 	 * The correct field should get stored into the Param slot at
 	 * execution in each part of the tree.
+	 *
+	 * We also need to demand a match on vartypmod.  This does not matter
+	 * for the Param itself, since those are not typmod-dependent, but it
+	 * does matter when make_subplan() instantiates a modified copy of the
+	 * Var for a subplan's args list.
 	 */
 	i = 0;
 	foreach(ppl, PlannerParamList)
@@ -129,7 +134,8 @@ replace_outer_var(Var *var)
 
 			if (pvar->varno == var->varno &&
 				pvar->varattno == var->varattno &&
-				pvar->vartype == var->vartype)
+				pvar->vartype == var->vartype &&
+				pvar->vartypmod == var->vartypmod)
 				break;
 		}
 		i++;