Skip to content

Commit 8e93a51

Browse files
committed
Don't propagate PGAPPNAME through pg_ctl in tests
When libpq is loaded in the server (for instance, by libpqwalreceiver), it may use libpq environment variables set in the postmaster environment for connection parameter defaults. This has some confusing effects in our test suites. For example, the TAP test infrastructure sets PGAPPNAME to allow identifying clients in the server log. But this environment variable is also inherited by temporary servers started with pg_ctl and is then in turn used by libpqwalreceiver as the application_name for connecting to remote servers where it then shows up in pg_stat_replication and is relevant for things like synchronous_standby_names. Replication already has a suitable default for application_name, and overriding that accidentally then requires the individual test cases to re-override that, which is all very confusing and unnecessary. To fix, unset PGAPPNAME temporarily before running pg_ctl start or restart in the tests. More comprehensive approaches like unsetting all environment variables in pg_ctl were considered but might be too complicated to achieve portably. The now unnecessary re-overriding of application_name by test cases is also removed. Reviewed-by: Noah Misch <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent c21d603 commit 8e93a51

13 files changed

+82
-71
lines changed

src/bin/pg_rewind/t/RewindTest.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ sub create_standby
160160

161161
$node_standby->append_conf(
162162
"postgresql.conf", qq(
163-
primary_conninfo='$connstr_master application_name=rewind_standby'
163+
primary_conninfo='$connstr_master'
164164
));
165165

166166
$node_standby->set_standby_mode();
@@ -180,7 +180,7 @@ sub promote_standby
180180
# up standby
181181

182182
# Wait for the standby to receive and write all WAL.
183-
$node_master->wait_for_catchup('rewind_standby', 'write');
183+
$node_master->wait_for_catchup($node_standby, 'write');
184184

185185
# Now promote standby and insert some new data on master, this will put
186186
# the master out-of-sync with the standby.

src/test/perl/PostgresNode.pm

+27-7
Original file line numberDiff line numberDiff line change
@@ -698,12 +698,24 @@ sub start
698698
my $port = $self->port;
699699
my $pgdata = $self->data_dir;
700700
my $name = $self->name;
701+
my $ret;
702+
701703
BAIL_OUT("node \"$name\" is already running") if defined $self->{_pid};
704+
702705
print("### Starting node \"$name\"\n");
703-
# Note: We set the cluster_name here, not in postgresql.conf (in
704-
# sub init) so that it does not get copied to standbys.
705-
my $ret = TestLib::system_log('pg_ctl', '-D', $self->data_dir, '-l',
706-
$self->logfile, '-o', "--cluster-name=$name", 'start');
706+
707+
{
708+
# Temporarily unset PGAPPNAME so that the server doesn't
709+
# inherit it. Otherwise this could affect libpqwalreceiver
710+
# connections in confusing ways.
711+
local %ENV = %ENV;
712+
delete $ENV{PGAPPNAME};
713+
714+
# Note: We set the cluster_name here, not in postgresql.conf (in
715+
# sub init) so that it does not get copied to standbys.
716+
$ret = TestLib::system_log('pg_ctl', '-D', $self->data_dir, '-l',
717+
$self->logfile, '-o', "--cluster-name=$name", 'start');
718+
}
707719

708720
if ($ret != 0)
709721
{
@@ -776,9 +788,17 @@ sub restart
776788
my $pgdata = $self->data_dir;
777789
my $logfile = $self->logfile;
778790
my $name = $self->name;
791+
779792
print "### Restarting node \"$name\"\n";
780-
TestLib::system_or_bail('pg_ctl', '-D', $pgdata, '-l', $logfile,
781-
'restart');
793+
794+
{
795+
local %ENV = %ENV;
796+
delete $ENV{PGAPPNAME};
797+
798+
TestLib::system_or_bail('pg_ctl', '-D', $pgdata, '-l', $logfile,
799+
'restart');
800+
}
801+
782802
$self->_update_pid(1);
783803
return;
784804
}
@@ -835,7 +855,7 @@ sub enable_streaming
835855
print "### Enabling streaming replication for node \"$name\"\n";
836856
$self->append_conf(
837857
'postgresql.conf', qq(
838-
primary_conninfo='$root_connstr application_name=$name'
858+
primary_conninfo='$root_connstr'
839859
));
840860
$self->set_standby_mode();
841861
return;

