AlterSubscription_refresh: avoid stomping on global variable
authorAlvaro Herrera
Fri, 7 May 2021 15:46:37 +0000 (11:46 -0400)
committerAlvaro Herrera
Fri, 7 May 2021 15:46:37 +0000 (11:46 -0400)
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[email protected]

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 
Reviewed-by: Bharath Rupireddy
Reviewed-by: Japin Li
Discussion: https://postgr.es/m/CAHut+Pu7Jv9L2BOEx_Z0UtJxfDevQSAUW2mJqWU+CtmDrEZVAg@mail.gmail.com

src/backend/commands/subscriptioncmds.c

index 9ebb026187f7503419ba97215c2cb804d7a39b02..03cce8a91235fb1712c0b134b4d72df4436f6be2 100644 (file)
@@ -517,6 +517,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data)
    List       *subrel_states;
    Oid        *subrel_local_oids;
    Oid        *pubrel_local_oids;
+   WalReceiverConn *wrconn;
    ListCell   *lc;
    int         off;
 
@@ -835,7 +836,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
    char        originname[NAMEDATALEN];
    char       *err = NULL;
    RepOriginId originid;
-   WalReceiverConn *wrconn = NULL;
+   WalReceiverConn *wrconn;
    StringInfoData cmd;
    Form_pg_subscription form;