Commit b75fbe91 authored by Tom Lane's avatar Tom Lane

Fix dblink's failure to report correct connection name in error messages.

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.
parent 566a1d43
...@@ -153,9 +153,10 @@ typedef struct remoteConnHashEnt ...@@ -153,9 +153,10 @@ typedef struct remoteConnHashEnt
do { \ do { \
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \ char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
rconn = getConnectionByName(conname_or_str); \ rconn = getConnectionByName(conname_or_str); \
if(rconn) \ if (rconn) \
{ \ { \
conn = rconn->conn; \ conn = rconn->conn; \
conname = conname_or_str; \
} \ } \
else \ else \
{ \ { \
...@@ -183,9 +184,9 @@ typedef struct remoteConnHashEnt ...@@ -183,9 +184,9 @@ typedef struct remoteConnHashEnt
#define DBLINK_GET_NAMED_CONN \ #define DBLINK_GET_NAMED_CONN \
do { \ do { \
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \ conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
rconn = getConnectionByName(conname); \ rconn = getConnectionByName(conname); \
if(rconn) \ if (rconn) \
conn = rconn->conn; \ conn = rconn->conn; \
else \ else \
DBLINK_CONN_NOT_AVAIL; \ DBLINK_CONN_NOT_AVAIL; \
...@@ -598,6 +599,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query); ...@@ -598,6 +599,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query);
Datum Datum
dblink_send_query(PG_FUNCTION_ARGS) dblink_send_query(PG_FUNCTION_ARGS)
{ {
char *conname = NULL;
PGconn *conn = NULL; PGconn *conn = NULL;
char *sql = NULL; char *sql = NULL;
remoteConn *rconn = NULL; remoteConn *rconn = NULL;
...@@ -919,6 +921,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy); ...@@ -919,6 +921,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy);
Datum Datum
dblink_is_busy(PG_FUNCTION_ARGS) dblink_is_busy(PG_FUNCTION_ARGS)
{ {
char *conname = NULL;
PGconn *conn = NULL; PGconn *conn = NULL;
remoteConn *rconn = NULL; remoteConn *rconn = NULL;
...@@ -945,6 +948,7 @@ Datum ...@@ -945,6 +948,7 @@ Datum
dblink_cancel_query(PG_FUNCTION_ARGS) dblink_cancel_query(PG_FUNCTION_ARGS)
{ {
int res = 0; int res = 0;
char *conname = NULL;
PGconn *conn = NULL; PGconn *conn = NULL;
remoteConn *rconn = NULL; remoteConn *rconn = NULL;
PGcancel *cancel; PGcancel *cancel;
...@@ -979,6 +983,7 @@ Datum ...@@ -979,6 +983,7 @@ Datum
dblink_error_message(PG_FUNCTION_ARGS) dblink_error_message(PG_FUNCTION_ARGS)
{ {
char *msg; char *msg;
char *conname = NULL;
PGconn *conn = NULL; PGconn *conn = NULL;
remoteConn *rconn = NULL; remoteConn *rconn = NULL;
...@@ -1488,6 +1493,7 @@ PG_FUNCTION_INFO_V1(dblink_get_notify); ...@@ -1488,6 +1493,7 @@ PG_FUNCTION_INFO_V1(dblink_get_notify);
Datum Datum
dblink_get_notify(PG_FUNCTION_ARGS) dblink_get_notify(PG_FUNCTION_ARGS)
{ {
char *conname = NULL;
PGconn *conn = NULL; PGconn *conn = NULL;
remoteConn *rconn = NULL; remoteConn *rconn = NULL;
PGnotify *notify; PGnotify *notify;
......
...@@ -371,7 +371,7 @@ SELECT * ...@@ -371,7 +371,7 @@ SELECT *
FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[]) FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
WHERE t.a > 7; WHERE t.a > 7;
NOTICE: relation "foobar" does not exist 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 a | b | c
---+---+--- ---+---+---
(0 rows) (0 rows)
...@@ -494,7 +494,7 @@ SELECT dblink_close('myconn','rmt_foo_cursor'); ...@@ -494,7 +494,7 @@ SELECT dblink_close('myconn','rmt_foo_cursor');
-- this should fail because there is no open transaction -- this should fail because there is no open transaction
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo'); SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
ERROR: DECLARE CURSOR can only be used in transaction blocks 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 -- reset remote transaction state
SELECT dblink_exec('myconn','ABORT'); SELECT dblink_exec('myconn','ABORT');
dblink_exec dblink_exec
......
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