Skip to content

Commit 19ab40f

Browse files
committed
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.
1 parent 0e74f1f commit 19ab40f

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

contrib/dblink/dblink.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ typedef struct remoteConnHashEnt
167167
do { \
168168
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
169169
rconn = getConnectionByName(conname_or_str); \
170-
if(rconn) \
170+
if (rconn) \
171171
{ \
172172
conn = rconn->conn; \
173+
conname = conname_or_str; \
173174
} \
174175
else \
175176
{ \
@@ -197,9 +198,9 @@ typedef struct remoteConnHashEnt
197198

198199
#define DBLINK_GET_NAMED_CONN \
199200
do { \
200-
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
201+
conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
201202
rconn = getConnectionByName(conname); \
202-
if(rconn) \
203+
if (rconn) \
203204
conn = rconn->conn; \
204205
else \
205206
DBLINK_CONN_NOT_AVAIL; \
@@ -718,6 +719,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query);
718719
Datum
719720
dblink_send_query(PG_FUNCTION_ARGS)
720721
{
722+
char *conname = NULL;
721723
PGconn *conn = NULL;
722724
char *connstr = NULL;
723725
char *sql = NULL;
@@ -1042,6 +1044,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy);
10421044
Datum
10431045
dblink_is_busy(PG_FUNCTION_ARGS)
10441046
{
1047+
char *conname = NULL;
10451048
PGconn *conn = NULL;
10461049
remoteConn *rconn = NULL;
10471050

@@ -1068,6 +1071,7 @@ Datum
10681071
dblink_cancel_query(PG_FUNCTION_ARGS)
10691072
{
10701073
int res = 0;
1074+
char *conname = NULL;
10711075
PGconn *conn = NULL;
10721076
remoteConn *rconn = NULL;
10731077
PGcancel *cancel;
@@ -1102,6 +1106,7 @@ Datum
11021106
dblink_error_message(PG_FUNCTION_ARGS)
11031107
{
11041108
char *msg;
1109+
char *conname = NULL;
11051110
PGconn *conn = NULL;
11061111
remoteConn *rconn = NULL;
11071112

contrib/dblink/expected/dblink.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ SELECT *
381381
FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
382382
WHERE t.a > 7;
383383
NOTICE: relation "foobar" does not exist
384-
CONTEXT: Error occurred on dblink connection named "unnamed": could not execute query.
384+
CONTEXT: Error occurred on dblink connection named "myconn": could not execute query.
385385
a | b | c
386386
---+---+---
387387
(0 rows)
@@ -504,7 +504,7 @@ SELECT dblink_close('myconn','rmt_foo_cursor');
504504
-- this should fail because there is no open transaction
505505
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
506506
ERROR: DECLARE CURSOR can only be used in transaction blocks
507-
CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
507+
CONTEXT: Error occurred on dblink connection named "myconn": could not execute command.
508508
-- reset remote transaction state
509509
SELECT dblink_exec('myconn','ABORT');
510510
dblink_exec

0 commit comments

Comments
 (0)