diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index b3510b9..0216dae 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -496,12 +496,12 @@ printCrosstab(const PGresult *results, int num_columns,
 				{
 					src_col = colsG[i];
 
-					content = (!PQgetisnull(results, rn, src_col)) ?
+					content = !PQgetisnull(results, rn, src_col) ?
 						PQgetvalue(results, rn, src_col) :
 						(popt.nullPrint ? popt.nullPrint : "");
 				}
 
-				if (cont.cells[idx] != NULL && cont.cells[idx][0] != '\0')
+				if (cont.cells[idx] != NULL)
 				{
 					/*
 					 * Multiple values for the same (row,col) are projected
@@ -509,12 +509,9 @@ printCrosstab(const PGresult *results, int num_columns,
 					 * previous content of the cell from the new value by a
 					 * newline.
 					 */
-					int			content_size;
 					char	   *new_content;
 					int			idx2;
 
-					content_size = strlen(cont.cells[idx]) + 2 + strlen(content) + 1;
-
 					/*
 					 * idx2 is an index into allocated_cells. It differs from
 					 * idx (index into cont.cells), because vertical and
@@ -524,34 +521,17 @@ printCrosstab(const PGresult *results, int num_columns,
 					idx2 = (row_number * num_columns) + col_number;
 
 					if (allocated_cells[idx2] != NULL)
-					{
-						new_content = pg_realloc(allocated_cells[idx2], content_size);
-					}
+						new_content = psprintf("%s%s%s",
+											   allocated_cells[idx2],
+											   i == 0 ? "\n" : " ",
+											   content);
 					else
-					{
-						/*
-						 * At this point, cont.cells[idx] still contains a
-						 * PQgetvalue() pointer.  Just after, it will contain
-						 * a new pointer maintained in allocated_cells[], and
-						 * freed at the end of this function.
-						 */
-						new_content = pg_malloc(content_size);
-						strcpy(new_content, cont.cells[idx]);
-					}
-					cont.cells[idx] = new_content;
-					allocated_cells[idx2] = new_content;
+						new_content = psprintf("%s", content);
 
-					/*
-					 * Contents that are on adjacent columns in the source
-					 * results get separated by one space in the target.
-					 * Contents that are on different rows in the source get
-					 * separated by newlines in the target.
-					 */
-					if (i == 0)
-						strcat(new_content, "\n");
-					else
-						strcat(new_content, " ");
-					strcat(new_content, content);
+					cont.cells[idx] = new_content;
+					if (allocated_cells[idx2] != NULL)
+						pg_free(allocated_cells[idx2]);
+					allocated_cells[idx2] = new_content;
 				}
 				else
 				{
