From bbaad161d5add2f895e99cf087accc2558fde551 Mon Sep 17 00:00:00 2001
From: Nisha Moond <nisha.moond412@gmail.com>
Date: Fri, 14 Mar 2025 16:52:29 +0530
Subject: [PATCH] Nisha_v12_fixes

---
 doc/src/sgml/ref/pg_createsubscriber.sgml   |  5 +-
 src/bin/pg_basebackup/pg_createsubscriber.c |  4 +-
 src/bin/pg_basebackup/t/042_all_option.pl   | 65 ++++++++++++++++-----
 3 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml
index a1abd64fd75..7376c61f3eb 100644
--- a/doc/src/sgml/ref/pg_createsubscriber.sgml
+++ b/doc/src/sgml/ref/pg_createsubscriber.sgml
@@ -114,8 +114,7 @@ PostgreSQL documentation
      <listitem>
       <para>
        For all source server non-template databases create subscriptions for
-       create subscriptions for databases with the same names on the
-       target server.
+       databases with the same names on the target server.
        Subscription names, publication names, and replication slot names are
        automatically generated. This option cannot be used together with
        <option>--database</option>, <option>--publication</option>,
@@ -131,7 +130,7 @@ PostgreSQL documentation
       <para>
        The name of the database in which to create a subscription.  Multiple
        databases can be selected by writing multiple <option>-d</option>
-       switches. This option cannot be together with <option>--all</option>.
+       switches. This option cannot be used together with <option>--all</option>.
        If <option>-d</option> option is not provided, the database name will be
        obtained from <option>-P</option> option. If the database name is not
        specified in either the <option>-d</option> option or
diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index d2771a81780..efaef97bd3a 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -1916,7 +1916,7 @@ fetch_source_databases(struct CreateSubscriberOptions *opt)
 	PGconn	   *conn;
 	PGresult   *res;
 
-	/* Establish a connection to the PostgreSQL server */
+	/* Establish a connection to the source server */
 	conn = connect_database(opt->pub_conninfo_str, true);
 
 	res = PQexec(conn, "SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn = true");
@@ -2141,7 +2141,7 @@ main(int argc, char **argv)
 		}
 	}
 
