diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index 21a4bbd1c75..6a829a70d69 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -1142,20 +1142,23 @@ PG_FUNCTION_INFO_V1(dblink_exec); Datum dblink_exec(PG_FUNCTION_ARGS) { + text *volatile sql_cmd_status = NULL; + PGconn *volatile conn = NULL; + volatile bool freeconn = false; + + DBLINK_INIT; + + PG_TRY(); + { char *msg; PGresult *res = NULL; - text *sql_cmd_status = NULL; TupleDesc tupdesc = NULL; - PGconn *conn = NULL; char *connstr = NULL; char *sql = NULL; char *conname = NULL; remoteConn *rconn = NULL; - bool freeconn = false; bool fail = true; /* default to backward compatible behavior */ - DBLINK_INIT; - if (PG_NARGS() == 3) { /* must be text,text,bool */ @@ -1234,8 +1237,17 @@ dblink_exec(PG_FUNCTION_ARGS) (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED), errmsg("statement returning results not allowed"))); } + } + PG_CATCH(); + { + /* if needed, close the connection to the database */ + if (freeconn) + PQfinish(conn); + PG_RE_THROW(); + } + PG_END_TRY(); - /* if needed, close the connection to the database and cleanup */ + /* if needed, close the connection to the database */ if (freeconn) PQfinish(conn);