8000 Fix dblink's failure to report correct connection name in error messa… · jandas/postgres@6205bb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6205bb6

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 efff1cc commit 6205bb6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

contrib/dblink/dblink.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ typedef struct remoteConnHashEnt
168168
do { \
169169
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
170170
rconn = getConnectionByName(conname_or_str); \
171-
if(rconn) \
171+
if (rconn) \
172172
{ \
173173
conn = rconn->conn; \
174+
conname = conname_or_str; \
174175
} \
175176
else \
176177
{ \
@@ -198,9 +199,9 @@ typedef struct remoteConnHashEnt
198199

199200
#define DBLINK_GET_NAMED_CONN \
200201
do { \
201-
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
202+
conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
202203
rconn = getConnectionByName(conname); \
203-
if(rconn) \
204+
if (rconn) \
204205
conn = rconn->conn; \
205206
else \
206207
DBLINK_CONN_NOT_AVAIL; \
@@ -613,6 +614,7 @@ PG_FUNCTION_INFO_V1(dblink_send_query);
613614
Datum
614615
dblink_send_query(PG_FUNCTION_ARGS)
615616
{
617+
char *conname = NULL;
616618
PGconn *conn = NULL;
617619
char *connstr = NULL;
618620
char *sql = NULL;
@@ -937,6 +939,7 @@ PG_FUNCTION_INFO_V1(dblink_is_busy);
937939
Datum
938940
dblink_is_busy(PG_FUNCTION_ARGS)
939941
{
942+
char *conname = NULL;
940943
PGconn *conn = NULL;
941944
remoteConn *rconn = NULL;
942945

@@ -963,6 +966,7 @@ Datum
963966
dblink_cancel_query(PG_FUNCTION_ARGS)
964967
{
965968
int res = 0;
969+
char *conname = NULL;
966970
PGconn *conn = NULL;
967971
remoteConn *rconn = NULL;
968972
PGcancel *cancel;
@@ -997,6 +1001,7 @@ Datum
9971001
dblink_error_message(PG_FUNCTION_ARGS)
9981002
{
9991003
char *msg;
1004+
char *conname = NULL;
10001005
PGconn *conn = NULL;
10011006
remoteConn *rconn = NULL;
10021007

@@ -1506,6 +1511,7 @@ PG_FUNCTION_INFO_V1(dblink_get_notify);
15061511
Datum
15071512
dblink_get_notify(PG_FUNCTION_ARGS)
15081513
{
1514+
char *conname = NULL;
15091515
PGconn *conn = NULL;
15101516
remoteConn *rconn = NULL;
15111517
PGnotify *notify;

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)
0