Fix ancient connection leak in dblink
authorJoe Conway <[email protected]>
Sat, 11 Mar 2017 21:32:40 +0000 (13:32 -0800)
committerJoe Conway <[email protected]>
Sat, 11 Mar 2017 21:32:40 +0000 (13:32 -0800)
When using unnamed connections with dblink, every time a new
connection is made, the old one is leaked. Fix that.

This has been an issue probably since dblink was first committed.
Someone complained almost ten years ago, but apparently I decided
not to pursue it at the time, and neither did anyone else, so it
slipped between the cracks. Now that someone else has complained,
fix in all supported branches.

Discussion: (orig) https://postgr.es/m/flat/F680AB59-6D6F-4026-9599-1BE28880273D%40decibel.org#F680AB59-6D6F-4026-9599-1BE28880273D@decibel.org
Discussion: (new) https://postgr.es/m/flat/0A3221C70F24FB45833433255569204D1F6ADF8C@G01JPEXMBYT05
Reported by: Jim Nasby and Takayuki Tsunakawa

contrib/dblink/dblink.c

index cdc39c10fc78acfb2b2fb4592ed73440f8f0349c..057a25ae1188940ff31573bd7da4485aa1c39a95 100644 (file)
@@ -301,7 +301,11 @@ dblink_connect(PG_FUNCTION_ARGS)
                createNewConnection(connname, rconn);
        }
        else
+       {
+               if (pconn->conn)
+                       PQfinish(pconn->conn);
                pconn->conn = conn;
+       }
 
        PG_RETURN_TEXT_P(cstring_to_text("OK"));
 }