@@ -113,7 +113,8 @@ static Relation get_rel_from_relname(text *relname_text, LOCKMODE lockmode, AclM
113
113
static char * generate_relation_name (Relation rel );
114
114
static void dblink_connstr_check (const char * connstr );
115
115
static void dblink_security_check (PGconn * conn ,remoteConn * rconn );
116
- static void dblink_res_error (const char * conname ,PGresult * res ,const char * dblink_context_msg ,bool fail );
116
+ static void dblink_res_error (PGconn * conn ,const char * conname ,PGresult * res ,
117
+ const char * dblink_context_msg ,bool fail );
117
118
static char * get_connect_string (const char * servername );
118
119
static char * escape_param_str (const char * from );
119
120
static void validate_pkattnums (Relation rel ,
@@ -428,7 +429,7 @@ dblink_open(PG_FUNCTION_ARGS)
428
429
res = PQexec (conn ,buf .data );
429
430
if (!res || PQresultStatus (res )!= PGRES_COMMAND_OK )
430
431
{
431
- dblink_res_error (conname ,res ,"could not open cursor" ,fail );
432
+ dblink_res_error (conn , conname ,res ,"could not open cursor" ,fail );
432
433
PG_RETURN_TEXT_P (cstring_to_text ("ERROR" ));
433
434
}
434
435
@@ -497,7 +498,7 @@ dblink_close(PG_FUNCTION_ARGS)
497
498
res = PQexec (conn ,buf .data );
498
499
if (!res || PQresultStatus (res )!= PGRES_COMMAND_OK )
499
500
{
500
- dblink_res_error (conname ,res ,"could not close cursor" ,fail );
501
+ dblink_res_error (conn , conname ,res ,"could not close cursor" ,fail );
501
502
PG_RETURN_TEXT_P (cstring_to_text ("ERROR" ));
502
503
}
503
504
@@ -600,7 +601,8 @@ dblink_fetch(PG_FUNCTION_ARGS)
600
601
(PQresultStatus (res )!= PGRES_COMMAND_OK &&
601
602
PQresultStatus (res )!= PGRES_TUPLES_OK ))
602
603
{
603
- dblink_res_error (conname ,res ,"could not fetch from cursor" ,fail );
604
+ dblink_res_error (conn ,conname ,res ,
605
+ "could not fetch from cursor" ,fail );
604
606
return (Datum )0 ;
605
607
}
606
608
else if (PQresultStatus (res )== PGRES_COMMAND_OK )
@@ -751,8 +753,8 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async)
751
753
if (PQresultStatus (res )!= PGRES_COMMAND_OK &&
752
754
PQresultStatus (res )!= PGRES_TUPLES_OK )
753
755
{
754
- dblink_res_error (conname , res , "could not execute query" ,
755
- fail );
756
+ dblink_res_error (conn , conname , res ,
757
+ "could not execute query" , fail );
756
758
/* if fail isn't set, we'll return an empty query result */
757
759
}
758
760
else
@@ -999,7 +1001,8 @@ materializeQueryResult(FunctionCallInfo fcinfo,
999
1001
PGresult * res1 = res ;
1000
1002
1001
1003
res = NULL ;
1002
- dblink_res_error (conname ,res1 ,"could not execute query" ,fail );
1004
+ dblink_res_error (conn ,conname ,res1 ,
1005
+ "could not execute query" ,fail );
1003
1006
/* if fail isn't set, we'll return an empty query result */
1004
1007
}
1005
1008
else if (PQresultStatus (res )== PGRES_COMMAND_OK )
@@ -1434,7 +1437,8 @@ dblink_exec(PG_FUNCTION_ARGS)
1434
1437
(PQresultStatus (res )!= PGRES_COMMAND_OK &&
1435
1438
PQresultStatus (res )!= PGRES_TUPLES_OK ))
1436
1439
{
1437
- dblink_res_error (conname ,res ,"could not execute command" ,fail );
1440
+ dblink_res_error (conn ,conname ,res ,
1441
+ "could not execute command" ,fail );
1438
1442
1439
1443
/*
1440
1444
* and save a copy of the command status string to return as our
@@ -2668,7 +2672,8 @@ dblink_connstr_check(const char *connstr)
2668
2672
}
2669
2673
2670
2674
static void
2671
- dblink_res_error (const char * conname ,PGresult * res ,const char * dblink_context_msg ,bool fail )
2675
+ dblink_res_error (PGconn * conn ,const char * conname ,PGresult * res ,
2676
+ const char * dblink_context_msg ,bool fail )
2672
2677
{
2673
2678
int level ;
2674
2679
char * pg_diag_sqlstate = PQresultErrorField (res ,PG_DIAG_SQLSTATE );
@@ -2702,6 +2707,14 @@ dblink_res_error(const char *conname, PGresult *res, const char *dblink_context_
2702
2707
xpstrdup (message_hint ,pg_diag_message_hint );
2703
2708
xpstrdup (message_context ,pg_diag_context );
2704
2709
2710
+ /*
2711
+ * If we don't get a message from the PGresult, try the PGconn. This
2712
+ * is needed because for connection-level failures, PQexec may just
2713
+ * return NULL, not a PGresult at all.
2714
+ */
2715
+ if (message_primary == NULL )
2716
+ message_primary = PQerrorMessage (conn );
2717
+
2705
2718
if (res )
2706
2719
PQclear (res );
2707
2720