Fix dblink's failure to report correct connection name in error messages.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Mar 2012 21:52:38 +0000 (17:52 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Mar 2012 21:52:38 +0000 (17:52 -0400)
The DBLINK_GET_CONN and DBLINK_GET_NAMED_CONN macros did not set the
surrounding function's conname variable, causing errors to be incorrectly
reported as having occurred on the "unnamed" connection in some cases.
This bug was actually visible in two cases in the regression tests,
but apparently whoever added those cases wasn't paying attention.

Noted by Kyotaro Horiguchi, though this is different from his proposed
patch.

Back-patch to 8.4; 8.3 does not have the same type of error reporting
so the patch is not relevant.

contrib/dblink/dblink.c
contrib/dblink/expected/dblink.out

index 09eea1881a64b12d404d71ea87af6a6f41d981c8..0a54ebac17868dc6ca99e0805f243ed6642ec096 100644 (file)
@@ -168,9 +168,10 @@ typedef struct remoteConnHashEnt
        do { \
                        char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
                        rconn = getConnectionByName(conname_or_str); \
-                       if(rconn) \
+                       if (rconn) \
                        { \
                                conn = rconn->conn; \
+                               conname = conname_or_str; \
                        } \
                        else \
                        { \
@@ -198,9 +199,9 @@ typedef struct remoteConnHashEnt
 
 #define DBLINK_GET_NAMED_CONN \
        do { \
-                       char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
+                       conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
                        rconn = getConnectionByName(conname); \
-                       if(rconn) \
+                       if (rconn) \
                                conn = rconn->conn; \
                        else \
                                DBLINK_CONN_NOT_AVAIL; \
@@ -613,6 +614,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query);
 Datum
 dblink_send_query(PG_FUNCTION_ARGS)
 {
+       char       *conname = NULL;
        PGconn     *conn = NULL;
        char       *connstr = NULL;
        char       *sql = NULL;
@@ -937,6 +939,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy);
 Datum
 dblink_is_busy(PG_FUNCTION_ARGS)
 {
+       char       *conname = NULL;
        PGconn     *conn = NULL;
        remoteConn *rconn = NULL;
 
@@ -963,6 +966,7 @@ Datum
 dblink_cancel_query(PG_FUNCTION_ARGS)
 {
        int                     res = 0;
+       char       *conname = NULL;
        PGconn     *conn = NULL;
        remoteConn *rconn = NULL;
        PGcancel   *cancel;
@@ -997,6 +1001,7 @@ Datum
 dblink_error_message(PG_FUNCTION_ARGS)
 {
        char       *msg;
+       char       *conname = NULL;
        PGconn     *conn = NULL;
        remoteConn *rconn = NULL;
 
@@ -1506,6 +1511,7 @@ PG_FUNCTION_INFO_V1(dblink_get_notify);
 Datum
 dblink_get_notify(PG_FUNCTION_ARGS)
 {
+       char       *conname = NULL;
        PGconn     *conn = NULL;
        remoteConn *rconn = NULL;
        PGnotify   *notify;
index c59a67c7374041580c42e201506d798e66c25c35..59e666ffec406948fa12364133ffb88606412a5a 100644 (file)
@@ -381,7 +381,7 @@ SELECT *
 FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
 WHERE t.a > 7;
 NOTICE:  relation "foobar" does not exist
-CONTEXT:  Error occurred on dblink connection named "unnamed": could not execute query.
+CONTEXT:  Error occurred on dblink connection named "myconn": could not execute query.
  a | b | c 
 ---+---+---
 (0 rows)
@@ -504,7 +504,7 @@ SELECT dblink_close('myconn','rmt_foo_cursor');
 -- this should fail because there is no open transaction
 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
 ERROR:  DECLARE CURSOR can only be used in transaction blocks
-CONTEXT:  Error occurred on dblink connection named "unnamed": could not execute command.
+CONTEXT:  Error occurred on dblink connection named "myconn": could not execute command.
 -- reset remote transaction state
 SELECT dblink_exec('myconn','ABORT');
  dblink_exec