src/test/recovery/t/004_timeline_switch.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
my $connstr_1 = $node_standby_1->connstr;
5151
$node_standby_2->append_conf(
5252
'postgresql.conf', qq(
53-
primary_conninfo='$connstr_1 application_name=@{[$node_standby_2->name]}'
53+
primary_conninfo='$connstr_1'
5454
));
5555
$node_standby_2->restart;
5656

src/test/subscription/t/001_rep_changes.pl

+13-14
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@
6363
$node_publisher->safe_psql('postgres',
6464
"ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins");
6565

66-
my $appname = 'tap_sub';
6766
$node_subscriber->safe_psql('postgres',
68-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub, tap_pub_ins_only"
67+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub, tap_pub_ins_only"
6968
);
7069

71-
$node_publisher->wait_for_catchup($appname);
70+
$node_publisher->wait_for_catchup('tap_sub');
7271

7372
# Also wait for initial table sync to finish
7473
my $synced_query =
@@ -103,7 +102,7 @@
103102
"DELETE FROM tab_include WHERE a > 20");
104103
$node_publisher->safe_psql('postgres', "UPDATE tab_include SET a = -a");
105104

106-
$node_publisher->wait_for_catchup($appname);
105+
$node_publisher->wait_for_catchup('tap_sub');
107106

108107
$result = $node_subscriber->safe_psql('postgres',
109108
"SELECT count(*), min(a), max(a) FROM tab_ins");
@@ -146,7 +145,7 @@
146145
$node_publisher->safe_psql('postgres',
147146
"UPDATE tab_full2 SET x = 'bb' WHERE x = 'b'");
148147

149-
$node_publisher->wait_for_catchup($appname);
148+
$node_publisher->wait_for_catchup('tap_sub');
150149

151150
$result = $node_subscriber->safe_psql('postgres',
152151
"SELECT count(*), min(a), max(a) FROM tab_full");
@@ -165,23 +164,23 @@
165164
# as we need to poll for a change but the test suite will fail none the less
166165
# when something goes wrong.
167166
my $oldpid = $node_publisher->safe_psql('postgres',
168-
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname';"
167+
"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub';"
169168
);
170169
$node_subscriber->safe_psql('postgres',
171-
"ALTER SUBSCRIPTION tap_sub CONNECTION 'application_name=$appname $publisher_connstr'"
170+
"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
172171
);
173172
$node_publisher->poll_query_until('postgres',
174-
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname';"
173+
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub';"
175174
) or die "Timed out while waiting for apply to restart";
176175

177176
$oldpid = $node_publisher->safe_psql('postgres',
178-
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname';"
177+
"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub';"
179178
);
180179
$node_subscriber->safe_psql('postgres',
181180
"ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub_ins_only WITH (copy_data = false)"
182181
);
183182
$node_publisher->poll_query_until('postgres',
184-
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname';"
183+
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub';"
185184
) or die "Timed out while waiting for apply to restart";
186185

187186
$node_publisher->safe_psql('postgres',
@@ -193,7 +192,7 @@
193192
$node_publisher->stop('fast');
194193
$node_publisher->start;
195194

196-
$node_publisher->wait_for_catchup($appname);
195+
$node_publisher->wait_for_catchup('tap_sub');
197196

198197
$result = $node_subscriber->safe_psql('postgres',
199198
"SELECT count(*), min(a), max(a) FROM tab_ins");
@@ -216,7 +215,7 @@
216215
);
217216
$node_publisher->safe_psql('postgres', "INSERT INTO tab_full VALUES(0)");
218217

219-
$node_publisher->wait_for_catchup($appname);
218+
$node_publisher->wait_for_catchup('tap_sub');
220219

