diff --git a/src/backend/utils/adt/rowtypes.c b/src/backend/utils/adt/rowtypes.c
index fe5edc0027..589e7550c5 100644
--- a/src/backend/utils/adt/rowtypes.c
+++ b/src/backend/utils/adt/rowtypes.c
@@ -143,6 +143,8 @@ record_in(PG_FUNCTION_ARGS)
 	values = (Datum *) palloc(ncolumns * sizeof(Datum));
 	nulls = (bool *) palloc(ncolumns * sizeof(bool));
 
+	initStringInfo(&buf);
+
 	/*
 	 * Scan the string.  We use "buf" to accumulate the de-quoted data for
 	 * each column, which is then fed to the appropriate input converter.
@@ -160,8 +162,6 @@ record_in(PG_FUNCTION_ARGS)
 		goto fail;
 	}
 
-	initStringInfo(&buf);
-
 	for (i = 0; i < ncolumns; i++)
 	{
 		Form_pg_attribute att = TupleDescAttr(tupdesc, i);
@@ -318,6 +318,9 @@ record_in(PG_FUNCTION_ARGS)
 
 	/* exit here once we've done lookup_rowtype_tupdesc */
 fail:
+	pfree(buf.data);
+	pfree(values);
+	pfree(nulls);
 	ReleaseTupleDesc(tupdesc);
 	PG_RETURN_NULL();
 }
 
