From fbed4bc59e362e13fb0d0e0b08c995c18c7e4d5a Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Tue, 31 Jan 2023 17:36:39 -0800 Subject: [PATCH] dblink: Fix variable confusion introduced in e4602483e95 Thanks to Robins to find the issue and Nathan for promptly writing a test case to prevent future problems like this. Reported-by: Nathan Bossart Reported-by: Robins Tharakan Author: Nathan Bossart Discussion: https://postgr.es/m/20230130193008.GA2740781@nathanxps13 --- contrib/dblink/dblink.c | 2 +- contrib/dblink/expected/dblink.out | 19 +++++++++++++++++++ contrib/dblink/sql/dblink.sql | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index 8982d623d3..78a8bcee6e 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -321,7 +321,7 @@ dblink_connect(PG_FUNCTION_ARGS) else { if (pconn->conn) - libpqsrv_disconnect(conn); + libpqsrv_disconnect(pconn->conn); pconn->conn = conn; } diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out index 14d015e4d5..0f5050b409 100644 --- a/contrib/dblink/expected/dblink.out +++ b/contrib/dblink/expected/dblink.out @@ -938,6 +938,25 @@ DROP SERVER fdtest; ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS (nonexistent 'fdw'); ERROR: invalid option "nonexistent" HINT: There are no valid options in this context. +-- test repeated calls to dblink_connect +SELECT dblink_connect(connection_parameters()); + dblink_connect +---------------- + OK +(1 row) + +SELECT dblink_connect(connection_parameters()); + dblink_connect +---------------- + OK +(1 row) + +SELECT dblink_disconnect(); + dblink_disconnect +------------------- + OK +(1 row) + -- test asynchronous notifications SELECT dblink_connect(connection_parameters()); dblink_connect diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql index e560260bfc..7870ce5d5a 100644 --- a/contrib/dblink/sql/dblink.sql +++ b/contrib/dblink/sql/dblink.sql @@ -489,6 +489,11 @@ DROP SERVER fdtest; -- should fail ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS (nonexistent 'fdw'); +-- test repeated calls to dblink_connect +SELECT dblink_connect(connection_parameters()); +SELECT dblink_connect(connection_parameters()); +SELECT dblink_disconnect(); + -- test asynchronous notifications SELECT dblink_connect(connection_parameters());