From c6ac57a19ca2befa492ee1f58e6528d18cf98fef Mon Sep 17 00:00:00 2001
From: Roberto Mello <roberto.mello@gmail.com>
Date: Sat, 5 Dec 2026 19:33:30 -0700
Subject: [PATCH v1] pg_restore: Report rows restored per table in verbose mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When restoring table data directly into a database with --verbose,
report the number of rows loaded by each COPY, taken from the COPY
command tag.  This pairs with the existing "processing data for table"
message and gives a cheap per-table check that data was restored.

Data restored with INSERT commands (pg_dump --inserts) is not covered.

Idea from Sönmez Başar.
---
 src/bin/pg_dump/pg_backup_archiver.c      |  9 +++++-
 src/bin/pg_dump/pg_backup_db.c            | 10 +++++-
 src/bin/pg_dump/pg_backup_db.h            |  2 +-
 src/bin/pg_dump/t/004_pg_dump_parallel.pl |  7 +++--
 src/bin/pg_dump/t/011_restore.pl          | 38 ++++++++++++++++++-----
 5 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 92bb0ed..f252500 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -1049,7 +1049,14 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
 					 */
 					if (AH->outputKind == OUTPUT_COPYDATA &&
 						RestoringToDB(AH))
-						EndDBCopyMode(&AH->public, te->tag);
+					{
+						int64		ntuples;
+
+						ntuples = EndDBCopyMode(&AH->public, te->tag);
+						if (ntuples >= 0)
+							pg_log_info("restored %" PRId64 " rows of table \"%s.%s\"",
+										ntuples, te->namespace, te->tag);
+					}
 					AH->outputKind = OUTPUT_SQLCMDS;
 
 					/* close out the transaction started above */
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index c226d24..04cac8e 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -443,11 +443,15 @@ ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
 
 /*
  * Terminate a COPY operation during direct-to-DB restore
+ *
+ * Returns the number of rows loaded, or -1 if not known (COPY failed, or no
+ * COPY was in progress).
  */
-void
+int64
 EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
 {
 	ArchiveHandle *AH = (ArchiveHandle *) AHX;
+	int64		ntuples = -1;
 
 	if (AH->pgCopyIn)
 	{
@@ -467,6 +471,8 @@ EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
 		if (PQresultStatus(res) != PGRES_COMMAND_OK)
 			warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
 								  tocEntryTag, PQerrorMessage(AH->connection));
+		else
+			ntuples = strtoi64(PQcmdTuples(res), NULL, 10);
 		PQclear(res);
 
 		/* Do this to ensure we've pumped libpq back to idle state */
@@ -476,6 +482,8 @@ EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
 
 		AH->pgCopyIn = false;
 	}
+
+	return ntuples;
 }
 
 void
diff --git a/src/bin/pg_dump/pg_backup_db.h b/src/bin/pg_dump/pg_backup_db.h
index 8888dd3..0924ab6 100644
--- a/src/bin/pg_dump/pg_backup_db.h
+++ b/src/bin/pg_dump/pg_backup_db.h
@@ -18,7 +18,7 @@ extern PGresult *ExecuteSqlQuery(Archive *AHX, const char *query,
 								 ExecStatusType status);
 extern PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, const char *query);
 
-extern void EndDBCopyMode(Archive *AHX, const char *tocEntryTag);
+extern int64 EndDBCopyMode(Archive *AHX, const char *tocEntryTag);
 
 extern void StartTransaction(Archive *AHX);
 extern void CommitTransaction(Archive *AHX);
diff --git a/src/bin/pg_dump/t/004_pg_dump_parallel.pl b/src/bin/pg_dump/t/004_pg_dump_parallel.pl
index 738f34b..6e4c3df 100644
--- a/src/bin/pg_dump/t/004_pg_dump_parallel.pl
+++ b/src/bin/pg_dump/t/004_pg_dump_parallel.pl
@@ -57,14 +57,17 @@ $node->command_ok(
 	],
 	'parallel dump');
 
-$node->command_ok(
+$node->command_checks_all(
 	[
 		'pg_restore', '--verbose',
 		'--dbname' => $node->connstr($dbname2),
 		'--jobs' => 3,
 		"$backupdir/dump1",
 	],
-	'parallel restore');
+	0,
+	[qr/^$/],
+	[qr/restored 1000 rows of table "public.tplain"/],
+	'parallel restore reports rows restored');
 
 $node->command_ok(
 	[
diff --git a/src/bin/pg_dump/t/011_restore.pl b/src/bin/pg_dump/t/011_restore.pl
index 9ce275e..69b091e 100644
--- a/src/bin/pg_dump/t/011_restore.pl
+++ b/src/bin/pg_dump/t/011_restore.pl
@@ -132,14 +132,36 @@ foreach my $case (@fmt_cases)
 	$want_dump_sql = $sql_from_pg_restore unless $want_dump_sql;
 
 	# direct pg_restore to database
-	$node->command_ok(
-		[
-			'pg_restore', @$extra_restore,
-			'--verbose', '--clean',
-			'--dbname' => $node->connstr('regression_utf8'),
-			$dump_file,
-		],
-		"restore$space_rextra from $fmt$space_dextra");
+	$result = run_log [
+		'pg_restore', @$extra_restore,
+		'--verbose', '--clean',
+		'--dbname' => $node->connstr('regression_utf8'),
+		$dump_file,
+	  ],
+	  '2>' => \$stderr;
+	ok($result, "restore$space_rextra from $fmt$space_dextra");
+
+	# COPY reports the rows restored; INSERT commands do not
+	if (grep { /inserts/ } @$extra_dump)
+	{
+		unlike(
+			$stderr,
+			qr/restored \d+ rows/,
+			"restore$space_rextra from $fmt$space_dextra: no row counts");
+	}
+	else
+	{
+		like(
+			$stderr,
+			qr/restored 3 rows of table "public\.t"/,
+			"restore$space_rextra from $fmt$space_dextra: reports rows restored"
+		);
+		like(
+			$stderr,
+			qr/restored 0 rows of table "public\.view_base_table"/,
+			"restore$space_rextra from $fmt$space_dextra: reports empty table"
+		);
+	}
 
 	# compare round trip
 	$result = run_log [
-- 
2.47.3

