diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 57320cc..696cc83 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -1550,6 +1550,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 	TableInfo  *tbinfo = tdinfo->tdtable;
 	const char *classname = tbinfo->dobj.name;
 	PQExpBuffer q = createPQExpBuffer();
+	PQExpBuffer collist = NULL;
 	PGresult   *res;
 	int			tuple;
 	int			nfields;
@@ -1595,30 +1596,34 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 			if (nfields == 0)
 			{
 				/* corner case for zero-column table */
-				archprintf(fout, "DEFAULT VALUES;\n");
+				archputs("DEFAULT VALUES;\n", fout);
 				continue;
 			}
 			if (column_inserts)
 			{
-				resetPQExpBuffer(q);
-				appendPQExpBuffer(q, "(");
-				for (field = 0; field < nfields; field++)
+				/* build the list of column names if required */
+				if (collist == NULL)
 				{
-					if (field > 0)
-						appendPQExpBuffer(q, ", ");
-					appendPQExpBufferStr(q, fmtId(PQfname(res, field)));
+					collist = createPQExpBuffer();
+					appendPQExpBuffer(collist, "(");
+					for (field = 0; field < nfields; field++)
+					{
+						if (field > 0)
+							appendPQExpBuffer(collist, ", ");
+						appendPQExpBufferStr(collist, fmtId(PQfname(res, field)));
+					}
+					appendPQExpBuffer(collist, ") ");
 				}
-				appendPQExpBuffer(q, ") ");
-				archputs(q->data, fout);
+				archputs(collist->data, fout);
 			}
-			archprintf(fout, "VALUES (");
+			archputs("VALUES (", fout);
 			for (field = 0; field < nfields; field++)
 			{
 				if (field > 0)
-					archprintf(fout, ", ");
+					archputs(", ", fout);
 				if (PQgetisnull(res, tuple, field))
 				{
-					archprintf(fout, "NULL");
+					archputs("NULL", fout);
 					continue;
 				}
 
@@ -1647,7 +1652,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 							const char *s = PQgetvalue(res, tuple, field);
 
 							if (strspn(s, "0123456789 +-eE.") == strlen(s))
-								archprintf(fout, "%s", s);
+								archputs(s, fout);
 							else
 								archprintf(fout, "'%s'", s);
 						}
@@ -1661,9 +1666,9 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 
 					case BOOLOID:
 						if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
-							archprintf(fout, "true");
+							archputs("true", fout);
 						else
-							archprintf(fout, "false");
+							archputs("false", fout);
 						break;
 
 					default:
@@ -1676,7 +1681,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 						break;
 				}
 			}
-			archprintf(fout, ");\n");
+			archputs(");\n", fout);
 		}
 
 		if (PQntuples(res) <= 0)
@@ -1687,11 +1692,15 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 		PQclear(res);
 	}
 
-	archprintf(fout, "\n\n");
+	archputs("\n\n", fout);
 
 	ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
 
 	destroyPQExpBuffer(q);
+
+	if (collist != NULL)
+		destroyPQExpBuffer(collist);
+
 	return 1;
 }
 
