Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b75fbe9

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 566a1d4 commit b75fbe9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

contrib/dblink/dblink.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ typedef struct remoteConnHashEnt
153153
do { \
154154
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
155155
rconn = getConnectionByName(conname_or_str); \
156-
if(rconn) \
156+
if (rconn) \
157157
{ \
158158
conn = rconn->conn; \
159+
conname = conname_or_str; \
159160
} \
160161
else \
161162
{ \
@@ -183,9 +184,9 @@ typedef struct remoteConnHashEnt
183184

184185
#define DBLINK_GET_NAMED_CONN \
185186
do { \
186-
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
187+
conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
187188
rconn = getConnectionByName(conname); \
188-
if(rconn) \
189+
if (rconn) \
189190
conn = rconn->conn; \
190191
else \
191192
DBLINK_CONN_NOT_AVAIL; \
@@ -598,6 +599,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query);
598599
Datum
599600
dblink_send_query(PG_FUNCTION_ARGS)
600601
{
602+
char *conname = NULL;
601603
PGconn *conn = NULL;
602604
char *sql = NULL;
603605
remoteConn *rconn = NULL;
@@ -919,6 +921,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy);
919921
Datum
920922
dblink_is_busy(PG_FUNCTION_ARGS)
921923
{
924+
char *conname = NULL;
922925
PGconn *conn = NULL;
923926
remoteConn *rconn = NULL;
924927

@@ -945,6 +948,7 @@ Datum
945948
dblink_cancel_query(PG_FUNCTION_ARGS)
946949
{
947950
int res = 0;
951+
char *conname = NULL;
948952
PGconn *conn = NULL;
949953
remoteConn *rconn = NULL;
950954
PGcancel *cancel;
@@ -979,6 +983,7 @@ Datum
979983
dblink_error_message(PG_FUNCTION_ARGS)
980984
{
981985
char *msg;
986+
char *conname = NULL;
982987
PGconn *conn = NULL;
983988
remoteConn *rconn = NULL;
984989

@@ -1488,6 +1493,7 @@ PG_FUNCTION_INFO_V1(dblink_get_notify);
14881493
Datum
14891494
dblink_get_notify(PG_FUNCTION_ARGS)
14901495
{
1496+
char *conname = NULL;
14911497
PGconn *conn = NULL;
14921498
remoteConn *rconn = NULL;
14931499
PGnotify *notify;

contrib/dblink/expected/dblink.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ SELECT *
371371
FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
372372
WHERE t.a > 7;
373373
NOTICE: relation "foobar" does not exist
374-
CONTEXT: Error occurred on dblink connection named "unnamed": could not execute query.
374+
CONTEXT: Error occurred on dblink connection named "myconn": could not execute query.
375375
a | b | c
376376
---+---+---
377377
(0 rows)
@@ -494,7 +494,7 @@ SELECT dblink_close('myconn','rmt_foo_cursor');
494494
-- this should fail because there is no open transaction
495495
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
496496
ERROR: DECLARE CURSOR can only be used in transaction blocks
497-
CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
497+
CONTEXT: Error occurred on dblink connection named "myconn": could not execute command.
498498
-- reset remote transaction state
499499
SELECT dblink_exec('myconn','ABORT');
500500
dblink_exec

0 commit comments

Comments
 (0)