221220
# note that data are different on provider and subscriber
222221
$result = $node_subscriber->safe_psql('postgres',
@@ -230,12 +229,12 @@
230229

231230
# check restart on rename
232231
$oldpid = $node_publisher->safe_psql('postgres',
233-
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname';"
232+
"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub';"
234233
);
235234
$node_subscriber->safe_psql('postgres',
236235
"ALTER SUBSCRIPTION tap_sub RENAME TO tap_sub_renamed");
237236
$node_publisher->poll_query_until('postgres',
238-
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname';"
237+
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub_renamed';"
239238
) or die "Timed out while waiting for apply to restart";
240239

241240
# check all the cleanup

src/test/subscription/t/002_types.pl

+6-7
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@
107107
$node_publisher->safe_psql('postgres',
108108
"CREATE PUBLICATION tap_pub FOR ALL TABLES");
109109

110-
my $appname = 'tap_sub';
111110
$node_subscriber->safe_psql('postgres',
112-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (slot_name = tap_sub_slot)"
111+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (slot_name = tap_sub_slot)"
113112
);
114113

115-
$node_publisher->wait_for_catchup($appname);
114+
$node_publisher->wait_for_catchup('tap_sub');
116115

117116
# Wait for initial sync to finish as well
118117
my $synced_query =
@@ -251,7 +250,7 @@
251250
INSERT INTO tst_dom_constr VALUES (10);
252251
));
253252

254-
$node_publisher->wait_for_catchup($appname);
253+
$node_publisher->wait_for_catchup('tap_sub');
255254

256255
# Check the data on subscriber
257256
my $result = $node_subscriber->safe_psql(
@@ -372,7 +371,7 @@
372371
UPDATE tst_hstore SET b = '"also"=>"updated"' WHERE a = 3;
373372
));
374373

375-
$node_publisher->wait_for_catchup($appname);
374+
$node_publisher->wait_for_catchup('tap_sub');
376375

377376
# Check the data on subscriber
378377
$result = $node_subscriber->safe_psql(
@@ -492,7 +491,7 @@
492491
DELETE FROM tst_hstore WHERE a = 1;
493492
));
494493

495-
$node_publisher->wait_for_catchup($appname);
494+
$node_publisher->wait_for_catchup('tap_sub');
496495

