Commit 4e8c0f1a authored by Alvaro Herrera's avatar Alvaro Herrera

AlterSubscription_refresh: avoid stomping on global variable

This patch replaces use of the global "wrconn" variable in
AlterSubscription_refresh with a local variable of the same name, making
it consistent with other functions in subscriptioncmds.c (e.g.
DropSubscription).

The global wrconn is only meant to be used for logical apply/tablesync worker.
Abusing it this way is known to cause trouble if an apply worker
manages to do a subscription refresh, such as reported by Jeremy Finzel
and diagnosed by Andres Freund back in November 2020, at
https://www.postgresql.org/message-id/20201111215820.qihhrz7fayu6myfi@alap3.anarazel.de

Backpatch to 10.  In branch master, also move the connection establishment
to occur outside the PG_TRY block; this way we can remove a test for NULL in
PG_FINALLY, and it also makes the code more consistent with similar code in
the same file.

Author: Peter Smith <peter.b.smith@fujitsu.com>
Reviewed-by: default avatarBharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Reviewed-by: default avatarJapin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/CAHut+Pu7Jv9L2BOEx_Z0UtJxfDevQSAUW2mJqWU+CtmDrEZVAg@mail.gmail.com
parent 8b82de01
...@@ -556,18 +556,19 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data) ...@@ -556,18 +556,19 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data)
char state; char state;
} SubRemoveRels; } SubRemoveRels;
SubRemoveRels *sub_remove_rels; SubRemoveRels *sub_remove_rels;
WalReceiverConn *wrconn;
/* Load the library providing us libpq calls. */ /* Load the library providing us libpq calls. */
load_file("libpqwalreceiver", false); load_file("libpqwalreceiver", false);
PG_TRY();
{
/* Try to connect to the publisher. */ /* Try to connect to the publisher. */
wrconn = walrcv_connect(sub->conninfo, true, sub->name, &err); wrconn = walrcv_connect(sub->conninfo, true, sub->name, &err);
if (!wrconn) if (!wrconn)
ereport(ERROR, ereport(ERROR,
(errmsg("could not connect to the publisher: %s", err))); (errmsg("could not connect to the publisher: %s", err)));
PG_TRY();
{
/* Get the table list from publisher. */ /* Get the table list from publisher. */
pubrel_names = fetch_table_list(wrconn, sub->publications); pubrel_names = fetch_table_list(wrconn, sub->publications);
...@@ -737,7 +738,6 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data) ...@@ -737,7 +738,6 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data)
} }
PG_FINALLY(); PG_FINALLY();
{ {
if (wrconn)
walrcv_disconnect(wrconn); walrcv_disconnect(wrconn);
} }
PG_END_TRY(); PG_END_TRY();
...@@ -1062,7 +1062,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) ...@@ -1062,7 +1062,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
ListCell *lc; ListCell *lc;
char originname[NAMEDATALEN]; char originname[NAMEDATALEN];
char *err = NULL; char *err = NULL;
WalReceiverConn *wrconn = NULL; WalReceiverConn *wrconn;
Form_pg_subscription form; Form_pg_subscription form;
List *rstates; List *rstates;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment