/* NB: we assume this struct contains no padding bytes */
    Oid         srctype;        /* source type for cast */
    Oid         dsttype;        /* destination type for cast */
+   int32       srctypmod;      /* source typmod for cast */
    int32       dsttypmod;      /* destination typmod for cast */
 } plpgsql_CastHashKey;
 
                Oid valtype, int32 valtypmod,
                Oid reqtype, int32 reqtypmod);
 static ExprState *get_cast_expression(PLpgSQL_execstate *estate,
-                   Oid srctype, Oid dsttype, int32 dsttypmod);
+                Oid srctype, int32 srctypmod, Oid dsttype, int32 dsttypmod);
 static void exec_init_tuple_store(PLpgSQL_execstate *estate);
 static void exec_set_found(PLpgSQL_execstate *estate, bool state);
 static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
    {
        ExprState  *cast_expr;
 
-       cast_expr = get_cast_expression(estate, valtype, reqtype, reqtypmod);
+       cast_expr = get_cast_expression(estate,
+                                       valtype, valtypmod,
+                                       reqtype, reqtypmod);
        if (cast_expr)
        {
            ExprContext *econtext = estate->eval_econtext;
  */
 static ExprState *
 get_cast_expression(PLpgSQL_execstate *estate,
-                   Oid srctype, Oid dsttype, int32 dsttypmod)
+                 Oid srctype, int32 srctypmod, Oid dsttype, int32 dsttypmod)
 {
    HTAB       *cast_hash = estate->func->cast_hash;
    plpgsql_CastHashKey cast_key;
    /* Look for existing entry */
    cast_key.srctype = srctype;
    cast_key.dsttype = dsttype;
+   cast_key.srctypmod = srctypmod;
    cast_key.dsttypmod = dsttypmod;
    cast_entry = (plpgsql_CastHashEntry *) hash_search(cast_hash,
                                                       (void *) &cast_key,
     */
    placeholder = makeNode(CaseTestExpr);
    placeholder->typeId = srctype;
-   placeholder->typeMod = -1;
+   placeholder->typeMod = srctypmod;
    placeholder->collation = get_typcollation(srctype);
    if (OidIsValid(estate->func->fn_input_collation) &&
        OidIsValid(placeholder->collation))