Skip to content

Commit 011c499

Browse files
committed
Add reconnect option to control automatic reconnection to MySQL server.
The MySQL client library can perform an automatic reconnection to the server if it finds the connection is down when one attempts to send a statement to the server to be executed, and it is controlled by MySQL option MYSQL_OPT_RECONNECT. This allows a seamless operation in case the connection got dropped intermittently. Add a mysql_fdw server-level option 'reconnect' to control this behavior for mysql_fdw internal connections. Reported on GitHub through issue #215 by Magmatrix. FDW-350, Suraj Kharage, reviewed by Jeevan Ladhe.
1 parent ab65966 commit 011c499

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ The following parameters can be set on a MySQL foreign server object:
115115
MySQL server.
116116
* `use_remote_estimate`: Controls whether mysql_fdw issues remote
117117
EXPLAIN commands to obtain cost estimates. Default is `false`
118+
* `reconnect`: Enable or disable automatic reconnection to the
119+
MySQL server if the existing connection is found to have been lost.
120+
Default is `false`.
118121
* `ssl_key`: The path name of the client private key file.
119122
* `ssl_cert`: The path name of the client public key certificate file.
120123
* `ssl_ca`: The path name of the Certificate Authority (CA) certificate

connection.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ mysql_connect(mysql_opt *opt)
244244
if (svr_init_command != NULL)
245245
mysql_options(conn, MYSQL_INIT_COMMAND, svr_init_command);
246246

247+
/*
248+
* Enable or disable automatic reconnection to the MySQL server if
249+
* the existing connection is found to have been lost.
250+
*/
251+
mysql_options(conn, MYSQL_OPT_RECONNECT, &opt->reconnect);
252+
247253
mysql_ssl_set(conn, opt->ssl_key, opt->ssl_cert, opt->ssl_ca,
248254
opt->ssl_capath, ssl_cipher);
249255

expected/server_options.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,32 @@ ERROR: "fetch_size" requires an integer value between 1 to 18446744073709551615
243243
-- Cleanup fetch_size test objects.
244244
DROP FOREIGN TABLE table30000;
245245
DROP SERVER fetch101;
246+
-- FDW-350: Support for reconnect option at server level.
247+
CREATE SERVER reconnect1 FOREIGN DATA WRAPPER mysql_fdw
248+
OPTIONS( reconnect 'true' );
249+
SELECT count(*)
250+
FROM pg_foreign_server
251+
WHERE srvname = 'reconnect1'
252+
AND srvoptions @> array['reconnect=true'];
253+
count
254+
-------
255+
1
256+
(1 row)
257+
258+
ALTER SERVER reconnect1 OPTIONS( SET reconnect 'false' );
259+
SELECT count(*)
260+
FROM pg_foreign_server
261+
WHERE srvname = 'reconnect1'
262+
AND srvoptions @> array['reconnect=false'];
263+
count
264+
-------
265+
1
266+
(1 row)
267+
268+
-- Negative test case for reconnect option, should error out.
269+
ALTER SERVER reconnect1 OPTIONS ( SET reconnect 'abc1' );
270+
ERROR: reconnect requires a Boolean value
271+
-- Cleanup reconnect option test objects.
272+
DROP SERVER reconnect1;
246273
-- Cleanup
247274
DROP EXTENSION mysql_fdw;

mysql_fdw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef struct mysql_opt
102102
* truncation */
103103
bool use_remote_estimate; /* use remote estimate for rows */
104104
unsigned long fetch_size; /* Number of rows to fetch from remote server */
105+
bool reconnect; /* set to true for automatic reconnection */
105106

106107
/* SSL parameters; unused options may be given as NULL */
107108
char *ssl_key; /* MySQL SSL: path to the key file */

option.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static struct MySQLFdwOption valid_options[] =
5151
/* fetch_size is available on both server and table */
5252
{"fetch_size", ForeignServerRelationId},
5353
{"fetch_size", ForeignTableRelationId},
54+
{"reconnect", ForeignServerRelationId},
5455
{"ssl_key", ForeignServerRelationId},
5556
{"ssl_cert", ForeignServerRelationId},
5657
{"ssl_ca", ForeignServerRelationId},
@@ -137,6 +138,11 @@ mysql_fdw_validator(PG_FUNCTION_ARGS)
137138
errmsg("\"%s\" requires an integer value between 1 to %lu",
138139
def->defname, ULONG_MAX)));
139140
}
141+
else if (strcmp(def->defname, "reconnect") == 0)
142+
{
143+
/* accept only boolean values */
144+
(void) defGetBoolean(def);
145+
}
140146
}
141147

142148
PG_RETURN_VOID();
@@ -203,6 +209,7 @@ mysql_get_options(Oid foreignoid, bool is_foreigntable)
203209
opt->svr_sa = true;
204210

205211
opt->use_remote_estimate = false;
212+
opt->reconnect = false;
206213

207214
/* Loop through the options */
208215
foreach(lc, options)
@@ -242,6 +249,9 @@ mysql_get_options(Oid foreignoid, bool is_foreigntable)
242249
if (strcmp(def->defname, "fetch_size") == 0)
243250
opt->fetch_size = strtoul(defGetString(def), NULL, 10);
244251

252+
if (strcmp(def->defname, "reconnect") == 0)
253+
opt->reconnect = defGetBoolean(def);
254+
245255
if (strcmp(def->defname, "ssl_key") == 0)
246256
opt->ssl_key = defGetString(def);
247257

sql/server_options.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,27 @@ ALTER FOREIGN TABLE table30000 OPTIONS ( SET fetch_size '999999999999999999999')
172172
DROP FOREIGN TABLE table30000;
173173
DROP SERVER fetch101;
174174

175+
-- FDW-350: Support for reconnect option at server level.
176+
CREATE SERVER reconnect1 FOREIGN DATA WRAPPER mysql_fdw
177+
OPTIONS( reconnect 'true' );
178+
179+
SELECT count(*)
180+
FROM pg_foreign_server
181+
WHERE srvname = 'reconnect1'
182+
AND srvoptions @> array['reconnect=true'];
183+
184+
ALTER SERVER reconnect1 OPTIONS( SET reconnect 'false' );
185+
186+
SELECT count(*)
187+
FROM pg_foreign_server
188+
WHERE srvname = 'reconnect1'
189+
AND srvoptions @> array['reconnect=false'];
190+
191+
-- Negative test case for reconnect option, should error out.
192+
ALTER SERVER reconnect1 OPTIONS ( SET reconnect 'abc1' );
193+
194+
-- Cleanup reconnect option test objects.
195+
DROP SERVER reconnect1;
196+
175197
-- Cleanup
176198
DROP EXTENSION mysql_fdw;

0 commit comments

Comments
 (0)