497496
# Check the data on subscriber
498497
$result = $node_subscriber->safe_psql(
@@ -554,7 +553,7 @@
554553
# which needs an active snapshot in order to operate.
555554
$node_publisher->safe_psql('postgres', "INSERT INTO tst_dom_constr VALUES (11)");
556555

557-
$node_publisher->wait_for_catchup($appname);
556+
$node_publisher->wait_for_catchup('tap_sub');
558557

559558
$result =
560559
$node_subscriber->safe_psql('postgres', "SELECT sum(a) FROM tst_dom_constr");

src/test/subscription/t/003_constraints.pl

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@
3434
$node_publisher->safe_psql('postgres',
3535
"CREATE PUBLICATION tap_pub FOR ALL TABLES;");
3636

37-
my $appname = 'tap_sub';
3837
$node_subscriber->safe_psql('postgres',
39-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (copy_data = false)"
38+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (copy_data = false)"
4039
);
4140

42-
$node_publisher->wait_for_catchup($appname);
41+
$node_publisher->wait_for_catchup('tap_sub');
4342

4443
$node_publisher->safe_psql('postgres',
4544
"INSERT INTO tab_fk (bid) VALUES (1);");
4645
$node_publisher->safe_psql('postgres',
4746
"INSERT INTO tab_fk_ref (id, bid) VALUES (1, 1);");
4847

49-
$node_publisher->wait_for_catchup($appname);
48+
$node_publisher->wait_for_catchup('tap_sub');
5049

5150
# Check data on subscriber
5251
my $result = $node_subscriber->safe_psql('postgres',
@@ -64,7 +63,7 @@
6463
$node_publisher->safe_psql('postgres',
6564
"INSERT INTO tab_fk_ref (id, bid) VALUES (2, 2);");
6665

67-
$node_publisher->wait_for_catchup($appname);
66+
$node_publisher->wait_for_catchup('tap_sub');
6867

6968
# FK is not enforced on subscriber
7069
$result = $node_subscriber->safe_psql('postgres',
@@ -98,7 +97,7 @@ BEGIN
9897
$node_publisher->safe_psql('postgres',
9998
"INSERT INTO tab_fk_ref (id, bid) VALUES (10, 10);");
10099

101-
$node_publisher->wait_for_catchup($appname);
100+
$node_publisher->wait_for_catchup('tap_sub');
102101

103102
# The row should be skipped on subscriber
104103
$result = $node_subscriber->safe_psql('postgres',

src/test/subscription/t/004_sync.pl

+7-8
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
$node_publisher->safe_psql('postgres',
3333
"CREATE PUBLICATION tap_pub FOR ALL TABLES");
3434

35-
my $appname = 'tap_sub';
3635
$node_subscriber->safe_psql('postgres',
37-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
36+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
3837
);
3938

40-
$node_publisher->wait_for_catchup($appname);
39+
$node_publisher->wait_for_catchup('tap_sub');
4140

4241
# Also wait for initial table sync to finish
4342
my $synced_query =
@@ -57,7 +56,7 @@
5756

5857
# recreate the subscription, it will try to do initial copy
5958
$node_subscriber->safe_psql('postgres',
60-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
59+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
6160
);
6261

6362
# but it will be stuck on data copy as it will fail on constraint
@@ -79,7 +78,7 @@
7978

8079
# now check another subscription for the same node pair
8180
$node_subscriber->safe_psql('postgres',
82-
"CREATE SUBSCRIPTION tap_sub2 CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (copy_data = false)"
81+
"CREATE SUBSCRIPTION tap_sub2 CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (copy_data = false)"
8382
);
8483

8584
# wait for it to start
@@ -101,7 +100,7 @@
101100

102101
# recreate the subscription again
103102
$node_subscriber->safe_psql('postgres',
104-
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
103+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
105104
);
106105

107106
# and wait for data sync to finish again
@@ -120,7 +119,7 @@
120119
$node_publisher->safe_psql('postgres',
121120
"CREATE TABLE tab_rep_next (a) AS SELECT generate_series(1,10)");
122121

123-
$node_publisher->wait_for_catchup($appname);
122+
$node_publisher->wait_for_catchup('tap_sub');
124123

125124
$result = $node_subscriber->safe_psql('postgres',
126125
"SELECT count(*) FROM tab_rep_next");
@@ -143,7 +142,7 @@
143142
$node_publisher->safe_psql('postgres',
144143
"INSERT INTO tab_rep_next SELECT generate_series(1,10)");
145144

146-
$node_publisher->wait_for_catchup($appname);
145+
$node_publisher->wait_for_catchup('tap_sub');
147146

148147
$result = $node_subscriber->safe_psql('postgres',
149148
"SELECT count(*) FROM tab_rep_next");

src/test/subscription/t/005_encoding.pl

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
$node_subscriber->safe_psql('postgres', $ddl);
2323

2424
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
25-
my $appname = 'encoding_test';
2625

2726
$node_publisher->safe_psql('postgres',
2827
"CREATE PUBLICATION mypub FOR ALL TABLES;");
2928
$node_subscriber->safe_psql('postgres',
30-
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
29+
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
3130
);
3231

33-
$node_publisher->wait_for_catchup($appname);
32+
$node_publisher->wait_for_catchup('mysub');
3433

3534
# Wait for initial sync to finish as well
3635
my $synced_query =
@@ -41,7 +40,7 @@
4140
$node_publisher->safe_psql('postgres',
4241
q{INSERT INTO test1 VALUES (1, E'Mot\xc3\xb6rhead')}); # hand-rolled UTF-8
4342

44-
$node_publisher->wait_for_catchup($appname);
43+
$node_publisher->wait_for_catchup('mysub');
4544

4645
is( $node_subscriber->safe_psql(
4746
'postgres', q{SELECT a FROM test1 WHERE b = E'Mot\xf6rhead'}

0 commit comments

Comments
 (0)