diff --git a/src/backend/utils/adt/rowtypes.c b/src/backend/utils/adt/rowtypes.c
index db843a0fbf..a0b0b248d2 100644
--- a/src/backend/utils/adt/rowtypes.c
+++ b/src/backend/utils/adt/rowtypes.c
@@ -1835,38 +1835,35 @@ hash_record(PG_FUNCTION_ARGS)
 	for (int i = 0; i < ncolumns; i++)
 	{
 		Form_pg_attribute att;
-		TypeCacheEntry *typentry;
-		uint32		element_hash;
 
 		att = TupleDescAttr(tupdesc, i);
 
 		if (att->attisdropped)
 			continue;
 
-		/*
-		 * Lookup the hash function if not done already
-		 */
-		typentry = my_extra->columns[i].typentry;
-		if (typentry == NULL ||
-			typentry->type_id != att->atttypid)
+		/* Compute hash of element */
+		if (!nulls[i])
 		{
-			typentry = lookup_type_cache(att->atttypid,
+			TypeCacheEntry *typentry;
+			uint32		element_hash;
+
+			/*
+			* Lookup the hash function if not done already
+			*/
+			typentry = my_extra->columns[i].typentry;
+			if (typentry == NULL ||
+				typentry->type_id != att->atttypid)
+			{
+				typentry = lookup_type_cache(att->atttypid,
 										 TYPECACHE_HASH_PROC_FINFO);
-			if (!OidIsValid(typentry->hash_proc_finfo.fn_oid))
-				ereport(ERROR,
-						(errcode(ERRCODE_UNDEFINED_FUNCTION),
-						 errmsg("could not identify a hash function for type %s",
-								format_type_be(typentry->type_id))));
-			my_extra->columns[i].typentry = typentry;
-		}
+				if (!OidIsValid(typentry->hash_proc_finfo.fn_oid))
+					ereport(ERROR,
+							(errcode(ERRCODE_UNDEFINED_FUNCTION),
+							errmsg("could not identify a hash function for type %s",
+									format_type_be(typentry->type_id))));
+				my_extra->columns[i].typentry = typentry;
+			}
 
-		/* Compute hash of element */
-		if (nulls[i])
-		{
-			element_hash = 0;
-		}
-		else
-		{
 			LOCAL_FCINFO(locfcinfo, 1);
 
 			InitFunctionCallInfoData(*locfcinfo, &typentry->hash_proc_finfo, 1,
@@ -1877,10 +1874,12 @@ hash_record(PG_FUNCTION_ARGS)
 
 			/* We don't expect hash support functions to return null */
 			Assert(!locfcinfo->isnull);
-		}
 
-		/* see hash_array() */
-		result = (result << 5) - result + element_hash;
+			/* see hash_array() */
+			result = (result << 5) - result + element_hash;
+		}
+		else
+			result = (result << 5) - result + 0;
 	}
 
 	pfree(values);