-	/* Validate that --all is not used with incompatible options. */
+	/* Validate that --all is not used with incompatible options */
 	if (opt.all_dbs)
 	{
 		char	   *bad_switch = NULL;
diff --git a/src/bin/pg_basebackup/t/042_all_option.pl b/src/bin/pg_basebackup/t/042_all_option.pl
index b228ed612b7..18e74c294b9 100644
--- a/src/bin/pg_basebackup/t/042_all_option.pl
+++ b/src/bin/pg_basebackup/t/042_all_option.pl
@@ -53,9 +53,10 @@ $node_p->start;
 my $db1 = generate_db($node_p, 'regression\\"\\', 1, 45, '\\\\"\\\\\\');
 my $db2 = generate_db($node_p, 'regression', 46, 90, '');
 
-$node_p->safe_psql($db1, 'CREATE TABLE tbl1 (a text)');
-$node_p->safe_psql($db1, "INSERT INTO tbl1 VALUES('first row')");
-$node_p->safe_psql($db2, 'CREATE TABLE tbl2 (a text)');
+$node_p->safe_psql('postgres', 'CREATE TABLE tbl1 (a text)');
+$node_p->safe_psql($db1, 'CREATE TABLE tbl2 (a text)');
+$node_p->safe_psql($db1, "INSERT INTO tbl2 VALUES('first row')");
+$node_p->safe_psql($db2, 'CREATE TABLE tbl3 (a text)');
 
 # Set up node S as standby linking to node P
 $node_p->backup('backup_1');
@@ -85,7 +86,7 @@ command_fails_like(
 	qr/--database cannot be used with --all/,
 	'fail if --database is used with --all');
 
-# run pg_createsubscriber with '--database' and '--all' without '--dry-run'
+# run pg_createsubscriber with '--all' and '--database' without '--dry-run'
 # and verify the failure
 command_fails_like(
 	[
@@ -95,13 +96,13 @@ command_fails_like(
 		'--publisher-server' => $node_p->connstr($db1),
 		'--socketdir' => $node_s->host,
 		'--subscriber-port' => $node_s->port,
-		'--database' => $db1,
 		'--all',
+		'--database' => $db1,
 	],
 	qr/--database cannot be used with --all/,
-	'fail if --database is used with --all');
+	'fail if --database is used with --all without --dry-run');
 
-# run pg_createsubscriber with '--publication' and '--all' and verify
+# run pg_createsubscriber with '--all' and '--publication' and verify
 # the failure
 command_fails_like(
 	[
@@ -118,7 +119,7 @@ command_fails_like(
 	qr/--publication cannot be used with --all/,
 	'fail if --publication is used with --all');
 
-# run pg_createsubscriber with '--replication-slot' and '--all' and
+# run pg_createsubscriber with '--all' and '--replication-slot' and
 # verify the failure
 command_fails_like(
 	[
@@ -129,13 +130,13 @@ command_fails_like(
 		'--publisher-server' => $node_p->connstr($db1),
 		'--socketdir' => $node_s->host,
 		'--subscriber-port' => $node_s->port,
-		'--replication-slot' => 'replslot1',
 		'--all',
+		'--replication-slot' => 'replslot1',
 	],
 	qr/--replication-slot cannot be used with --all/,
 	'fail if --replication-slot is used with --all');
 
-# run pg_createsubscriber with '--subscription' and '--all' and
+# run pg_createsubscriber with '--all' and '--subscription' and
 # verify the failure
 command_fails_like(
 	[
@@ -177,7 +178,7 @@ command_ok(
 		'--subscriber-port' => $node_s->port,
 		'--all',
 	],
-	'run pg_createsubscriber with --all');
+	'run pg_createsubscriber with --all without --dry-run');
 
 $node_s->start;
 
@@ -191,7 +192,9 @@ my $bgconn = $node_s->background_psql('postgres');
 $bgconn->query_safe(
 	qq[
 	BEGIN;
-	DECLARE cursor CURSOR FOR SELECT datname FROM pg_subscription, pg_database WHERE subdbid = pg_database.oid and datistemplate = 'f' ORDER BY pg_database.oid;
+	DECLARE cursor CURSOR FOR SELECT datname FROM pg_subscription, pg_database
+	WHERE subdbid = pg_database.oid and datistemplate = 'f'
+	ORDER BY pg_database.oid;
 ]);
 
 # Fetch from the cursor three times and confirm the existence of the
@@ -205,11 +208,45 @@ foreach my $dbname (@user_dbs)
 $bgconn->quit;
 
 # Verify template databases do not have subscriptions
-my $result = $node_s->safe_psql('postgres',
-	"SELECT count(*) FROM pg_subscription, pg_database WHERE subdbid = pg_database.oid and datistemplate = 't';"
+my $result = $node_s->safe_psql(
+	'postgres',
+	"SELECT count(*) FROM pg_subscription, pg_database
+	 WHERE subdbid = pg_database.oid and datistemplate = 't';"
 );
 is($result, '0', 'subscription is not created on template databases');
 
+# Verify logical replication works for all databases
+# Insert rows on node P
+$node_p->safe_psql('postgres', "INSERT INTO tbl1 VALUES('first row')");
+$node_p->safe_psql($db1, "INSERT INTO tbl2 VALUES('second row')");
+$node_p->safe_psql($db2, "INSERT INTO tbl3 VALUES('first row')");
+
+# Get subscription names
+$result = $node_s->safe_psql(
+	'postgres', qq(
+	SELECT subname FROM pg_subscription WHERE subname ~ '^pg_createsubscriber_'
+));
+my @subnames = split("\n", $result);
+
+# Wait subscriber to catch up
+$node_s->wait_for_subscription_sync($node_p, $subnames[0]);
+$node_s->wait_for_subscription_sync($node_p, $subnames[1]);
+$node_s->wait_for_subscription_sync($node_p, $subnames[2]);
+
+# Check result in database 'postgres' of node S
+$result = $node_s->safe_psql('postgres', 'SELECT * FROM tbl1');
+is($result, qq(first row), "logical replication works in database postgres");
+
+# Check result in database $db1 of node S
+$result = $node_s->safe_psql($db1, 'SELECT * FROM tbl2');
+is( $result, qq(first row
+second row),
+	"logical replication works in database $db1");
+
+# Check result in database $db2 of node S
+$result = $node_s->safe_psql($db2, 'SELECT * FROM tbl3');
+is($result, qq(first row), "logical replication works in database $db2");
+
 # clean up
 $node_p->teardown_node;
 $node_s->teardown_node;
-- 
2.34.1

