diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c
index a4e494b..b64d40b 100644
--- a/src/backend/parser/parse_coerce.c
+++ b/src/backend/parser/parse_coerce.c
@@ -221,7 +221,7 @@ coerce_type(ParseState *pstate, Node *node,
 			return node;
 		}
 	}
-	if (inputTypeId == UNKNOWNOID && IsA(node, Const))
+	if (inputTypeId == UNKNOWNOID)
 	{
 		/*
 		 * Input is a string constant with previously undetermined type. Apply
@@ -275,6 +275,29 @@ coerce_type(ParseState *pstate, Node *node,
 
 		targetType = typeidType(baseTypeId);
 
+		/* Perform on the fly conversion for non-constants */
+		if(!IsA(node, Const))
+		{
+			Form_pg_type typform = (Form_pg_type) GETSTRUCT(targetType);
+			Node *result = 
+				(Node*) makeFuncExpr(typform->typinput,
+						 targetTypeId,
+						 list_make3(node,
+									makeConst(OIDOID, -1, InvalidOid,
+											  sizeof(Oid),
+											  ObjectIdGetDatum(InvalidOid),
+											  false, true),
+									makeConst(INT4OID, -1, InvalidOid,
+											  sizeof(uint32),
+											  Int32GetDatum(inputTypeMod),
+											  false, true)),
+						  InvalidOid, InvalidOid,
+						  COERCE_IMPLICIT_CAST);
+			ReleaseSysCache(targetType);
+
+			return result;
+		}
+
 		newcon->consttype = baseTypeId;
 		newcon->consttypmod = inputTypeMod;
 		newcon->constcollid = typeTypeCollation(targetType);
